pg.ddx.io pgsql-committers@postgresql.org mailing list archivehelp / color / mirror / Atom feed
pgsql: Fix possible crash with Incremental Sort when boundedness change 6+ messages / 1 participants [nested] [flat]
* pgsql: Fix possible crash with Incremental Sort when boundedness change @ 2026-11-18 01:14 David Rowley <drowley@postgresql.org> 0 siblings, 0 replies; 6+ messages in thread From: David Rowley @ 2026-11-18 01:14 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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 ------ master Details ------- https://git.postgresql.org/pg/commitdiff/65507f0daa8fa08a2319b37ec1b65d3f044d3b8f 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(-) ^ permalink raw reply [nested|flat] 6+ messages in thread
* pgsql: Fix possible crash with Incremental Sort when boundedness change @ 2026-11-18 01:14 David Rowley <drowley@postgresql.org> 0 siblings, 0 replies; 6+ messages in thread From: David Rowley @ 2026-11-18 01:14 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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_19_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/882f6b32f4ae42d32d63730985658040e88e7dc0 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(-) ^ permalink raw reply [nested|flat] 6+ messages in thread
* pgsql: Fix possible crash with Incremental Sort when boundedness change @ 2026-11-18 01:14 David Rowley <drowley@postgresql.org> 0 siblings, 0 replies; 6+ messages in thread From: David Rowley @ 2026-11-18 01:14 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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_18_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/4af67e0e97bce148d5b0bbb89f80f7c1ed94d10b 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(-) ^ permalink raw reply [nested|flat] 6+ messages in thread
* pgsql: Fix possible crash with Incremental Sort when boundedness change @ 2026-11-18 01:15 David Rowley <drowley@postgresql.org> 0 siblings, 0 replies; 6+ messages in thread From: David Rowley @ 2026-11-18 01:15 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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_17_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/f9c88051d64670847cf678b44ecba590dbbc3cca 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(-) ^ permalink raw reply [nested|flat] 6+ messages in thread
* pgsql: Fix possible crash with Incremental Sort when boundedness change @ 2026-11-18 01:15 David Rowley <drowley@postgresql.org> 0 siblings, 0 replies; 6+ messages in thread From: David Rowley @ 2026-11-18 01:15 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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(-) ^ permalink raw reply [nested|flat] 6+ messages in thread
* pgsql: Fix possible crash with Incremental Sort when boundedness change @ 2026-11-18 01:15 David Rowley <drowley@postgresql.org> 0 siblings, 0 replies; 6+ messages in thread From: David Rowley @ 2026-11-18 01:15 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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_15_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/c62daefd62c1b428b6776a0b3e7c909c7fd7f1a0 Modified Files -------------- src/backend/executor/nodeIncrementalSort.c | 10 ++++++- src/backend/utils/sort/tuplesort.c | 16 +++++++++++ src/include/utils/tuplesort.h | 3 ++- src/test/regress/expected/incremental_sort.out | 37 ++++++++++++++++++++++++++ src/test/regress/sql/incremental_sort.sql | 19 +++++++++++++ 5 files changed, 83 insertions(+), 2 deletions(-) ^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2026-11-18 01:15 UTC | newest] Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2026-11-18 01:14 pgsql: Fix possible crash with Incremental Sort when boundedness change David Rowley <drowley@postgresql.org> 2026-11-18 01:14 pgsql: Fix possible crash with Incremental Sort when boundedness change David Rowley <drowley@postgresql.org> 2026-11-18 01:14 pgsql: Fix possible crash with Incremental Sort when boundedness change David Rowley <drowley@postgresql.org> 2026-11-18 01:15 pgsql: Fix possible crash with Incremental Sort when boundedness change David Rowley <drowley@postgresql.org> 2026-11-18 01:15 pgsql: Fix possible crash with Incremental Sort when boundedness change David Rowley <drowley@postgresql.org> 2026-11-18 01:15 pgsql: Fix possible crash with Incremental Sort when boundedness change David Rowley <drowley@postgresql.org>
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