Arrays

An array is a collection of values of the same data type, which can include:

  • Primitive types (e.g., INTEGER, TEXT, BOOLEAN)

  • Objects

  • Geographic types

Arrays can also be nested (arrays of arrays).


Defining Arrays

Arrays can be declared in table definitions using the ARRAY(<type>) syntax, for example:

CREATE TABLE my_table_arrays (
    tags ARRAY(TEXT),
    objects ARRAY(OBJECT AS (age INTEGER, name TEXT))
);

You can also use the shorthand <type>[]:

TEXT[]        -- equivalent to ARRAY(TEXT)
OBJECT[]      -- equivalent to ARRAY(OBJECT)

Inserting Array Values

Array literals are enclosed in square brackets []:

Result:


Array Syntax

Arrays can be written using:

  • Short form: []

  • Constructor form: ARRAY[...]

Examples:

All elements in an array must share the same data type. If empty, the element type is inferred from the context.


PostgreSQL-Compatible String Literal Syntax

You can also define arrays using PostgreSQL’s string literal format:

Result:

  • null (without quotes) → null value

  • "null" (with quotes) → the string "null"

This form exists for PostgreSQL compatibility, but the bracket syntax is preferred.


Nested Arrays

You can define arrays of arrays directly:

They can also be passed to functions, for example:

Nested arrays can be built with ARRAY_AGG:


Notes:

  • Accessing nested arrays can be slow, as it may require reading data directly from disk. If you use them often, consider restructuring into multiple tables.

  • Nested arrays cannot be created dynamically (either as top-level columns or inside dynamic objects).

Last updated