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-vs2dzhj81wcn...@mail.gmail.com Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/a65724dfa73db8b451d0c874a9161935a34a914e Modified Files -------------- src/backend/optimizer/path/allpaths.c | 16 ++++++++++++++-- src/backend/optimizer/plan/planner.c | 1 + src/backend/optimizer/plan/subselect.c | 28 ++++++++++++++++++---------- src/backend/optimizer/util/pathnode.c | 5 +++-- src/include/nodes/pathnodes.h | 3 +++ src/include/optimizer/pathnode.h | 2 +- src/test/regress/expected/with.out | 17 +++++++++++++++++ src/test/regress/sql/with.sql | 7 +++++++ 8 files changed, 64 insertions(+), 15 deletions(-)