I want to create an index on my table for start_time, a timestamptz (timestamp with time zone) field in my json column called match.
Following this question and this article I understand that you can't create an index on a timestamptz field because of different timezones and localisation. Both of these indicate that you can create an index on a timestamp (converted to text), so I tried the following function:
CREATE OR REPLACE FUNCTION to_text(timestamptz)
RETURNS text AS $$
SELECT to_char($1 at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US')
$$
LANGUAGE sql IMMUTABLE;
Which I believe has no issues with timezones and localisation.
CREATE INDEX i_match_start_time ON matches (to_text(((match->>'start_time')::timestamptz)));
This returns the following:
ERROR: functions in index expression must be marked IMMUTABLE
I have also tried functions that return a timestamp:
SELECT ($1 at time zone 'UTC')
And functions that return unix time (tried double and casted into decimal):
SELECT EXTRACT(EPOCH FROM $1)
Each of these returns the same error.
I need to index on start_time because virtually all select queries to this table will be ordered by start_time.
to_text()might be immutable, butto_text(((match->>'start_time')::timestamptz))is not