Postgres JSONB date range search I have a JSONB object with many elements, one of which is a date.  The following query is now very slow with millions of rows in my table:
SELECT * FROM table WHERE obj-&gt;&gt;'dob' BETWEEN '2019-01-01' AND '2020-01-01'.

How can I improve performance on this query?
You can create an index on the column. However casting strings to dates for an index isn't straightforward, as the conversion isn't recognized as &quot;immutable&quot; by Postgres (although it is).
With a little workaround, this can be done though:
create function dob(p_value jsonb)
returns date 
as
$$
  select (p_value -&gt;&gt; 'dob')::date;
$$
language sql
immutable;

Then you can create an index:
create index on the_table ( (dob(obj)) );

And use the function in the WHERE clause:
SELECT * 
FROM the_table 
WHERE dob(obj) BETWEEN DATE '2019-01-01' AND DATE '2020-01-01'