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
Syntax
array_length(array, dimension)
Parameters
| Parameter | Description |
|---|---|
array |
An array value. |
dimension |
Which dimension, usually 1. |
Return value
integer, or NULL for an empty array.
Examples
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.
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.
Related
CARDINALITY()· Return the total number of elements in an array across all dimensions, and 0 for an empty array.UNNEST()· Expand an array into a set of rows, one per element. With several arrays it zips them into columns.- SQL String Functions · tutorial