Revert "Move PartitionPruneInfo out of plan nodes into PlannedStmt"
authorAlvaro Herrera <[email protected]>
Thu, 4 May 2023 10:09:59 +0000 (12:09 +0200)
committerAlvaro Herrera <[email protected]>
Thu, 4 May 2023 10:09:59 +0000 (12:09 +0200)
This reverts commit ec386948948c and its fixup 589bb816499e.

This change was intended to support query planning avoiding acquisition
of locks on partitions that were going to be pruned; however, the
overall project took a different direction at [1] and this bit is no
longer needed.  Put things back the way they were as agreed in [2], to
avoid unnecessary complexity.

Discussion: [1] https://postgr.es/m/4191508.1674157166@sss.pgh.pa.us
Discussion: [2] https://postgr.es/m/20230502175409[email protected]

16 files changed:
src/backend/executor/execMain.c
src/backend/executor/execParallel.c
src/backend/executor/execPartition.c
src/backend/executor/execUtils.c
src/backend/executor/nodeAppend.c
src/backend/executor/nodeMergeAppend.c
src/backend/optimizer/plan/createplan.c
src/backend/optimizer/plan/planner.c
src/backend/optimizer/plan/setrefs.c
src/backend/partitioning/partprune.c
src/include/catalog/catversion.h
src/include/executor/execPartition.h
src/include/nodes/execnodes.h
src/include/nodes/pathnodes.h
src/include/nodes/plannodes.h
src/include/partitioning/partprune.h

index eaf6f31a154d46a6f320cfebba9beaf8332a7cd5..00a1f158d8ef12f6a5ca037f278fe2a62228dfbf 100644 (file)
@@ -824,7 +824,6 @@ InitPlan(QueryDesc *queryDesc, int eflags)
        ExecInitRangeTable(estate, rangeTable, plannedstmt->permInfos);
 
        estate->es_plannedstmt = plannedstmt;
-       estate->es_part_prune_infos = plannedstmt->partPruneInfos;
 
        /*
         * Next, build the ExecRowMark array from the PlanRowMark(s), if any.
index aa3f283453724a1ece06a7e393590b53fdb4a460..cc2b8ccab70d4152b6d55f52271ab9a456630e24 100644 (file)
@@ -183,7 +183,6 @@ ExecSerializePlan(Plan *plan, EState *estate)
        pstmt->dependsOnRole = false;
        pstmt->parallelModeNeeded = false;
        pstmt->planTree = plan;
-       pstmt->partPruneInfos = estate->es_part_prune_infos;
        pstmt->rtable = estate->es_range_table;
        pstmt->permInfos = estate->es_rteperminfos;
        pstmt->resultRelations = NIL;
index 9799968a4281c78e8144839428bc2fbbf1d111de..eb8a87fd632d3e9fea2287a7b55d7f3414b91838 100644 (file)
@@ -1778,9 +1778,6 @@ adjust_partition_colnos_using_map(List *colnos, AttrMap *attrMap)
  *             Initialize data structure needed for run-time partition pruning and
  *             do initial pruning if needed
  *
- * 'root_parent_relids' identifies the relation to which both the parent plan
- * and the PartitionPruneInfo given by 'part_prune_index' belong.
- *
  * On return, *initially_valid_subplans is assigned the set of indexes of
  * child subplans that must be initialized along with the parent plan node.
  * Initial pruning is performed here if needed and in that case only the
@@ -1793,24 +1790,11 @@ adjust_partition_colnos_using_map(List *colnos, AttrMap *attrMap)
 PartitionPruneState *
 ExecInitPartitionPruning(PlanState *planstate,
                                                 int n_total_subplans,
-                                                int part_prune_index,
-                                                Bitmapset *root_parent_relids,
+                                                PartitionPruneInfo *pruneinfo,
                                                 Bitmapset **initially_valid_subplans)
 {
        PartitionPruneState *prunestate;
        EState     *estate = planstate->state;
-       PartitionPruneInfo *pruneinfo;
-
-       /* Obtain the pruneinfo we need, and make sure it's the right one */
-       pruneinfo = list_nth(estate->es_part_prune_infos, part_prune_index);
-       if (!bms_equal(root_parent_relids, pruneinfo->root_parent_relids))
-               ereport(ERROR,
-                               errcode(ERRCODE_INTERNAL_ERROR),
-                               errmsg_internal("mismatching PartitionPruneInfo found at part_prune_index %d",
-                                                               part_prune_index),
-                               errdetail_internal("plan node relids %s, pruneinfo relids %s",
-                                                                  bmsToString(root_parent_relids),
-                                                                  bmsToString(pruneinfo->root_parent_relids)));
 
        /* We may need an expression context to evaluate partition exprs */
        ExecAssignExprContext(estate, planstate);
