diff options
Diffstat (limited to 'src/backend/optimizer/path/allpaths.c')
-rw-r--r-- | src/backend/optimizer/path/allpaths.c | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index 0b98f0856e9..95f3368c214 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -2872,16 +2872,19 @@ set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte) static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte) { + Path *ctepath; Plan *cteplan; PlannerInfo *cteroot; Index levelsup; + List *pathkeys; int ndx; ListCell *lc; int plan_id; Relids required_outer; /* - * Find the referenced CTE, and locate the plan previously made for it. + * Find the referenced CTE, and locate the path and plan previously made + * for it. */ levelsup = rte->ctelevelsup; cteroot = root; @@ -2913,11 +2916,20 @@ set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte) plan_id = list_nth_int(cteroot->cte_plan_ids, ndx); if (plan_id <= 0) elog(ERROR, "no plan was made for CTE \"%s\"", rte->ctename); + + Assert(list_length(root->glob->subpaths) == list_length(root->glob->subplans)); + ctepath = (Path *) list_nth(root->glob->subpaths, plan_id - 1); cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1); /* Mark rel with estimated output rows, width, etc */ set_cte_size_estimates(root, rel, cteplan->plan_rows); + /* Convert the ctepath's pathkeys to outer query's representation */ + pathkeys = convert_subquery_pathkeys(root, + rel, + ctepath->pathkeys, + cteplan->targetlist); + /* * We don't support pushing join clauses into the quals of a CTE scan, but * it could still have required parameterization due to LATERAL refs in @@ -2926,7 +2938,7 @@ set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte) required_outer = rel->lateral_relids; /* Generate appropriate path */ - add_path(rel, create_ctescan_path(root, rel, required_outer)); + add_path(rel, create_ctescan_path(root, rel, pathkeys, required_outer)); } /* |