An array type can be created by applying the ARRAY suffix to another
type. For example INT ARRAY is an array of integers. Array indexes
start from 1. Array sizes are limited to 2^31 elements. Array values
can be nullable types, e.g., INT ARRAY NULL. Multidimensional arrays
are possible, e.g. VARCHAR ARRAY ARRAY is a two-dimensional array.
In CREATE TABLE and CREATE TYPE declarations there is no way to
specify the nullability of the elements of an ARRAY. The compiler
will always assume that array elements are nullable:
CREATE TABLE t(a INT ARRAY);Table T will have a single column a whose values are nullable
arrays; the array elements will be nullable INT values.
Array literals have the syntax ARRAY[expr [,expr]*]. An example
creating a nested array is: ARRAY[ARRAY[1, 2], ARRAY[3, 4]].
The UNNEST operator takes an ARRAY and returns a table with a
row for each element in the ARRAY: UNNEST(ARRAY) [WITH ORDINALITY]. If the input is an array with 5 elements, the output
is a table with 5 rows, each row holding one element of the array.
The additional keyword WITH ORDINALITY creates an output table
with two columns, where the second column is the index of the element
within the array, with numbering starting at 1. If the array contains
duplicated values, the resulting table will be a multiset.
The UNNEST operator can be used in self-joins as follows:
CREATE TABLE data(cities VARCHAR ARRAY, country VARCHAR);
CREATE VIEW v AS SELECT city, country
FROM data, UNNEST(cities) AS t (city);The previous query is a shortcut for a CROSS-JOIN query:
CREATE VIEW v AS SELECT city, data.country
FROM data CROSS JOIN UNNEST(data.cities) AS city;CROSS APPLY is another spelling for the same query:
CREATE VIEW v AS SELECT city, data.country
FROM data CROSS apply UNNEST(data.cities) AS city;UNNEST applied to a NULL value returns an empty table. As a
consequence, the queries above drop the rows of data where cities
is NULL or empty. The join forms that instead keep such rows,
LEFT JOIN UNNEST(...) ON TRUE and OUTER APPLY UNNEST(...), are not
yet supported; see unsupported
operations. To keep the
rows where the array is NULL, substitute a one-element array in the
UNNEST argument:
-- A row with a NULL cities array produces one row with a NULL city
CREATE VIEW v AS SELECT city, country
FROM data, UNNEST(COALESCE(cities, ARRAY[NULL])) AS t (city);Note that applying UNNEST to an ARRAY of structure-typed objects
will produce a collection whose columns are the fields of the
structure, instead of a collection with a single structure-typed
column.
Comparison operations (=, <>, !=, >, <, >=, <=) in arrays occur lexicographically, from left to right. The elements are compared one by one until a difference is found. Arrays can be compared even if they are different in size.
A NULL array element is smaller than any other element,
whatever the NULLS FIRST or NULLS LAST clause of an enclosing ORDER BY says.
On arrays, = is equivalent to IS NOT DISTINCT FROM and <> is
equivalent to IS DISTINCT FROM: two NULL elements compare as
equal, so ARRAY[1, NULL] = ARRAY[1, NULL] is true. See
comparing complex values.
Examples:
[45] > [22][23, 56, 16] > [23, NULL][NULL] < [1]