From d5ff703a92a12b9f27c4d69d23c59a5131bf2339 Mon Sep 17 00:00:00 2001 From: Vyacheslav Kompan Date: Wed, 1 Nov 2023 20:17:05 +0500 Subject: [PATCH] Reuse CTE subplan & rename shared_plan to subplan --- src/backend/optimizer/path/allpaths.c | 17 +++++++++-------- src/include/nodes/relation.h | 6 ++---- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index 84918f40e771..7b7855985ad8 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -2104,7 +2104,7 @@ set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte) * subplan will not be used by InitPlans, so that they can be shared * if this CTE is referenced multiple times (excluding in InitPlans). */ - if (cteplaninfo->shared_plan == NULL) + if (cteplaninfo->subplan == NULL) { PlannerConfig *config = CopyPlannerConfig(root->config); @@ -2125,8 +2125,9 @@ set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte) subplan = subquery_planner(cteroot->glob, subquery, cteroot, cte->cterecursive, tuple_fraction, &subroot, config); - if (subplan->flow->locustype != CdbLocusType_SegmentGeneral) - cteplaninfo->shared_plan = prepare_plan_for_sharing(cteroot, subplan); + cteplaninfo->subplan = (subplan->flow->locustype != CdbLocusType_SegmentGeneral) ? + prepare_plan_for_sharing(cteroot, subplan) : + subplan; cteplaninfo->subroot = subroot; } @@ -2139,11 +2140,11 @@ set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte) * shared plan with another SegmentGeneral node. Thus, we should avoid * sharing SegmentGeneral subplans. */ - if (cteplaninfo->shared_plan) - { - subplan = share_prepared_plan(cteroot, cteplaninfo->shared_plan); - subroot = cteplaninfo->subroot; - } + subplan = (cteplaninfo->subplan->flow->locustype != CdbLocusType_SegmentGeneral) ? + share_prepared_plan(cteroot, cteplaninfo->subplan) : + (Plan *) copyObject(cteplaninfo->subplan); + + subroot = cteplaninfo->subroot; } pathkeys = subroot->query_pathkeys; diff --git a/src/include/nodes/relation.h b/src/include/nodes/relation.h index b609b3406c25..b86310c05bca 100644 --- a/src/include/nodes/relation.h +++ b/src/include/nodes/relation.h @@ -322,11 +322,9 @@ typedef struct PlannerInfo typedef struct CtePlanInfo { /* - * A subplan, prepared for sharing among many CTE references by - * prepare_plan_for_sharing(), that implements the CTE. NULL if the - * CTE is not shared among references. + * A subplan that implements the CTE. */ - Plan *shared_plan; + Plan *subplan; /* * The subroot corresponding to the subplan.