postgres.git / summary / log / commit / refs
commit bfbb3d84209f60698f327ad54176240d27e5ee2a
Author: David Rowley <drowley@postgresql.org>
Date: Wed Nov 04 21:21:17 2026 +0000
First released in: 15.20, 16.16, 17.12, 18.7, 19.0
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
src/backend/executor/nodeSort.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/src/backend/executor/nodeSort.c b/src/backend/executor/nodeSort.c
index c5219fe880b..da0f1f60dfd 100644
--- a/src/backend/executor/nodeSort.c
+++ b/src/backend/executor/nodeSort.c
@@ -279,10 +279,14 @@ ExecInitSort(Sort *node, EState *estate, int eflags)
outerTupDesc = ExecGetResultType(outerPlanState(sortstate));
/*
- * We perform a Datum sort when we're sorting just a single column,
- * otherwise we perform a tuple sort.
+ * We perform a Datum sort when we're sorting just a single column by a
+ * single key, otherwise we perform a tuple sort. You might think that
+ * only 1 sort key would be relevant if there's only 1 output column since
+ * all keys must be on that column, but the sort keys may differ in
+ * collation, and if the first key's collation is nondeterministic then
+ * ignoring subsequent keys could lead to an incorrect sort order.
*/
- if (outerTupDesc->natts == 1)
+ if (outerTupDesc->natts == 1 && node->numCols == 1)
sortstate->datumSort = true;
else
sortstate->datumSort = false;
[parent: 558c00f9676e]