pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Fix accidental ignoring of subsequent keys in Sort nodes
6+ messages / 1 participants
[nested] [flat]

* pgsql: Fix accidental ignoring of subsequent keys in Sort nodes
@ 2026-11-04 21:23 David Rowley <drowley@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: David Rowley @ 2026-11-04 21:23 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/b715192e52e7310a041b9ac64485560c20cfce39

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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix accidental ignoring of subsequent keys in Sort nodes
@ 2026-11-04 21:24 David Rowley <drowley@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: David Rowley @ 2026-11-04 21:24 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/1c2ba4df37c6270d98782d5ad20e6bbd199dd448

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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix accidental ignoring of subsequent keys in Sort nodes
@ 2026-11-04 21:24 David Rowley <drowley@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: David Rowley @ 2026-11-04 21:24 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/bfbb3d84209f60698f327ad54176240d27e5ee2a

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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix accidental ignoring of subsequent keys in Sort nodes
@ 2026-11-04 21:24 David Rowley <drowley@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: David Rowley @ 2026-11-04 21:24 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/9171fca990e668a0fb50008232722e835878cdd1

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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix accidental ignoring of subsequent keys in Sort nodes
@ 2026-11-04 21:24 David Rowley <drowley@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: David Rowley @ 2026-11-04 21:24 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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(-)



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix accidental ignoring of subsequent keys in Sort nodes
@ 2026-11-04 21:24 David Rowley <drowley@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: David Rowley @ 2026-11-04 21:24 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/6e860511622a8f6894ae9294280b584cd98e3fdb

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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2026-11-04 21:24 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-04 21:23 pgsql: Fix accidental ignoring of subsequent keys in Sort nodes David Rowley <drowley@postgresql.org>
2026-11-04 21:24 pgsql: Fix accidental ignoring of subsequent keys in Sort nodes David Rowley <drowley@postgresql.org>
2026-11-04 21:24 pgsql: Fix accidental ignoring of subsequent keys in Sort nodes David Rowley <drowley@postgresql.org>
2026-11-04 21:24 pgsql: Fix accidental ignoring of subsequent keys in Sort nodes David Rowley <drowley@postgresql.org>
2026-11-04 21:24 pgsql: Fix accidental ignoring of subsequent keys in Sort nodes David Rowley <drowley@postgresql.org>
2026-11-04 21:24 pgsql: Fix accidental ignoring of subsequent keys in Sort nodes David Rowley <drowley@postgresql.org>

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