From 5487585e376dd25d2cabc8a63a33c0286b3a199c Mon Sep 17 00:00:00 2001 From: Peter Geoghegan Date: Wed, 20 Apr 2022 17:17:37 -0700 Subject: Fix CLUSTER tuplesorts on abbreviated expressions. CLUSTER sort won't use the datum1 SortTuple field when clustering against an index whose leading key is an expression. This makes it unsafe to use the abbreviated keys optimization, which was missed by the logic that sets up SortSupport state. Affected tuplesorts output tuples in a completely bogus order as a result (the wrong SortSupport based comparator was used for the leading attribute). This issue is similar to the bug fixed on the master branch by recent commit cc58eecc5d. But it's a far older issue, that dates back to the introduction of the abbreviated keys optimization by commit 4ea51cdfe8. Backpatch to all supported versions. Author: Peter Geoghegan Author: Thomas Munro Discussion: https://postgr.es/m/CA+hUKG+bA+bmwD36_oDxAoLrCwZjVtST2fqe=b4=qZcmU7u89A@mail.gmail.com Backpatch: 10- --- src/backend/utils/sort/tuplesort.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/backend/utils') diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c index f975d24a98c..043d01f7d61 100644 --- a/src/backend/utils/sort/tuplesort.c +++ b/src/backend/utils/sort/tuplesort.c @@ -884,6 +884,7 @@ tuplesort_begin_cluster(TupleDesc tupDesc, { Tuplesortstate *state = tuplesort_begin_common(workMem, coordinate, randomAccess); + AttrNumber leading; BTScanInsert indexScanKey; MemoryContext oldcontext; int i; @@ -916,6 +917,7 @@ tuplesort_begin_cluster(TupleDesc tupDesc, state->abbrevNext = 10; state->indexInfo = BuildIndexInfo(indexRel); + leading = state->indexInfo->ii_IndexAttrNumbers[0]; state->tupDesc = tupDesc; /* assume we need not copy tupDesc */ @@ -954,7 +956,7 @@ tuplesort_begin_cluster(TupleDesc tupDesc, (scanKey->sk_flags & SK_BT_NULLS_FIRST) != 0; sortKey->ssup_attno = scanKey->sk_attno; /* Convey if abbreviation optimization is applicable in principle */ - sortKey->abbreviate = (i == 0); + sortKey->abbreviate = (i == 0 && leading != 0); AssertState(sortKey->ssup_attno != 0); -- cgit v1.2.3