PostgreSQL array_length() Function

The PostgreSQL array_length() function returns the length of the specified array dimension.

array_length() Syntax

Here is the syntax of the PostgreSQL array_length() function:

array_length(array, dimension) -> integer

Parameters

array

Required. array.

dimension

Required. The dimension of the array, which is an integer.

Return value

The PostgreSQL array_length() function returns an integer that is the length of the specified array dimension.

If you specify a dimension that does not exist in the array, the function will return NULL.

array_length() Examples

This example shows how to use a PostgreSQL array_length() function to return the length of an one-demension array.

SELECT array_length(ARRAY[0, 1, 2], 1);
 array_length
--------------
            3

For a multidimensional array, you can get the length of an array dimension, for example:

SELECT array_length(ARRAY[[1,2], [3,4], [5,6]], 2);
 array_length
--------------
            2

Here, the length of the second demension in the array is 2.