index 872cbd454dffcc37c617e7e322cc659bc71d66ff..25aba9a243af23d8ff9c5fdd7dffaac4f893d343 100644 (file)
@@ -123,7 +123,6 @@ CreateExecutorState(void)
        estate->es_rowmarks = NULL;
        estate->es_rteperminfos = NIL;
        estate->es_plannedstmt = NULL;
-       estate->es_part_prune_infos = NIL;
 
        estate->es_junkFilter = NULL;
 
index c185b11c6711b0b86832184c499c8a51bae599cf..609df6b9e626f26ebc0cbe02672c66be0d281716 100644 (file)
@@ -134,7 +134,7 @@ ExecInitAppend(Append *node, EState *estate, int eflags)
        appendstate->as_begun = false;
 
        /* If run-time partition pruning is enabled, then set that up now */
-       if (node->part_prune_index >= 0)
+       if (node->part_prune_info != NULL)
        {
                PartitionPruneState *prunestate;
 
@@ -145,8 +145,7 @@ ExecInitAppend(Append *node, EState *estate, int eflags)
                 */
                prunestate = ExecInitPartitionPruning(&appendstate->ps,
                                                                                          list_length(node->appendplans),
-                                                                                         node->part_prune_index,
-                                                                                         node->apprelids,
+                                                                                         node->part_prune_info,
                                                                                          &validsubplans);
                appendstate->as_prune_state = prunestate;
                nplans = bms_num_members(validsubplans);
index 399b39c598e842518d306a510861927af7e7e41d..21b5726e6edc27a7ae8bd34d2e36e8d665606a30 100644 (file)
@@ -82,7 +82,7 @@ ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
        mergestate->ps.ExecProcNode = ExecMergeAppend;
 
        /* If run-time partition pruning is enabled, then set that up now */
-       if (node->part_prune_index >= 0)
+       if (node->part_prune_info != NULL)
        {
                PartitionPruneState *prunestate;
 
@@ -93,8 +93,7 @@ ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
                 */
                prunestate = ExecInitPartitionPruning(&mergestate->ps,
                                                                                          list_length(node->mergeplans),
-                                                                                         node->part_prune_index,
-                                                                                         node->apprelids,
+                                                                                         node->part_prune_info,
                                                                                          &validsubplans);
                mergestate->ms_prune_state = prunestate;
                nplans = bms_num_members(validsubplans);
index 910ffbf1e138d775e55bca03ab118ddd612919af..4bb38160b338a61c18d91edc749a68cebc5f8a38 100644 (file)
@@ -1203,6 +1203,7 @@ create_append_plan(PlannerInfo *root, AppendPath *best_path, int flags)
        ListCell   *subpaths;
        int                     nasyncplans = 0;
        RelOptInfo *rel = best_path->path.parent;
+       PartitionPruneInfo *partpruneinfo = NULL;
        int                     nodenumsortkeys = 0;
        AttrNumber *nodeSortColIdx = NULL;
        Oid                *nodeSortOperators = NULL;
@@ -1353,9 +1354,6 @@ create_append_plan(PlannerInfo *root, AppendPath *best_path, int flags)
                subplans = lappend(subplans, subplan);
        }
 
-       /* Set below if we find quals that we can use to run-time prune */
-       plan->part_prune_index = -1;
-
        /*
         * If any quals exist, they may be useful to perform further partition
         * pruning during execution.  Gather information needed by the executor to
@@ -1379,14 +1377,16 @@ create_append_plan(PlannerInfo *root, AppendPath *best_path, int flags)
                }
 
                if (prunequal != NIL)
-                       plan->part_prune_index = make_partition_pruneinfo(root, rel,
-                                                                                                                         best_path->subpaths,
-                                                                                                                         prunequal);
+                       partpruneinfo =
+                               make_partition_pruneinfo(root, rel,
+                                                                                best_path->subpaths,
+                                                                                prunequal);
        }
 
        plan->appendplans = subplans;
        plan->nasyncplans = nasyncplans;
        plan->first_partial_plan = best_path->first_partial_path;
+       plan->part_prune_info = partpruneinfo;
 
        copy_generic_path_info(&plan->plan, (Path *) best_path);
 
@@ -1425,6 +1425,7 @@ create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path,
        List       *subplans = NIL;
        ListCell   *subpaths;
        RelOptInfo *rel = best_path->path.parent;
+       PartitionPruneInfo *partpruneinfo = NULL;
 
        /*
         * We don't have the actual creation of the MergeAppend node split out
@@ -1517,9 +1518,6 @@ create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path,
                subplans = lappend(subplans, subplan);
        }
 
-       /* Set below if we find quals that we can use to run-time prune */
-       node->part_prune_index = -1;
-
        /*
         * If any quals exist, they may be useful to perform further partition
         * pruning during execution.  Gather information needed by the executor to
@@ -1535,13 +1533,13 @@ create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path,
                Assert(best_path->path.param_info == NULL);
 
                if (prunequal != NIL)
-                       node->part_prune_index = make_partition_pruneinfo(root, rel,
-                                                                                                                         best_path->subpaths,
-                                                                                                                         prunequal);
+                       partpruneinfo = make_partition_pruneinfo(root, rel,
+                                                                                                        best_path->subpaths,
+                                                                                                        prunequal);
        }
 
        node->mergeplans = subplans;
-
+       node->part_prune_info = partpruneinfo;
 
        /*
         * If prepare_sort_from_pathkeys added sort columns, but we were told to
index 502ccbcea235ac230afbecbe38bb5ee99b12023b..1e4dd27dbafbe07e92a91c1125eb65798c16828c 100644 (file)
@@ -522,7 +522,6 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
        result->dependsOnRole = glob->dependsOnRole;
        result->parallelModeNeeded = glob->parallelModeNeeded;
        result->planTree = top_plan;
-       result->partPruneInfos = glob->partPruneInfos;
        result->rtable = glob->finalrtable;
        result->permInfos = glob->finalrteperminfos;
        result->resultRelations = glob->resultRelations;
index 1812db7f2fd56b7ad85a6cbadeafd72b65f622d0..f6f8a79354c5013174cbee1b1ef7781535168503 100644 (file)
@@ -350,29 +350,6 @@ set_plan_references(PlannerInfo *root, Plan *plan)
                        palloc0(list_length(glob->subplans) * sizeof(bool));
        }
 
-       /* Also fix up the information in PartitionPruneInfos. */
-       foreach(lc, root->partPruneInfos)
-       {
-               PartitionPruneInfo *pruneinfo = lfirst(lc);
-               ListCell   *l;
-
-               pruneinfo->root_parent_relids =
-                       offset_relid_set(pruneinfo->root_parent_relids, rtoffset);
-               foreach(l, pruneinfo->prune_infos)
-               {
-                       List       *prune_infos = lfirst(l);
-                       ListCell   *l2;
-
-                       foreach(l2, prune_infos)
-                       {
-                               PartitionedRelPruneInfo *pinfo = lfirst(l2);
-
-                               /* RT index of the table to which the pinfo belongs. */
-                               pinfo->rtindex += rtoffset;
-                       }
-               }
-       }
-
        /* Now fix the Plan tree */
        result = set_plan_refs(root, plan, rtoffset);
 
@@ -1728,29 +1705,6 @@ set_customscan_references(PlannerInfo *root,
        cscan->custom_relids = offset_relid_set(cscan->custom_relids, rtoffset);
 }
 
-/*
- * register_partpruneinfo
- *             Subroutine for set_append_references and set_mergeappend_references
- *
- * Add the PartitionPruneInfo from root->partPruneInfos at the given index
- * into PlannerGlobal->partPruneInfos and return its index there.
- */
-static int
-register_partpruneinfo(PlannerInfo *root, int part_prune_index)
-{
-       PlannerGlobal  *glob = root->glob;
-       PartitionPruneInfo *pruneinfo;
-
-       Assert(part_prune_index >= 0 &&
-                  part_prune_index < list_length(root->partPruneInfos));
-       pruneinfo = list_nth_node(PartitionPruneInfo, root->partPruneInfos,
-                                                         part_prune_index);
-
-       glob->partPruneInfos = lappend(glob->partPruneInfos, pruneinfo);
-
-       return list_length(glob->partPruneInfos) - 1;
-}
-
 /*
  * set_append_references
  *             Do set_plan_references processing on an Append
@@ -1803,12 +1757,21 @@ set_append_references(PlannerInfo *root,
 
        aplan->apprelids = offset_relid_set(aplan->apprelids, rtoffset);
 
-       /*
-        * Add PartitionPruneInfo, if any, to PlannerGlobal and update the index.
-        */
-       if (aplan->part_prune_index >= 0)
-               aplan->part_prune_index =
-                       register_partpruneinfo(root, aplan->part_prune_index);
+       if (aplan->part_prune_info)
+       {
+               foreach(l, aplan->part_prune_info->prune_infos)
+               {
+                       List       *prune_infos = lfirst(l);
+                       ListCell   *l2;
+
+                       foreach(l2, prune_infos)
+                       {
+                               PartitionedRelPruneInfo *pinfo = lfirst(l2);
+
+                               pinfo->rtindex += rtoffset;
+                       }
+               }
+       }
 
        /* We don't need to recurse to lefttree or righttree ... */
        Assert(aplan->plan.lefttree == NULL);
@@ -1870,12 +1833,21 @@ set_mergeappend_references(PlannerInfo *root,
 
        mplan->apprelids = offset_relid_set(mplan->apprelids, rtoffset);
 
-       /*
-        * Add PartitionPruneInfo, if any, to PlannerGlobal and update the index.
-        */
-       if (mplan->part_prune_index >= 0)
-               mplan->part_prune_index =
-                       register_partpruneinfo(root, mplan->part_prune_index);
+       if (mplan->part_prune_info)
+       {
+               foreach(l, mplan->part_prune_info->prune_infos)
+               {
+                       List       *prune_infos = lfirst(l);
+                       ListCell   *l2;
+
+                       foreach(l2, prune_infos)
+                       {
+                               PartitionedRelPruneInfo *pinfo = lfirst(l2);
+
+                               pinfo->rtindex += rtoffset;
+                       }
+               }
+       }
 
        /* We don't need to recurse to lefttree or righttree ... */
        Assert(mplan->plan.lefttree == NULL);
index 0fb1035127883e4e18c078f0223325e23fdb0f0c..7179b22a057d7d10280d64d41e49d04c29b1513a 100644 (file)
@@ -210,20 +210,16 @@ static void partkey_datum_from_expr(PartitionPruneContext *context,
 
 /*
  * make_partition_pruneinfo
- *             Checks if the given set of quals can be used to build pruning steps
- *             that the executor can use to prune away unneeded partitions.  If
- *             suitable quals are found then a PartitionPruneInfo is built and tagged
- *             onto the PlannerInfo's partPruneInfos list.
- *
- * The return value is the 0-based index of the item added to the
- * partPruneInfos list or -1 if nothing was added.
+ *             Builds a PartitionPruneInfo which can be used in the executor to allow
+ *             additional partition pruning to take place.  Returns NULL when
+ *             partition pruning would be useless.
  *
  * 'parentrel' is the RelOptInfo for an appendrel, and 'subpaths' is the list
  * of scan paths for its child rels.
  * 'prunequal' is a list of potential pruning quals (i.e., restriction
  * clauses that are applicable to the appendrel).
  */
-int
+PartitionPruneInfo *
 make_partition_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel,
                                                 List *subpaths,
                                                 List *prunequal)
@@ -337,11 +333,10 @@ make_partition_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel,
         * quals, then we can just not bother with run-time pruning.
         */
        if (prunerelinfos == NIL)
-               return -1;
+               return NULL;
 
        /* Else build the result data structure */
        pruneinfo = makeNode(PartitionPruneInfo);
-       pruneinfo->root_parent_relids = parentrel->relids;
        pruneinfo->prune_infos = prunerelinfos;
 
        /*
@@ -364,9 +359,7 @@ make_partition_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel,
        else
                pruneinfo->other_subplans = NULL;
 
-       root->partPruneInfos = lappend(root->partPruneInfos, pruneinfo);
-
-       return list_length(root->partPruneInfos) - 1;
+       return pruneinfo;
 }
 
 /*
index 44d7e521bdbcac1b9b57cb2c187885fb683a9532..a091f2ca264855b3067c817872a12e6c3220b4df 100644 (file)
@@ -57,6 +57,6 @@
  */
 
 /*                                                     yyyymmddN */
-#define CATALOG_VERSION_NO     202304211
+#define CATALOG_VERSION_NO     202305041
 
 #endif
index ee487e42ddec5fecb54528bef8ae04f74721629b..15ec869ac812497c3e6595385b31b6c53c32532a 100644 (file)
@@ -123,9 +123,9 @@ typedef struct PartitionPruneState
 
 extern PartitionPruneState *ExecInitPartitionPruning(PlanState *planstate,
                                                                                                         int n_total_subplans,
-                                                                                                        int part_prune_index,
-                                                                                                        Bitmapset *root_parent_relids,
+                                                                                                        PartitionPruneInfo *pruneinfo,
                                                                                                         Bitmapset **initially_valid_subplans);
 extern Bitmapset *ExecFindMatchingSubPlans(PartitionPruneState *prunestate,
                                                                                   bool initial_prune);
+
 #endif                                                 /* EXECPARTITION_H */
index 695ff056ba8982915e9e2d77bbfc3d58ee0b95e5..61b3517906f2be575dd58b51b769289d01ef7e3d 100644 (file)
@@ -623,7 +623,6 @@ typedef struct EState
                                                                                 * ExecRowMarks, or NULL if none */
        List       *es_rteperminfos;    /* List of RTEPermissionInfo */
        PlannedStmt *es_plannedstmt;    /* link to top of plan tree */
-       List       *es_part_prune_infos;        /* PlannedStmt.partPruneInfos */
        const char *es_sourceText;      /* Source text from QueryDesc */
 
        JunkFilter *es_junkFilter;      /* top-level junk filter, if any */
index cf28416da8437f7df7a1224c668a6bf71717cba4..7d4f24d2508c5c677860e2c0b3ff2ba7e408a460 100644 (file)
@@ -125,9 +125,6 @@ typedef struct PlannerGlobal
        /* "flat" list of AppendRelInfos */
        List       *appendRelations;
 
-       /* List of PartitionPruneInfo contained in the plan */
-       List       *partPruneInfos;
-
        /* OIDs of relations the plan depends on */
        List       *relationOids;
 
@@ -547,9 +544,6 @@ struct PlannerInfo
 
        /* Does this query modify any partition key columns? */
        bool            partColsUpdated;
-
-       /* PartitionPruneInfos added in this query's plan. */
-       List       *partPruneInfos;
 };
 
 
index 659bd05c0c11841d9987d50dd389a1ff3403501b..1b787fe03184abdee3df8c32d6c0e4e01e4e9428 100644 (file)
@@ -70,9 +70,6 @@ typedef struct PlannedStmt
 
        struct Plan *planTree;          /* tree of Plan nodes */
 
-       List       *partPruneInfos; /* List of PartitionPruneInfo contained in the
-                                                                * plan */
-
        List       *rtable;                     /* list of RangeTblEntry nodes */
 
        List       *permInfos;          /* list of RTEPermissionInfo nodes for rtable
@@ -276,8 +273,8 @@ typedef struct Append
         */
        int                     first_partial_plan;
 
-       /* Index to PlannerInfo.partPruneInfos or -1 if no run-time pruning */
-       int                     part_prune_index;
+       /* Info for run-time subplan pruning; NULL if we're not doing that */
+       struct PartitionPruneInfo *part_prune_info;
 } Append;
 
 /* ----------------
@@ -311,8 +308,8 @@ typedef struct MergeAppend
        /* NULLS FIRST/LAST directions */
        bool       *nullsFirst pg_node_attr(array_size(numCols));
 
