pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: noskov.d@gmail.com
To: pgsql-bugs@postgresql.org
Subject: BUG #14711: Inconsistent statistics on expression with partial index
Date: Sun, 18 Jun 2017 12:57:51 +0000
Message-ID: <20170618125751.1544.95074@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

The following bug has been logged on the website:

Bug reference:      14711
Logged by:          Dzmitry Naskou
Email address:      noskov.d@gmail.com
PostgreSQL version: 9.6.1
Operating system:   Debian 6.2.0-10
Description:        

[PostgreSQL 9.6.1 on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.2.0-10)
6.2.0 20161027, 64-bit]

I have a table with timestamp ranges:

create table testing.test as 
select tsrange(d, null) ts from 
generate_series(timestamp '2000-01-01', timestamp '2018-01-01', interval '1
minute') s(d);
I need to run the following query:

select * 
from testing.test 
where lower(ts)> '2017-06-17 20:00:00'::timestamp and upper_inf(ts)
Explain analyze result for table without indexes:

Seq Scan on test  (cost=0.00..72482.26 rows=1052013 width=14) (actual
time=2165.477..2239.781 rows=283920 loops=1)
Filter: (upper_inf(ts) AND (lower(ts) > '2017-06-17 20:00:00'::timestamp
without time zone))
Rows Removed by Filter: 9184081
Planning time: 0.046 ms
Execution time: 2250.221 ms
Next I'm going to add a following partial index:

create index lower_rt_inf ON testing.test using btree(lower(ts)) where
upper_inf(ts);    
analyze testing.test;
Explain analyze result for table with partial index:

Index Scan using lower_rt_inf on test  (cost=0.04..10939.03 rows=1051995
width=14) (actual time=0.037..52.083 rows=283920 loops=1)
Index Cond: (lower(ts) > '2017-06-17 20:00:00'::timestamp without time
zone)
Planning time: 0.156 ms
Execution time: 62.900 ms                                                   
                                                     
And:

SELECT null_frac, n_distinct, correlation FROM pg_catalog.pg_stats WHERE
tablename = 'lower_rt_inf'

null_frac |n_distinct |correlation |
----------|-----------|------------|
0         |-1         |1           |
Then I create an index similar to the previous one, but without partial
condition:

create index lower_rt_full ON testing.test using btree(lower(ts));
analyze testing.test;
And now the same index is used, but the cost/rows are different:

Index Scan using lower_rt_inf on test  (cost=0.04..1053.87 rows=101256
width=14) (actual time=0.029..58.613 rows=283920 loops=1)
Index Cond: (lower(ts) > '2017-06-17 20:00:00'::timestamp without time
zone)
Planning time: 0.280 ms
Execution time: 71.794 ms                                                   
                                                   
And a bit more: select * from testing.test where lower(ts)> '2017-06-17
20:00:00'::timestamp

Index Scan using lower_rt_full on test  (cost=0.04..3159.52 rows=303767
width=14) (actual time=0.036..64.208 rows=283920 loops=1)
Index Cond: (lower(ts) > '2017-06-17 20:00:00'::timestamp without time
zone)
Planning time: 0.099 ms
Execution time: 78.759 ms

How can I effectively use partial indexes for expressions?


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


Message-ID: <20170618125751.1544.95074@wrigleys.postgresql.org>
Permalink:  ../20170618125751.1544.95074@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20170618125751.1544.95074@wrigleys.postgresql.org

 · 

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-bugs@postgresql.org
  Cc: noskov.d@gmail.com
  Subject: Re: BUG #14711: Inconsistent statistics on expression with partial index
  In-Reply-To: <20170618125751.1544.95074@wrigleys.postgresql.org>
  References: <20170618125751.1544.95074@wrigleys.postgresql.org>

* 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