1 = ALL(arr) IS NULL AND 2 = ALL(arr) IS NULL
1
and 2
can be any two distinct numbers.
Alternatives and performance
There are many ways. I assembled a quick test case:
SELECT arr::text
, -1 = ALL(arr) IS NULL AS xsimple
, 1 = ALL(arr) IS NULL AND 2 = ALL(arr) IS NULL AS simple
, array_remove(arr, NULL) = '{}' AS array_rem
, cardinality(array_positions(arr, NULL))
= cardinality(arr) AS array_pos
, TRUE = ALL (SELECT unnest(arr) IS NULL) AS michael
, (SELECT bool_and(e IS NULL) FROM unnest(arr) e) AS bool_and
, NOT EXISTS (SELECT unnest(arr) EXCEPT SELECT null) AS exist
FROM (
VALUES
('{1,2,NULL,3}'::int[])
, ('{1,1,1}')
, ('{2,2,2}')
, ('{NULL,NULL,NULL}')
, ('{}'::int[])
) t(arr);
arr | xsimple | simple | array_rem | array_pos | michael | bool_and | exist
------------------+---------+--------+-----------+-----------+---------+----------+-------
{1,2,NULL,3} | f | f | f | f | f | f | f
{1,1,1} | f | f | f | f | f | f | f
{2,2,2} | f | f | f | f | f | f | f
{NULL,NULL,NULL} | t | t | t | t | t | t | t
{} | f | f | t | t | t | | t
array_remove()
requires Postgres 9.3 or later.
array_positions()
requires Postgres 9.5 or later.
chk_michael
is from the currently accepted answer by @michael.
The columns are in order of performance of the expression. Fastest first.
My simple checks dominate performance, with array_remove()
next. The rest cannot keep up.
The special case empty array ({}
) requires attention. Define the expected result and either pick a fitting expression or add an additional check.
db<>fiddle here - with performance test
Old sqlfiddle
How does it work?
The expression 1 = ALL(arr)
yields:
TRUE
.. if all elements are 1
FALSE
.. if any element is <> 1
(any element that IS NOT NULL
)
NULL
.. if at least one element IS NULL
and no element is <> 1
So, if we know a single element that cannot show up (enforced by a CHECK
constraint), like -1
, we can simplify to:
-1 = ALL(arr) IS NULL
If any number can show up, check for two distinct numbers. The result can only be NULL
for both if the array contains nothing but NULL
. Voilá.