Commit 428b260f authored by Tom Lane's avatar Tom Lane

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
parent ad3107b9
...@@ -7141,9 +7141,9 @@ select * from bar where f1 in (select f1 from foo) for update; ...@@ -7141,9 +7141,9 @@ select * from bar where f1 in (select f1 from foo) for update;
QUERY PLAN QUERY PLAN
---------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------
LockRows LockRows
Output: bar.f1, bar.f2, bar.ctid, bar.*, bar.tableoid, foo.ctid, foo.*, foo.tableoid Output: bar.f1, bar.f2, bar.ctid, foo.ctid, bar.*, bar.tableoid, foo.*, foo.tableoid
-> Hash Join -> Hash Join
Output: bar.f1, bar.f2, bar.ctid, bar.*, bar.tableoid, foo.ctid, foo.*, foo.tableoid Output: bar.f1, bar.f2, bar.ctid, foo.ctid, bar.*, bar.tableoid, foo.*, foo.tableoid
Inner Unique: true Inner Unique: true
Hash Cond: (bar.f1 = foo.f1) Hash Cond: (bar.f1 = foo.f1)
-> Append -> Append
...@@ -7153,15 +7153,15 @@ select * from bar where f1 in (select f1 from foo) for update; ...@@ -7153,15 +7153,15 @@ select * from bar where f1 in (select f1 from foo) for update;
Output: bar2.f1, bar2.f2, bar2.ctid, bar2.*, bar2.tableoid Output: bar2.f1, bar2.f2, bar2.ctid, bar2.*, bar2.tableoid
Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct2 FOR UPDATE Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct2 FOR UPDATE
-> Hash -> Hash
Output: foo.ctid, foo.*, foo.tableoid, foo.f1 Output: foo.ctid, foo.f1, foo.*, foo.tableoid
-> HashAggregate -> HashAggregate
Output: foo.ctid, foo.*, foo.tableoid, foo.f1 Output: foo.ctid, foo.f1, foo.*, foo.tableoid
Group Key: foo.f1 Group Key: foo.f1
-> Append -> Append
-> Seq Scan on public.foo -> Seq Scan on public.foo
Output: foo.ctid, foo.*, foo.tableoid, foo.f1 Output: foo.ctid, foo.f1, foo.*, foo.tableoid
-> Foreign Scan on public.foo2 -> Foreign Scan on public.foo2
Output: foo2.ctid, foo2.*, foo2.tableoid, foo2.f1 Output: foo2.ctid, foo2.f1, foo2.*, foo2.tableoid
Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct1 Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct1
(23 rows) (23 rows)
...@@ -7179,9 +7179,9 @@ select * from bar where f1 in (select f1 from foo) for share; ...@@ -7179,9 +7179,9 @@ select * from bar where f1 in (select f1 from foo) for share;
QUERY PLAN QUERY PLAN
---------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------
LockRows LockRows
Output: bar.f1, bar.f2, bar.ctid, bar.*, bar.tableoid, foo.ctid, foo.*, foo.tableoid Output: bar.f1, bar.f2, bar.ctid, foo.ctid, bar.*, bar.tableoid, foo.*, foo.tableoid
-> Hash Join -> Hash Join
Output: bar.f1, bar.f2, bar.ctid, bar.*, bar.tableoid, foo.ctid, foo.*, foo.tableoid Output: bar.f1, bar.f2, bar.ctid, foo.ctid, bar.*, bar.tableoid, foo.*, foo.tableoid
Inner Unique: true Inner Unique: true
Hash Cond: (bar.f1 = foo.f1) Hash Cond: (bar.f1 = foo.f1)
-> Append -> Append
...@@ -7191,15 +7191,15 @@ select * from bar where f1 in (select f1 from foo) for share; ...@@ -7191,15 +7191,15 @@ select * from bar where f1 in (select f1 from foo) for share;
Output: bar2.f1, bar2.f2, bar2.ctid, bar2.*, bar2.tableoid Output: bar2.f1, bar2.f2, bar2.ctid, bar2.*, bar2.tableoid
Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct2 FOR SHARE Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct2 FOR SHARE
-> Hash -> Hash
Output: foo.ctid, foo.*, foo.tableoid, foo.f1 Output: foo.ctid, foo.f1, foo.*, foo.tableoid
-> HashAggregate -> HashAggregate
Output: foo.ctid, foo.*, foo.tableoid, foo.f1 Output: foo.ctid, foo.f1, foo.*, foo.tableoid
Group Key: foo.f1 Group Key: foo.f1
-> Append -> Append
-> Seq Scan on public.foo -> Seq Scan on public.foo
Output: foo.ctid, foo.*, foo.tableoid, foo.f1 Output: foo.ctid, foo.f1, foo.*, foo.tableoid
-> Foreign Scan on public.foo2 -> Foreign Scan on public.foo2
Output: foo2.ctid, foo2.*, foo2.tableoid, foo2.f1 Output: foo2.ctid, foo2.f1, foo2.*, foo2.tableoid
Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct1 Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct1
(23 rows) (23 rows)
...@@ -7228,15 +7228,15 @@ update bar set f2 = f2 + 100 where f1 in (select f1 from foo); ...@@ -7228,15 +7228,15 @@ update bar set f2 = f2 + 100 where f1 in (select f1 from foo);
-> Seq Scan on public.bar -> Seq Scan on public.bar
Output: bar.f1, bar.f2, bar.ctid Output: bar.f1, bar.f2, bar.ctid
-> Hash -> Hash
Output: foo.ctid, foo.*, foo.tableoid, foo.f1 Output: foo.ctid, foo.f1, foo.*, foo.tableoid
-> HashAggregate -> HashAggregate
Output: foo.ctid, foo.*, foo.tableoid, foo.f1 Output: foo.ctid, foo.f1, foo.*, foo.tableoid
Group Key: foo.f1 Group Key: foo.f1
-> Append -> Append
-> Seq Scan on public.foo -> Seq Scan on public.foo
Output: foo.ctid, foo.*, foo.tableoid, foo.f1 Output: foo.ctid, foo.f1, foo.*, foo.tableoid
-> Foreign Scan on public.foo2 -> Foreign Scan on public.foo2
Output: foo2.ctid, foo2.*, foo2.tableoid, foo2.f1 Output: foo2.ctid, foo2.f1, foo2.*, foo2.tableoid
Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct1 Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct1
-> Hash Join -> Hash Join
Output: bar2.f1, (bar2.f2 + 100), bar2.f3, bar2.ctid, foo.ctid, foo.*, foo.tableoid Output: bar2.f1, (bar2.f2 + 100), bar2.f3, bar2.ctid, foo.ctid, foo.*, foo.tableoid
...@@ -7246,15 +7246,15 @@ update bar set f2 = f2 + 100 where f1 in (select f1 from foo); ...@@ -7246,15 +7246,15 @@ update bar set f2 = f2 + 100 where f1 in (select f1 from foo);
Output: bar2.f1, bar2.f2, bar2.f3, bar2.ctid Output: bar2.f1, bar2.f2, bar2.f3, bar2.ctid
Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct2 FOR UPDATE Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct2 FOR UPDATE
-> Hash -> Hash
Output: foo.ctid, foo.*, foo.tableoid, foo.f1 Output: foo.ctid, foo.f1, foo.*, foo.tableoid
-> HashAggregate -> HashAggregate
Output: foo.ctid, foo.*, foo.tableoid, foo.f1 Output: foo.ctid, foo.f1, foo.*, foo.tableoid
Group Key: foo.f1 Group Key: foo.f1
-> Append -> Append
-> Seq Scan on public.foo -> Seq Scan on public.foo
Output: foo.ctid, foo.*, foo.tableoid, foo.f1 Output: foo.ctid, foo.f1, foo.*, foo.tableoid
-> Foreign Scan on public.foo2 -> Foreign Scan on public.foo2
Output: foo2.ctid, foo2.*, foo2.tableoid, foo2.f1 Output: foo2.ctid, foo2.f1, foo2.*, foo2.tableoid
Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct1 Remote SQL: SELECT f1, f2, f3, ctid FROM public.loct1
(39 rows) (39 rows)
...@@ -8460,7 +8460,7 @@ SELECT t1.a,t2.b,t2.c FROM fprt1 t1 LEFT JOIN (SELECT * FROM fprt2 WHERE a < 10) ...@@ -8460,7 +8460,7 @@ SELECT t1.a,t2.b,t2.c FROM fprt1 t1 LEFT JOIN (SELECT * FROM fprt2 WHERE a < 10)
Foreign Scan Foreign Scan
Output: t1.a, ftprt2_p1.b, ftprt2_p1.c Output: t1.a, ftprt2_p1.b, ftprt2_p1.c
Relations: (public.ftprt1_p1 t1) LEFT JOIN (public.ftprt2_p1 fprt2) Relations: (public.ftprt1_p1 t1) LEFT JOIN (public.ftprt2_p1 fprt2)
Remote SQL: SELECT r5.a, r7.b, r7.c FROM (public.fprt1_p1 r5 LEFT JOIN public.fprt2_p1 r7 ON (((r5.a = r7.b)) AND ((r5.b = r7.a)) AND ((r7.a < 10)))) WHERE ((r5.a < 10)) ORDER BY r5.a ASC NULLS LAST, r7.b ASC NULLS LAST, r7.c ASC NULLS LAST Remote SQL: SELECT r5.a, r6.b, r6.c FROM (public.fprt1_p1 r5 LEFT JOIN public.fprt2_p1 r6 ON (((r5.a = r6.b)) AND ((r5.b = r6.a)) AND ((r6.a < 10)))) WHERE ((r5.a < 10)) ORDER BY r5.a ASC NULLS LAST, r6.b ASC NULLS LAST, r6.c ASC NULLS LAST
(4 rows) (4 rows)
SELECT t1.a,t2.b,t2.c FROM fprt1 t1 LEFT JOIN (SELECT * FROM fprt2 WHERE a < 10) t2 ON (t1.a = t2.b and t1.b = t2.a) WHERE t1.a < 10 ORDER BY 1,2,3; SELECT t1.a,t2.b,t2.c FROM fprt1 t1 LEFT JOIN (SELECT * FROM fprt2 WHERE a < 10) t2 ON (t1.a = t2.b and t1.b = t2.a) WHERE t1.a < 10 ORDER BY 1,2,3;
......
...@@ -1654,9 +1654,17 @@ ExecCreatePartitionPruneState(PlanState *planstate, ...@@ -1654,9 +1654,17 @@ ExecCreatePartitionPruneState(PlanState *planstate,
memcpy(pprune->subplan_map, pinfo->subplan_map, memcpy(pprune->subplan_map, pinfo->subplan_map,
sizeof(int) * pinfo->nparts); sizeof(int) * pinfo->nparts);
/* Double-check that list of relations has not changed. */ /*
Assert(memcmp(partdesc->oids, pinfo->relid_map, * Double-check that the list of unpruned relations has not
pinfo->nparts * sizeof(Oid)) == 0); * changed. (Pruned partitions are not in relid_map[].)
*/
#ifdef USE_ASSERT_CHECKING
for (int k = 0; k < pinfo->nparts; k++)
{
Assert(partdesc->oids[k] == pinfo->relid_map[k] ||
pinfo->subplan_map[k] == -1);
}
#endif
} }
else else
{ {
......
...@@ -139,9 +139,6 @@ static void subquery_push_qual(Query *subquery, ...@@ -139,9 +139,6 @@ static void subquery_push_qual(Query *subquery,
static void recurse_push_qual(Node *setOp, Query *topquery, static void recurse_push_qual(Node *setOp, Query *topquery,
RangeTblEntry *rte, Index rti, Node *qual); RangeTblEntry *rte, Index rti, Node *qual);
static void remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel); static void remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel);
static bool apply_child_basequals(PlannerInfo *root, RelOptInfo *rel,
RelOptInfo *childrel,
RangeTblEntry *childRTE, AppendRelInfo *appinfo);
/* /*
...@@ -396,8 +393,9 @@ set_rel_size(PlannerInfo *root, RelOptInfo *rel, ...@@ -396,8 +393,9 @@ set_rel_size(PlannerInfo *root, RelOptInfo *rel,
else if (rte->relkind == RELKIND_PARTITIONED_TABLE) else if (rte->relkind == RELKIND_PARTITIONED_TABLE)
{ {
/* /*
* A partitioned table without any partitions is marked as * We could get here if asked to scan a partitioned table
* a dummy rel. * with ONLY. In that case we shouldn't scan any of the
* partitions, so mark it as a dummy rel.
*/ */
set_dummy_rel_pathlist(rel); set_dummy_rel_pathlist(rel);
} }
...@@ -946,8 +944,6 @@ set_append_rel_size(PlannerInfo *root, RelOptInfo *rel, ...@@ -946,8 +944,6 @@ set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
double *parent_attrsizes; double *parent_attrsizes;
int nattrs; int nattrs;
ListCell *l; ListCell *l;
Relids live_children = NULL;
bool did_pruning = false;
/* Guard against stack overflow due to overly deep inheritance tree. */ /* Guard against stack overflow due to overly deep inheritance tree. */
check_stack_depth(); check_stack_depth();
...@@ -965,21 +961,6 @@ set_append_rel_size(PlannerInfo *root, RelOptInfo *rel, ...@@ -965,21 +961,6 @@ set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
if (rte->relkind == RELKIND_PARTITIONED_TABLE) if (rte->relkind == RELKIND_PARTITIONED_TABLE)
rel->partitioned_child_rels = list_make1_int(rti); rel->partitioned_child_rels = list_make1_int(rti);
/*
* If the partitioned relation has any baserestrictinfo quals then we
* attempt to use these quals to prune away partitions that cannot
* possibly contain any tuples matching these quals. In this case we'll
* store the relids of all partitions which could possibly contain a
* matching tuple, and skip anything else in the loop below.
*/
if (enable_partition_pruning &&
rte->relkind == RELKIND_PARTITIONED_TABLE &&
rel->baserestrictinfo != NIL)
{
live_children = prune_append_rel_partitions(rel);
did_pruning = true;
}
/* /*
* If this is a partitioned baserel, set the consider_partitionwise_join * If this is a partitioned baserel, set the consider_partitionwise_join
* flag; currently, we only consider partitionwise joins with the baserel * flag; currently, we only consider partitionwise joins with the baserel
...@@ -1034,30 +1015,17 @@ set_append_rel_size(PlannerInfo *root, RelOptInfo *rel, ...@@ -1034,30 +1015,17 @@ set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
childrel = find_base_rel(root, childRTindex); childrel = find_base_rel(root, childRTindex);
Assert(childrel->reloptkind == RELOPT_OTHER_MEMBER_REL); Assert(childrel->reloptkind == RELOPT_OTHER_MEMBER_REL);
if (did_pruning && !bms_is_member(appinfo->child_relid, live_children)) /* We may have already proven the child to be dummy. */
{ if (IS_DUMMY_REL(childrel))
/* This partition was pruned; skip it. */
set_dummy_rel_pathlist(childrel);
continue; continue;
}
/* /*
* We have to copy the parent's targetlist and quals to the child, * We have to copy the parent's targetlist and quals to the child,
* with appropriate substitution of variables. If any constant false * with appropriate substitution of variables. However, the
* or NULL clauses turn up, we can disregard the child right away. If * baserestrictinfo quals were already copied/substituted when the
* not, we can apply constraint exclusion with just the * child RelOptInfo was built. So we don't need any additional setup
* baserestrictinfo quals. * before applying constraint exclusion.
*/
if (!apply_child_basequals(root, rel, childrel, childRTE, appinfo))
{
/*
* Some restriction clause reduced to constant FALSE or NULL after
* substitution, so this child need not be scanned.
*/ */
set_dummy_rel_pathlist(childrel);
continue;
}
if (relation_excluded_by_constraints(root, childrel, childRTE)) if (relation_excluded_by_constraints(root, childrel, childRTE))
{ {
/* /*
...@@ -1069,7 +1037,8 @@ set_append_rel_size(PlannerInfo *root, RelOptInfo *rel, ...@@ -1069,7 +1037,8 @@ set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
} }
/* /*
* CE failed, so finish copying/modifying targetlist and join quals. * Constraint exclusion failed, so copy the parent's join quals and
* targetlist to the child, with appropriate variable substitutions.
* *
* NB: the resulting childrel->reltarget->exprs may contain arbitrary * NB: the resulting childrel->reltarget->exprs may contain arbitrary
* expressions, which otherwise would not occur in a rel's targetlist. * expressions, which otherwise would not occur in a rel's targetlist.
...@@ -3596,133 +3565,6 @@ generate_partitionwise_join_paths(PlannerInfo *root, RelOptInfo *rel) ...@@ -3596,133 +3565,6 @@ generate_partitionwise_join_paths(PlannerInfo *root, RelOptInfo *rel)
list_free(live_children); list_free(live_children);
} }
/*
* apply_child_basequals
* Populate childrel's quals based on rel's quals, translating them using
* appinfo.
*
* If any of the resulting clauses evaluate to false or NULL, we return false
* and don't apply any quals. Caller can mark the relation as a dummy rel in
* this case, since it needn't be scanned.
*
* If any resulting clauses evaluate to true, they're unnecessary and we don't
* apply then.
*/
static bool
apply_child_basequals(PlannerInfo *root, RelOptInfo *rel,
RelOptInfo *childrel, RangeTblEntry *childRTE,
AppendRelInfo *appinfo)
{
List *childquals;
Index cq_min_security;
ListCell *lc;
/*
* The child rel's targetlist might contain non-Var expressions, which
* means that substitution into the quals could produce opportunities for
* const-simplification, and perhaps even pseudoconstant quals. Therefore,
* transform each RestrictInfo separately to see if it reduces to a
* constant or pseudoconstant. (We must process them separately to keep
* track of the security level of each qual.)
*/
childquals = NIL;
cq_min_security = UINT_MAX;
foreach(lc, rel->baserestrictinfo)
{
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
Node *childqual;
ListCell *lc2;
Assert(IsA(rinfo, RestrictInfo));
childqual = adjust_appendrel_attrs(root,
(Node *) rinfo->clause,
1, &appinfo);
childqual = eval_const_expressions(root, childqual);
/* check for flat-out constant */
if (childqual && IsA(childqual, Const))
{
if (((Const *) childqual)->constisnull ||
!DatumGetBool(((Const *) childqual)->constvalue))
{
/* Restriction reduces to constant FALSE or NULL */
return false;
}
/* Restriction reduces to constant TRUE, so drop it */
continue;
}
/* might have gotten an AND clause, if so flatten it */
foreach(lc2, make_ands_implicit((Expr *) childqual))
{
Node *onecq = (Node *) lfirst(lc2);
bool pseudoconstant;
/* check for pseudoconstant (no Vars or volatile functions) */
pseudoconstant =
!contain_vars_of_level(onecq, 0) &&
!contain_volatile_functions(onecq);
if (pseudoconstant)
{
/* tell createplan.c to check for gating quals */
root->hasPseudoConstantQuals = true;
}
/* reconstitute RestrictInfo with appropriate properties */
childquals = lappend(childquals,
make_restrictinfo((Expr *) onecq,
rinfo->is_pushed_down,
rinfo->outerjoin_delayed,
pseudoconstant,
rinfo->security_level,
NULL, NULL, NULL));
/* track minimum security level among child quals */
cq_min_security = Min(cq_min_security, rinfo->security_level);
}
}
/*
* In addition to the quals inherited from the parent, we might have
* securityQuals associated with this particular child node. (Currently
* this can only happen in appendrels originating from UNION ALL;
* inheritance child tables don't have their own securityQuals, see
* expand_inherited_rtentry().) Pull any such securityQuals up into the
* baserestrictinfo for the child. This is similar to
* process_security_barrier_quals() for the parent rel, except that we
* can't make any general deductions from such quals, since they don't
* hold for the whole appendrel.
*/
if (childRTE->securityQuals)
{
Index security_level = 0;
foreach(lc, childRTE->securityQuals)
{
List *qualset = (List *) lfirst(lc);
ListCell *lc2;
foreach(lc2, qualset)
{
Expr *qual = (Expr *) lfirst(lc2);
/* not likely that we'd see constants here, so no check */
childquals = lappend(childquals,
make_restrictinfo(qual,
true, false, false,
security_level,
NULL, NULL, NULL));
cq_min_security = Min(cq_min_security, security_level);
}
security_level++;
}
Assert(security_level <= root->qual_security_level);
}
/*
* OK, we've got all the baserestrictinfo quals for this child.
*/
childrel->baserestrictinfo = childquals;
childrel->baserestrict_min_security = cq_min_security;
return true;
}
/***************************************************************************** /*****************************************************************************
* DEBUG SUPPORT * DEBUG SUPPORT
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "nodes/nodeFuncs.h" #include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h" #include "optimizer/clauses.h"
#include "optimizer/cost.h" #include "optimizer/cost.h"
#include "optimizer/inherit.h"
#include "optimizer/joininfo.h" #include "optimizer/joininfo.h"
#include "optimizer/optimizer.h" #include "optimizer/optimizer.h"
#include "optimizer/pathnode.h" #include "optimizer/pathnode.h"
...@@ -159,12 +160,7 @@ add_other_rels_to_query(PlannerInfo *root) ...@@ -159,12 +160,7 @@ add_other_rels_to_query(PlannerInfo *root)
/* If it's marked as inheritable, look for children. */ /* If it's marked as inheritable, look for children. */
if (rte->inh) if (rte->inh)
{ expand_inherited_rtentry(root, rel, rte, rti);
/* Only relation and subquery RTEs can have children. */
Assert(rte->rtekind == RTE_RELATION ||
rte->rtekind == RTE_SUBQUERY);
add_appendrel_other_rels(root, rel, rti);
}
} }
} }
......
This diff is collapsed.
...@@ -121,7 +121,9 @@ preprocess_targetlist(PlannerInfo *root) ...@@ -121,7 +121,9 @@ preprocess_targetlist(PlannerInfo *root)
/* /*
* Add necessary junk columns for rowmarked rels. These values are needed * Add necessary junk columns for rowmarked rels. These values are needed
* for locking of rels selected FOR UPDATE/SHARE, and to do EvalPlanQual * for locking of rels selected FOR UPDATE/SHARE, and to do EvalPlanQual
* rechecking. See comments for PlanRowMark in plannodes.h. * rechecking. See comments for PlanRowMark in plannodes.h. If you
* change this stanza, see also expand_inherited_rtentry(), which has to
* be able to add on junk columns equivalent to these.
*/ */
foreach(lc, root->rowMarks) foreach(lc, root->rowMarks)
{ {
......
This diff is collapsed.
...@@ -2113,7 +2113,10 @@ set_relation_partition_info(PlannerInfo *root, RelOptInfo *rel, ...@@ -2113,7 +2113,10 @@ set_relation_partition_info(PlannerInfo *root, RelOptInfo *rel,
{ {
PartitionDesc partdesc; PartitionDesc partdesc;
Assert(relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE); /* Create the PartitionDirectory infrastructure if we didn't already */
if (root->glob->partition_directory == NULL)
root->glob->partition_directory =
CreatePartitionDirectory(CurrentMemoryContext);
partdesc = PartitionDirectoryLookup(root->glob->partition_directory, partdesc = PartitionDirectoryLookup(root->glob->partition_directory,
relation); relation);
......
...@@ -20,11 +20,11 @@ ...@@ -20,11 +20,11 @@
#include "optimizer/appendinfo.h" #include "optimizer/appendinfo.h"
#include "optimizer/clauses.h" #include "optimizer/clauses.h"
#include "optimizer/cost.h" #include "optimizer/cost.h"
#include "optimizer/inherit.h"
#include "optimizer/pathnode.h" #include "optimizer/pathnode.h"
#include "optimizer/paths.h" #include "optimizer/paths.h"
#include "optimizer/placeholder.h" #include "optimizer/placeholder.h"
#include "optimizer/plancat.h" #include "optimizer/plancat.h"
#include "optimizer/prep.h"
#include "optimizer/restrictinfo.h" #include "optimizer/restrictinfo.h"
#include "optimizer/tlist.h" #include "optimizer/tlist.h"
#include "partitioning/partbounds.h" #include "partitioning/partbounds.h"
...@@ -131,6 +131,49 @@ setup_append_rel_array(PlannerInfo *root) ...@@ -131,6 +131,49 @@ setup_append_rel_array(PlannerInfo *root)
} }
} }
/*
* expand_planner_arrays
* Expand the PlannerInfo's per-RTE arrays by add_size members
* and initialize the newly added entries to NULLs
*/
void
expand_planner_arrays(PlannerInfo *root, int add_size)
{
int new_size;
Assert(add_size > 0);
new_size = root->simple_rel_array_size + add_size;
root->simple_rte_array = (RangeTblEntry **)
repalloc(root->simple_rte_array,
sizeof(RangeTblEntry *) * new_size);
MemSet(root->simple_rte_array + root->simple_rel_array_size,
0, sizeof(RangeTblEntry *) * add_size);
root->simple_rel_array = (RelOptInfo **)
repalloc(root->simple_rel_array,
sizeof(RelOptInfo *) * new_size);
MemSet(root->simple_rel_array + root->simple_rel_array_size,
0, sizeof(RelOptInfo *) * add_size);
if (root->append_rel_array)
{
root->append_rel_array = (AppendRelInfo **)
repalloc(root->append_rel_array,
sizeof(AppendRelInfo *) * new_size);
MemSet(root->append_rel_array + root->simple_rel_array_size,
0, sizeof(AppendRelInfo *) * add_size);
}
else
{
root->append_rel_array = (AppendRelInfo **)
palloc0(sizeof(AppendRelInfo *) * new_size);
}
root->simple_rel_array_size = new_size;
}
/* /*
* build_simple_rel * build_simple_rel
* Construct a new RelOptInfo for a base relation or 'other' relation. * Construct a new RelOptInfo for a base relation or 'other' relation.
...@@ -281,93 +324,42 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent) ...@@ -281,93 +324,42 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
break; break;
} }
/* Save the finished struct in the query's simple_rel_array */
root->simple_rel_array[relid] = rel;
/* /*
* This is a convenient spot at which to note whether rels participating * This is a convenient spot at which to note whether rels participating
* in the query have any securityQuals attached. If so, increase * in the query have any securityQuals attached. If so, increase
* root->qual_security_level to ensure it's larger than the maximum * root->qual_security_level to ensure it's larger than the maximum
* security level needed for securityQuals. * security level needed for securityQuals. (Must do this before we call
* apply_child_basequals, else we'll hit an Assert therein.)
*/ */
if (rte->securityQuals) if (rte->securityQuals)
root->qual_security_level = Max(root->qual_security_level, root->qual_security_level = Max(root->qual_security_level,
list_length(rte->securityQuals)); list_length(rte->securityQuals));
return rel;
}
/*
* add_appendrel_other_rels
* Add "other rel" RelOptInfos for the children of an appendrel baserel
*
* "rel" is a relation that (still) has the rte->inh flag set, meaning it
* has appendrel children listed in root->append_rel_list. We need to build
* a RelOptInfo for each child relation so that we can plan scans on them.
* (The parent relation might be a partitioned table, a table with
* traditional inheritance children, or a flattened UNION ALL subquery.)
*/
void
add_appendrel_other_rels(PlannerInfo *root, RelOptInfo *rel, Index rti)
{
int cnt_parts = 0;
ListCell *l;
/* /*
* If rel is a partitioned table, then we also need to build a part_rels * Copy the parent's quals to the child, with appropriate substitution of
* array so that the child RelOptInfos can be conveniently accessed from * variables. If any constant false or NULL clauses turn up, we can mark
* the parent. * the child as dummy right away. (We must do this immediately so that
* pruning works correctly when recursing in expand_partitioned_rtentry.)
*/ */
if (rel->part_scheme != NULL) if (parent)
{ {
Assert(rel->nparts > 0); AppendRelInfo *appinfo = root->append_rel_array[relid];
rel->part_rels = (RelOptInfo **)
palloc0(sizeof(RelOptInfo *) * rel->nparts);
}
foreach(l, root->append_rel_list) Assert(appinfo != NULL);
if (!apply_child_basequals(root, parent, rel, rte, appinfo))
{ {
AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
Index childRTindex = appinfo->child_relid;
RangeTblEntry *childrte;
RelOptInfo *childrel;
/* append_rel_list contains all append rels; ignore others */
if (appinfo->parent_relid != rti)
continue;
/* find the child RTE, which should already exist */
Assert(childRTindex < root->simple_rel_array_size);
childrte = root->simple_rte_array[childRTindex];
Assert(childrte != NULL);
/* build child RelOptInfo, and add to main query data structures */
childrel = build_simple_rel(root, childRTindex, rel);
/* /*
* If rel is a partitioned table, fill in the part_rels array. The * Some restriction clause reduced to constant FALSE or NULL after
* order in which child tables appear in append_rel_list is the same * substitution, so this child need not be scanned.
* as the order in which they appear in the parent's PartitionDesc, so
* assigning partitions like this works.
*/ */
if (rel->part_scheme != NULL) mark_dummy_rel(rel);
{
Assert(cnt_parts < rel->nparts);
rel->part_rels[cnt_parts++] = childrel;
}
/* Child may itself be an inherited relation. */
if (childrte->inh)
{
/* Only relation and subquery RTEs can have children. */
Assert(childrte->rtekind == RTE_RELATION ||
childrte->rtekind == RTE_SUBQUERY);
add_appendrel_other_rels(root, childrel, childRTindex);
} }
} }
/* We should have filled all of the part_rels array if it's partitioned */ /* Save the finished struct in the query's simple_rel_array */
Assert(cnt_parts == rel->nparts); root->simple_rel_array[relid] = rel;
return rel;
} }
/* /*
......
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
#include "nodes/makefuncs.h" #include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h" #include "nodes/nodeFuncs.h"
#include "optimizer/appendinfo.h" #include "optimizer/appendinfo.h"
#include "optimizer/cost.h"
#include "optimizer/optimizer.h" #include "optimizer/optimizer.h"
#include "optimizer/pathnode.h" #include "optimizer/pathnode.h"
#include "parser/parsetree.h" #include "parser/parsetree.h"
...@@ -474,18 +475,24 @@ make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel, ...@@ -474,18 +475,24 @@ make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel,
* is, not pruned already). * is, not pruned already).
*/ */
subplan_map = (int *) palloc(nparts * sizeof(int)); subplan_map = (int *) palloc(nparts * sizeof(int));
memset(subplan_map, -1, nparts * sizeof(int));
subpart_map = (int *) palloc(nparts * sizeof(int)); subpart_map = (int *) palloc(nparts * sizeof(int));
relid_map = (Oid *) palloc(nparts * sizeof(Oid)); memset(subpart_map, -1, nparts * sizeof(int));
relid_map = (Oid *) palloc0(nparts * sizeof(Oid));
present_parts = NULL; present_parts = NULL;
for (i = 0; i < nparts; i++) for (i = 0; i < nparts; i++)
{ {
RelOptInfo *partrel = subpart->part_rels[i]; RelOptInfo *partrel = subpart->part_rels[i];
int subplanidx = relid_subplan_map[partrel->relid] - 1; int subplanidx;
int subpartidx = relid_subpart_map[partrel->relid] - 1; int subpartidx;
subplan_map[i] = subplanidx; /* Skip processing pruned partitions. */
subpart_map[i] = subpartidx; if (partrel == NULL)
continue;
subplan_map[i] = subplanidx = relid_subplan_map[partrel->relid] - 1;
subpart_map[i] = subpartidx = relid_subpart_map[partrel->relid] - 1;
relid_map[i] = planner_rt_fetch(partrel->relid, root)->relid; relid_map[i] = planner_rt_fetch(partrel->relid, root)->relid;
if (subplanidx >= 0) if (subplanidx >= 0)
{ {
...@@ -567,29 +574,33 @@ gen_partprune_steps(RelOptInfo *rel, List *clauses, bool *contradictory) ...@@ -567,29 +574,33 @@ gen_partprune_steps(RelOptInfo *rel, List *clauses, bool *contradictory)
/* /*
* prune_append_rel_partitions * prune_append_rel_partitions
* Returns RT indexes of the minimum set of child partitions which must * Returns indexes into rel->part_rels of the minimum set of child
* be scanned to satisfy rel's baserestrictinfo quals. * partitions which must be scanned to satisfy rel's baserestrictinfo
* quals.
* *
* Callers must ensure that 'rel' is a partitioned table. * Callers must ensure that 'rel' is a partitioned table.
*/ */
Relids Bitmapset *
prune_append_rel_partitions(RelOptInfo *rel) prune_append_rel_partitions(RelOptInfo *rel)
{ {
Relids result;
List *clauses = rel->baserestrictinfo; List *clauses = rel->baserestrictinfo;
List *pruning_steps; List *pruning_steps;
bool contradictory; bool contradictory;
PartitionPruneContext context; PartitionPruneContext context;
Bitmapset *partindexes;
int i;
Assert(clauses != NIL);
Assert(rel->part_scheme != NULL); Assert(rel->part_scheme != NULL);
/* If there are no partitions, return the empty set */ /* If there are no partitions, return the empty set */
if (rel->nparts == 0) if (rel->nparts == 0)
return NULL; return NULL;
/*
* If pruning is disabled or if there are no clauses to prune with, return
* all partitions.
*/
if (!enable_partition_pruning || clauses == NIL)
return bms_add_range(NULL, 0, rel->nparts - 1);
/* /*
* Process clauses. If the clauses are found to be contradictory, we can * Process clauses. If the clauses are found to be contradictory, we can
* return the empty set. * return the empty set.
...@@ -617,15 +628,7 @@ prune_append_rel_partitions(RelOptInfo *rel) ...@@ -617,15 +628,7 @@ prune_append_rel_partitions(RelOptInfo *rel)
context.evalexecparams = false; context.evalexecparams = false;
/* Actual pruning happens here. */ /* Actual pruning happens here. */
partindexes = get_matching_partitions(&context, pruning_steps); return get_matching_partitions(&context, pruning_steps);
/* Add selected partitions' RT indexes to result. */
i = -1;
result = NULL;
while ((i = bms_next_member(partindexes, i)) >= 0)
result = bms_add_member(result, rel->part_rels[i]->relid);
return result;
} }
/* /*
......
...@@ -1103,6 +1103,7 @@ typedef struct PartitionPruneInfo ...@@ -1103,6 +1103,7 @@ typedef struct PartitionPruneInfo
* it is -1 if the partition is a leaf or has been pruned. Note that subplan * it is -1 if the partition is a leaf or has been pruned. Note that subplan
* indexes, as stored in 'subplan_map', are global across the parent plan * indexes, as stored in 'subplan_map', are global across the parent plan
* node, but partition indexes are valid only within a particular hierarchy. * node, but partition indexes are valid only within a particular hierarchy.
* relid_map[p] contains the partition's OID, or 0 if the partition was pruned.
*/ */
typedef struct PartitionedRelPruneInfo typedef struct PartitionedRelPruneInfo
{ {
...@@ -1115,7 +1116,7 @@ typedef struct PartitionedRelPruneInfo ...@@ -1115,7 +1116,7 @@ typedef struct PartitionedRelPruneInfo
int nexprs; /* Length of hasexecparam[] */ int nexprs; /* Length of hasexecparam[] */
int *subplan_map; /* subplan index by partition index, or -1 */ int *subplan_map; /* subplan index by partition index, or -1 */
int *subpart_map; /* subpart index by partition index, or -1 */ int *subpart_map; /* subpart index by partition index, or -1 */
Oid *relid_map; /* relation OID by partition index, or -1 */ Oid *relid_map; /* relation OID by partition index, or 0 */
bool *hasexecparam; /* true if corresponding pruning_step contains bool *hasexecparam; /* true if corresponding pruning_step contains
* any PARAM_EXEC Params. */ * any PARAM_EXEC Params. */
bool do_initial_prune; /* true if pruning should be performed bool do_initial_prune; /* true if pruning should be performed
......
...@@ -17,6 +17,11 @@ ...@@ -17,6 +17,11 @@
#include "nodes/pathnodes.h" #include "nodes/pathnodes.h"
extern void expand_inherited_tables(PlannerInfo *root); extern void expand_inherited_rtentry(PlannerInfo *root, RelOptInfo *rel,
RangeTblEntry *rte, Index rti);
extern bool apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel,
RelOptInfo *childrel, RangeTblEntry *childRTE,
AppendRelInfo *appinfo);
#endif /* INHERIT_H */ #endif /* INHERIT_H */
...@@ -277,10 +277,9 @@ extern Path *reparameterize_path_by_child(PlannerInfo *root, Path *path, ...@@ -277,10 +277,9 @@ extern Path *reparameterize_path_by_child(PlannerInfo *root, Path *path,
*/ */
extern void setup_simple_rel_arrays(PlannerInfo *root); extern void setup_simple_rel_arrays(PlannerInfo *root);
extern void setup_append_rel_array(PlannerInfo *root); extern void setup_append_rel_array(PlannerInfo *root);
extern void expand_planner_arrays(PlannerInfo *root, int add_size);
extern RelOptInfo *build_simple_rel(PlannerInfo *root, int relid, extern RelOptInfo *build_simple_rel(PlannerInfo *root, int relid,
RelOptInfo *parent); RelOptInfo *parent);
extern void add_appendrel_other_rels(PlannerInfo *root, RelOptInfo *rel,
Index rti);
extern RelOptInfo *find_base_rel(PlannerInfo *root, int relid); extern RelOptInfo *find_base_rel(PlannerInfo *root, int relid);
extern RelOptInfo *find_join_rel(PlannerInfo *root, Relids relids); extern RelOptInfo *find_join_rel(PlannerInfo *root, Relids relids);
extern RelOptInfo *build_join_rel(PlannerInfo *root, extern RelOptInfo *build_join_rel(PlannerInfo *root,
......
...@@ -144,7 +144,7 @@ SELECT c, sum(a) FROM pagg_tab WHERE 1 = 2 GROUP BY c; ...@@ -144,7 +144,7 @@ SELECT c, sum(a) FROM pagg_tab WHERE 1 = 2 GROUP BY c;
QUERY PLAN QUERY PLAN
-------------------------------- --------------------------------
HashAggregate HashAggregate
Group Key: pagg_tab.c Group Key: c
-> Result -> Result
One-Time Filter: false One-Time Filter: false
(4 rows) (4 rows)
...@@ -159,7 +159,7 @@ SELECT c, sum(a) FROM pagg_tab WHERE c = 'x' GROUP BY c; ...@@ -159,7 +159,7 @@ SELECT c, sum(a) FROM pagg_tab WHERE c = 'x' GROUP BY c;
QUERY PLAN QUERY PLAN
-------------------------------- --------------------------------
GroupAggregate GroupAggregate
Group Key: pagg_tab.c Group Key: c
-> Result -> Result
One-Time Filter: false One-Time Filter: false
(4 rows) (4 rows)
......
...@@ -2568,6 +2568,60 @@ table ab; ...@@ -2568,6 +2568,60 @@ table ab;
1 | 3 1 | 3
(1 row) (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; drop table ab, lprt_a;
-- Join -- Join
create table tbl1(col1 int); create table tbl1(col1 int);
......
...@@ -588,6 +588,13 @@ explain (analyze, costs off, summary off, timing off) ...@@ -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; update ab_a1 set b = 3 from ab where ab.a = 1 and ab.a = ab_a1.a;
table ab; 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; drop table ab, lprt_a;
-- Join -- Join
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment