postgresql/src/include/nodes
David Rowley 1349d2790b 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
2022-08-02 23:11:45 +12:00
..
.gitignore Automatically generate node support functions 2022-07-09 08:53:59 +02:00
bitmapset.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
execnodes.h Doc: rearrange high-level commentary about node support coverage. 2022-07-09 15:10:15 -04:00
extensible.h Automatically generate node support functions 2022-07-09 08:53:59 +02:00
lockoptions.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
makefuncs.h Rename JsonIsPredicate.value_type, fix JSON backend/nodes/ infrastructure. 2022-05-13 11:40:08 -04:00
memnodes.h Mark PlanState as an abstract node type. 2022-07-09 13:35:37 -04:00
nodeFuncs.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
nodes.h Add defenses against unexpected changes in the NodeTag enum list. 2022-07-12 11:22:52 -04:00
params.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
parsenodes.h Tighten up parsing logic in gen_node_support.pl. 2022-07-14 09:04:23 -04:00
pathnodes.h Improve performance of ORDER BY / DISTINCT aggregates 2022-08-02 23:11:45 +12:00
pg_list.h Tidy up code in get_cheapest_group_keys_order() 2022-07-13 14:02:20 +12:00
plannodes.h Mark Scan as an abstract node type, too. 2022-07-09 13:58:06 -04:00
primnodes.h Improve performance of ORDER BY / DISTINCT aggregates 2022-08-02 23:11:45 +12:00
print.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
readfuncs.h Apply PGDLLIMPORT markings broadly. 2022-04-08 08:16:38 -04:00
replnodes.h Fix limitations on what SQL commands can be issued to a walsender. 2022-01-24 15:33:38 -05:00
subscripting.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
supportnodes.h Teach planner and executor about monotonic window funcs 2022-04-08 10:34:36 +12:00
tidbitmap.h Update copyright for 2022 2022-01-07 19:04:57 -05:00
value.h Automatically generate node support functions 2022-07-09 08:53:59 +02:00