From 37b449742384c72a0755ecd7a8503cb0a2dca970 Mon Sep 17 00:00:00 2001 From: zhenglong li Date: Wed, 28 Oct 2026 15:47:12 +0800 Subject: [PATCH v7 1/2] Ignore sort direction when matching GROUP BY keys to input pathkeys get_useful_group_keys_orderings() tries to reorder the GROUP BY keys to match the ordering of the input path, so that a presorted input such as an index scan can be used for the grouping step without an extra sort. However, both group_keys_reorder_by_pathkeys() and the GROUP BY check in truncate_useless_pathkeys() match pathkeys by pointer, which requires the input ordering to agree with the GROUP BY keys in sort direction and NULLS ordering as well. That is more than grouping needs. Grouping only requires equal values to be adjacent, which any ordering by the same equivalence class and operator family guarantees, whichever its direction. The sort direction of a GROUP BY key is merely taken from a matching ORDER BY item by the parser, to make it likely that one sort can serve both steps. So when the ORDER BY sorts in a direction the input path doesn't provide, as in CREATE INDEX ON t (b, c); SELECT b, c, count(*) FROM t GROUP BY b, c ORDER BY b, c DESC; the grouping keys are b ASC, c DESC, and the index ordering b ASC, c ASC is useful only for its first key. We then sort the whole input by b, c DESC incrementally, rather than grouping straight off the index and sorting only the (typically far fewer) groups for the ORDER BY. Teach group_keys_reorder_by_pathkeys() to accept an input pathkey that matches a GROUP BY pathkey in equivalence class and operator family, regardless of its sort direction and NULLS ordering. In that case the reordered GROUP BY keys use the input pathkey, along with a copy of the GROUP BY clause whose sort operator and NULLS ordering are adjusted to agree with it. Likewise, make truncate_useless_pathkeys() consider such pathkeys useful for GROUP BY, so that they survive to be matched. The aggregate pathkeys that may follow the grouping keys in group_pathkeys are still matched exactly, since an aggregate's ORDER BY does depend on the sort direction. Do the same for the partial aggregation that eager aggregation pushes below a join. generate_grouped_paths() required its input to be sorted exactly by the grouping pathkeys, so once the GROUP BY keys took a direction the input path doesn't provide, the partial aggregation had to sort its whole input, and then lost out to a plan without it, as the final aggregation can now group straight off the input. Partial aggregation compares the grouping keys only for equality, and its output keeps the ordering of its input, so let it take the sort direction from the input path, using the new function adjust_group_pathkeys_to_input(). If the operator family lacks the sort operator for the direction of the input pathkey, which is possible only with an operator class that does not follow the btree rules, stop matching there rather than raise an error, since the GROUP BY keys can still be sorted in their own direction. For the same reason, truncate_useless_pathkeys() doesn't consider such a pathkey useful for GROUP BY. Keeping a path ordered by it would gain nothing for grouping, and other steps could then try to sort by it, as a merge join does when it sorts its other input to match an index scan, and fail. As before, this ordering is only offered as an additional candidate, so it is chosen only if it is cheaper. All of this is done only if enable_group_by_reordering is on, so that turning it off gives the same plans as before. Adjust the assertions in get_useful_group_keys_orderings() accordingly, as the reordered clauses and pathkeys no longer need to be identical to the original ones, only to cover the same grouping keys. Update the documentation of enable_group_by_reordering accordingly. Reviewed-by: Henson Choi --- doc/src/sgml/config.sgml | 11 +- src/backend/optimizer/path/allpaths.c | 26 +- src/backend/optimizer/path/pathkeys.c | 293 +++++++++++++++++- src/include/optimizer/paths.h | 2 + src/test/regress/expected/aggregates.out | 225 ++++++++++++++ src/test/regress/expected/eager_aggregate.out | 256 +++++++++++++++ src/test/regress/sql/aggregates.sql | 90 ++++++ src/test/regress/sql/eager_aggregate.sql | 143 +++++++++ 8 files changed, 1021 insertions(+), 25 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 886e8c44847..78f713864c2 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -5987,11 +5987,12 @@ ANY num_sync (