From 977711a2414b534697824eca0df77ab21e2130d7 Mon Sep 17 00:00:00 2001 From: Peter Geoghegan Date: Sat, 10 Oct 2026 23:13:20 -0400 Subject: [PATCH v38 06/12] Rename READ_STREAM_SEQUENTIAL to READ_STREAM_NO_ADVICE. With io_method=sync, a read stream simulates asynchronous I/O by issuing posix_fadvise(WILLNEED) hints for the blocks that it expects to read soon. The flag that disables this was named after the only reason that its callers had to disable it: explicit advice is known to perform worse than the kernel's own read-ahead for sequential access. Rename the flag to READ_STREAM_NO_ADVICE, in preparation for index prefetching, which will give it a second kind of user. Index prefetching will pass the flag for the read stream that prefetches heap blocks during index scans, opting out of advice-based prefetching with io_method=sync altogether. This is necessary to avoid regressing io_method=sync: testing showed that index prefetching with advice-based I/O gave wildly varying performance, so the safest thing is to just keep reading each heap block synchronously, at the point where it is needed, with that io_method. This commit has no functional impact. The flag keeps its value, and its existing callers keep passing it. Author: Peter Geoghegan --- src/include/storage/read_stream.h | 13 ++++++++----- src/backend/access/heap/heapam.c | 2 +- src/backend/storage/aio/read_stream.c | 5 ++--- contrib/amcheck/verify_heapam.c | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/include/storage/read_stream.h b/src/include/storage/read_stream.h index e2dcf1be5..4f3541e7c 100644 --- a/src/include/storage/read_stream.h +++ b/src/include/storage/read_stream.h @@ -28,12 +28,15 @@ #define READ_STREAM_MAINTENANCE 0x01 /* - * We usually avoid issuing prefetch advice automatically when sequential - * access is detected, but this flag explicitly disables it, for cases that - * might not be correctly detected. Explicit advice is known to perform worse - * than letting the kernel (at least Linux) detect sequential access. + * Don't issue read-ahead advice (posix_fadvise) to simulate asynchronous I/O + * under io_method=sync. We usually avoid issuing advice automatically when + * sequential access is detected, but this flag disables it entirely, for + * sequential cases that might not be correctly detected, as well as for + * callers that are better off without advice for other reasons. Explicit + * advice is known to perform worse than letting the kernel (at least Linux) + * detect sequential access. */ -#define READ_STREAM_SEQUENTIAL 0x02 +#define READ_STREAM_NO_ADVICE 0x02 /* * We usually ramp up from smaller reads to larger ones, to support users who diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 6a09a1a15..cea245e6a 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -1296,7 +1296,7 @@ heap_beginscan(Relation relation, Snapshot snapshot, * - in the parallel case, only spinlocks and atomics are used * --- */ - scan->rs_read_stream = read_stream_begin_relation(READ_STREAM_SEQUENTIAL | + scan->rs_read_stream = read_stream_begin_relation(READ_STREAM_NO_ADVICE | READ_STREAM_USE_BATCHING, scan->rs_strategy, scan->rs_base.rs_rd, diff --git a/src/backend/storage/aio/read_stream.c b/src/backend/storage/aio/read_stream.c index fddf6ef44..0d0f4f8cb 100644 --- a/src/backend/storage/aio/read_stream.c +++ b/src/backend/storage/aio/read_stream.c @@ -893,12 +893,11 @@ read_stream_begin_impl(int flags, /* * Read-ahead advice simulating asynchronous I/O with synchronous calls. * Issue advice only if AIO is not used, direct I/O isn't enabled, the - * caller hasn't promised sequential access (overriding our detection - * heuristics), and max_ios hasn't been set to zero. + * caller hasn't disabled advice, and max_ios hasn't been set to zero. */ if (stream->sync_mode && (io_direct_flags & IO_DIRECT_DATA) == 0 && - (flags & READ_STREAM_SEQUENTIAL) == 0 && + (flags & READ_STREAM_NO_ADVICE) == 0 && max_ios > 0) stream->advice_enabled = true; #endif diff --git a/contrib/amcheck/verify_heapam.c b/contrib/amcheck/verify_heapam.c index 46942b358..10ab82bca 100644 --- a/contrib/amcheck/verify_heapam.c +++ b/contrib/amcheck/verify_heapam.c @@ -456,7 +456,7 @@ verify_heapam(PG_FUNCTION_ARGS) * locks. */ stream_cb = block_range_read_stream_cb; - stream_flags = READ_STREAM_SEQUENTIAL | + stream_flags = READ_STREAM_NO_ADVICE | READ_STREAM_FULL | READ_STREAM_USE_BATCHING; stream_data = &stream_skip_data.range; -- 2.55.0