aboutsummaryrefslogtreecommitdiff
path: root/src/backend/optimizer/util/pathnode.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/optimizer/util/pathnode.c')
-rw-r--r--src/backend/optimizer/util/pathnode.c18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.c
index 5f5596841c8..e518a07e2c6 100644
--- a/src/backend/optimizer/util/pathnode.c
+++ b/src/backend/optimizer/util/pathnode.c
@@ -3127,10 +3127,26 @@ create_agg_path(PlannerInfo *root,
pathnode->path.parallel_safe = rel->consider_parallel &&
subpath->parallel_safe;
pathnode->path.parallel_workers = subpath->parallel_workers;
+
if (aggstrategy == AGG_SORTED)
- pathnode->path.pathkeys = subpath->pathkeys; /* preserves order */
+ {
+ /*
+ * Attempt to preserve the order of the subpath. Additional pathkeys
+ * may have been added in adjust_group_pathkeys_for_groupagg() to
+ * support ORDER BY / DISTINCT aggregates. Pathkeys added there
+ * belong to columns within the aggregate function, so we must strip
+ * these additional pathkeys off as those columns are unavailable
+ * above the aggregate node.
+ */
+ if (list_length(subpath->pathkeys) > root->num_groupby_pathkeys)
+ pathnode->path.pathkeys = list_copy_head(subpath->pathkeys,
+ root->num_groupby_pathkeys);
+ else
+ pathnode->path.pathkeys = subpath->pathkeys; /* preserves order */
+ }
else
pathnode->path.pathkeys = NIL; /* output is unordered */
+
pathnode->subpath = subpath;
pathnode->aggstrategy = aggstrategy;