Sub topic 6
NULL Elements and Empty Arrays
This chapter assumes you know SELECT, WHERE, casts, and SQL’s three-valued logic. PostgreSQL arrays have two different kinds of “nothing”:
- A
NULLarray has no array value at all. - An empty array is a real array with zero elements.
- An array can also contain one or more
NULLelements.
These cases look similar in query results, but PostgreSQL handles them differently.
Three meanings of “no value”
The following query creates examples of each case:
SELECT
NULL::text[] AS missing_array,
'{}'::text[] AS empty_array,
ARRAY[NULL]::text[] AS one_null_element,
ARRAY['a', NULL] AS value_and_null;missing_array is unknown. empty_array exists but contains no positions. one_null_element has position 1, but that position has an unknown value.
You can inspect the difference with cardinality:
SELECT
cardinality(NULL::text[]) AS missing_cardinality,
cardinality('{}'::text[]) AS empty_cardinality,
cardinality(ARRAY[NULL]::text[]) AS null_element_cardinality;The results are:
| Expression | Result |
|---|---|
| `cardinality(NULL::text[])` | `NULL` |
| `cardinality('{}'::text[])` | `0` |
| `cardinality(ARRAY[NULL]::text[])` | `1` |
cardinality returns the total number of elements. It returns NULL when the whole array is NULL.
Length functions and empty arrays
array_length(array, dimension) reports the length of one dimension. For an empty one-dimensional array, PostgreSQL returns NULL, because there is no dimension with an actual range of positions.
SELECT
array_length(NULL::text[], 1) AS missing_length,
array_length('{}'::text[], 1) AS empty_length,
array_length(ARRAY[NULL]::text[], 1) AS null_element_length,
cardinality('{}'::text[]) AS empty_cardinality;The first two length values are NULL. The length of ARRAY[NULL] is 1, because the array has one position even though its value is NULL. The cardinality of the empty array is 0.
Use cardinality when you need to ask, “How many elements are present?” Use array_length when you need information about a particular dimension.
NULL elements in comparisons
Array elements can be NULL, so operators such as ANY and ALL can produce an unknown result.
value = ANY(array) is true when at least one comparison is true. It is false when every comparison is false and there are no NULL results. It is NULL when no comparison is true and at least one comparison is unknown.
SELECT
2 = ANY (ARRAY[1, 2, NULL]) AS finds_two,
4 = ANY (ARRAY[1, 2, NULL]) AS no_match_but_null,
4 = ANY ('{}'::int[]) AS no_elements,
1 = ALL (ARRAY[1, 1]) AS every_value_matches,
1 = ALL ('{}'::int[]) AS all_over_empty;The results are true, NULL, false, true, and true.
The last result follows the rule for a universal condition: every element of an empty set satisfies “all.” This is often surprising.
An array that is itself NULL also produces an unknown result:
SELECT
2 = ANY (NULL::int[]) AS any_in_missing_array,
2 = ALL (NULL::int[]) AS all_in_missing_array;Both results are NULL.
Finding and handling NULL elements
array_position uses a comparison that can match NULL with NULL. This lets you locate a null element:
SELECT
array_position(ARRAY['a', NULL, 'c']::text[], NULL::text)
AS position_of_null;The result is 2.
To normalize a possibly missing array before counting it, use COALESCE. COALESCE returns the first non-NULL expression:
SELECT
cardinality(COALESCE(NULL::text[], '{}'::text[])) AS safe_count,
cardinality(COALESCE(ARRAY[NULL]::text[], '{}'::text[]))
AS count_with_null_element;The results are 0 and 1. COALESCE replaces a missing array, but it does not remove NULL elements inside an existing array.
Common mistakes
- Treating
NULL::text[]and'{}'::text[]as interchangeable. - Using
array_length(..., 1) = 0to detect an empty array. - Expecting
4 = ANY(ARRAY[1, 2, NULL])to returnfalse. TheNULLcomparison makes the result unknown. - Assuming
COALESCE(tags, '{}')removes null elements. It only replaces a null array value. - Forgetting that
unnestpreserves null elements but emits no rows for an empty array.
When a missing array should mean “no values,” normalize it at the boundary with a default or with COALESCE. Keep null elements only when “an element exists, but its value is unknown” has a meaningful business interpretation.
Exercise
What do these expressions return: cardinality('{}'::int[]), array_length('{}'::int[], 1), and 3 = ALL('{}'::int[])?
Show answer
They return 0, NULL, and true, respectively. The empty array has zero elements, its one-dimensional length is NULL, and every element in the empty set satisfies the ALL condition.