Is array all NULLs in PostgreSQL
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á.
I'm not exactly proud of this but:
=> select not exists (
select 1
from (select all unnest(ARRAY[NULL, NULL, NULL]) is null as x) as dt
where x = 'f'
);
?column?
----------
t
(1 row)
=> select not exists (
select 1
from (select all unnest(ARRAY[NULL, 11, NULL]) is null as x) as dt
where x = 'f'
);
?column?
----------
f
(1 row)
Yes, there are subqueries galore but maybe you can make it work or simplify it into something that will work.
I think I got the shortest answer, while still preserving 4 = ALL (ARRAY[4,5]::integer[]);
construct:
Live test: https://www.db-fiddle.com/f/6DuB1N4FdcvZdxKiHczu5y/1
select
y, true = ALL (select unnest(z) is null)
from x