postgresql/src
Alexander Korotkov 0452b461bc Explore alternative orderings of group-by pathkeys during optimization.
When evaluating a query with a multi-column GROUP BY clause, we can minimize
sort operations or avoid them if we synchronize the order of GROUP BY clauses
with the ORDER BY sort clause or sort order, which comes from the underlying
query tree. Grouping does not imply any ordering, so we can compare
the keys in arbitrary order, and a Hash Agg leverages this. But for Group Agg,
we simply compared keys in the order specified in the query. This commit
explores alternative ordering of the keys, trying to find a cheaper one.

The ordering of group keys may interact with other parts of the query, some of
which may not be known while planning the grouping. For example, there may be
an explicit ORDER BY clause or some other ordering-dependent operation higher up
in the query, and using the same ordering may allow using either incremental
sort or even eliminating the sort entirely.

The patch always keeps the ordering specified in the query, assuming the user
might have additional insights.

This introduces a new GUC enable_group_by_reordering so that the optimization
may be disabled if needed.

Discussion: https://postgr.es/m/7c79e6a5-8597-74e8-0671-1c39d124c9d6%40sigaev.ru
Author: Andrei Lepikhov, Teodor Sigaev
Reviewed-by: Tomas Vondra, Claudio Freire, Gavin Flower, Dmitry Dolgov
Reviewed-by: Robert Haas, Pavel Borisov, David Rowley, Zhihong Yu
Reviewed-by: Tom Lane, Alexander Korotkov, Richard Guo, Alena Rybakina
2024-01-21 22:21:36 +02:00
..
backend Explore alternative orderings of group-by pathkeys during optimization. 2024-01-21 22:21:36 +02:00
bin Rename COPY option from SAVE_ERROR_TO to ON_ERROR 2024-01-19 15:15:51 +02:00
common Cleanup for unicode-update build target and test. 2024-01-11 12:35:29 -08:00
fe_utils Update copyright for 2024 2024-01-03 20:49:05 -05:00
include Explore alternative orderings of group-by pathkeys during optimization. 2024-01-21 22:21:36 +02:00
interfaces Remove some comments related to pqPipelineSync() and PQsendPipelineSync() 2024-01-17 15:53:59 +09:00
makefiles Update copyright for 2024 2024-01-03 20:49:05 -05:00
pl Fix plpgsql to allow new-style SQL CREATE FUNCTION as a SQL command. 2024-01-18 16:11:16 -05:00
port Update copyright for 2024 2024-01-03 20:49:05 -05:00
template Use unnamed POSIX semaphores on Cygwin. 2023-01-06 10:33:28 +13:00
test Explore alternative orderings of group-by pathkeys during optimization. 2024-01-21 22:21:36 +02:00
timezone Update copyright for 2024 2024-01-03 20:49:05 -05:00
tools Explore alternative orderings of group-by pathkeys during optimization. 2024-01-21 22:21:36 +02:00
tutorial Update copyright for 2024 2024-01-03 20:49:05 -05:00
.gitignore Convert cvsignore to gitignore, and add .gitignore for build targets. 2010-09-22 12:57:04 +02:00
DEVELOPERS Replace a couple of references to files that no longer exist in the source 2009-05-04 08:08:47 +00:00
Makefile Remove distprep 2023-11-06 15:18:04 +01:00
Makefile.global.in Initialize data directories with --lc-messages=C for tests. 2023-12-21 15:10:13 -08:00
Makefile.shlib Stop using "-multiply_defined suppress" on macOS. 2023-09-26 21:06:21 -04:00
meson.build Update copyright for 2024 2024-01-03 20:49:05 -05:00
nls-global.mk Remove distprep 2023-11-06 15:18:04 +01:00