From d3919a7497390c25865788b16e00b887770170e1 Mon Sep 17 00:00:00 2001 From: Chengpeng Yan Date: Thu, 3 Dec 2026 21:58:27 +0800 Subject: [PATCH v2 1/2] Skip copying rejected Datums in bounded sorts Once a bounded sort's heap is full, most inputs lose to its root and are freed right after being copied. For pass-by-reference Datums, compare the input with the root first, and skip the copy when the input would be discarded. --- src/backend/utils/sort/tuplesort.c | 3 ++ src/backend/utils/sort/tuplesortvariants.c | 40 +++++++++++++++++++--- src/include/utils/tuplesort.h | 3 ++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c index 31ce2e661e3..574550ccc8d 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; @@ -2534,6 +2535,7 @@ make_bounded_heap(Tuplesortstate *state) Assert(state->memtupcount == state->bound); state->status = TSS_BOUNDED; + state->base.boundedRoot = &state->memtuples[0]; } /* @@ -2572,6 +2574,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..dbc8dedfd24 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,30 @@ tuplesort_begin_datum(Oid datumType, Oid sortOperator, Oid sortCollation, return state; } +/* + * Check whether the full bounded heap would discard an input with this + * leading key. + */ +static inline bool +bounded_discard(TuplesortPublic *base, Datum datum1, bool isnull1) +{ + MemoryContext oldcontext; + int compare; + + /* bounded sorts never use abbreviated keys */ + Assert(base->sortKeys->abbrev_converter == NULL); + + oldcontext = MemoryContextSwitchTo(base->sortcontext); + compare = ApplySortComparator(datum1, isnull1, + base->boundedRoot->datum1, + base->boundedRoot->isnull1, + base->sortKeys); + MemoryContextSwitchTo(oldcontext); + + /* the heap is in reverse sort order; later keys may break a tie */ + return compare < 0 || (compare == 0 && base->nKeys == 1); +} + /* * Accept one tuple while collecting input data for sort. * @@ -940,13 +965,12 @@ 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) { TuplesortPublic *base = TuplesortstateGetPublic(state); - MemoryContext oldcontext = MemoryContextSwitchTo(base->tuplecontext); TuplesortDatumArg *arg = (TuplesortDatumArg *) base->arg; SortTuple stup; Size tuplen; @@ -976,6 +1000,15 @@ tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull) } else { + MemoryContext oldcontext; + + if (base->boundedRoot != NULL && bounded_discard(base, val, false)) + { + CHECK_FOR_INTERRUPTS(); + return; + } + + oldcontext = MemoryContextSwitchTo(base->tuplecontext); stup.isnull1 = false; stup.datum1 = datumCopy(val, false, arg->datumTypeLen); stup.tuple = DatumGetPointer(stup.datum1); @@ -987,13 +1020,12 @@ tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull) arg->datumTypeLen)); else tuplen = GetMemoryChunkSpace(stup.tuple); + MemoryContextSwitchTo(oldcontext); } tuplesort_puttuple_common(state, &stup, base->tuples && base->sortKeys->abbrev_converter && !isNull, tuplen); - - MemoryContextSwitchTo(oldcontext); } /* diff --git a/src/include/utils/tuplesort.h b/src/include/utils/tuplesort.h index 1e77a21692f..251cc8efc3b 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; -- 2.50.1 (Apple Git-155)