How to deal with IMMUTABLE error in indexes that work with timestamps
Say that in an older version of PostgreSQL you had an index like this:
CREATE INDEX logTypeDay ON myLog (eventType, date_trunc(‘day’, creationTime));
and now after upgrading to 8.1+ you’re getting:
ERROR: functions in index expression must be marked IMMUTABLE
The reason is that date_trunc may return different results based on the current time zone. There’s an example query in PostgreSQL Mailing List Archives that you can run to believe.
If you are absolutely sure that you will always access your data with the same time zone you can redefine your index like this:
CREATE INDEX logTypeDay ON myLog (eventType, date_trunc(‘day’::text, creationTime AT TIME ZONE ‘Europe/Prague’));
The whole immutable business with indexes containing functions that process timestamps is well explained in these threads:
All credit goes to Tom Lane. I admire him for being always so patient and polite, and for spending so much of his valuable time with talking to users. I would love to link to his homepage but I failed to find it.