Re: Range contains element filter not using index of the element column - Mailing list pgsql-general

From Tom Lane
Subject Re: Range contains element filter not using index of the element column
Date
Msg-id 6965.1574867639@sss.pgh.pa.us
Whole thread Raw
In response to Range contains element filter not using index of the element column  (Lauri Kajan <lauri.kajan@gmail.com>)
Responses Re: Range contains element filter not using index of the element column  (Lauri Kajan <lauri.kajan@gmail.com>)
List pgsql-general
Lauri Kajan <lauri.kajan@gmail.com> writes:
> I have a table with a timestamp column that has a btree index.
> I would like to do a query:
> SELECT * FROM table WHERE ts <@ tsrange($1, $2, '(]');
> The index is not used and a seq scan is done instead.
> To use the index correctly I have to do the query like this:
> SELECT * FROM table WHERE  ($1 IS null OR $1 < ts) AND ($2 IS null OR ts <=
> $2);
> I like the <@ syntax more. Is there something I can do differently? Maybe a
> different type of index instead?

As others mentioned, a gist index on a tsrange expression could be
used for this, but another idea is to build some syntactic sugar
using a custom operator.  Light testing suggests that this works:

create function expand_range_contain(anyelement, anyrange)
returns bool language sql parallel safe as
$$ select
case when lower_inf($2) then true
     when lower_inc($2) then $1 >= lower($2)
     else $1 > lower($2) end
and
case when upper_inf($2) then true
     when upper_inc($2) then $1 <= upper($2)
     else $1 < upper($2) end
$$;

create operator <<@ (
  function = expand_range_contain,
  leftarg = anyelement,
  rightarg = anyrange
);

select * from table where ts <<@ tsrange($1, $2, '(]');

An important caveat though is that the range operand *must* reduce
to a constant.  If the planner fails to const-simplify those CASE
expressions, you'll not only not get an indexscan, but you'll be worse
off than with the native <@ operator.  So this isn't an all-purpose
fix --- but it might cover your needs and be nicer than maintaining a
second index on the column.

            regards, tom lane



pgsql-general by date:

Previous
From: Олег Самойлов
Date:
Subject: pg_restore with connection limit 0
Next
From: Adrian Klaver
Date:
Subject: Re: pg_restore with connection limit 0