1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-27 23:21:58 +03:00

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
This commit is contained in:
David Rowley
2022-08-02 23:11:45 +12:00
parent a69959fab2
commit 1349d2790b
24 changed files with 850 additions and 139 deletions

View File

@ -3666,13 +3666,17 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
scratch.resnull = &state->resnull;
}
argno++;
Assert(pertrans->numInputs == argno);
}
else if (pertrans->numSortCols == 0)
else if (!pertrans->aggsortrequired)
{
ListCell *arg;
/*
* Normal transition function without ORDER BY / DISTINCT.
* Normal transition function without ORDER BY / DISTINCT or with
* ORDER BY / DISTINCT but the planner has given us pre-sorted
* input.
*/
strictargs = trans_fcinfo->args + 1;
@ -3680,6 +3684,13 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
{
TargetEntry *source_tle = (TargetEntry *) lfirst(arg);
/*
* Don't initialize args for any ORDER BY clause that might
* exist in a presorted aggregate.
*/
if (argno == pertrans->numTransInputs)
break;
/*
* Start from 1, since the 0th arg will be the transition
* value
@ -3689,11 +3700,13 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
&trans_fcinfo->args[argno + 1].isnull);
argno++;
}
Assert(pertrans->numTransInputs == argno);
}
else if (pertrans->numInputs == 1)
{
/*
* DISTINCT and/or ORDER BY case, with a single column sorted on.
* Non-presorted DISTINCT and/or ORDER BY case, with a single
* column sorted on.
*/
TargetEntry *source_tle =
(TargetEntry *) linitial(pertrans->aggref->args);
@ -3705,11 +3718,14 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
&state->resnull);
strictnulls = &state->resnull;
argno++;
Assert(pertrans->numInputs == argno);
}
else
{
/*
* DISTINCT and/or ORDER BY case, with multiple columns sorted on.
* Non-presorted DISTINCT and/or ORDER BY case, with multiple
* columns sorted on.
*/
Datum *values = pertrans->sortslot->tts_values;
bool *nulls = pertrans->sortslot->tts_isnull;
@ -3725,8 +3741,8 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
&values[argno], &nulls[argno]);
argno++;
}
Assert(pertrans->numInputs == argno);
}
Assert(pertrans->numInputs == argno);
/*
* For a strict transfn, nothing happens when there's a NULL input; we
@ -3748,6 +3764,21 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
state->steps_len - 1);
}
/* Handle DISTINCT aggregates which have pre-sorted input */
if (pertrans->numDistinctCols > 0 && !pertrans->aggsortrequired)
{
if (pertrans->numDistinctCols > 1)
scratch.opcode = EEOP_AGG_PRESORTED_DISTINCT_MULTI;
else
scratch.opcode = EEOP_AGG_PRESORTED_DISTINCT_SINGLE;
scratch.d.agg_presorted_distinctcheck.pertrans = pertrans;
scratch.d.agg_presorted_distinctcheck.jumpdistinct = -1; /* adjust later */
ExprEvalPushStep(state, &scratch);
adjust_bailout = lappend_int(adjust_bailout,
state->steps_len - 1);
}
/*
* Call transition function (once for each concurrently evaluated
* grouping set). Do so for both sort and hash based computations, as
@ -3808,6 +3839,12 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
Assert(as->d.agg_deserialize.jumpnull == -1);
as->d.agg_deserialize.jumpnull = state->steps_len;
}
else if (as->opcode == EEOP_AGG_PRESORTED_DISTINCT_SINGLE ||
as->opcode == EEOP_AGG_PRESORTED_DISTINCT_MULTI)
{
Assert(as->d.agg_presorted_distinctcheck.jumpdistinct == -1);
as->d.agg_presorted_distinctcheck.jumpdistinct = state->steps_len;
}
else
Assert(false);
}
@ -3857,7 +3894,8 @@ ExecBuildAggTransCall(ExprState *state, AggState *aggstate,
/*
* Determine appropriate transition implementation.
*
* For non-ordered aggregates:
* For non-ordered aggregates and ORDER BY / DISTINCT aggregates with
* presorted input:
*
* If the initial value for the transition state doesn't exist in the
* pg_aggregate table then we will let the first non-NULL value returned
@ -3887,7 +3925,7 @@ ExecBuildAggTransCall(ExprState *state, AggState *aggstate,
* process_ordered_aggregate_{single, multi} and
* advance_transition_function.
*/
if (pertrans->numSortCols == 0)
if (!pertrans->aggsortrequired)
{
if (pertrans->transtypeByVal)
{