From: David Rowley <drowley@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix crash with single-input aggregates with multiple sort keys
Date: Fri, 06 Nov 2026 07:20:54 +0000
Message-ID: <E1xPEFq-00000000R3b-3JV0@gemulon.postgresql.org> (raw)
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
Branch
------
REL_15_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/21a53865fb5a199fc7f234a9f9fce5608a39e581
Modified Files
--------------
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(-)
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: Fix crash with single-input aggregates with multiple sort keys
In-Reply-To: <E1xPEFq-00000000R3b-3JV0@gemulon.postgresql.org>
References: <E1xPEFq-00000000R3b-3JV0@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