summaryrefslogtreecommitdiff
path: root/src/backend/optimizer/path/allpaths.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2024-03-26 13:05:49 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2024-03-26 13:05:51 -0400
commita65724dfa73db8b451d0c874a9161935a34a914e (patch)
tree04bdd460d3344447dcc29501e733d3dd6e8928aa /src/backend/optimizer/path/allpaths.c
parente648e77e25708874196326b6e4da30e7717156ab (diff)
Propagate pathkeys from CTEs up to the outer query.
If we know the sort order of a CTE's output, and it is relevant to the outer query, label the CTE's outer-query access path using those pathkeys. This may enable optimizations such as avoiding a sort in the outer query. The code for hoisting pathkeys into the outer query already exists for regular RTE_SUBQUERY subqueries, but it wasn't getting used for CTEs, possibly out of concern for maintaining an optimization fence between the CTE and the outer query. However, on the same arguments used for commit f7816aec2, there seems no harm in letting the outer query know what the inner query decided to do. In support of this, we now remember the best Path as well as Plan for each subquery for the rest of the planner run. There may be future applications for having that at hand, and it surely costs little to build one more List. Richard Guo (minor mods by me) Discussion: https://postgr.es/m/CAMbWs49xYd3f8CrE8-WW3--dV1zH_sDSDn-vs2DzHj81Wcnsew@mail.gmail.com
Diffstat (limited to 'src/backend/optimizer/path/allpaths.c')
-rw-r--r--src/backend/optimizer/path/allpaths.c16
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));
}
/*