How can I index a to_char() of a column?
I have tried:
adam_db=> CREATE INDEX updates_hourly_idx
ON updates (to_char(update_time, 'YYYY-MM-DD HH24:00'));
But got the error:
ERROR: functions in index expression must be marked
IMMUTABLE
Which seems strange, since the to_char() of a timestamp is reasonably immutable.
Any ideas how to generate that index?
to_charof atimestampto be immutable, yes. Not fortimestamptz, but that's not what you're using. However,\df+ to_charshows all theto_charvariants are onlystable. – Craig Ringer Sep 22 '14 at 14:22to_char()aside (Daniel nailed it) - may I inquire the purpose of such index? My educated guess is you do not need it. Rather cast search expressions totimestampand use a plain index on the timestamp column, or if you need values truncated to full hours, usedate_trunc('hour', update_time)instead, which is immutable fortimestamp(but not fortimestamptz, obviously). – Erwin Brandstetter Sep 22 '14 at 21:42