0

I have a table with an Array column of varchar(256)-values:

ALTER TABLE "my_table" ADD "subject" VARCHAR(256)[] NULL

And I want to query this field like this:

SELECT * FROM mytable WHERE subject && '{"journal", "book"}';

Since I want to query on values in that particular array, I need an index for performance reasons. However, every attempt failed due to missing storage classes or unsupported column type.

I have tried:

CREATE INDEX idx_daily_subject on "daily" USING GIN ("subject" gin_trgm_ops)
CREATE INDEX idx_daily_subject on "daily" USING GIN (to_tsvector('english', subject))

But then I receive the following error messages:

ERROR: operator class "gin_trgm_ops" does not accept data type character varying[]

successive

ERROR: function to_tsvector(unknown, character varying[]) does not exist

I found a workaround here: Postgres - Indexing long character varying array type column using GIN? but this makes any ORM mapper that I use obsolete (TypeORm in my case).

I have seen examples that for integer-arrays, there is a storage class available gin__int_ops. I couldn't find a suitable thing for text or varchar.

Is there anything that helps me?

2
  • 1
    Which operation are you trying to support? Commented Jan 14, 2024 at 16:38
  • @jjanes I want support a query like this SELECT * FROM mytable WHERE subject && '{"journal", "book"}'; I've updated my question accordingly, thanks for the hint Commented Jan 15, 2024 at 3:12

1 Answer 1

1

There is a default operator class for GIN for varchar[] which supports &&.

CREATE INDEX idx_daily_subject on "daily" USING GIN ("subject")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.