Re: Range contains element filter not using index of the element column

From: Игорь Выскорко <vyskorko(dot)igor(at)yandex(dot)ru>
To: Lauri Kajan <lauri(dot)kajan(at)gmail(dot)com>, "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Range contains element filter not using index of the element column
Date: 2019-11-27 11:05:33
Message-ID: 91984401574852733@iva5-64778ce1ba26.qloud-c.yandex.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

27.11.2019, 16:32, "Lauri Kajan" <lauri(dot)kajan(at)gmail(dot)com>:
> 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?
>
> Thanks,
>
> -Lauri

Hi!
Do you use GIST index?
According to https://www.postgresql.org/docs/12/rangetypes.html#RANGETYPES-INDEXING <@ operator is supported:
> A GiST or SP-GiST index can accelerate queries involving these range operators: =, &&, <@, @>, <<, >>, -|-, &<, and &>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Lauri Kajan 2019-11-27 11:33:50 Re: Range contains element filter not using index of the element column
Previous Message Игорь Выскорко 2019-11-27 10:32:51 Re: Weird seqscan node plan