Improve performance of ORDER BY / DISTINCT aggregates
authorDavid Rowley <drowley@postgresql.org>
Tue, 2 Aug 2022 11:11:45 +0000 (23:11 +1200)
committerDavid Rowley <drowley@postgresql.org>
Tue, 2 Aug 2022 11:11:45 +0000 (23:11 +1200)
commit1349d2790bf48a4de072931c722f39337e72055e
tree3b525f30da6d37513522cdb5ea34ce14b653de87
parenta69959fab2f3633992b5cabec85acecbac6074c8
Improve performance of ORDER BY / DISTINCT aggregates

ORDER BY / DISTINCT aggreagtes have, since implemented in Postgres, been
executed by always performing a sort in nodeAgg.c to sort the tuples in
the current group into the correct order before calling the transition
function on the sorted tuples.  This was not great as often there might be
an index that could have provided pre-sorted input and allowed the
transition functions to be called as the rows come in, rather than having
to store them in a tuplestore in order to sort them once all the tuples
for the group have arrived.

Here we change the planner so it requests a path with a sort order which
supports the most amount of ORDER BY / DISTINCT aggregate functions and
add new code to the executor to allow it to support the processing of
ORDER BY / DISTINCT aggregates where the tuples are already sorted in the
correct order.

Since there can be many ORDER BY / DISTINCT aggregates in any given query
level, it's very possible that we can't find an order that suits all of
these aggregates.  The sort order that the planner chooses is simply the
one that suits the most aggregate functions.  We take the most strictly
sorted variation of each order and see how many aggregate functions can
use that, then we try again with the order of the remaining aggregates to
see if another order would suit more aggregate functions.  For example:

SELECT agg(a ORDER BY a),agg2(a ORDER BY a,b) ...

would request the sort order to be {a, b} because {a} is a subset of the
sort order of {a,b}, but;

SELECT agg(a ORDER BY a),agg2(a ORDER BY c) ...

would just pick a plan ordered by {a} (we give precedence to aggregates
which are earlier in the targetlist).

SELECT agg(a ORDER BY a),agg2(a ORDER BY b),agg3(a ORDER BY b) ...

would choose to order by {b} since two aggregates suit that vs just one
that requires input ordered by {a}.

Author: David Rowley
Reviewed-by: Ronan Dunklau, James Coleman, Ranier Vilela, Richard Guo, Tom Lane
Discussion: https://postgr.es/m/CAApHDvpHzfo92%3DR4W0%2BxVua3BUYCKMckWAmo-2t_KiXN-wYH%3Dw%40mail.gmail.com
24 files changed:
contrib/postgres_fdw/expected/postgres_fdw.out
contrib/postgres_fdw/sql/postgres_fdw.sql
src/backend/executor/execExpr.c
src/backend/executor/execExprInterp.c
src/backend/executor/nodeAgg.c
src/backend/jit/llvm/llvmjit_expr.c
src/backend/jit/llvm/llvmjit_types.c
src/backend/optimizer/path/pathkeys.c
src/backend/optimizer/plan/planagg.c
src/backend/optimizer/plan/planner.c
src/backend/optimizer/prep/prepagg.c
src/backend/parser/parse_expr.c
src/backend/parser/parse_func.c
src/include/catalog/catversion.h
src/include/executor/execExpr.h
src/include/executor/nodeAgg.h
src/include/nodes/pathnodes.h
src/include/nodes/primnodes.h
src/include/optimizer/paths.h
src/test/regress/expected/aggregates.out
src/test/regress/expected/partition_aggregate.out
src/test/regress/expected/sqljson.out
src/test/regress/expected/tuplesort.out
src/test/regress/sql/aggregates.sql