Skip to content

Commit

Permalink
Reuse CTE subplan & rename shared_plan to subplan
Browse files Browse the repository at this point in the history
  • Loading branch information
Vyacheslav Kompan committed Nov 21, 2023
1 parent 66ae556 commit d5ff703
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
17 changes: 9 additions & 8 deletions src/backend/optimizer/path/allpaths.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/include/nodes/relation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit d5ff703

Please sign in to comment.