Re: [GENERAL] ERROR: functions in index expression must be marked IMMUTABLE - Mailing list pgsql-general

From Geoff Winkless
Subject Re: [GENERAL] ERROR: functions in index expression must be marked IMMUTABLE
Date
Msg-id CAEzk6ffpV+8En-9fZ=T6zyMW57KAHMgRw8DKhHYa4MSESunE6Q@mail.gmail.com
Whole thread Raw
In response to Re: [GENERAL] ERROR: functions in index expression must be markedIMMUTABLE  (Adrian Klaver <adrian.klaver@aklaver.com>)
Responses Re: [GENERAL] ERROR: functions in index expression must be marked IMMUTABLE  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: [GENERAL] ERROR: functions in index expression must be markedIMMUTABLE  (Adrian Klaver <adrian.klaver@aklaver.com>)
List pgsql-general
On 26 February 2017 at 16:09, Adrian Klaver <adrian.klaver@aklaver.com> wrote:
On 02/26/2017 07:56 AM, Geoff Winkless wrote:
> On 26 February 2017 at 10:09, Sven R. Kunze <srkunze@mail.de
> <mailto:srkunze@mail.de>>wrote:
>
>     >>># create index docs_birthdate_idx ON docs using btree
>     (((meta->>'birthdate')::date));
>     ERROR:  functions in index expression must be marked IMMUTABLE
>
>     So, what is the problem here?
>
>
> ​Date functions are inherently not immutable because of timezones. Your
> solution of using to_timestamp doesn't help because it automatically
> returns a value in WITH TIMESTAMP. Do you get anywhere by using
> "::timestamp without time zone" instead, as suggested here?

​Of course I meant "WITH TIMEZONE" here, finger slippage.
My attempts at working the OP's problem passed through that:

​​Apologies, I don't have that reply in the thread in my mailbox.

test=> create index docs_birthdate_idx ON docs using btree (((meta->>'birthdate')::timestamp));
ERROR:  functions in index expression must be marked IMMUTABLE
 
​ Isn't the point that casting to ::timestamp will still keep the timezone?  Hence casting to "without timezone".

This works:

test=> create index docs_birthdate_idx ON docs using btree ((meta->>'birthdate'));
CREATE INDEX

It is the act of casting that fails. Other then the OP's own suggestion of creating
a function that wraps the operation and marks it immutable I don't have a solution at
this time

​I can imagine that without a cast, depending on the way birthdate is stored, it may behave differently to a cast index for ordering.

Geoff

pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: [GENERAL] ERROR: functions in index expression must be markedIMMUTABLE
Next
From: Tom Lane
Date:
Subject: Re: [GENERAL] ERROR: functions in index expression must be marked IMMUTABLE