array_fill()

A function returning a pre-filled array

array_fill() is a system function which returns an array filled with a value with user-defined dimensions.

array_fill() was added in PostgreSQL 8.4.

Usage

array_fill ( anyelement, integer[] [, integer[] ] ) → anyarray

Change history

Examples

Basic usage examples for array_fill():

postgres=# SELECT array_fill(1, ARRAY[3]);
 array_fill 
------------
 {1,1,1}
(1 row)

postgres=# SELECT array_fill(1, ARRAY[2,3]);
    array_fill     
-------------------
 {{1,1,1},{1,1,1}}
(1 row)

The type of the provided value may need to be explicitly provided:

postgres=# SELECT array_fill('foo', ARRAY[2,3]);
ERROR:  could not determine polymorphic type because input has type unknown

postgres=# SELECT array_fill('foo'::TEXT, ARRAY[2,3]);
          array_fill           
-------------------------------
 {{foo,foo,foo},{foo,foo,foo}}
(1 row)

It is possibly to specify the lower bounds for each array dimension:

postgres=# SELECT array_fill(1, ARRAY[2,3], ARRAY[1,2]);
          array_fill          
------------------------------
 [1:2][2:4]={{1,1,1},{1,1,1}}
(1 row)

Categories

Arrays, System function

See also

array_ndims(), array_lower(), array_upper()