Using postgres 10.4.
I have a table my_table1
with a column attr_a
declared as jsonb
Within that field I have { my_attrs: {UpdatedDttm:'<date as iso8601 str>}}
for example:
{ my_attrs: {UpdatedDttm:'2018-09-20T17:55:52Z'}}
I use the date in a query that needs to do order by (and to compare with another date).
Because dates are strings, I have to convert them on the fly to a timestamp, using to_timestamp.
But I also need to add that order & filtering condition to an index. So that Postgres can leverage that.
Problem is, my index creation is failing with the message:
create index my_table1__attr_a_jb_UpdatedDttm__idx ON my_table1 using btree (((to_timestamp(attr_a->'my_attrs'->>'UpdatedDttm', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')) ) DESC);
ERROR: functions in index expression must be marked IMMUTABLE SQL state: 42P17
Is there a method to get this to work (hopefully, without creating a custom date formatting function), and I need to keep the time zone (cannot drop it).
I checked a similar question
And I am using what's recommended there to_timestamp and without the ::timestamptz typecast.
Still getting the error if I am using the built in function.
That answer suggests to create my own function (but I was trying to avoid that (as noted in my question at the bottom, because it requires rewritting/changing all the sql statemetns that use the table).