pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>
To: zbigniew.szot@softiq.pl
To: pgsql-bugs@postgresql.org
Subject: Re: BUG #14565: query planner does not use partial index in partiton if query is performed on multiple partitions
Date: Fri, 24 Feb 2017 14:15:06 +0900
Message-ID: <50bb62ba-e275-04ff-276b-3f0d49c493ca@lab.ntt.co.jp> (raw)
In-Reply-To: <20170223111027.1528.80806@wrigleys.postgresql.org>
References: <20170223111027.1528.80806@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

On 2017/02/23 20:10, zbigniew.szot@softiq.pl wrote:
> The following bug has been logged on the website:
> 
> Bug reference:      14565
> Logged by:          Zbigniew Szot
> Email address:      zbigniew.szot@softiq.pl
> PostgreSQL version: 9.5.6
> Operating system:    linux and  AWS cloud (9.6.1)
> Description:        
> 
> drop schema if exists bug_showcase cascade;
> create schema bug_showcase;
> set search_path to bug_showcase;
> 
> -- crate table
> CREATE TABLE  test_table (
>     chec_key UUID  NOT NULL  ,
>    some_date  date,
>     some_data_2 varchar,
>      some_data_3 varchar,
>       some_data_4 varchar
>     );
> -- partiton the table + indexes in partitions    
> DO $$
> BEGIN
>  FOR i IN 0..15 LOOP
>    EXECUTE 'CREATE TABLE test_table_'||to_hex(i)||' ( CHECK (chec_key >=
> UUID '''||to_hex(i)||'0000000-0000-0000-0000-000000000000'' AND chec_key <=
> UUID '''||
>         to_hex(i)||'fffffff-ffff-ffff-ffff-ffffffffffff'' ) ) INHERITS
> (test_table);';
>         EXECUTE 'CREATE INDEX test_table_'||to_hex(i)||'_brin on
> test_table_'||to_hex(i)||' USING brin (chec_key, some_date);';
>     END LOOP;
> END$$;
> 
> -- this index would be used instead of partial ones but thats not what we
> need 
> drop index test_table_4_brin;
> 
> -- create partial indexes in one of partitions 
> DO $$
> BEGIN
>  FOR i IN 0..15 LOOP
>         EXECUTE 'create index partial_not_working_'||to_hex(i)||' on
> test_table_4 USING brin ( chec_key, some_date) where (chec_key >= UUID
> ''4'||to_hex(i)||'000000-0000-0000-0000-000000000000'' AND chec_key <= UUID
> ''4'||
>         to_hex(i)||'ffffff-ffff-ffff-ffff-ffffffffffff'' );';
> END LOOP;   
> END$$;
> 
> -- populate table
> 
> insert into test_table_1  (chec_key , some_date , some_data_2 ,
> some_data_3,some_data_4 ) select  
> uuid('1'||lpad(to_hex(i),5,'0')||'00-0000-4000-a000-000000000000'),date
> '2015-10-1' + random() *  interval '2 days' ,md5(random()::text)
> ,md5(random()::text),md5(random()::text)  from generate_Series(0,1048575) i
> ;
> insert into test_table_3  (chec_key , some_date , some_data_2 ,
> some_data_3,some_data_4  ) select  
> uuid('3'||lpad(to_hex(i),5,'0')||'00-0000-4000-a000-000000000000'),date
> '2015-10-1' + random() *  interval '2 days' ,md5(random()::text)
> ,md5(random()::text),md5(random()::text)  from generate_Series(0,1048575) i
> ;
> insert into test_table_4  (chec_key , some_date , some_data_2 ,
> some_data_3,some_data_4  ) select  
> uuid('4'||lpad(to_hex(i),5,'0')||'00-0000-4000-a000-000000000000'),date
> '2015-10-1' + random() *  interval '2 days' ,md5(random()::text)
> ,md5(random()::text),md5(random()::text)  from generate_Series(0,1048575) i
> ;
> insert into test_table_7  (chec_key , some_date , some_data_2 ,
> some_data_3,some_data_4  ) select  
> uuid('7'||lpad(to_hex(i),5,'0')||'00-0000-4000-a000-000000000000'),date
> '2015-10-1' + random() *  interval '2 days' ,md5(random()::text)
> ,md5(random()::text),md5(random()::text)  from generate_Series(0,1048575) i
> ;
> 
> -- .. ;-)
> analyse test_table;
> 
> -- make strong sugestion to use indexes if possible
> set enable_seqscan = off;
> -- this one uses  partial_not_working_4
> explain select * from test_table where chec_key  in
> ('4400df00-0000-4000-a000-000000000000' )and some_date <'2015-11-02';
> -- this one uses  partial_not_working_4
> explain select * from test_table where chec_key  in
> ('4400df00-0000-4000-a000-000000000000','4401df00-0000-4000-a000-000000000000'
> )and some_date <'2015-11-02';
> 
> -- this one DOES NOT use  partial_not_working_4 .. bug or feature ? ;-)
> explain select * from test_table where chec_key  in
> ('4400df00-0000-4000-a000-000000000000'
> ,'1400df00-0000-4000-a000-000000000000')and some_date <'2015-11-02';

Not a bug, I'd think.

The WHERE condition does not imply partial_not_working_4's predicate, so
it cannot possibly be used.  The index does not contain entries for some
of the rows being requested by the query (i.e. those for which chec_key =
'1400df00-0000-4000-a000-000000000000').

A simpler example:

create table foo (a) as select generate_series(1, 100000);
create index on foo (a) where a >= 1 and a < 10;

explain (costs off) select * from foo where a in (1, 9);
                     QUERY PLAN
----------------------------------------------------
 Bitmap Heap Scan on foo
   Recheck Cond: (a = ANY ('{1,9}'::integer[]))
   ->  Bitmap Index Scan on foo_a_idx
         Index Cond: (a = ANY ('{1,9}'::integer[]))
(4 rows)

-- foo_a_idx does not contain 10
explain (costs off) select * from foo where a in (1, 10);
                QUERY PLAN
-------------------------------------------
 Seq Scan on foo
   Filter: (a = ANY ('{1,10}'::integer[]))
(2 rows)

Thanks,
Amit




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



view thread (4+ messages)  latest in thread

Message-ID: <50bb62ba-e275-04ff-276b-3f0d49c493ca@lab.ntt.co.jp>
Permalink:  ../50bb62ba-e275-04ff-276b-3f0d49c493ca@lab.ntt.co.jp/
Also on:    postgresql.org/message-id/50bb62ba-e275-04ff-276b-3f0d49c493ca@lab.ntt.co.jp

 · 

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: Langote_Amit_f8@lab.ntt.co.jp, zbigniew.szot@softiq.pl
  Subject: Re: BUG #14565: query planner does not use partial index in partiton if query is performed on multiple partitions
  In-Reply-To: <50bb62ba-e275-04ff-276b-3f0d49c493ca@lab.ntt.co.jp>
  References: <20170223111027.1528.80806@wrigleys.postgresql.org> <50bb62ba-e275-04ff-276b-3f0d49c493ca@lab.ntt.co.jp>

* 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