pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedRange contains element filter not using index of the element column
7+ messages / 5 participants
[nested] [flat]
* Range contains element filter not using index of the element column
@ 2019-11-27 09:32 Lauri Kajan <lauri.kajan@gmail.com>
0 siblings, 3 replies; 7+ messages in thread
From: Lauri Kajan @ 2019-11-27 09:32 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
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
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Range contains element filter not using index of the element column
@ 2019-11-27 11:05 Игорь Выскорко <vyskorko.igor@yandex.ru>
parent: Lauri Kajan <lauri.kajan@gmail.com>
2 siblings, 1 reply; 7+ messages in thread
From: Игорь Выскорко @ 2019-11-27 11:05 UTC (permalink / raw)
To: Lauri Kajan <lauri.kajan@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
27.11.2019, 16:32, "Lauri Kajan" <lauri.kajan@gmail.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 &>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Range contains element filter not using index of the element column
@ 2019-11-27 11:33 Lauri Kajan <lauri.kajan@gmail.com>
parent: Игорь Выскорко <vyskorko.igor@yandex.ru>
0 siblings, 1 reply; 7+ messages in thread
From: Lauri Kajan @ 2019-11-27 11:33 UTC (permalink / raw)
To: Игорь Выскорко <vyskorko.igor@yandex.ru>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Wed, Nov 27, 2019 at 1:05 PM Игорь Выскорко <vyskorko.igor@yandex.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.
-Lauri
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Range contains element filter not using index of the element column
@ 2019-11-27 13:36 Joe Conway <mail@joeconway.com>
parent: Lauri Kajan <lauri.kajan@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Joe Conway @ 2019-11-27 13:36 UTC (permalink / raw)
To: Lauri Kajan <lauri.kajan@gmail.com>; Игорь Выскорко <vyskorko.igor@yandex.ru>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On 11/27/19 6:33 AM, Lauri Kajan wrote:
> On Wed, Nov 27, 2019 at 1:05 PM Игорь Выскорко <vyskorko.igor@yandex.ru
> <mailto:vyskorko.igor@yandex.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
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Range contains element filter not using index of the element column
@ 2019-11-27 15:13 Tom Lane <tgl@sss.pgh.pa.us>
parent: Lauri Kajan <lauri.kajan@gmail.com>
2 siblings, 1 reply; 7+ messages in thread
From: Tom Lane @ 2019-11-27 15:13 UTC (permalink / raw)
To: Lauri Kajan <lauri.kajan@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
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
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Range contains element filter not using index of the element column
@ 2019-11-28 06:15 Lauri Kajan <lauri.kajan@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 7+ messages in thread
From: Lauri Kajan @ 2019-11-28 06:15 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Thank you Jon and Tom!
Both of those ideas seem to work.
Do you think this is worth of a feature request? Would there be any use if
btree index is used in these certain situations directly with @>?
Thanks,
Lauri
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Range contains element filter not using index of the element column
@ 2019-11-28 08:03 Alban Hertroys <haramrae@gmail.com>
parent: Lauri Kajan <lauri.kajan@gmail.com>
2 siblings, 0 replies; 7+ messages in thread
From: Alban Hertroys @ 2019-11-28 08:03 UTC (permalink / raw)
To: Lauri Kajan <lauri.kajan@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
> 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.
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2019-11-28 08:03 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-11-27 09:32 Range contains element filter not using index of the element column Lauri Kajan <lauri.kajan@gmail.com>
2019-11-27 11:05 ` Игорь Выскорко <vyskorko.igor@yandex.ru>
2019-11-27 11:33 ` Lauri Kajan <lauri.kajan@gmail.com>
2019-11-27 13:36 ` Joe Conway <mail@joeconway.com>
2019-11-27 15:13 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-11-28 06:15 ` Lauri Kajan <lauri.kajan@gmail.com>
2019-11-28 08:03 ` Alban Hertroys <haramrae@gmail.com>
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