PostgreSQL 18 reference · Array Functions

PostgreSQL CARDINALITY() Function

Return the total number of elements in an array across all dimensions, and 0 for an empty array.

PostgreSQL 18 Since 9.4

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

Syntax

cardinality(array)

Parameters

ParameterDescription
array An array value.

Return value

integer.

Examples

All dimensions
SELECT cardinality(ARRAY[[1, 2], [3, 4]]) AS elements;

Output

 elements
----------
        4
(1 row)

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

Count collected values
SELECT country, cardinality(array_agg(name)) AS customers
FROM customers
GROUP BY country
ORDER BY customers DESC
LIMIT 2;

Output

 country | customers
---------+-----------
 USA     |         5
 UK      |         2
(2 rows)

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

Version history

VersionChange
PostgreSQL 9.4Added

Was this page helpful?