I want to create an array of IDs once in a CTE and use it in multiple ANYs. But I'm getting stuck at the first one. Using PG 9.3
WITH x AS (
SELECT ARRAY(SELECT * FROM generate_series(1, 10)) AS a
)
SELECT 1
WHERE 2 = ANY(SELECT a FROM x)
What I expect is that the SELECT statement in ANY will return the previously created array. Instead I get this error:
ERROR: operator does not exist: integer = integer[]
LINE 6: WHERE 2 = ANY(SELECT a FROM x)
I don't understand what the issue is because the select statement should return an array, and ANY takes an array.
I could of course not create the array in the CTE and create it on the fly in ANY every time, but I assume that would be less performant than creating it once. And I must have an array, because ANY without the array changes the query plan (when doing this on my actual table, not this small example) to something less performant.