aboutsummaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2018-08-11 15:53:20 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2018-08-11 15:53:20 -0400
commit0ff8f521d40f271c57f29f0ca773981e4465b35b (patch)
tree3934eaf5287a12c66e1a956a49240ad96fdd0bbc /src/backend
parentafff44303cc316d2fe8ad15ac5a5fc90d59dcd67 (diff)
downloadpostgresql-0ff8f521d40f271c57f29f0ca773981e4465b35b.tar.gz
postgresql-0ff8f521d40f271c57f29f0ca773981e4465b35b.zip
Fix wrong order of operations in inheritance_planner.
When considering a partitioning parent rel, we should stop processing that subroot as soon as we've done adjust_appendrel_attrs and any securityQuals updates. The rest of this is unnecessary, and indeed adding duplicate subquery RTEs to the subroot is *wrong*. As the code stood, the children of that partition ended up with two sets of copied subquery RTEs, confusing matters greatly. Even more hilarity ensued if all of the children got excluded by constraint exclusion, so that the extra RTEs didn't make it back into the parent rtable. Per fuzz testing by Andreas Seltenreich. Back-patch to v11 where this got broken (by commit 0a480502b, it looks like). Discussion: https://postgr.es/m/87va8g7vq0.fsf@ansel.ydns.eu
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/optimizer/plan/planner.c106
1 files changed, 56 insertions, 50 deletions
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 173f0d21fed..f982aeb0495 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -1324,6 +1324,59 @@ inheritance_planner(PlannerInfo *root)
parent_rte->securityQuals = NIL;
/*
+ * Mark whether we're planning a query to a partitioned table or an
+ * inheritance parent.
+ */
+ subroot->inhTargetKind =
+ partitioned_relids ? INHKIND_PARTITIONED : INHKIND_INHERITED;
+
+ /*
+ * If this child is further partitioned, remember it as a parent.
+ * Since a partitioned table does not have any data, we don't need to
+ * create a plan for it, and we can stop processing it here. We do,
+ * however, need to remember its modified PlannerInfo for use when
+ * processing its children, since we'll update their varnos based on
+ * the delta from immediate parent to child, not from top to child.
+ *
+ * Note: a very non-obvious point is that we have not yet added
+ * duplicate subquery RTEs to the subroot's rtable. We mustn't,
+ * because then its children would have two sets of duplicates,
+ * confusing matters.
+ */
+ if (child_rte->inh)
+ {
+ Assert(child_rte->relkind == RELKIND_PARTITIONED_TABLE);
+ parent_relids = bms_add_member(parent_relids, appinfo->child_relid);
+ parent_roots[appinfo->child_relid] = subroot;
+
+ continue;
+ }
+
+ /*
+ * Set the nominal target relation of the ModifyTable node if not
+ * already done. We use the inheritance parent RTE as the nominal
+ * target relation if it's a partitioned table (see just above this
+ * loop). In the non-partitioned parent case, we'll use the first
+ * child relation (even if it's excluded) as the nominal target
+ * relation. Because of the way expand_inherited_rtentry works, the
+ * latter should be the RTE representing the parent table in its role
+ * as a simple member of the inheritance set.
+ *
+ * It would be logically cleaner to *always* use the inheritance
+ * parent RTE as the nominal relation; but that RTE is not otherwise
+ * referenced in the plan in the non-partitioned inheritance case.
+ * Instead the duplicate child RTE created by expand_inherited_rtentry
+ * is used elsewhere in the plan, so using the original parent RTE
+ * would give rise to confusing use of multiple aliases in EXPLAIN
+ * output for what the user will think is the "same" table. OTOH,
+ * it's not a problem in the partitioned inheritance case, because the
+ * duplicate child RTE added for the parent does not appear anywhere
+ * else in the plan tree.
+ */
+ if (nominalRelation < 0)
+ nominalRelation = appinfo->child_relid;
+
+ /*
* The rowMarks list might contain references to subquery RTEs, so
* make a copy that we can apply ChangeVarNodes to. (Fortunately, the
* executor doesn't need to see the modified copies --- we can just
@@ -1426,57 +1479,10 @@ inheritance_planner(PlannerInfo *root)
/* and we haven't created PlaceHolderInfos, either */
Assert(subroot->placeholder_list == NIL);
- /*
- * Mark if we're planning a query to a partitioned table or an
- * inheritance parent.
- */
- subroot->inhTargetKind =
- partitioned_relids ? INHKIND_PARTITIONED : INHKIND_INHERITED;
-
- /*
- * If the child is further partitioned, remember it as a parent. Since
- * a partitioned table does not have any data, we don't need to create
- * a plan for it. We do, however, need to remember the PlannerInfo for
- * use when processing its children.
- */
- if (child_rte->inh)
- {
- Assert(child_rte->relkind == RELKIND_PARTITIONED_TABLE);
- parent_relids =
- bms_add_member(parent_relids, appinfo->child_relid);
- parent_roots[appinfo->child_relid] = subroot;
-
- continue;
- }
-
/* Generate Path(s) for accessing this result relation */
grouping_planner(subroot, true, 0.0 /* retrieve all tuples */ );
/*
- * Set the nomimal target relation of the ModifyTable node if not
- * already done. We use the inheritance parent RTE as the nominal
- * target relation if it's a partitioned table (see just above this
- * loop). In the non-partitioned parent case, we'll use the first
- * child relation (even if it's excluded) as the nominal target
- * relation. Because of the way expand_inherited_rtentry works, the
- * latter should be the RTE representing the parent table in its role
- * as a simple member of the inheritance set.
- *
- * It would be logically cleaner to *always* use the inheritance
- * parent RTE as the nominal relation; but that RTE is not otherwise
- * referenced in the plan in the non-partitioned inheritance case.
- * Instead the duplicate child RTE created by expand_inherited_rtentry
- * is used elsewhere in the plan, so using the original parent RTE
- * would give rise to confusing use of multiple aliases in EXPLAIN
- * output for what the user will think is the "same" table. OTOH,
- * it's not a problem in the partitioned inheritance case, because the
- * duplicate child RTE added for the parent does not appear anywhere
- * else in the plan tree.
- */
- if (nominalRelation < 0)
- nominalRelation = appinfo->child_relid;
-
- /*
* Select cheapest path in case there's more than one. We always run
* modification queries to conclusion, so we care only for the
* cheapest-total path.
@@ -1493,9 +1499,9 @@ inheritance_planner(PlannerInfo *root)
continue;
/*
- * Add the current parent's RT index to the partitione_rels set if
- * we're going to create the ModifyTable path for a partitioned root
- * table.
+ * Add the current parent's RT index to the partitioned_relids set if
+ * we're creating the ModifyTable path for a partitioned root table.
+ * (We only care about parents of non-excluded children.)
*/
if (partitioned_relids)
partitioned_relids = bms_add_member(partitioned_relids,