pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Rowley <drowley@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix accidental ignoring of subsequent keys in Sort nodes
Date: Wed, 04 Nov 2026 21:24:44 +0000
Message-ID: <E1xOiTN-00000000DUV-00qc@gemulon.postgresql.org> (raw)

Fix accidental ignoring of subsequent keys in Sort nodes

91e9e89dc (v15) changed nodeSort.c to allow the slightly more efficient
Datum sort method to be used when the Sort node was only outputting a
single column.  That commit mistakenly failed to consider additional
sort keys.

This wasn't trivial to hit as adding more sort keys on another column
would add a resjunk target entry, so Datum sort wouldn't be used.  Hitting
this required all sort keys to be on the only output column, and then you
might think that subsequent sort keys *could* be ignored in that case, but
the collation for subsequent keys might differ from the first, and if the
first sort key had a nondeterministic collation, ignoring subsequent keys
isn't correct.

Ordinary ORDER BY queries were not affected by this as repeating the same
column in the ORDER BY clause with different collations would lead to
additional redundant target entries and would have failed the one-column
requirement to enable the Datum sort optimization.  The only known case
where problems could occur was sorting for the presorted aggregate
optimization added in 1349d2790 (v16).

A query such as the following would trigger this issue if the "x" column
was defined with a nondeterministic collation as the presorted input
would arrive at the aggregate only presorted by 'x' (using its
nondeterministic collation), rather than 'x, x COLLATE "C"':

SELECT count(DISTINCT x COLLATE "C") FROM t GROUP BY x;

This could lead to COUNT(DISTINCT) returning a count higher than the actual
number of distinct values according to the "C" collation.

Fix this by only using the Datum sort optimization when there's 1 output
column and 1 sort key, rather than just checking for 1 output column.

Reported-by: Nidal FAKIH <nidal.fakih@soptimal.com>
Author: David Rowley <dgrowleyml@gmail.com>
Discussion: https://postgr.es/m/c6790a7b-7c10-4baf-9c8f-b404420c63f9@soptimal.com
Backpatch-through: 15

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/0500fb3ab2472b14686972959a0bea309e8662a2

Modified Files
--------------
src/backend/executor/nodeSort.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)



view thread (6+ messages)  latest in thread

Message-ID: <E1xOiTN-00000000DUV-00qc@gemulon.postgresql.org>
Permalink:  ../E1xOiTN-00000000DUV-00qc@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xOiTN-00000000DUV-00qc@gemulon.postgresql.org

 · 

reply

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 accidental ignoring of subsequent keys in Sort nodes
  In-Reply-To: <E1xOiTN-00000000DUV-00qc@gemulon.postgresql.org>
  References: <E1xOiTN-00000000DUV-00qc@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