I have a table with a column itemsIds containing an array of ids. I have created a gin index for that column. I'm trying to find rows that don't contain an element with the following query:
select * from "Posts" where not (ARRAY[8] @> "Posts".itemids); The thing is that the gin index is not being used when I search with not but if the query is without it Postgres use that index.
EXPLAIN output:
Without NOT:
"Bitmap Heap Scan on ""Posts"" (cost=12.03..25.78 rows=4 width=334)" Recheck Cond: ('{8}'::integer[] @> itemids) -> Bitmap Index Scan on posts_itemids_index (cost=0.00..12.03 rows=4 width=0) Index Cond: (itemids <@ '{8}'::integer[])` With NOT:
"Seq Scan on ""Posts"" (cost=0.00..140.76 rows=2537 width=334)" Filter: (NOT ('{8}'::integer[] @> itemids)) I tried rewriting the query with:
select * from "Posts" where (ARRAY[8] @> "Posts".itemids) = false; Same result.
explain(analyze, verbose, buffers, settings)for this query? And the DDL for the table and indexes. All in plain text, as an update of the question.