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

From: Joe Conway <mail(at)joeconway(dot)com>
To: Lauri Kajan <lauri(dot)kajan(at)gmail(dot)com>, Игорь Выскорко <vyskorko(dot)igor(at)yandex(dot)ru>
Cc: "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 13:36:17
Message-ID: 2eae9b64-e1e6-279e-2134-928d97322ecc@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 11/27/19 6:33 AM, Lauri Kajan wrote:
> On Wed, Nov 27, 2019 at 1:05 PM Игорь Выскорко <vyskorko(dot)igor(at)yandex(dot)ru
> <mailto:vyskorko(dot)igor(at)yandex(dot)ru>> wrote:
>
> 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 &>
>
>
> Hi,
>
> I have understood that gist indexes can be used if the column is range
> type but my column is just plain timestamp.
> I tried actually to add gist index for the timestamp column. That was
> not possible without installing the btree_gist extension. But that
> didn't work.

Try this:

create table tstest(id int, ts timestamptz);
insert into tstest
select
g.i,
now() - (g.i::text || ' days')::interval
from generate_series(1, 100000) as g(i);

create index tstest_gin
on tstest using gist((tstzrange(ts,ts,'[]')));

explain analyze
select * from tstest
where
tstzrange(ts,ts,'[]') <@
tstzrange(now()- '9 days'::interval,
now()-'7 days'::interval,'(]');
QUERY PLAN

--------------------------------------------------------
Bitmap Heap Scan on tstest (cost=24.17..590.16 rows=500 width=12)
(actual time=0.069..0.070 rows=2 loops=1)
Recheck Cond: (tstzrange(ts, ts, '[]'::text) <@ tstzrange((now() - '9
days'::interval), (now() - '7 days'::interval), '(]'::text))
Heap Blocks: exact=1
-> Bitmap Index Scan on tstest_gin (cost=0.00..24.04 rows=500
width=0) (actual time=0.063..0.063 rows=2 loops=1)
Index Cond: (tstzrange(ts, ts, '[]'::text) <@ tstzrange((now()
- '9 days'::interval), (now() - '7 days'::interval), '(]'::text))
Planning Time: 20.920 ms
Execution Time: 0.115 ms
(7 rows)

HTH,

Joe

--
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Олег Самойлов 2019-11-27 15:06:44 pg_restore with connection limit 0
Previous Message Lauri Kajan 2019-11-27 11:33:50 Re: Range contains element filter not using index of the element column