1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Speed up planning when partitions can be pruned at plan time.

Previously, the planner created RangeTblEntry and RelOptInfo structs
for every partition of a partitioned table, even though many of them
might later be deemed uninteresting thanks to partition pruning logic.
This incurred significant overhead when there are many partitions.
Arrange to postpone creation of these data structures until after
we've processed the query enough to identify restriction quals for
the partitioned table, and then apply partition pruning before not
after creation of each partition's data structures.  In this way
we need not open the partition relations at all for partitions that
the planner has no real interest in.

For queries that can be proven at plan time to access only a small
number of partitions, this patch improves the practical maximum
number of partitions from under 100 to perhaps a few thousand.

Amit Langote, reviewed at various times by Dilip Kumar, Jesper Pedersen,
Yoshikazu Imai, and David Rowley

Discussion: https://postgr.es/m/9d7c5112-cb99-6a47-d3be-cf1ee6862a1d@lab.ntt.co.jp
This commit is contained in:
Tom Lane
2019-03-30 18:58:55 -04:00
parent ad3107b973
commit 428b260f87
16 changed files with 888 additions and 622 deletions

View File

@ -144,7 +144,7 @@ SELECT c, sum(a) FROM pagg_tab WHERE 1 = 2 GROUP BY c;
QUERY PLAN
--------------------------------
HashAggregate
Group Key: pagg_tab.c
Group Key: c
-> Result
One-Time Filter: false
(4 rows)
@ -159,7 +159,7 @@ SELECT c, sum(a) FROM pagg_tab WHERE c = 'x' GROUP BY c;
QUERY PLAN
--------------------------------
GroupAggregate
Group Key: pagg_tab.c
Group Key: c
-> Result
One-Time Filter: false
(4 rows)

View File

@ -2568,6 +2568,60 @@ table ab;
1 | 3
(1 row)
-- Test UPDATE where source relation has run-time pruning enabled
truncate ab;
insert into ab values (1, 1), (1, 2), (1, 3), (2, 1);
explain (analyze, costs off, summary off, timing off)
update ab_a1 set b = 3 from ab_a2 where ab_a2.b = (select 1);
QUERY PLAN
----------------------------------------------------------------------
Update on ab_a1 (actual rows=0 loops=1)
Update on ab_a1_b1
Update on ab_a1_b2
Update on ab_a1_b3
InitPlan 1 (returns $0)
-> Result (actual rows=1 loops=1)
-> Nested Loop (actual rows=1 loops=1)
-> Seq Scan on ab_a1_b1 (actual rows=1 loops=1)
-> Materialize (actual rows=1 loops=1)
-> Append (actual rows=1 loops=1)
-> Seq Scan on ab_a2_b1 (actual rows=1 loops=1)
Filter: (b = $0)
-> Seq Scan on ab_a2_b2 (never executed)
Filter: (b = $0)
-> Seq Scan on ab_a2_b3 (never executed)
Filter: (b = $0)
-> Nested Loop (actual rows=1 loops=1)
-> Seq Scan on ab_a1_b2 (actual rows=1 loops=1)
-> Materialize (actual rows=1 loops=1)
-> Append (actual rows=1 loops=1)
-> Seq Scan on ab_a2_b1 (actual rows=1 loops=1)
Filter: (b = $0)
-> Seq Scan on ab_a2_b2 (never executed)
Filter: (b = $0)
-> Seq Scan on ab_a2_b3 (never executed)
Filter: (b = $0)
-> Nested Loop (actual rows=1 loops=1)
-> Seq Scan on ab_a1_b3 (actual rows=1 loops=1)
-> Materialize (actual rows=1 loops=1)
-> Append (actual rows=1 loops=1)
-> Seq Scan on ab_a2_b1 (actual rows=1 loops=1)
Filter: (b = $0)
-> Seq Scan on ab_a2_b2 (never executed)
Filter: (b = $0)
-> Seq Scan on ab_a2_b3 (never executed)
Filter: (b = $0)
(36 rows)
select tableoid::regclass, * from ab;
tableoid | a | b
----------+---+---
ab_a1_b3 | 1 | 3
ab_a1_b3 | 1 | 3
ab_a1_b3 | 1 | 3
ab_a2_b1 | 2 | 1
(4 rows)
drop table ab, lprt_a;
-- Join
create table tbl1(col1 int);

View File

@ -588,6 +588,13 @@ explain (analyze, costs off, summary off, timing off)
update ab_a1 set b = 3 from ab where ab.a = 1 and ab.a = ab_a1.a;
table ab;
-- Test UPDATE where source relation has run-time pruning enabled
truncate ab;
insert into ab values (1, 1), (1, 2), (1, 3), (2, 1);
explain (analyze, costs off, summary off, timing off)
update ab_a1 set b = 3 from ab_a2 where ab_a2.b = (select 1);
select tableoid::regclass, * from ab;
drop table ab, lprt_a;
-- Join