pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Lauri Kajan <lauri.kajan@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Range contains element filter not using index of the element column
Date: Wed, 27 Nov 2019 10:13:59 -0500
Message-ID: <6965.1574867639@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKWoFMJTND5-qt9bKkj7c=j10xUGhsYTSbtK57sSDxBcc=Hu=A@mail.gmail.com>
References: <CAKWoFMJTND5-qt9bKkj7c=j10xUGhsYTSbtK57sSDxBcc=Hu=A@mail.gmail.com>

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





view thread (7+ messages)  latest in thread

Message-ID: <6965.1574867639@sss.pgh.pa.us>
Permalink:  ../6965.1574867639@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/6965.1574867639@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, lauri.kajan@gmail.com
  Subject: Re: Range contains element filter not using index of the element column
  In-Reply-To: <6965.1574867639@sss.pgh.pa.us>
  References: <CAKWoFMJTND5-qt9bKkj7c=j10xUGhsYTSbtK57sSDxBcc=Hu=A@mail.gmail.com> <6965.1574867639@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox