From e925998d4cee94941af52d13864ef994452e7402 Mon Sep 17 00:00:00 2001 From: Chengpeng Yan Date: Sat, 28 Nov 2026 10:32:47 +0800 Subject: [PATCH v1 1/2] Skip copying rejected Datums in bounded sorts A full bounded heap can reject a Datum without copying it. Compare pass-by-reference inputs with the root first and reuse that comparison when admitting a copy. Existing tests cover this path. --- src/backend/utils/sort/tuplesort.c | 26 +++++++++++ src/backend/utils/sort/tuplesortvariants.c | 54 ++++++++++++++++++++-- src/include/utils/tuplesort.h | 16 +++++++ src/tools/pgindent/typedefs.list | 1 + 4 files changed, 93 insertions(+), 4 deletions(-) diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c index 31ce2e661e3..a688afafd7f 100644 --- a/src/backend/utils/sort/tuplesort.c +++ b/src/backend/utils/sort/tuplesort.c @@ -680,6 +680,7 @@ tuplesort_begin_batch(Tuplesortstate *state) state->status = TSS_INITIAL; state->bounded = false; state->boundUsed = false; + state->base.boundedRoot = NULL; state->availMem = state->allowedMem; @@ -1069,6 +1070,29 @@ noalloc: return false; } +/* + * Replace the root of the full bounded heap with a copied tuple already + * known to sort before it. + */ +void +tuplesort_bounded_puttuple(Tuplesortstate *state, SortTuple *tuple, + Size tuplen) +{ + MemoryContext oldcontext = MemoryContextSwitchTo(state->base.sortcontext); + + Assert(state->status == TSS_BOUNDED); + Assert(state->base.boundedRoot == &state->memtuples[0]); + + /* account for the memory used for this tuple */ + USEMEM(state, tuplen); + state->tupleMem += tuplen; + + free_sort_tuple(state, &state->memtuples[0]); + tuplesort_heap_replace_top(state, tuple); + + MemoryContextSwitchTo(oldcontext); +} + /* * Shared code for tuple and datum cases. */ @@ -2534,6 +2558,7 @@ make_bounded_heap(Tuplesortstate *state) Assert(state->memtupcount == state->bound); state->status = TSS_BOUNDED; + state->base.boundedRoot = &state->memtuples[0]; } /* @@ -2572,6 +2597,7 @@ sort_bounded_heap(Tuplesortstate *state) state->status = TSS_SORTEDINMEM; state->boundUsed = true; + state->base.boundedRoot = NULL; } diff --git a/src/backend/utils/sort/tuplesortvariants.c b/src/backend/utils/sort/tuplesortvariants.c index f1051098b3d..bc6a2e18a29 100644 --- a/src/backend/utils/sort/tuplesortvariants.c +++ b/src/backend/utils/sort/tuplesortvariants.c @@ -28,6 +28,7 @@ #include "catalog/index.h" #include "catalog/pg_collation.h" #include "executor/executor.h" +#include "miscadmin.h" #include "pg_trace.h" #include "utils/builtins.h" #include "utils/datum.h" @@ -739,6 +740,36 @@ tuplesort_begin_datum(Oid datumType, Oid sortOperator, Oid sortCollation, return state; } +/* + * Compare an input's leading key with that of the bounded heap's root, to + * decide whether the input is kept. + */ +static inline TuplesortBoundedCheck +bounded_check(TuplesortPublic *base, Datum datum1, bool isnull1) +{ + MemoryContext oldcontext; + int compare; + + /* tuplesort_set_bound() disabled abbreviated keys */ + Assert(base->sortKeys->abbrev_converter == NULL); + /* one sort key, so a tie is final and the input is discarded */ + Assert(base->nKeys == 1); + + /* Use the same context as tuplesort_puttuple_common(). */ + oldcontext = MemoryContextSwitchTo(base->sortcontext); + compare = ApplySortComparator(datum1, isnull1, + base->boundedRoot->datum1, + base->boundedRoot->isnull1, + base->sortKeys); + MemoryContextSwitchTo(oldcontext); + + if (compare > 0) + return TUPLESORT_BOUNDED_REPLACE; + + CHECK_FOR_INTERRUPTS(); + return TUPLESORT_BOUNDED_DISCARD; +} + /* * Accept one tuple while collecting input data for sort. * @@ -940,7 +971,7 @@ tuplesort_putgintuple(Tuplesortstate *state, GinTuple *tuple, Size size) /* * Accept one Datum while collecting input data for sort. * - * If the Datum is pass-by-ref type, the value will be copied. + * If the Datum is pass-by-ref type, the value will be copied if it is kept. */ void tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull) @@ -950,6 +981,7 @@ tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull) TuplesortDatumArg *arg = (TuplesortDatumArg *) base->arg; SortTuple stup; Size tuplen; + TuplesortBoundedCheck check = TUPLESORT_BOUNDED_UNKNOWN; /* * Pass-by-value types or null values are just stored directly in @@ -976,6 +1008,16 @@ tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull) } else { + if (base->boundedRoot != NULL) + { + check = bounded_check(base, val, false); + if (check == TUPLESORT_BOUNDED_DISCARD) + { + MemoryContextSwitchTo(oldcontext); + return; + } + } + stup.isnull1 = false; stup.datum1 = datumCopy(val, false, arg->datumTypeLen); stup.tuple = DatumGetPointer(stup.datum1); @@ -989,9 +1031,13 @@ tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull) tuplen = GetMemoryChunkSpace(stup.tuple); } - tuplesort_puttuple_common(state, &stup, - base->tuples && - base->sortKeys->abbrev_converter && !isNull, tuplen); + if (check == TUPLESORT_BOUNDED_UNKNOWN) + tuplesort_puttuple_common(state, &stup, + base->tuples && + base->sortKeys->abbrev_converter && !isNull, + tuplen); + else + tuplesort_bounded_puttuple(state, &stup, tuplen); MemoryContextSwitchTo(oldcontext); } diff --git a/src/include/utils/tuplesort.h b/src/include/utils/tuplesort.h index 1e77a21692f..9ff0338ffa5 100644 --- a/src/include/utils/tuplesort.h +++ b/src/include/utils/tuplesort.h @@ -216,6 +216,9 @@ typedef struct bool tuples; /* Can SortTuple.tuple ever be set? */ + /* Root of the full bounded heap during input, or NULL. */ + SortTuple *boundedRoot; + void *arg; /* Specific information for the sort variant */ } TuplesortPublic; @@ -344,6 +347,19 @@ extern Tuplesortstate *tuplesort_begin_common(int workMem, int sortopt); extern void tuplesort_set_bound(Tuplesortstate *state, int64 bound); extern bool tuplesort_used_bound(Tuplesortstate *state); + +/* + * Result of comparing an input tuple's leading key with that of boundedRoot. + */ +typedef enum TuplesortBoundedCheck +{ + TUPLESORT_BOUNDED_UNKNOWN, /* not compared; add the tuple as usual */ + TUPLESORT_BOUNDED_DISCARD, + TUPLESORT_BOUNDED_REPLACE, +} TuplesortBoundedCheck; + +extern void tuplesort_bounded_puttuple(Tuplesortstate *state, + SortTuple *tuple, Size tuplen); extern void tuplesort_puttuple_common(Tuplesortstate *state, SortTuple *tuple, bool useAbbrev, Size tuplen); diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index f10eb469266..906ce62d50f 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -3268,6 +3268,7 @@ TupleHashTableData TupleQueueReader TupleTableSlot TupleTableSlotOps +TuplesortBoundedCheck TuplesortClusterArg TuplesortDatumArg TuplesortIndexArg -- 2.50.1 (Apple Git-155)