Contents
array_prepend()
A function for prepending to an array
array_prepend()
is a is a system function for prepending an element to the front of an array.
array_prepend()
was added in PostgreSQL 7.4.
Usage
array_prepend (anyelement
,anyarray
) →anyarray
Change history
- PostgreSQL 7.4
- added (commit 730840c9)
Examples
Basic usage examples for array_prepend()
:
postgres=# SELECT array_prepend(1, ARRAY[2,3]); array_prepend --------------- {1,2,3} (1 row) postgres=# SELECT array_prepend('foo', ARRAY['bar','baz']); array_prepend --------------- {foo,bar,baz} (1 row)
Note that the anyelement
||
anyarray
operator is equivalent:
postgres=# SELECT 1 || ARRAY[2,3]; ?column? ---------- {1,2,3} (1 row)
It is not possible to prepend arbitrary data types to an array of a particular type:
postgres=# SELECT array_prepend(1, ARRAY['bar','baz']); ERROR: function array_prepend(integer, text[]) does not exist LINE 1: SELECT array_prepend(1, ARRAY['bar','baz']); ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts
References
- PostgreSQL documentation: Array Functions