From b462609ad016ff451a76b31fbd2b08917aabf15a Mon Sep 17 00:00:00 2001
From: zengxx <xiangxin_zeng@qq.com>
Date: Fri, 27 Nov 2026 10:51:01 +0800
Subject: [PATCH 1/2] Put presorted aggregate sort keys first in Aggregate
 input

With enable_presorted_aggregate, the input to an Aggregate node is
sorted by the grouping keys followed by the sort keys of the ordered
aggregates.  make_group_input_target() appended the Vars used by the
aggregates in expression-tree order, so the argument columns come before
the key columns, e.g. (g, v, k) for

    SELECT g, string_agg(v, ',' ORDER BY k) FROM t GROUP BY g

with a text column v.

tuplesort caches only the first sort key.  Every other key is fetched
from the tuple during each comparison that ties on the earlier keys.
When a varlena argument column sits in front of such a key, its length
header has to be read first, every time.  Put the Vars of the sort keys
right after the grouping columns instead, as make_window_input_target()
already does for window keys.

Only presorted aggregates are considered: the others sort their own
input inside the Aggregate node, and a varlena key of an aggregate that
was not chosen would end up in front of the real sort keys.  Nothing is
done when no aggregate sort key was appended to the grouping keys, or
when there is a single sort key, so plans stay the same for queries that
gain nothing.

A query that benefits may get one more projection (Result or Subquery
Scan) when its relation is a non-partitioned appendrel or a subquery,
because those do not regenerate their paths for the new target.

On a table of 2 million rows with 10 groups and a text column v, with a
serial plan and the sort in memory, the query above takes 1090 ms
instead of 1465 ms (median of 6 rounds of 10 runs each).  A fixed-width
argument column has nothing to gain: the same query with an int column
takes 1022 ms instead of 1028 ms.
---
 src/backend/optimizer/plan/planner.c | 82 ++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)

diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 568a1b2a34..d26ef807a9 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -5814,6 +5814,62 @@ create_ordered_paths(PlannerInfo *root,
 	return ordered_rel;
 }
 
+/*
+ * collect_agg_key_vars_walker
+ *		Expression-tree walker callback to collect the Vars used in the
+ *		ORDER BY / DISTINCT key expressions of presorted aggregates.
+ *
+ * Only presorted aggregates are of interest: the others sort their own
+ * input inside the Aggregate node, so their keys are not sort keys of the
+ * Aggregate's input.
+ *
+ * The pull_var_clause flags must match the ones make_group_input_target
+ * uses for non_group_vars, so that add_new_columns_to_pathtarget() can
+ * de-duplicate them.
+ */
+static bool
+collect_agg_key_vars_walker(Node *node, void *context)
+{
+	List	  **vars = context;
+
+	if (node == NULL)
+		return false;
+
+	if (IsA(node, Aggref))
+	{
+		Aggref	   *aggref = (Aggref *) node;
+
+		if (aggref->aggpresorted)
+		{
+			/*
+			 * Same sort list as the planner and the executor use
+			 * (adjust_group_pathkeys_for_groupagg(),
+			 * build_pertrans_for_aggref()).  With DISTINCT, the
+			 * ORDER BY keys are a prefix of the DISTINCT keys.  Take
+			 * the keys in sort order.
+			 */
+			List	   *sortlist = aggref->aggdistinct ?
+											aggref->aggdistinct : aggref->aggorder;
+			ListCell   *lc;
+
+			foreach(lc, sortlist)
+			{
+				SortGroupClause *sortcl = lfirst_node(SortGroupClause, lc);
+				TargetEntry *tle = get_sortgroupclause_tle(sortcl,
+														   aggref->args);
+
+				*vars = list_concat(*vars,
+									pull_var_clause((Node *) tle->expr,
+													PVC_RECURSE_AGGREGATES |
+													PVC_RECURSE_WINDOWFUNCS |
+													PVC_INCLUDE_PLACEHOLDERS));
+			}
+		}
+	}
+
+	return expression_tree_walker(node, collect_agg_key_vars_walker,
+								  context);
+}
 
 /*
  * make_group_input_target
@@ -5836,6 +5892,9 @@ create_ordered_paths(PlannerInfo *root,
  *		a+b,c,d
  * where the a+b target will be used by the Sort/Group steps, and the
  * other targets will be used for computing the final results.
+ * If ordered aggregates make the subplan sort by keys beyond the GROUP
+ * BY ones, the Vars in those keys are placed right after the GROUP BY
+ * items and ahead of the remaining Vars (see the comment in the body).
  *
  * 'final_target' is the query's final target list (in PathTarget form)
  *
@@ -5928,6 +5987,29 @@ make_group_input_target(PlannerInfo *root, PathTarget *final_target)
 								  bms_make_singleton(root->group_rtindex),
 								  NULL);
 	}
+
+	/*
+	 * Only do that when it can matter.
+	 * adjust_group_pathkeys_for_groupagg() appends the keys of presorted
+	 * aggregates to root->group_pathkeys, after the plain GROUP BY keys, and
+	 * the input is sorted by that list.
+	 * tuplesort caches only the first sort key, so a key column gains only
+	 * when there is more than one sort key and at least one of them beyond
+	 * the GROUP BY keys.  The first test is the same one that
+	 * gather_grouping_paths() uses to detect the extra keys.
+	 */
+	if (list_length(root->group_pathkeys) > root->num_groupby_pathkeys &&
+		list_length(root->group_pathkeys) > 1)
+	{
+		List	   *agg_key_vars = NIL;
+
+		(void) expression_tree_walker((Node *) non_group_cols,
+									  collect_agg_key_vars_walker,
+									  &agg_key_vars);
+		add_new_columns_to_pathtarget(input_target, agg_key_vars);
+		list_free(agg_key_vars);
+	}
+
 	add_new_columns_to_pathtarget(input_target, non_group_vars);
 
 	/* clean up cruft */
