PostgreSQL 18 reference · Array Functions

PostgreSQL ARRAY_LENGTH() Function

Return the length of one dimension of an array (use 1 for ordinary arrays). Empty arrays give NULL, not 0.

PostgreSQL 18

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_length(array, dimension)

Parameters

ParameterDescription
array An array value.
dimension Which dimension, usually 1.

Return value

integer, or NULL for an empty array.

Examples

Length
SELECT array_length(ARRAY[10, 20, 30], 1) AS len, array_length('{}'::int[], 1) AS empty;

Output

 len | empty
-----+-------
   3 |  NULL
(1 row)

Run this on your computer — see “Get Started” for setup.

cardinality() gives 0 for empty arrays
SELECT cardinality(ARRAY[10, 20, 30]) AS n, cardinality('{}'::int[]) AS empty;

Output

 n | empty
---+-------
 3 |     0
(1 row)

Run this on your computer — see “Get Started” for setup.

Was this page helpful?