Augmenting Laurenz Albe's answer with my own to be able to post code, I'm using the following.
Assuming you have a table mytable
with JSONB field thedata
, which contains data like { "datetime": "2020-03-21T33:44:55.193843281Z ", ... }
:
-- Create immutable UTC parsing function, see:
-- * https://stackoverflow.com/questions/58877503/cant-create-an-index-on-a-jsonb-field-with-date
-- * https://stackoverflow.com/questions/5973030/error-functions-in-index-expression-must-be-marked-immutable-in-postgres
-- It parses dates of format:
-- YYYY-MM-DDTHH:MM:SSZ
-- (with a 'T' between date and time, and a 'Z' at the end) for example:
-- 2020-03-21T33:44:55Z
-- 2020-03-21T33:44:55.193843281Z
CREATE OR REPLACE FUNCTION utc_to_timestamp(some_time text)
RETURNS timestamp with time zone
AS
$BODY$
select to_timestamp($1, 'YYYY-MM-DD"T"HH24:MI:SS"Z"');
$BODY$
LANGUAGE sql
IMMUTABLE;
-- Define the index:
CREATE INDEX mytable_expr_datetime on mytable (utc_to_timestamp(the_data ->> 'datetime'));
So for the table/field names in your question, you have to replace the_data ->> 'datetime'
by events->'START'->> 'date'
, and mytable
by events
.