pgsql: Recreate read stream across rescans

From: Melanie Plageman <melanieplageman(at)gmail(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Recreate read stream across rescans
Date: 2026-11-18 22:39:52
Message-ID: E1xToJj-00000000Ixc-2zdK@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Recreate read stream across rescans

Heap scan descriptors persist across rescans, but initscan() can add or
remove the buffer access strategy when the relation size or caller flags
change.

The ReadStream stored the strategy pointer and sized its queue capacity
based on the strategy at ReadStream creation, which was only before the
first scan. This meant that on rescan, enabling a bulk-read strategy
left the existing stream using its NULL strategy and old capacity.
And disabling the strategy left the stream pointing to freed memory.

Reset and reuse the stream when strategy use is unchanged. Otherwise,
end it before changing the strategy and create a correctly sized
replacement.

Reported-by: Nazir Bilal Yavuz <byavuz81(at)gmail(dot)com>
Reported-by: Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>
Author: Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>
Author: Melanie Plageman <melanieplageman(at)gmail(dot)com>
Reviewed-by: Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>
Reviewed-by: Nazir Bilal Yavuz <byavuz81(at)gmail(dot)com>
Reviewed-by: shihao zhong <zhong950419(at)gmail(dot)com> (earlier version)
Reviewed-by: Joao Rito <joaovictor(dot)rito(at)gmail(dot)com> (earlier version)
Discussion: https://postgr.es/m/aumB7BHGWewR4x%2FS%40bdtpg
Discussion: https://postgr.es/m/CAN55FZ3Lik2m9J%2BEQjzEHSMSBNgAXU5Myt8LFg%3DD2OQ0i_jUMQ%40mail.gmail.com
Backpatch-through: 17

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/9107706245e2368b9e3519629761b70cb93993e7

Modified Files
--------------
src/backend/access/heap/heapam.c | 140 +++++++++++----------
src/include/access/heapam.h | 11 +-
src/test/modules/test_misc/meson.build | 1 +
.../modules/test_misc/t/016_heap_scan_stream.pl | 62 +++++++++
4 files changed, 146 insertions(+), 68 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Melanie Plageman 2026-11-18 22:43:33 pgsql: Recreate read stream across rescans
Previous Message Jeff Davis 2026-11-18 22:37:11 pgsql: Recovery: ensure CLOG updates wait for minRecoveryPoint.