How to use Postgresql GIN index with ARRAY keyword

Viewed 1576

I'd like to create GIN index on a scalar text column using an ARRAY[] expression like so:

CREATE TABLE mytab (
 scalar_column TEXT
)

CREATE INDEX idx_gin ON mytab USING GIN(ARRAY[scalar_column]);

Postgres reports an error on ARRAY keyword.

I'll use this index later in a query like so:

SELECT * FROM mytab WHERE ARRAY[scalar_column] <@ ARRAY['some', 'other', 'values'];

How do I create such an index?

1 Answers

You forgot to add an extra pair of parentheses that is necessary for syntactical reasons:

CREATE INDEX idx_gin ON mytab USING gin ((ARRAY[scalar_column]));

The index does not make a lot of sense. If you need to search for membership in a given array, use a regular B-tree index with = ANY.

Related