From: David Rowley <drowley@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Improve performance of ORDER BY / DISTINCT aggregates
Date: Tue, 02 Aug 2022 11:12:22 +0000
Message-ID: <E1oIpoz-002OTq-SN@gemulon.postgresql.org> (raw)
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
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/1349d2790bf48a4de072931c722f39337e72055e
Modified Files
--------------
contrib/postgres_fdw/expected/postgres_fdw.out | 32 ++-
contrib/postgres_fdw/sql/postgres_fdw.sql | 2 +
src/backend/executor/execExpr.c | 52 +++-
src/backend/executor/execExprInterp.c | 102 +++++++
src/backend/executor/nodeAgg.c | 34 ++-
src/backend/jit/llvm/llvmjit_expr.c | 48 ++++
src/backend/jit/llvm/llvmjit_types.c | 2 +
src/backend/optimizer/path/pathkeys.c | 45 +++-
src/backend/optimizer/plan/planagg.c | 2 +-
src/backend/optimizer/plan/planner.c | 310 ++++++++++++++++++++--
src/backend/optimizer/prep/prepagg.c | 7 +-
src/backend/parser/parse_expr.c | 1 +
src/backend/parser/parse_func.c | 1 +
src/include/catalog/catversion.h | 2 +-
src/include/executor/execExpr.h | 17 ++
src/include/executor/nodeAgg.h | 8 +
src/include/nodes/pathnodes.h | 16 +-
src/include/nodes/primnodes.h | 7 +
src/include/optimizer/paths.h | 4 +-
src/test/regress/expected/aggregates.out | 80 +++++-
src/test/regress/expected/partition_aggregate.out | 118 ++++----
src/test/regress/expected/sqljson.out | 12 +-
src/test/regress/expected/tuplesort.out | 42 +--
src/test/regress/sql/aggregates.sql | 43 +++
24 files changed, 849 insertions(+), 138 deletions(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: drowley@postgresql.org
Subject: Re: pgsql: Improve performance of ORDER BY / DISTINCT aggregates
In-Reply-To: <E1oIpoz-002OTq-SN@gemulon.postgresql.org>
References: <E1oIpoz-002OTq-SN@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox