postgres.git / summary / log / commit / refs

commit    f72e4686e213d86e5e34a474019e500e3f0fcc63
Author:   David Rowley <drowley@postgresql.org>
Date:     Fri Nov 06 07:18:18 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0

    Fix crash with single-input aggregates with multiple sort keys
    
    An aggregate's ORDER BY can list the same sort key more than once with
    different sort operators, and transformDistinctClause() copies such
    items into the DISTINCT list.  So an aggregate with a single input can
    have several sort keys and several DISTINCT columns.
    
    nodeAgg.c and execExpr.c mistakenly only checked the number of inputs
    when deciding if a Datum sort was possible.  That check must also
    consider the number of sort keys.
    
    Fix by using the Datum sort only for a single input with a single sort
    key.
    
    Also for v16+, fix crash in pre-sorted aggregate code where the uniqslot
    was only created when the number of inputs for a DISTINCT aggregate was
    above 1.  The uniqslot is required when there are multiple DISTINCT
    columns, and an ORDER BY clause may cause more than one of these to exist
    when there's only a single input column.
    
    Author: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
    Reviewed-by: David Rowley <dgrowleyml@gmail.com>
    Discussion: https://postgr.es/m/CAJTYsWXMuo5QZSy_az+86mty2XdbDw_7uB3C4FjWpFACvD_rzg@mail.gmail.com
    Backpatch-through: 14


src/backend/executor/execExpr.c | 10 ++++++---- src/backend/executor/nodeAgg.c | 22 ++++++++++++++-------- src/test/regress/expected/aggregates.out | 17 +++++++++++++++++ src/test/regress/sql/aggregates.sql | 9 +++++++++ 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/backend/executor/execExpr.c b/src/backend/executor/execExpr.c index 47f28156d60..65b06a68a7d 100644 --- a/src/backend/executor/execExpr.c +++ b/src/backend/executor/execExpr.c @@ -3498,10 +3498,11 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase, argno++; } } - else if (pertrans->numInputs == 1) + else if (pertrans->numInputs == 1 && pertrans->numSortCols == 1) { /* - * DISTINCT and/or ORDER BY case, with a single column sorted on. + * DISTINCT and/or ORDER BY case, with a single input column + * sorted on a single key. */ TargetEntry *source_tle = (TargetEntry *) linitial(pertrans->aggref->args); @@ -3517,7 +3518,8 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase, else { /* - * DISTINCT and/or ORDER BY case, with multiple columns sorted on. + * DISTINCT and/or ORDER BY case, with multiple input columns or + * sort keys. */ Datum *values = pertrans->sortslot->tts_values; bool *nulls = pertrans->sortslot->tts_isnull; @@ -3718,7 +3720,7 @@ ExecBuildAggTransCall(ExprState *state, AggState *aggstate, scratch->opcode = EEOP_AGG_PLAIN_TRANS_BYREF; } } - else if (pertrans->numInputs == 1) + else if (pertrans->numInputs == 1 && pertrans->numSortCols == 1) scratch->opcode = EEOP_AGG_ORDERED_TRANS_DATUM; else scratch->opcode = EEOP_AGG_ORDERED_TRANS_TUPLE; diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c index 425f4d17419..b87b7ec2ed1 100644 --- a/src/backend/executor/nodeAgg.c +++ b/src/backend/executor/nodeAgg.c @@ -612,11 +612,11 @@ initialize_aggregate(AggState *aggstate, AggStatePerTrans pertrans, /* - * We use a plain Datum sorter when there's a single input column; - * otherwise sort the full tuple. (See comments for - * process_ordered_aggregate_single.) + * We use a plain Datum sorter when there's a single input column and + * a single sort key; otherwise sort the full tuple. (See comments + * for process_ordered_aggregate_single.) */ - if (pertrans->numInputs == 1) + if (pertrans->numInputs == 1 && pertrans->numSortCols == 1) { Form_pg_attribute attr = TupleDescAttr(pertrans->sortdesc, 0); @@ -948,7 +948,7 @@ process_ordered_aggregate_single(AggState *aggstate, /* * Run the transition function for a DISTINCT or ORDER BY aggregate - * with more than one input. This is called after we have completed + * with multiple inputs or sort keys. This is called after we have completed * entering all the input values into the sort object. We complete the * sort, read out the values in sorted order, and run the transition * function on each value (applying DISTINCT if appropriate). @@ -1332,7 +1332,7 @@ finalize_aggregates(AggState *aggstate, Assert(aggstate->aggstrategy != AGG_HASHED && aggstate->aggstrategy != AGG_MIXED); - if (pertrans->numInputs == 1) + if (pertrans->numInputs == 1 && pertrans->numSortCols == 1) process_ordered_aggregate_single(aggstate, pertrans, pergroupstate); @@ -4285,9 +4285,15 @@ build_pertrans_for_aggref(AggStatePerTrans pertrans, &pertrans->inputtypeLen, &pertrans->inputtypeByVal); } - else if (numDistinctCols > 0) + + if (numDistinctCols > 1) { - /* we will need an extra slot to store prior values */ + /* + * With more than one DISTINCT column we'll need an extra slot to + * store the prior values. Note that this can happen even with a + * single input, since the aggregate's ORDER BY may list the same + * argument more than once with different sort operators. + */ pertrans->uniqslot = ExecInitExtraTupleSlot(estate, pertrans->sortdesc, &TTSOpsMinimalTuple); diff --git a/src/test/regress/expected/aggregates.out b/src/test/regress/expected/aggregates.out index 9d941c28fd7..80753ec1fa2 100644 --- a/src/test/regress/expected/aggregates.out +++ b/src/test/regress/expected/aggregates.out @@ -1559,6 +1559,23 @@ select aggfns(distinct a,b,c order by a,c using ~<~,b) {"(0,,)","(1,3,foo)","(2,2,bar)","(3,1,baz)"} (1 row) +-- single input sorted on two keys; 0 and -0 differ only for *< +select array_agg(distinct r order by r using <, r using *<) + from (values (row(-0.0::float8)), (row(0.0::float8))) v(r), + generate_series(1,2) i; + array_agg +------------ + {(0),(-0)} +(1 row) + +-- likewise, but without DISTINCT; the second sort key must not be ignored +select array_agg(r order by r using <, r using *<) + from (values (row(-0.0::float8)), (row(0.0::float8))) v(r); + array_agg +------------ + {(0),(-0)} +(1 row) + -- check node I/O via view creation and usage, also deparsing logic create view agg_view1 as select aggfns(a,b,c) diff --git a/src/test/regress/sql/aggregates.sql b/src/test/regress/sql/aggregates.sql index e3ee84e0e20..509a5b66083 100644 --- a/src/test/regress/sql/aggregates.sql +++ b/src/test/regress/sql/aggregates.sql @@ -559,6 +559,15 @@ select aggfns(distinct a,b,c order by a,c using ~<~,b) from (values (1,3,'foo'),(0,null,null),(2,2,'bar'),(3,1,'baz')) v(a,b,c), generate_series(1,2) i; +-- single input sorted on two keys; 0 and -0 differ only for *< +select array_agg(distinct r order by r using <, r using *<) + from (values (row(-0.0::float8)), (row(0.0::float8))) v(r), + generate_series(1,2) i; + +-- likewise, but without DISTINCT; the second sort key must not be ignored +select array_agg(r order by r using <, r using *<) + from (values (row(-0.0::float8)), (row(0.0::float8))) v(r); + -- check node I/O via view creation and usage, also deparsing logic create view agg_view1 as [parent: 7e5d06901fc9]