From 81632e4088bf1b711c80ff07762ac72cd74d9327 Mon Sep 17 00:00:00 2001 From: amit Date: Fri, 8 Feb 2019 16:16:38 +0900 Subject: [PATCH 1/5] Overhaul inheritance update/delete planning With the current method, inheritance_planner, which handles the planning for update/delete commands targeting inheritance trees, repeatedly calls query_planner for each child table. That's inefficient for two reasons: 1. partprune.c cannot be used to perform partition pruning, because it can only be invoked from query_planner. With the current approach, query_planner only sees the individual partitions in the query tree it receives, not the parent partitioned table. This leaves each partition to be pruned using constraint exclusion. (constraint exclusion cannon prune hash partitions which means UPDATE/DELETE don't support hash partition pruning.) 2. Repeated invocation of query_planner results in allocating large amounts of memory, especially if there are many child tables. Also, it involves repeatedly performing the same processing, such as jointree processing. This commit addresses both of the issues by modifying query_planner to handle the inheritance target case directly. Instead of having the caller create separate PlannerInfo's for individual child target relations and pass them to query_planner (actually, via grouping_planner), this teaches query_planner itself to create child PlannerInfo's from the informaton in the original parent PlannerInfo. It creates them *after* it has generated enough state in the original, PlannerInfo, such as RelOptInfos for various relations, EquivalenceClasses after processing the join tree, and *before* generating the scan and join paths. Scan paths are generated normally, except that Paths of target children are not "appended". Join paths are then generated separately for each child target relation, after replacing the parent relation references in the original join tree with the child relation references. Join paths corresponding to each child target are remembered for further processing by stuffing stuffing the joinrel RelOptInfos in a list in the original PlannerInfo. So, inheritance_planner now calls query_planner just once at the beginning with the original unmodified query, which takes care of generating top-level join rels for the child queries as described above. Once join paths have been created for all unpruned child target relations, back in inheritance_planner, the aforementioned paths are adjusted so that they produce the query's top-level target list that is expanded according to a given child relation's descriptor. This removes some existing code in inheritance_planner that dealt with any subquery RTEs in the query. The rationale of that code was that the subquery RTEs may change during each iteration of planning (that is, for different children), so different iterations better use different copies of those RTEs. That was handled by making fresh copies of those RTEs for each iteration of planning which were appended to the range table, accompanied by modifying all expressions and auxiliary structures that referenced the original subquery RTEs to instead reference the copies (that is, change the varnos). This copying meant we would end up adding S * N new entries to the original range table by the time we got to the last unpruned child, where S is the number of subquery RTEs in the original query and N the number of unpruned children. Since with the new code we perform planning just once, I think we don't need this special handling. Actually, there is a regression test output change due to no longer having copies of subquery RTEs (see the diff of partition_join.out file.) Regression test output change in partition_join.out is due to the fact that we no longer create duplicates of subquery RTEs in child range tables. --- doc/src/sgml/ddl.sgml | 17 +- src/backend/nodes/outfuncs.c | 2 + src/backend/optimizer/path/allpaths.c | 449 ++++++++++++++++++++-- src/backend/optimizer/path/equivclass.c | 160 ++++++++ src/backend/optimizer/plan/planmain.c | 41 +- src/backend/optimizer/plan/planner.c | 550 ++++++++++----------------- src/backend/optimizer/util/appendinfo.c | 30 +- src/backend/optimizer/util/inherit.c | 127 +++++++ src/include/nodes/pathnodes.h | 30 ++ src/include/optimizer/inherit.h | 1 + src/include/optimizer/paths.h | 2 + src/test/regress/expected/partition_join.out | 4 +- 12 files changed, 1002 insertions(+), 411 deletions(-) diff --git a/doc/src/sgml/ddl.sgml b/doc/src/sgml/ddl.sgml index 8314fce78f..1da28357ef 100644 --- a/doc/src/sgml/ddl.sgml +++ b/doc/src/sgml/ddl.sgml @@ -4439,24 +4439,11 @@ EXPLAIN SELECT count(*) FROM measurement WHERE logdate >= DATE '2008-01-01'; - Currently, pruning of partitions during the planning of an - UPDATE or DELETE command is - implemented using the constraint exclusion method (however, it is - controlled by the enable_partition_pruning rather than - constraint_exclusion) — see the following section - for details and caveats that apply. - - - Execution-time partition pruning currently only occurs for the Append and MergeAppend node types. It is not yet implemented for the ModifyTable node - type. - - - - Both of these behaviors are likely to be changed in a future release - of PostgreSQL. + type, but that is likely to be changed in a future release of + PostgreSQL. diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 65302fe65b..c5c08efac1 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -2203,6 +2203,7 @@ _outPlannerInfo(StringInfo str, const PlannerInfo *node) WRITE_NODE_FIELD(distinct_pathkeys); WRITE_NODE_FIELD(sort_pathkeys); WRITE_NODE_FIELD(processed_tlist); + WRITE_NODE_FIELD(unexpanded_tlist); WRITE_NODE_FIELD(minmax_aggs); WRITE_FLOAT_FIELD(total_table_pages, "%.0f"); WRITE_FLOAT_FIELD(tuple_fraction, "%.4f"); @@ -2218,6 +2219,7 @@ _outPlannerInfo(StringInfo str, const PlannerInfo *node) WRITE_BITMAPSET_FIELD(curOuterRels); WRITE_NODE_FIELD(curOuterParams); WRITE_BOOL_FIELD(partColsUpdated); + WRITE_NODE_FIELD(inh_target_child_rels); } static void diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index 0debac75c6..0a4729e646 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -39,6 +39,7 @@ #include "optimizer/pathnode.h" #include "optimizer/paths.h" #include "optimizer/plancat.h" +#include "optimizer/planmain.h" #include "optimizer/planner.h" #include "optimizer/restrictinfo.h" #include "optimizer/tlist.h" @@ -92,8 +93,12 @@ static void set_foreign_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte); static void set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte); +static void set_inherited_target_rel_sizes(PlannerInfo *root, RelOptInfo *rel, + Index rti, RangeTblEntry *rte); static void set_append_rel_size(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte); +static void set_inherited_target_rel_pathlists(PlannerInfo *root, + RelOptInfo *rel, Index rti, RangeTblEntry *rte); static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte); static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel, @@ -122,6 +127,8 @@ static void set_result_pathlist(PlannerInfo *root, RelOptInfo *rel, static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte); static RelOptInfo *make_rel_from_joinlist(PlannerInfo *root, List *joinlist); +static RelOptInfo *inheritance_make_rel_from_joinlist(PlannerInfo *root, + List *joinlist); static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery, pushdown_safety_info *safetyInfo); static bool recurse_pushdown_safe(Node *setOp, Query *topquery, @@ -155,27 +162,6 @@ make_one_rel(PlannerInfo *root, List *joinlist) Index rti; double total_pages; - /* - * Construct the all_baserels Relids set. - */ - root->all_baserels = NULL; - for (rti = 1; rti < root->simple_rel_array_size; rti++) - { - RelOptInfo *brel = root->simple_rel_array[rti]; - - /* there may be empty slots corresponding to non-baserel RTEs */ - if (brel == NULL) - continue; - - Assert(brel->relid == rti); /* sanity check on array */ - - /* ignore RTEs that are "other rels" */ - if (brel->reloptkind != RELOPT_BASEREL) - continue; - - root->all_baserels = bms_add_member(root->all_baserels, brel->relid); - } - /* Mark base rels as to whether we care about fast-start plans */ set_base_rel_consider_startup(root); @@ -223,13 +209,35 @@ make_one_rel(PlannerInfo *root, List *joinlist) /* * Generate access paths for the entire join tree. + * + * For UPDATE/DELETE on an inheritance parent, join paths should be + * generated for each child result rel separately. */ - rel = make_rel_from_joinlist(root, joinlist); + if (root->parse->resultRelation > 0 && + root->simple_rte_array[root->parse->resultRelation]->inh) + { + /* + * RelOptInfo corresponding to the query's original target relation + * is returned. Join paths (if any) are attached to child joinrels, + * not this rel. Also, this rel's sole path (ModifyTable) will be set + * by inheritance_planner later, so we can't check its paths yet. + */ + rel = inheritance_make_rel_from_joinlist(root, joinlist); + } + else + { + rel = make_rel_from_joinlist(root, joinlist); - /* - * The result should join all and only the query's base rels. - */ - Assert(bms_equal(rel->relids, root->all_baserels)); + /* + * The result should join all and only the query's base rels. + */ + Assert(bms_equal(rel->relids, root->all_baserels)); + + /* Check that we got at least one usable path */ + if (!rel || !rel->cheapest_total_path || + rel->cheapest_total_path->param_info != NULL) + elog(ERROR, "failed to construct the join relation"); + } return rel; } @@ -361,6 +369,8 @@ static void set_rel_size(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte) { + bool inherited_update = false; + if (rel->reloptkind == RELOPT_BASEREL && relation_excluded_by_constraints(root, rel, rte)) { @@ -379,8 +389,18 @@ set_rel_size(PlannerInfo *root, RelOptInfo *rel, } else if (rte->inh) { - /* It's an "append relation", process accordingly */ - set_append_rel_size(root, rel, rti, rte); + /* + * If it's a target relation, set the sizes of children instead. + * Otherwise, we'll append the outputs of children, so process it as + * an "append relation". + */ + if (rti == root->parse->resultRelation) + { + inherited_update = true; + set_inherited_target_rel_sizes(root, rel, rti, rte); + } + else + set_append_rel_size(root, rel, rti, rte); } else { @@ -457,8 +477,9 @@ set_rel_size(PlannerInfo *root, RelOptInfo *rel, /* * We insist that all non-dummy rels have a nonzero rowcount estimate. + * An exception is target relations that are inheritance parents. */ - Assert(rel->rows > 0 || IS_DUMMY_REL(rel)); + Assert(rel->rows > 0 || IS_DUMMY_REL(rel) || inherited_update); } /* @@ -469,14 +490,26 @@ static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte) { + bool inherited_update = false; + if (IS_DUMMY_REL(rel)) { /* We already proved the relation empty, so nothing more to do */ } else if (rte->inh) { - /* It's an "append relation", process accordingly */ - set_append_rel_pathlist(root, rel, rti, rte); + /* + * If it's a target relation, set the pathlists of children instead. + * Otherwise, we'll append the outputs of children, so process it as + * an "append relation". + */ + if (rti == root->parse->resultRelation) + { + inherited_update = true; + set_inherited_target_rel_pathlists(root, rel, rti, rte); + } + else + set_append_rel_pathlist(root, rel, rti, rte); } else { @@ -530,6 +563,19 @@ set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, } /* + * If the relation is an inheritance parent and the query's result + * relation, no paths have been generated for it yet, so skip the following + * steps. + */ + if (inherited_update) + { +#ifdef OPTIMIZER_DEBUG + debug_print_rel(root, rel); +#endif + return; + } + + /* * Allow a plugin to editorialize on the set of Paths for this base * relation. It could add new paths (such as CustomPaths) by calling * add_path(), or add_partial_path() if parallel aware. It could also @@ -924,6 +970,207 @@ set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte) } /* + * set_inherited_target_rel_sizes + * Set size estimates for the child target relations + * + * The passed-in rel represents the target relation of the query that is + * known to have inheritance children. This is very much like + * set_append_rel_size, except it doesn't set the size estimates for the + * passed-in rel itself, because we don't need to "append" the children + * in this case. + */ +static void +set_inherited_target_rel_sizes(PlannerInfo *root, RelOptInfo *rel, + Index rti, RangeTblEntry *rte) +{ + int parentRTindex = rti; + bool has_live_children = false; + ListCell *l; + Relids live_children = NULL; + bool did_pruning = false; + + /* Guard against stack overflow due to overly deep inheritance tree. */ + check_stack_depth(); + + Assert(IS_SIMPLE_REL(rel)); + + /* + * 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; + } + + foreach(l, root->append_rel_list) + { + AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l); + int childRTindex; + RangeTblEntry *childRTE; + RelOptInfo *childrel; + PlannerInfo *subroot; + ListCell *lc; + List *translated_exprs; + + /* append_rel_list contains all append rels; ignore others */ + if (appinfo->parent_relid != parentRTindex) + continue; + + childRTindex = appinfo->child_relid; + childRTE = root->simple_rte_array[childRTindex]; + + /* + * The child rel's RelOptInfo was already created during + * add_base_rels_to_query. + */ + childrel = find_base_rel(root, childRTindex); + Assert(childrel->reloptkind == RELOPT_OTHER_MEMBER_REL); + + if (did_pruning && !bms_is_member(appinfo->child_relid, live_children)) + { + /* This partition was pruned; skip it. */ + set_dummy_rel_pathlist(childrel); + continue; + } + + /* + * We have to copy the parent's targetlist and quals to the child, + * with appropriate substitution of variables. If any constant false + * or NULL clauses turn up, we can disregard the child right away. + * If not, we can apply constraint exclusion with just the + * baserestrictinfo quals. + */ + 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)) + { + /* + * This child need not be scanned, so we can omit it from the + * appendrel. + */ + set_dummy_rel_pathlist(childrel); + continue; + } + + /* + * Add missing Vars to child's reltarget. + * + * create_inherited_target_child_root() would've added only those that + * are needed due to being referenced in the top-level tlist (or ones + * that preprocess_targetlist thinks are needed to be in the tlist.) + * We may need other attributes such as those contained in WHERE + * clauses, which are already computed for the parent during + * deconstruct_jointree processing of the original query (child's + * query never goes through deconstruct_jointree.) + */ + translated_exprs = (List *) + adjust_appendrel_attrs(root, + (Node *) rel->reltarget->exprs, + 1, &appinfo); + foreach(lc, translated_exprs) + { + Expr *expr = lfirst(lc); + + if (!list_member(childrel->reltarget->exprs, expr)) + childrel->reltarget->exprs = + lappend(childrel->reltarget->exprs, expr); + } + + childrel->has_eclass_joins = rel->has_eclass_joins; + + subroot = root->inh_target_child_roots[childRTindex]; + + /* Translate join quals. */ + childrel->joininfo = (List *) + adjust_appendrel_attrs(subroot, + (Node *) rel->joininfo, + 1, &appinfo); + + /* + * Compute the child's size using possibly modified subroot. + */ + set_rel_size(subroot, childrel, childRTindex, childRTE); + + /* + * If the child is a partitioned table it may have been marked as a + * dummy rel from having all its partitions pruned. + */ + if (IS_DUMMY_REL(childrel)) + continue; + + /* We have at least one live child. */ + has_live_children = true; + + Assert(childrel->rows > 0 || childRTE->inh); + } + + /* + * All children were excluded by constraints, so mark the relation + * as dummy. We must do this in this phase so that the rel's + * dummy-ness is visible when we generate paths for other rels. + */ + if (!has_live_children) + set_dummy_rel_pathlist(rel); +} + +/* + * set_inherited_target_rel_pathlists + * Build access paths for the child target relations + * + * Similar to set_append_rel_pathlist, except that we build paths of the + * children, but don't build an Append path. + */ +static void +set_inherited_target_rel_pathlists(PlannerInfo *root, RelOptInfo *rel, + Index rti, RangeTblEntry *rte) +{ + int parentRTindex = rti; + ListCell *l; + + /* Generate access paths for each of the children of passed-in rel */ + foreach(l, root->append_rel_list) + { + AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l); + int childRTindex; + RangeTblEntry *childRTE; + RelOptInfo *childrel; + PlannerInfo *subroot; + + /* append_rel_list contains all append rels; ignore others */ + if (appinfo->parent_relid != parentRTindex) + continue; + + /* Re-locate the child RTE and RelOptInfo */ + childRTindex = appinfo->child_relid; + childRTE = root->simple_rte_array[childRTindex]; + childrel = root->simple_rel_array[childRTindex]; + subroot = root->inh_target_child_roots[childRTindex]; + /* Transfer the value from main root to subroot. */ + subroot->total_table_pages = root->total_table_pages; + + /* + * Compute the child's access paths. + */ + set_rel_pathlist(subroot, childrel, childRTindex, childRTE); + } +} + +/* * set_append_rel_size * Set size estimates for a simple "append relation" * @@ -2566,6 +2813,146 @@ generate_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows) } /* + * inheritance_make_rel_from_joinlist + * Perform join planning for all non-dummy leaf inheritance children + * in their role as an UPDATE/DELETE query's target relation + * + * If a child relation is a partitioned table, its children are processed in + * turn by recursively calling this function. + */ +static RelOptInfo * +inheritance_make_rel_from_joinlist(PlannerInfo *root, List *joinlist) +{ + Index resultRelation = root->parse->resultRelation; + RelOptInfo *resultrel; + ListCell *lc; + + /* For UPDATE/DELETE queries, the top parent can only ever be a table. */ + Assert(root->parse->commandType == CMD_UPDATE || + root->parse->commandType == CMD_DELETE); + Assert(planner_rt_fetch(resultRelation, root)->rtekind == RTE_RELATION); + resultrel = find_base_rel(root, resultRelation); + + /* Nothing to do. */ + if (IS_DUMMY_REL(resultrel)) + return resultrel; + + foreach(lc, root->append_rel_list) + { + AppendRelInfo *appinfo = lfirst(lc); + PlannerInfo *subroot; + RelOptInfo *childrel; + RelOptInfo *childjoinrel; + List *translated_joinlist; + + if (appinfo->parent_relid != resultRelation) + continue; + + childrel = find_base_rel(root, appinfo->child_relid); + + /* Ignore excluded/pruned children. */ + if (IS_DUMMY_REL(childrel)) + continue; + + /* Add this child. */ + root->inh_target_child_rels = lappend_int(root->inh_target_child_rels, + appinfo->child_relid); + + /* Perform join planning with child subroot. */ + subroot = root->inh_target_child_roots[appinfo->child_relid]; + Assert(subroot->parse->resultRelation > 0); + + /* + * Modify joinlist such that relations joined to the top parent rel + * appear to be joined to the child rel instead. Do the same for + * any SpecialJoinInfo structs. + */ + translated_joinlist = (List *) + adjust_appendrel_attrs(subroot, + (Node *) joinlist, + 1, &appinfo); + subroot->join_info_list = (List *) + adjust_appendrel_attrs(subroot, + (Node *) root->join_info_list, + 1, &appinfo); + + /* + * Sub-partitioned tables have to be processed recursively using the + * translated subroot as the parent, because AppendRelInfos link + * sub-partitions to their immediate parents, not the root partitioned + * table. + */ + if (childrel->part_scheme != NULL) + { + /* + * inheritance_make_rel_from_joinlist() return the target relation + * RelOptInfo. + */ + childrel = + inheritance_make_rel_from_joinlist(subroot, + translated_joinlist); + + /* + * Add this child relation as a placeholder in the parent root's + * inh_target_child_path_rels so that inheritance_planner sees + * same number of entries in it as inh_target_child_rels. + */ + root->inh_target_child_path_rels = + lappend(root->inh_target_child_path_rels, childrel); + + /* + * Also propagate this child's own children into the parent's + * list. + */ + if (subroot->inh_target_child_rels != NIL) + { + root->inh_target_child_rels = + list_concat(root->inh_target_child_rels, + subroot->inh_target_child_rels); + root->inh_target_child_path_rels = + list_concat(root->inh_target_child_path_rels, + subroot->inh_target_child_path_rels); + } + continue; + } + + /* + * Since we added the child rel directly into the join tree, we must + * modify it to be a "base" rel instead of an "other" rel, which the + * join planning code expects the relations being joined to be. + */ + childrel->reloptkind = RELOPT_BASEREL; + + Assert(subroot->join_rel_list == NIL); + Assert(subroot->join_rel_hash == NULL); + + /* Perform join planning and save the resulting RelOptInfo. */ + childjoinrel = make_rel_from_joinlist(subroot, translated_joinlist); + + /* + * The following implements essentially the same Assert as in + * make_one_rel, our caller. + */ + Assert(bms_equal(childjoinrel->relids, subroot->all_baserels)); + + /* Check that we got at least one usable path */ + if (!childjoinrel || !childjoinrel->cheapest_total_path || + childjoinrel->cheapest_total_path->param_info != NULL) + elog(ERROR, "failed to construct the child join relation"); + + /* + * Remember the paths of this child target rel. inheritance_planner + * perform remaining steps necessary fot these paths to produce the + * desired targetlist. + */ + root->inh_target_child_path_rels = + lappend(root->inh_target_child_path_rels, childjoinrel); + } + + return resultrel; +} + +/* * make_rel_from_joinlist * Build access paths using a "joinlist" to guide the join path search. * diff --git a/src/backend/optimizer/path/equivclass.c b/src/backend/optimizer/path/equivclass.c index 23792508b7..11244fc57d 100644 --- a/src/backend/optimizer/path/equivclass.c +++ b/src/backend/optimizer/path/equivclass.c @@ -31,6 +31,11 @@ #include "optimizer/restrictinfo.h" #include "utils/lsyscache.h" +typedef struct translate_eq_classes_context +{ + PlannerInfo *root; + AppendRelInfo *appinfo; +} translate_eq_classes_context; static EquivalenceMember *add_eq_member(EquivalenceClass *ec, Expr *expr, Relids relids, Relids nullable_relids, @@ -64,6 +69,10 @@ static bool reconsider_outer_join_clause(PlannerInfo *root, bool outer_on_left); static bool reconsider_full_join_clause(PlannerInfo *root, RestrictInfo *rinfo); +static EquivalenceClass * adjust_inherited_eq_class(PlannerInfo *root, + EquivalenceClass *new_ec, AppendRelInfo *appinfo); +static Node *translate_eq_classes_mutator(Node *node, + translate_eq_classes_context *context); /* @@ -2185,6 +2194,157 @@ add_child_rel_equivalences(PlannerInfo *root, } } +/* + * translate_eq_classes_for_child + * This creates a copy of the list of EquivalenceClasses, each of + * which is translated with the provided AppendRelInfo + * + * Whereas add_child_rel_equivalences simply adds child Vars to a given + * EquivalenceClasses if it contains the Vars of a given parent relation, + * this creates a whole new EquivalenceClasses copied from the original one + * and if it contains Vars of the parent relation, they're *replaced* with + * the corresponding child Vars. + */ +List * +translate_eq_classes_for_child(PlannerInfo *root, List *eq_classes, + AppendRelInfo *appinfo) +{ + translate_eq_classes_context context; + + context.root = root; + context.appinfo = appinfo; + + return (List *) translate_eq_classes_mutator((Node *) eq_classes, + &context); +} + +static Node * +translate_eq_classes_mutator(Node *node, + translate_eq_classes_context *context) +{ + if (node == NULL) + return NULL; + + if (IsA(node, EquivalenceClass)) + { + EquivalenceClass *ec = (EquivalenceClass *) node; + EquivalenceClass *new_ec = makeNode(EquivalenceClass); + + /* + * First memcpy the existing EC to the new EC, which creates shallow + * copies of all the members and then make copies of members that + * we might change below. + * + * XXX comment in _copyPathKey says it's OK to recycle EC + * pointers, but as long as we do the whole planning for a + * given child using a given root, copying ECs like this + * shouldn't be a problem. Maybe, the following code could + * be in _copyEquivalenceClass()? + */ + memcpy(new_ec, ec, sizeof(EquivalenceClass)); + new_ec->ec_opfamilies = list_copy(ec->ec_opfamilies); + new_ec->ec_sources = list_copy(ec->ec_sources); + new_ec->ec_derives = list_copy(ec->ec_derives); + new_ec->ec_relids = bms_copy(ec->ec_relids); + new_ec->ec_members = list_copy(ec->ec_members); + + /* + * No point in searching if parent rel not mentioned in eclass; but we + * can't tell that for sure if parent rel is itself a child. + */ + if (bms_is_member(context->appinfo->parent_relid, ec->ec_relids)) + new_ec = adjust_inherited_eq_class(context->root, new_ec, + context->appinfo); + + return (Node *) new_ec; + } + + return expression_tree_mutator(node, translate_eq_classes_mutator, + (void *) context); +} + +/* + * adjust_inherited_eq_class + * This translates members of EquivalenceClass such that references to + * an inheritance parent relation are replaced by the references to + * a given child relation + * + * This performs in-place updates of the members of the input struct, so the + * callers should've made a copy of the original EC first. + */ +static EquivalenceClass * +adjust_inherited_eq_class(PlannerInfo *root, EquivalenceClass *new_ec, + AppendRelInfo *appinfo) +{ + ListCell *lc; + RelOptInfo *parent_rel = find_base_rel(root, appinfo->parent_relid), + *child_rel = find_base_rel(root, appinfo->child_relid); + + /* + * Check if this EC contains an expression referencing the parent + * relation, translate it to child, and store it in place of + * the original parent expression. + */ + foreach(lc, new_ec->ec_members) + { + EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc); + + if (cur_em->em_is_const) + continue; /* ignore consts here */ + + /* Does it reference parent_rel? */ + if (bms_overlap(cur_em->em_relids, parent_rel->relids)) + { + /* Yes, generate transformed child version */ + Expr *new_expr; + Relids new_relids; + Relids new_nullable_relids; + + new_expr = (Expr *) + adjust_appendrel_attrs(root, + (Node *) cur_em->em_expr, + 1, &appinfo); + + /* + * Transform em_relids to match. Note we do *not* do + * pull_varnos(child_expr) here, as for example the + * transformation might have substituted a constant, but we + * don't want the child member to be marked as constant. + */ + new_relids = bms_difference(cur_em->em_relids, parent_rel->relids); + new_relids = bms_add_members(new_relids, child_rel->relids); + + /* + * And likewise for nullable_relids. Note this code assumes + * parent and child relids are singletons. + */ + new_nullable_relids = cur_em->em_nullable_relids; + if (bms_overlap(new_nullable_relids, parent_rel->relids)) + { + new_nullable_relids = bms_difference(new_nullable_relids, + parent_rel->relids); + new_nullable_relids = bms_add_members(new_nullable_relids, + child_rel->relids); + } + + /* + * The new expression simply replaces the old parent one, and + * em_is_child is set to true so that it's recognized as such + * during child planning. + */ + lfirst(lc) = add_eq_member(new_ec, new_expr, + new_relids, new_nullable_relids, + false, cur_em->em_datatype); + } + } + + /* Now fix up EC's relids set. */ + new_ec->ec_relids = bms_del_members(new_ec->ec_relids, parent_rel->relids); + new_ec->ec_relids = bms_add_members(new_ec->ec_relids, child_rel->relids); + + return new_ec; +} + /* * generate_implied_equalities_for_column diff --git a/src/backend/optimizer/plan/planmain.c b/src/backend/optimizer/plan/planmain.c index 3cedd01c98..c7f2323852 100644 --- a/src/backend/optimizer/plan/planmain.c +++ b/src/backend/optimizer/plan/planmain.c @@ -20,6 +20,7 @@ */ #include "postgres.h" +#include "nodes/pathnodes.h" #include "optimizer/appendinfo.h" #include "optimizer/clauses.h" #include "optimizer/inherit.h" @@ -29,6 +30,7 @@ #include "optimizer/paths.h" #include "optimizer/placeholder.h" #include "optimizer/planmain.h" +#include "optimizer/prep.h" /* @@ -60,6 +62,7 @@ query_planner(PlannerInfo *root, List *tlist, Query *parse = root->parse; List *joinlist; RelOptInfo *final_rel; + Index rti; /* * Init planner lists to empty. @@ -260,14 +263,42 @@ query_planner(PlannerInfo *root, List *tlist, extract_restriction_or_clauses(root); /* + * Construct the all_baserels Relids set. + */ + root->all_baserels = NULL; + for (rti = 1; rti < root->simple_rel_array_size; rti++) + { + RelOptInfo *brel = root->simple_rel_array[rti]; + + /* there may be empty slots corresponding to non-baserel RTEs */ + if (brel == NULL) + continue; + + Assert(brel->relid == rti); /* sanity check on array */ + + /* ignore RTEs that are "other rels" */ + if (brel->reloptkind != RELOPT_BASEREL) + continue; + + root->all_baserels = bms_add_member(root->all_baserels, brel->relid); + } + + /* + * Add child subroots needed during planning for individual child targets. + */ + if (parse->resultRelation && + root->simple_rte_array[parse->resultRelation]->inh) + { + root->inh_target_child_roots = (PlannerInfo **) + palloc0(root->simple_rel_array_size * + sizeof(PlannerInfo *)); + add_inherited_target_child_roots(root); + } + + /* * Ready to do the primary planning. */ final_rel = make_one_rel(root, joinlist); - /* Check that we got at least one usable path */ - if (!final_rel || !final_rel->cheapest_total_path || - final_rel->cheapest_total_path->param_info != NULL) - elog(ERROR, "failed to construct the join relation"); - return final_rel; } diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 1a58d733fa..cf64688e76 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -130,8 +130,7 @@ typedef struct static Node *preprocess_expression(PlannerInfo *root, Node *expr, int kind); static void preprocess_qual_conditions(PlannerInfo *root, Node *jtnode); static void inheritance_planner(PlannerInfo *root); -static void grouping_planner(PlannerInfo *root, bool inheritance_update, - double tuple_fraction); +static void grouping_planner(PlannerInfo *root, double tuple_fraction); static grouping_sets_data *preprocess_grouping_sets(PlannerInfo *root); static List *remap_to_groupclause_idx(List *groupClause, List *gsets, int *tleref_to_colnum_map); @@ -241,6 +240,10 @@ static void apply_scanjoin_target_to_paths(PlannerInfo *root, List *scanjoin_targets_contain_srfs, bool scanjoin_target_parallel_safe, bool tlist_same_exprs); +static void apply_scanjoin_target_to_paths_guts(PlannerInfo *root, + RelOptInfo *rel, + PathTarget *scanjoin_target, + bool tlist_same_exprs); static void create_partitionwise_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *grouped_rel, @@ -997,7 +1000,7 @@ subquery_planner(PlannerGlobal *glob, Query *parse, rt_fetch(parse->resultRelation, parse->rtable)->inh) inheritance_planner(root); else - grouping_planner(root, false, tuple_fraction); + grouping_planner(root, tuple_fraction); /* * Capture the set of outer-level param IDs we have access to, for use in @@ -1173,13 +1176,38 @@ preprocess_phv_expression(PlannerInfo *root, Expr *expr) * inheritance set. * * We have to handle this case differently from cases where a source relation - * is an inheritance set. Source inheritance is expanded at the bottom of the - * plan tree (see allpaths.c), but target inheritance has to be expanded at - * the top. The reason is that for UPDATE, each target relation needs a - * different targetlist matching its own column set. Fortunately, + * is an inheritance set, where outputs of all the children are combined and + * combined output consumed using the source relation's (parent's) column set. + * For UPDATE, each target relation, where the query's output will go, needs + * a different targetlist matching its own column set. So, we will need to + * translate the query such that it produces the desired output required by + * each of the child target relations and plan each separately. Fortunately, * the UPDATE/DELETE target can never be the nullable side of an outer join, * so it's OK to generate the plan this way. * + * Planning steps that need to be performed separately for each child and + * any preliminary processing needed for that are handled by the following + * sub-routines of query_planner: + * + * add_inherited_target_child_roots - this creates copies of PlannerInfo for + * each child after query_planner has finished processing the join tree, with + * translated copies of parsetree, eq_classes, etc. + * + * set_inherited_target_rel_sizes - this sets size estimates for child + * relations + * + * set_inherited_target_rel_pathlists - this creates Paths for scanning + * individual child relations + * + * inheritance_make_rel_from_joinlist - this translates the jointree, + * replacing the target relation in the original jointree (the root parent) + * by individual child target relations and performs join planning on the + * resulting join tree, saving the RelOptInfos of resulting join relations + * into the top-level PlannerInfo + * + * Finally, here we adjust the Paths of individual children do that they + * produces the desired targetlist. + * * Returns nothing; the useful output is in the Paths we attach to * the (UPPERREL_FINAL, NULL) upperrel stored in *root. * @@ -1191,14 +1219,8 @@ inheritance_planner(PlannerInfo *root) { Query *parse = root->parse; int top_parentRTindex = parse->resultRelation; - Bitmapset *subqueryRTindexes; - Bitmapset *modifiableARIindexes; int nominalRelation = -1; Index rootRelation = 0; - List *final_rtable = NIL; - int save_rel_array_size = 0; - RelOptInfo **save_rel_array = NULL; - AppendRelInfo **save_append_rel_array = NULL; List *subpaths = NIL; List *subroots = NIL; List *resultRelations = NIL; @@ -1206,70 +1228,52 @@ inheritance_planner(PlannerInfo *root) List *returningLists = NIL; List *rowMarks; RelOptInfo *final_rel; - ListCell *lc; - Index rti; + ListCell *lc1, + *lc2; RangeTblEntry *parent_rte; - PlannerInfo *parent_root; - Query *parent_parse; - Bitmapset *parent_relids = bms_make_singleton(top_parentRTindex); - PlannerInfo **parent_roots = NULL; + List *tlist; + standard_qp_extra qp_extra; + RelOptInfo *planned_rel; + /* Inheritance is never used for insert. */ Assert(parse->commandType != CMD_INSERT); /* - * We generate a modified instance of the original Query for each target - * relation, plan that, and put all the plans into a list that will be - * controlled by a single ModifyTable node. All the instances share the - * same rangetable, but each instance must have its own set of subquery - * RTEs within the finished rangetable because (1) they are likely to get - * scribbled on during planning, and (2) it's not inconceivable that - * subqueries could get planned differently in different cases. We need - * not create duplicate copies of other RTE kinds, in particular not the - * target relations, because they don't have either of those issues. Not - * having to duplicate the target relations is important because doing so - * (1) would result in a rangetable of length O(N^2) for N targets, with - * at least O(N^3) work expended here; and (2) would greatly complicate - * management of the rowMarks list. + * Let query_planner generate the access paths for the query for each + * target child relation. * - * To begin with, generate a bitmapset of the relids of the subquery RTEs. + * First, save the unexpanded version of the query's targetlist. + * create_inherited_target_child_root will use it as base when expanding + * it for a given child relation as the query's target relation instead + * of the parent. */ - subqueryRTindexes = NULL; - rti = 1; - foreach(lc, parse->rtable) - { - RangeTblEntry *rte = lfirst_node(RangeTblEntry, lc); - - if (rte->rtekind == RTE_SUBQUERY) - subqueryRTindexes = bms_add_member(subqueryRTindexes, rti); - rti++; - } + root->unexpanded_tlist = list_copy(root->parse->targetList); + tlist = preprocess_targetlist(root); + root->processed_tlist = tlist; + qp_extra.tlist = tlist; + qp_extra.activeWindows = NIL; + qp_extra.groupClause = NIL; + planned_rel = query_planner(root, tlist, standard_qp_callback, &qp_extra); /* - * Next, we want to identify which AppendRelInfo items contain references - * to any of the aforesaid subquery RTEs. These items will need to be - * copied and modified to adjust their subquery references; whereas the - * other ones need not be touched. It's worth being tense over this - * because we can usually avoid processing most of the AppendRelInfo - * items, thereby saving O(N^2) space and time when the target is a large - * inheritance tree. We can identify AppendRelInfo items by their - * child_relid, since that should be unique within the list. + * If it turned out during query planning that all the children are dummy + * (pruned or excluded by constraints), no need to do the steps below. */ - modifiableARIindexes = NULL; - if (subqueryRTindexes != NULL) + if (IS_DUMMY_REL(planned_rel)) { - foreach(lc, root->append_rel_list) - { - AppendRelInfo *appinfo = lfirst_node(AppendRelInfo, lc); - - if (bms_is_member(appinfo->parent_relid, subqueryRTindexes) || - bms_is_member(appinfo->child_relid, subqueryRTindexes) || - bms_overlap(pull_varnos((Node *) appinfo->translated_vars), - subqueryRTindexes)) - modifiableARIindexes = bms_add_member(modifiableARIindexes, - appinfo->child_relid); - } + /* + * Result path, even if dummy, must go into outer query's FINAL + * upperrel. + */ + final_rel = fetch_upper_rel(root, UPPERREL_FINAL, NULL); + final_rel->reltarget = create_pathtarget(root, tlist); + mark_dummy_rel(final_rel); + return; } + Assert(planned_rel->relid == top_parentRTindex); + Assert(planned_rel->reloptkind == RELOPT_BASEREL); + /* * If the parent RTE is a partitioned table, we should use that as the * nominal target relation, because the RTEs added for partitioned tables @@ -1277,7 +1281,7 @@ inheritance_planner(PlannerInfo *root) * not appear anywhere else in the plan, so the confusion explained below * for non-partitioning inheritance cases is not possible. */ - parent_rte = rt_fetch(top_parentRTindex, root->parse->rtable); + parent_rte = planner_rt_fetch(top_parentRTindex, root); if (parent_rte->relkind == RELKIND_PARTITIONED_TABLE) { nominalRelation = top_parentRTindex; @@ -1285,66 +1289,34 @@ inheritance_planner(PlannerInfo *root) } /* - * The PlannerInfo for each child is obtained by translating the relevant - * members of the PlannerInfo for its immediate parent, which we find - * using the parent_relid in its AppendRelInfo. We save the PlannerInfo - * for each parent in an array indexed by relid for fast retrieval. Since - * the maximum number of parents is limited by the number of RTEs in the - * query, we use that number to allocate the array. An extra entry is - * needed since relids start from 1. + * Get on with finalizing the path for each child target relation by + * adjust their Paths to get desired PathTarget. */ - parent_roots = (PlannerInfo **) palloc0((list_length(parse->rtable) + 1) * - sizeof(PlannerInfo *)); - parent_roots[top_parentRTindex] = root; - - /* - * And now we can get on with generating a plan for each child table. - */ - foreach(lc, root->append_rel_list) + forboth(lc1, root->inh_target_child_rels, + lc2, root->inh_target_child_path_rels) { - AppendRelInfo *appinfo = lfirst_node(AppendRelInfo, lc); - PlannerInfo *subroot; + Index childRTindex = lfirst_int(lc1); + RelOptInfo *childbaserel = find_base_rel(root, childRTindex); + RelOptInfo *childjoinrel = lfirst(lc2); + PlannerInfo *subroot = root->inh_target_child_roots[childRTindex]; + AppendRelInfo *appinfo = root->append_rel_array[childRTindex]; RangeTblEntry *child_rte; RelOptInfo *sub_final_rel; + ListCell *lc; Path *subpath; + List *tlist = subroot->processed_tlist; + PathTarget *scanjoin_target; + bool tlist_same_exprs; - /* append_rel_list contains all append rels; ignore others */ - if (!bms_is_member(appinfo->parent_relid, parent_relids)) - continue; - - /* - * expand_inherited_rtentry() always processes a parent before any of - * that parent's children, so the parent_root for this relation should - * already be available. - */ - parent_root = parent_roots[appinfo->parent_relid]; - Assert(parent_root != NULL); - parent_parse = parent_root->parse; - - /* - * We need a working copy of the PlannerInfo so that we can control - * propagation of information back to the main copy. - */ - subroot = makeNode(PlannerInfo); - memcpy(subroot, parent_root, sizeof(PlannerInfo)); - - /* - * Generate modified query with this rel as target. We first apply - * adjust_appendrel_attrs, which copies the Query and changes - * references to the parent RTE to refer to the current child RTE, - * then fool around with subquery RTEs. - */ - subroot->parse = (Query *) - adjust_appendrel_attrs(parent_root, - (Node *) parent_parse, - 1, &appinfo); + Assert(subroot != NULL); + Assert(subroot->parse->resultRelation == childRTindex); /* * If there are securityQuals attached to the parent, move them to the * child rel (they've already been transformed properly for that). */ - parent_rte = rt_fetch(appinfo->parent_relid, subroot->parse->rtable); - child_rte = rt_fetch(appinfo->child_relid, subroot->parse->rtable); + parent_rte = planner_rt_fetch(appinfo->parent_relid, subroot); + child_rte = planner_rt_fetch(appinfo->child_relid, subroot); child_rte->securityQuals = parent_rte->securityQuals; parent_rte->securityQuals = NIL; @@ -1356,24 +1328,12 @@ inheritance_planner(PlannerInfo *root) (rootRelation != 0) ? 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. + * Ignore a partitioned child. Instead, the paths of its children will + * be added to subpaths. */ - if (child_rte->inh) + if (childjoinrel->part_scheme) { - Assert(child_rte->relkind == RELKIND_PARTITIONED_TABLE); - parent_relids = bms_add_member(parent_relids, appinfo->child_relid); - parent_roots[appinfo->child_relid] = subroot; - + Assert(child_rte->inh); continue; } @@ -1401,161 +1361,57 @@ inheritance_planner(PlannerInfo *root) 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 - * pass it the original rowMarks list.) - */ - subroot->rowMarks = copyObject(parent_root->rowMarks); - - /* - * The append_rel_list likewise might contain references to subquery - * RTEs (if any subqueries were flattenable UNION ALLs). So prepare - * to apply ChangeVarNodes to that, too. As explained above, we only - * want to copy items that actually contain such references; the rest - * can just get linked into the subroot's append_rel_list. - * - * If we know there are no such references, we can just use the outer - * append_rel_list unmodified. - */ - if (modifiableARIindexes != NULL) - { - ListCell *lc2; - - subroot->append_rel_list = NIL; - foreach(lc2, parent_root->append_rel_list) - { - AppendRelInfo *appinfo2 = lfirst_node(AppendRelInfo, lc2); - - if (bms_is_member(appinfo2->child_relid, modifiableARIindexes)) - appinfo2 = copyObject(appinfo2); - - subroot->append_rel_list = lappend(subroot->append_rel_list, - appinfo2); - } - } - - /* - * Add placeholders to the child Query's rangetable list to fill the - * RT indexes already reserved for subqueries in previous children. - * These won't be referenced, so there's no need to make them very - * valid-looking. - */ - while (list_length(subroot->parse->rtable) < list_length(final_rtable)) - subroot->parse->rtable = lappend(subroot->parse->rtable, - makeNode(RangeTblEntry)); - - /* - * If this isn't the first child Query, generate duplicates of all - * subquery RTEs, and adjust Var numbering to reference the - * duplicates. To simplify the loop logic, we scan the original rtable - * not the copy just made by adjust_appendrel_attrs; that should be OK - * since subquery RTEs couldn't contain any references to the target - * rel. - */ - if (final_rtable != NIL && subqueryRTindexes != NULL) - { - ListCell *lr; - - rti = 1; - foreach(lr, parent_parse->rtable) - { - RangeTblEntry *rte = lfirst_node(RangeTblEntry, lr); - - if (bms_is_member(rti, subqueryRTindexes)) - { - Index newrti; - - /* - * The RTE can't contain any references to its own RT - * index, except in its securityQuals, so we can save a - * few cycles by applying ChangeVarNodes to the rest of - * the rangetable before we append the RTE to it. - */ - newrti = list_length(subroot->parse->rtable) + 1; - ChangeVarNodes((Node *) subroot->parse, rti, newrti, 0); - ChangeVarNodes((Node *) subroot->rowMarks, rti, newrti, 0); - /* Skip processing unchanging parts of append_rel_list */ - if (modifiableARIindexes != NULL) - { - ListCell *lc2; - - foreach(lc2, subroot->append_rel_list) - { - AppendRelInfo *appinfo2 = lfirst_node(AppendRelInfo, lc2); - - if (bms_is_member(appinfo2->child_relid, - modifiableARIindexes)) - ChangeVarNodes((Node *) appinfo2, rti, newrti, 0); - } - } - rte = copyObject(rte); - ChangeVarNodes((Node *) rte->securityQuals, rti, newrti, 0); - subroot->parse->rtable = lappend(subroot->parse->rtable, - rte); - } - rti++; - } - } - - /* There shouldn't be any OJ info to translate, as yet */ - Assert(subroot->join_info_list == NIL); - /* and we haven't created PlaceHolderInfos, either */ - Assert(subroot->placeholder_list == NIL); - - /* Generate Path(s) for accessing this result relation */ - grouping_planner(subroot, true, 0.0 /* retrieve all tuples */ ); + /* Apply the top-level targetlist to childjoinrel's path. */ + scanjoin_target = create_pathtarget(subroot, tlist); + tlist_same_exprs = equal(scanjoin_target->exprs, + childjoinrel->reltarget->exprs); + apply_scanjoin_target_to_paths_guts(subroot, childjoinrel, + scanjoin_target, + tlist_same_exprs); /* + * Transfer paths from childjoinrel's pathlist to sub_final_rel. * 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. */ sub_final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL); + foreach(lc, childjoinrel->pathlist) + add_path(sub_final_rel, (Path *) lfirst(lc)); set_cheapest(sub_final_rel); subpath = sub_final_rel->cheapest_total_path; /* - * If this child rel was excluded by constraint exclusion, exclude it - * from the result plan. + * If the childbaserel belongs to a single FDW, so does the + * corresponding sub_final_rel. */ - if (IS_DUMMY_PATH(subpath)) - continue; + sub_final_rel->serverid = childbaserel->serverid; + sub_final_rel->userid = childbaserel->userid; + sub_final_rel->useridiscurrent = childbaserel->useridiscurrent; + sub_final_rel->fdwroutine = childbaserel->fdwroutine; /* - * If this is the first non-excluded child, its post-planning rtable - * becomes the initial contents of final_rtable; otherwise, append - * just its modified subquery RTEs to final_rtable. + * If there is an FDW that's responsible for all baserels of the query, + * let it consider adding ForeignPaths. */ - if (final_rtable == NIL) - final_rtable = subroot->parse->rtable; - else - final_rtable = list_concat(final_rtable, - list_copy_tail(subroot->parse->rtable, - list_length(final_rtable))); + if (sub_final_rel->fdwroutine && + sub_final_rel->fdwroutine->GetForeignUpperPaths) + sub_final_rel->fdwroutine->GetForeignUpperPaths(subroot, + UPPERREL_FINAL, + childjoinrel, + sub_final_rel, + NULL); + + /* Let extensions possibly add some more paths to sub_final_rel. */ + if (create_upper_paths_hook) + (*create_upper_paths_hook) (subroot, UPPERREL_FINAL, childjoinrel, + sub_final_rel, NULL); /* - * We need to collect all the RelOptInfos from all child plans into - * the main PlannerInfo, since setrefs.c will need them. We use the - * last child's simple_rel_array (previous ones are too short), so we - * have to propagate forward the RelOptInfos that were already built - * in previous children. + * child rel cannot be empty, or inheritance_make_rel_from_joinlist + * wouldn't have put it in the list to begin with. */ - Assert(subroot->simple_rel_array_size >= save_rel_array_size); - for (rti = 1; rti < save_rel_array_size; rti++) - { - RelOptInfo *brel = save_rel_array[rti]; - - if (brel) - subroot->simple_rel_array[rti] = brel; - } - save_rel_array_size = subroot->simple_rel_array_size; - save_rel_array = subroot->simple_rel_array; - save_append_rel_array = subroot->append_rel_array; - - /* Make sure any initplans from this rel get into the outer list */ - root->init_plans = subroot->init_plans; + Assert(!IS_DUMMY_PATH(subpath)); /* Build list of sub-paths */ subpaths = lappend(subpaths, subpath); @@ -1587,36 +1443,6 @@ inheritance_planner(PlannerInfo *root) */ /* - * If we managed to exclude every child rel, return a dummy plan; it - * doesn't even need a ModifyTable node. - */ - if (subpaths == NIL) - { - set_dummy_rel_pathlist(final_rel); - return; - } - - /* - * Put back the final adjusted rtable into the master copy of the Query. - * (We mustn't do this if we found no non-excluded children.) - */ - parse->rtable = final_rtable; - root->simple_rel_array_size = save_rel_array_size; - root->simple_rel_array = save_rel_array; - root->append_rel_array = save_append_rel_array; - - /* Must reconstruct master's simple_rte_array, too */ - root->simple_rte_array = (RangeTblEntry **) - palloc0((list_length(final_rtable) + 1) * sizeof(RangeTblEntry *)); - rti = 1; - foreach(lc, final_rtable) - { - RangeTblEntry *rte = lfirst_node(RangeTblEntry, lc); - - root->simple_rte_array[rti++] = rte; - } - - /* * If there was a FOR [KEY] UPDATE/SHARE clause, the LockRows node will * have dealt with fetching non-locked marked rows, else we need to have * ModifyTable do that. @@ -1651,11 +1477,6 @@ inheritance_planner(PlannerInfo *root) * This function adds all required top-level processing to the scan/join * Path(s) produced by query_planner. * - * If inheritance_update is true, we're being called from inheritance_planner - * and should not include a ModifyTable step in the resulting Path(s). - * (inheritance_planner will create a single ModifyTable node covering all the - * target tables.) - * * tuple_fraction is the fraction of tuples we expect will be retrieved. * tuple_fraction is interpreted as follows: * 0: expect all tuples to be retrieved (normal case) @@ -1673,8 +1494,7 @@ inheritance_planner(PlannerInfo *root) *-------------------- */ static void -grouping_planner(PlannerInfo *root, bool inheritance_update, - double tuple_fraction) +grouping_planner(PlannerInfo *root, double tuple_fraction) { Query *parse = root->parse; List *tlist; @@ -2172,11 +1992,8 @@ grouping_planner(PlannerInfo *root, bool inheritance_update, offset_est, count_est); } - /* - * If this is an INSERT/UPDATE/DELETE, and we're not being called from - * inheritance_planner, add the ModifyTable node. - */ - if (parse->commandType != CMD_SELECT && !inheritance_update) + /* If this is an INSERT/UPDATE/DELETE, add the ModifyTable node. */ + if (parse->commandType != CMD_SELECT) { Index rootRelation; List *withCheckOptionLists; @@ -6914,7 +6731,6 @@ apply_scanjoin_target_to_paths(PlannerInfo *root, bool scanjoin_target_parallel_safe, bool tlist_same_exprs) { - ListCell *lc; PathTarget *scanjoin_target; bool is_dummy_rel = IS_DUMMY_REL(rel); @@ -6985,53 +6801,8 @@ apply_scanjoin_target_to_paths(PlannerInfo *root, /* Extract SRF-free scan/join target. */ scanjoin_target = linitial_node(PathTarget, scanjoin_targets); - - /* - * Adjust each input path. If the tlist exprs are the same, we can just - * inject the sortgroupref information into the existing pathtarget. - * Otherwise, replace each path with a projection path that generates the - * SRF-free scan/join target. This can't change the ordering of paths - * within rel->pathlist, so we just modify the list in place. - */ - foreach(lc, rel->pathlist) - { - Path *subpath = (Path *) lfirst(lc); - Path *newpath; - - Assert(subpath->param_info == NULL); - - if (tlist_same_exprs) - subpath->pathtarget->sortgrouprefs = - scanjoin_target->sortgrouprefs; - else - { - newpath = (Path *) create_projection_path(root, rel, subpath, - scanjoin_target); - lfirst(lc) = newpath; - } - } - - /* Same for partial paths. */ - foreach(lc, rel->partial_pathlist) - { - Path *subpath = (Path *) lfirst(lc); - Path *newpath; - - /* Shouldn't have any parameterized paths anymore */ - Assert(subpath->param_info == NULL); - - if (tlist_same_exprs) - subpath->pathtarget->sortgrouprefs = - scanjoin_target->sortgrouprefs; - else - { - newpath = (Path *) create_projection_path(root, - rel, - subpath, - scanjoin_target); - lfirst(lc) = newpath; - } - } + apply_scanjoin_target_to_paths_guts(root, rel, scanjoin_target, + tlist_same_exprs); /* Now fix things up if scan/join target contains SRFs */ if (root->parse->hasTargetSRFs) @@ -7059,6 +6830,10 @@ apply_scanjoin_target_to_paths(PlannerInfo *root, int nappinfos; List *child_scanjoin_targets = NIL; + /* Skip processing pruned partitions. */ + if (child_rel == NULL) + continue; + /* Translate scan/join targets for this child. */ appinfos = find_appinfos_by_relids(root, child_rel->relids, &nappinfos); @@ -7112,6 +6887,67 @@ apply_scanjoin_target_to_paths(PlannerInfo *root, } /* + * apply_scanjoin_target_to_paths_guts + * Portion of apply_scanjoin_target_to_paths that's useful in some places + * where full powers of the former are unnecessary + */ +static void +apply_scanjoin_target_to_paths_guts(PlannerInfo *root, + RelOptInfo *rel, + PathTarget *scanjoin_target, + bool tlist_same_exprs) +{ + ListCell *lc; + + /* + * Adjust each input path. If the tlist exprs are the same, we can just + * inject the sortgroupref information into the existing pathtarget. + * Otherwise, replace each path with a projection path that generates the + * SRF-free scan/join target. This can't change the ordering of paths + * within rel->pathlist, so we just modify the list in place. + */ + foreach(lc, rel->pathlist) + { + Path *subpath = (Path *) lfirst(lc); + Path *newpath; + + Assert(subpath->param_info == NULL); + + if (tlist_same_exprs) + subpath->pathtarget->sortgrouprefs = + scanjoin_target->sortgrouprefs; + else + { + newpath = (Path *) create_projection_path(root, rel, subpath, + scanjoin_target); + lfirst(lc) = newpath; + } + } + + /* Same for partial paths. */ + foreach(lc, rel->partial_pathlist) + { + Path *subpath = (Path *) lfirst(lc); + Path *newpath; + + /* Shouldn't have any parameterized paths anymore */ + Assert(subpath->param_info == NULL); + + if (tlist_same_exprs) + subpath->pathtarget->sortgrouprefs = + scanjoin_target->sortgrouprefs; + else + { + newpath = (Path *) create_projection_path(root, + rel, + subpath, + scanjoin_target); + lfirst(lc) = newpath; + } + } +} + +/* * create_partitionwise_grouping_paths * * If the partition keys of input relation are part of the GROUP BY clause, all diff --git a/src/backend/optimizer/util/appendinfo.c b/src/backend/optimizer/util/appendinfo.c index ca6622ece9..8914c464d3 100644 --- a/src/backend/optimizer/util/appendinfo.c +++ b/src/backend/optimizer/util/appendinfo.c @@ -18,6 +18,8 @@ #include "nodes/makefuncs.h" #include "nodes/nodeFuncs.h" #include "optimizer/appendinfo.h" +#include "optimizer/pathnode.h" +#include "optimizer/paths.h" #include "parser/parsetree.h" #include "utils/lsyscache.h" #include "utils/rel.h" @@ -394,8 +396,34 @@ adjust_appendrel_attrs_mutator(Node *node, context->appinfos); return (Node *) phv; } + + if (IsA(node, SpecialJoinInfo)) + { + SpecialJoinInfo *oldinfo = (SpecialJoinInfo *) node; + SpecialJoinInfo *newinfo = makeNode(SpecialJoinInfo); + + memcpy(newinfo, oldinfo, sizeof(SpecialJoinInfo)); + newinfo->min_lefthand = adjust_child_relids(oldinfo->min_lefthand, + context->nappinfos, + context->appinfos); + newinfo->min_righthand = adjust_child_relids(oldinfo->min_righthand, + context->nappinfos, + context->appinfos); + newinfo->syn_lefthand = adjust_child_relids(oldinfo->syn_lefthand, + context->nappinfos, + context->appinfos); + newinfo->syn_righthand = adjust_child_relids(oldinfo->syn_righthand, + context->nappinfos, + context->appinfos); + newinfo->semi_rhs_exprs = + (List *) expression_tree_mutator((Node *) + oldinfo->semi_rhs_exprs, + adjust_appendrel_attrs_mutator, + (void *) context); + return (Node *) newinfo; + } + /* Shouldn't need to handle planner auxiliary nodes here */ - Assert(!IsA(node, SpecialJoinInfo)); Assert(!IsA(node, AppendRelInfo)); Assert(!IsA(node, PlaceHolderInfo)); Assert(!IsA(node, MinMaxAggInfo)); diff --git a/src/backend/optimizer/util/inherit.c b/src/backend/optimizer/util/inherit.c index eaf788e578..1d16a9f2ff 100644 --- a/src/backend/optimizer/util/inherit.c +++ b/src/backend/optimizer/util/inherit.c @@ -21,11 +21,16 @@ #include "miscadmin.h" #include "optimizer/appendinfo.h" #include "optimizer/inherit.h" +#include "optimizer/optimizer.h" +#include "optimizer/paths.h" +#include "optimizer/planmain.h" #include "optimizer/planner.h" #include "optimizer/prep.h" #include "utils/rel.h" +static PlannerInfo *create_inherited_target_child_root(PlannerInfo *root, + AppendRelInfo *appinfo); static void expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti); static void expand_partitioned_rtentry(PlannerInfo *root, @@ -492,3 +497,125 @@ translate_col_privs(const Bitmapset *parent_privs, return child_privs; } + +/* + * add_inherited_target_child_roots + * For each child of the query's result relation, this translates the + * original query to match the child and creates a PlannerInfo containing + * the translated query + * + * Child PlannerInfo reuses most of the parent PlannerInfo's fields unchanged, + * except unexpanded_tlist, processed_tlist, and all_baserels, all of which + * are based on the child relation. + */ +void +add_inherited_target_child_roots(PlannerInfo *root) +{ + Index resultRelation = root->parse->resultRelation; + ListCell *lc; + + Assert(root->inh_target_child_roots != NULL); + + foreach(lc, root->append_rel_list) + { + AppendRelInfo *appinfo = lfirst(lc); + RangeTblEntry *childRTE; + PlannerInfo *subroot; + + if (appinfo->parent_relid != resultRelation) + continue; + + /* + * Create a PlannerInfo for processing this child target relation + * with. + */ + subroot = create_inherited_target_child_root(root, appinfo); + root->inh_target_child_roots[appinfo->child_relid] = subroot; + + /* + * If the child is a partitioned table, recurse to do this for its + * partitions. + */ + childRTE = root->simple_rte_array[appinfo->child_relid]; + if (childRTE->inh) + add_inherited_target_child_roots(subroot); + } +} + +/* + * create_inherited_target_child_root + * Workhorse of add_inherited_target_child_roots + */ +static PlannerInfo * +create_inherited_target_child_root(PlannerInfo *root, AppendRelInfo *appinfo) +{ + PlannerInfo *subroot; + List *tlist, + *tlist_vars; + + + Assert(root->parse->commandType == CMD_UPDATE || + root->parse->commandType == CMD_DELETE); + + /* + * Translate the original query to replace Vars of the parent table + * by the corresponding Vars of the child table and to make child the main + * target relation of the query. + */ + subroot = makeNode(PlannerInfo); + memcpy(subroot, root, sizeof(PlannerInfo)); + + /* + * Restore the original, unexpanded targetlist, that is, the one before + * preprocess_targetlist was run on the original query. We'll run + * preprocess_targetlist after translating the query and the targetlist, + * so that it is expanded according to child's tuple descriptor. + */ + root->parse->targetList = root->unexpanded_tlist; + subroot->parse = (Query *) adjust_appendrel_attrs(root, + (Node *) root->parse, + 1, &appinfo); + + /* + * Translate ECs. This copies the list of ECs, translating the + * relevant members of individual ECs to replace the references to + * the parent RTE by child RTE, including the EC members. + */ + subroot->eq_classes = + translate_eq_classes_for_child(root, root->eq_classes, + appinfo); + + /* + * Save the just translated targetlist as unexpanded_tlist in the child's + * subroot, so that this child's own children can use it. Must use copy + * because subroot->parse->targetList will be modified soon. + */ + subroot->unexpanded_tlist = list_copy(subroot->parse->targetList); + + /* + * Apply planner's expansion of targetlist, such as adding various junk + * column, filling placeholder entries for dropped columns, etc., using + * the child's TupleDesc. + */ + tlist = preprocess_targetlist(subroot); + subroot->processed_tlist = tlist; + + /* + * Add any newly added Vars to the child RelOptInfo. They'll be needed + * at the top level, so we pass {0} for where_needed. + */ + tlist_vars = pull_var_clause((Node *) tlist, + PVC_RECURSE_AGGREGATES | + PVC_RECURSE_WINDOWFUNCS | + PVC_INCLUDE_PLACEHOLDERS); + add_vars_to_targetlist(root, tlist_vars, bms_make_singleton(0), false); + + /* + * Adjust all_baserels to replace the original target relation with the + * child target relation. + */ + subroot->all_baserels = adjust_child_relids(subroot->all_baserels, + 1, &appinfo); + + return subroot; +} diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h index a008ae07da..2dc3059f16 100644 --- a/src/include/nodes/pathnodes.h +++ b/src/include/nodes/pathnodes.h @@ -217,6 +217,17 @@ struct PlannerInfo struct AppendRelInfo **append_rel_array; /* + * inh_target_child_roots is same length as above arrays, indexed by + * rangetable index (entry 0 is wasted like simple_rel_array). Only + * elements corresponding to inheritance child target relations are + * non-NULL. Content of each PlannerInfo is mostly same as the parent + * PlannerInfo, except for few fields such as the parse tree which is + * a translated copy of the parent's parse tree, EC list which contain + * child member expressions, etc. + */ + struct PlannerInfo **inh_target_child_roots; + + /* * all_baserels is a Relids set of all base relids (but not "other" * relids) in the query; that is, the Relids identifier of the final join * we need to form. This is computed in make_one_rel, just before we @@ -310,6 +321,12 @@ struct PlannerInfo */ List *processed_tlist; + /* + * This stores the original version of the query's targetlist that's not + * modified by the planner. Useful for target inheritance planning. + */ + List *unexpanded_tlist; + /* Fields filled during create_plan() for use in setrefs.c */ AttrNumber *grouping_map; /* for GroupingFunc fixup */ List *minmax_aggs; /* List of MinMaxAggInfos */ @@ -348,9 +365,22 @@ struct PlannerInfo /* Does this query modify any partition key columns? */ bool partColsUpdated; + + /* List of RT indexes of child target relations. */ + List *inh_target_child_rels; + + /* + * RelOptInfos corresponding to each child target rel. For leaf children, + * it's the RelOptInfo representing the output of make_rel_from_joinlist() + * called with the parent rel in the original join tree replaced by a + * given leaf child. For non-leaf children, it's the baserel RelOptInfo + * itself, left as a placeholder. + */ + List *inh_target_child_path_rels; }; + /* * In places where it's known that simple_rte_array[] must have been prepared * already, we just index into it to fetch RTEs. In code that might be diff --git a/src/include/optimizer/inherit.h b/src/include/optimizer/inherit.h index d2418f15cf..4f06cdd322 100644 --- a/src/include/optimizer/inherit.h +++ b/src/include/optimizer/inherit.h @@ -18,5 +18,6 @@ extern void expand_inherited_tables(PlannerInfo *root); +extern void add_inherited_target_child_roots(PlannerInfo *root); #endif /* INHERIT_H */ diff --git a/src/include/optimizer/paths.h b/src/include/optimizer/paths.h index 040335a7c5..e8caaaf643 100644 --- a/src/include/optimizer/paths.h +++ b/src/include/optimizer/paths.h @@ -154,6 +154,8 @@ extern void add_child_rel_equivalences(PlannerInfo *root, AppendRelInfo *appinfo, RelOptInfo *parent_rel, RelOptInfo *child_rel); +extern List *translate_eq_classes_for_child(PlannerInfo *root, + List *eq_classes, AppendRelInfo *appinfo); extern List *generate_implied_equalities_for_column(PlannerInfo *root, RelOptInfo *rel, ec_matches_callback_type callback, diff --git a/src/test/regress/expected/partition_join.out b/src/test/regress/expected/partition_join.out index bbdc373782..fd85b5ead3 100644 --- a/src/test/regress/expected/partition_join.out +++ b/src/test/regress/expected/partition_join.out @@ -1780,7 +1780,7 @@ WHERE EXISTS ( Filter: (c IS NULL) -> Nested Loop -> Seq Scan on int4_tbl - -> Subquery Scan on ss_1 + -> Subquery Scan on ss -> Limit -> Seq Scan on int8_tbl int8_tbl_1 -> Nested Loop Semi Join @@ -1788,7 +1788,7 @@ WHERE EXISTS ( Filter: (c IS NULL) -> Nested Loop -> Seq Scan on int4_tbl - -> Subquery Scan on ss_2 + -> Subquery Scan on ss -> Limit -> Seq Scan on int8_tbl int8_tbl_2 (28 rows) -- 2.11.0