From: David Rowley <drowley@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix possible crash with Incremental Sort when boundedness change
Date: Wed, 18 Nov 2026 01:15:18 +0000
Message-ID: <E1xTUGb-00000000ANH-3MSD@gemulon.postgresql.org> (raw)
Fix possible crash with Incremental Sort when boundedness changes
ExecSetTupleBound() can adjust the IncrementalSortState bounded flag
between rescans of the Incremental Sort node. Starting with 6ed83d5fa,
tuplesort.c uses the Bump memory context type when bounded sorts are not
required, as we never need to pfree individual tuples when not
performing a bounded sort. If the initial tuplesort was created without
TUPLESORT_ALLOWBOUNDED, and a subsequent scan became bounded, then
tuplesort would mistakenly store the tuples in a Bump context, but
bounded sorts pfree() tuples, which Bump does not support as it does not
use chunk headers.
This can happen when a LIMIT node's tuple limit isn't a Const, as the
actual limit may be known for some scans but not others. If the LIMIT
isn't known for the first scan, then the tuplesorts will be initialized
without the TUPLESORT_ALLOWBOUNDED flag, and if a later scan obtains a
bound, then the tuplesort will mistakenly use a Bump context type.
Here we fix this by adding a new external function for tuplesort which
allows the TUPLESORT_ALLOWBOUNDED flag to be changed. This must be
changed before calling tuplesort_reset() as that's the point when
tuplesort_begin_batch() creates the tuplecontext as either an AllocSet
context or Bump context. Ideally tuplesort_reset() would grow an extra
boolean parameter to control this on reset, but we can't change the
signature of that function in the backbranches.
Versions prior to v17 didn't have 6ed83d5fa or Bump memory contexts.
Non-bounded sorts use Generation contexts in v15 and v16, which do
support pfree(). This change is still required there to fix an Assert
failure in tuplesort_set_bound() and to resolve inefficient memory
usage as the Generation context is not well suited for pfreeing chunks
in an arbitrary order. AllocSet is better suited as it can reuse pfree'd
memory.
Reported-by: shihao zhong <zhong950419@gmail.com>
Author: David Rowley <dgrowleyml@gmail.com>
Tested-by: shihao zhong <zhong950419@gmail.com>
Discussion: https://postgr.es/m/CAGRkXqRXMiR9EBxBT7VEF0jF_9NJAxVcGrtRdsTZOfNkHfVtsg@mail.gmail.com
Backpatch-through: 15
Branch
------
REL_16_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/83b44ae91baa941787f0e7a97676950f38cea1fa
Modified Files
--------------
src/backend/executor/nodeIncrementalSort.c | 10 ++++++-
src/backend/utils/sort/tuplesort.c | 16 +++++++++++
src/include/utils/tuplesort.h | 2 ++
src/test/regress/expected/incremental_sort.out | 37 ++++++++++++++++++++++++++
src/test/regress/sql/incremental_sort.sql | 19 +++++++++++++
5 files changed, 83 insertions(+), 1 deletion(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: drowley@postgresql.org
Subject: Re: pgsql: Fix possible crash with Incremental Sort when boundedness change
In-Reply-To: <E1xTUGb-00000000ANH-3MSD@gemulon.postgresql.org>
References: <E1xTUGb-00000000ANH-3MSD@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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