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_char
of atimestamp
to be immutable, yes. Not fortimestamptz
, but that's not what you're using. However,\df+ to_char
shows all theto_char
variants are onlystable
.to_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 totimestamp
and 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).