-       /* Index to PlannerInfo.partPruneInfos or -1 if no run-time pruning */
-       int                     part_prune_index;
+       /* Info for run-time subplan pruning; NULL if we're not doing that */
+       struct PartitionPruneInfo *part_prune_info;
 } MergeAppend;
 
 /* ----------------
@@ -1414,8 +1411,6 @@ typedef struct PlanRowMark
  * Then, since an Append-type node could have multiple partitioning
  * hierarchies among its children, we have an unordered List of those Lists.
  *
- * root_parent_relids  RelOptInfo.relids of the relation to which the parent
- *                                             plan node and this PartitionPruneInfo node belong
  * prune_infos                 List of Lists containing PartitionedRelPruneInfo nodes,
  *                                             one sublist per run-time-prunable partition hierarchy
  *                                             appearing in the parent plan node's subplans.
@@ -1428,7 +1423,6 @@ typedef struct PartitionPruneInfo
        pg_node_attr(no_equal, no_query_jumble)
 
        NodeTag         type;
-       Bitmapset  *root_parent_relids;
        List       *prune_infos;
        Bitmapset  *other_subplans;
 } PartitionPruneInfo;
index c0d6889d4781ce6d4a1600d458e596dc9b8a0811..8636e04e374e0c3456e7ae7e38b6ba9ecdc8aca9 100644 (file)
@@ -70,10 +70,10 @@ typedef struct PartitionPruneContext
 #define PruneCxtStateIdx(partnatts, step_id, keyno) \
        ((partnatts) * (step_id) + (keyno))
 
-extern int make_partition_pruneinfo(struct PlannerInfo *root,
-                                                                       struct RelOptInfo *parentrel,
-                                                                       List *subpaths,
-                                                                       List *prunequal);
+extern PartitionPruneInfo *make_partition_pruneinfo(struct PlannerInfo *root,
+                                                                                                       struct RelOptInfo *parentrel,
+                                                                                                       List *subpaths,
+                                                                                                       List *prunequal);
 extern Bitmapset *prune_append_rel_partitions(struct RelOptInfo *rel);
 extern Bitmapset *get_matching_partitions(PartitionPruneContext *context,
                                                                                  List *pruning_steps);