Skip to content
Advertisement

PostgreSQL array overlap with the ALL construct

I want to achieve the same behavior as in next example:

using the ALL construct like so:

By logic both queries should return false, but only the first does, while the second query returns true.

Can anybody explain this behavior and hint me how to fix it?

Advertisement

Answer

Instead of passing a single multi-dimensional array, pass a set of 1-dimensional arrays – or really any dimensions, as dimensions are just noise in this case. This works:

Which is more useful when applied to a table column:

The ALL or ANY constructs either expect a set or an array to the right. Your example only works with a set. I can see how one might conclude to pass a multi-dimensional array (an array of arrays). Unfortunately, that’s not how it works. The manual:

The left-hand expression is evaluated and compared to each element of the array […],

Bold emphasis mine. Element. Not sub-array or array-slice. So the attempt:

… results in this error message accordingly:

You tried something in between:

Effectively a set containing single multi-dimensional array. It boils down to:

… which yields true like you observed because, again, “overlap” is tested on the level of elements, ignoring dimensions. Effectively the same as:

Or, to prove my point:

Related:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement