Re: filter records by substring match of an postgresql array column

From: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
To: Arup Rakshit <aruprakshit1987(at)outlook(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: filter records by substring match of an postgresql array column
Date: 2017-11-08 19:10:51
Message-ID: CAMkU=1wkontRvzgdYr_DwLW8-1oQyt+C6rEEnzsO6BpR63Jhog@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Nov 8, 2017 at 4:28 AM, Arup Rakshit <aruprakshit1987(at)outlook(dot)com>
wrote:

>
> And to do I wanted to add an index like:
>
> CREATE INDEX trgm_idx_video_tags ON videos USING gist
> ((array_to_string(tags, ', ')) gist_trgm_ops)
>
> But on running this I get an error as:
>
> ERROR: functions in index expression must be marked IMMUTABLE
>
> How can I fix this?
>
>
wrap array_to_string with text[] argument into a function and mark it as
immutable:

create function txt_array_to_string (text[]) returns text language sql
immutable as $$ select array_to_string($1,', ') $$;

And then build your index on that function.

I don't think there are any caveats on this. Array_to_string is not
immutable because it can work with dates and numbers, which can change with
configuration settings, such as timezone. But when given text[] argument,
I think it actually is immutable.

Cheers,

Jeff

In response to

Browse pgsql-general by date

  From Date Subject
Next Message DrakoRod 2017-11-08 19:28:23 Because PostgreSQL is compiling in old versions of OS?
Previous Message rammohan ganapavarapu 2017-11-08 16:17:01 Re: Can master and slave on different PG versions?