From: Alban Hertroys <haramrae@gmail.com>
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: Thu, 28 Nov 2019 09:03:10 +0100
Message-ID: <E2161293-2446-47E1-A805-E3B74757D5A7@gmail.com> (raw)
In-Reply-To: <CAKWoFMJTND5-qt9bKkj7c=j10xUGhsYTSbtK57sSDxBcc=Hu=A@mail.gmail.com>
References: <CAKWoFMJTND5-qt9bKkj7c=j10xUGhsYTSbtK57sSDxBcc=Hu=A@mail.gmail.com>
> On 27 Nov 2019, at 10:32, Lauri Kajan <lauri.kajan@gmail.com> wrote:
>
> Hi all,
> I'm wondering if there are anything to do to utilize a index when doing a range contains element query. I have tested this with 9.6 and 12.0.
>
> 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?
Does it help to use timestamps -infinity and infinity instead of nulls in your case?
=> select t, t < current_timestamp, current_timestamp <= t from (values ('-infinity'::timestamp), ('infinity'::timestamp)) x(t);
t | ?column? | ?column?
-----------+----------+----------
-infinity | t | f
infinity | f | t
(2 rows)
Regards,
Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.
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: haramrae@gmail.com, lauri.kajan@gmail.com
Subject: Re: Range contains element filter not using index of the element column
In-Reply-To: <E2161293-2446-47E1-A805-E3B74757D5A7@gmail.com>
References: <CAKWoFMJTND5-qt9bKkj7c=j10xUGhsYTSbtK57sSDxBcc=Hu=A@mail.gmail.com> <E2161293-2446-47E1-A805-E3B74757D5A7@gmail.com>
* 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