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 <[email protected]>
Reported-by: Bertrand Drouvot <[email protected]>
Author: Bertrand Drouvot <[email protected]>
Author: Melanie Plageman <[email protected]>
Reviewed-by: Bertrand Drouvot <[email protected]>
Reviewed-by: Nazir Bilal Yavuz <[email protected]>
Reviewed-by: shihao zhong <[email protected]> (earlier version)
Reviewed-by: Joao Rito <[email protected]> (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_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/e645b48a6921a6293e1b451f2f234cd6aea51be1

Modified Files
--------------
src/backend/access/heap/heapam.c                   | 155 +++++++++++----------
src/backend/storage/aio/read_stream.c              |   4 +-
src/include/access/heapam.h                        |  11 +-
src/test/modules/test_misc/meson.build             |   1 +
.../modules/test_misc/t/016_heap_scan_stream.pl    |  62 +++++++++
5 files changed, 156 insertions(+), 77 deletions(-)

Reply via email to