Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jThsA-0002FW-GH for pgsql-bugs@arkaria.postgresql.org; Wed, 29 Apr 2020 08:15:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jThs9-0008TR-B4 for pgsql-bugs@arkaria.postgresql.org; Wed, 29 Apr 2020 08:15:13 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jThs9-0008TH-1O for pgsql-bugs@lists.postgresql.org; Wed, 29 Apr 2020 08:15:13 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jThs4-000494-T0 for pgsql-bugs@lists.postgresql.org; Wed, 29 Apr 2020 08:15:11 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 6B35C21C2074 for ; Wed, 29 Apr 2020 11:15:04 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.8.101] (unknown [176.59.52.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id E485321C04FB for ; Wed, 29 Apr 2020 11:15:03 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1588148104; bh=CspMmb/nfoIj55sCEynfyWsjHJ3npFLmKPpXBTw8wFM=; h=To:From:Subject:Date; b=MOMWZIsEORThr+JGlmYNpb+DLChCf9+U2ZPRh4WUC/ZUniE9jZiSuA7KAvBkri+R+ 1H7HRFq6Yjx+l4DaI0f0fFGvLOPTpVW2u4N8JROiizz8Bm6kWsym4Lh0o2SvWdzuWN wC/Ls0GvcYyRqNG+Ofk76U/cqn3T4CKi5eNe0h1Q= To: pgsql-bugs@lists.postgresql.org From: Konstantin Knizhnik Subject: Equality of columns isn't taken in account when performing partition pruning Message-ID: Date: Wed, 29 Apr 2020 11:15:03 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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.