pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Konstantin Knizhnik <k.knizhnik@postgrespro.ru>
To: pgsql-bugs@lists.postgresql.org
Subject: Equality of columns isn't taken in account when performing partition pruning
Date: Wed, 29 Apr 2020 11:15:03 +0300
Message-ID: <effa5924-6b58-b4a8-f282-763f6668e093@postgrespro.ru> (raw)

I noticed some limitation of Postgres optimizer: it doesn't take in 
account information about equality of columns of joined tables when 
performs partition pruning:

create table i (pk integer primary key) partition by range(pk);
create table i_1 partition of i for values from (0) to (10);
create table i_2 partition of i for values from (10) to (20);
create table o (pk integer primary key);

explain select * from o join i on o.pk = i.pk where i.pk between 0 and 9;
explain select * from o join i on o.pk = i.pk where o.pk between 0 and 9;


Plan for the first query is optimal and access only affected partition i_1:
  Nested Loop  (cost=4.44..53.26 rows=13 width=8)
    ->  Bitmap Heap Scan on i_1 i  (cost=4.29..15.02 rows=13 width=4)
          Recheck Cond: ((pk >= 0) AND (pk <= 9))
          ->  Bitmap Index Scan on i_1_pkey  (cost=0.00..4.29 rows=13 
width=0)
                Index Cond: ((pk >= 0) AND (pk <= 9))
    ->  Index Only Scan using o_pkey on o  (cost=0.15..2.94 rows=1 width=4)
          Index Cond: (pk = i.pk)


But plan for the second query (although it is doing actually the same 
things) affects both partitions:

  Nested Loop  (cost=4.44..91.89 rows=26 width=8)
    ->  Bitmap Heap Scan on o  (cost=4.29..15.02 rows=13 width=4)
          Recheck Cond: ((pk >= 0) AND (pk <= 9))
          ->  Bitmap Index Scan on o_pkey  (cost=0.00..4.29 rows=13 width=0)
                Index Cond: ((pk >= 0) AND (pk <= 9))
    ->  Append  (cost=0.15..5.89 rows=2 width=4)
          ->  Index Only Scan using i_1_pkey on i_1 (cost=0.15..2.94 
rows=1 width=4)
                Index Cond: (pk = o.pk)
          ->  Index Only Scan using i_2_pkey on i_2 (cost=0.15..2.94 
rows=1 width=4)
                Index Cond: (pk = o.pk)


I have not investigated yet how difficult it will be to fix it.
May be there is some person more familiar with this part of optimizer 
than I.
But if nobody wants to look at it, I can try to investigate it myself.







view thread (2+ messages)  latest in thread

Message-ID: <effa5924-6b58-b4a8-f282-763f6668e093@postgrespro.ru>
Permalink:  ../effa5924-6b58-b4a8-f282-763f6668e093@postgrespro.ru/
Also on:    postgresql.org/message-id/effa5924-6b58-b4a8-f282-763f6668e093@postgrespro.ru

 · 

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: k.knizhnik@postgrespro.ru
  Subject: Re: Equality of columns isn't taken in account when performing partition pruning
  In-Reply-To: <effa5924-6b58-b4a8-f282-763f6668e093@postgrespro.ru>
  References: <effa5924-6b58-b4a8-f282-763f6668e093@postgrespro.ru>

* 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