From ca2ef3f816b5dc03e8090194421a5f20c0b89d43 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Tue, 3 Mar 2026 18:00:53 -0500 Subject: [PATCH v14 12/16] WIP: read_stream: Only increase distance when waiting for IO This avoids increasing the distance to the maximum in cases where the IO subsystem is already keeping up. TODO: This might be too aggressive without a subsequent patch that reduces how often we decrease the distance. Author: Reviewed-by: Discussion: https://postgr.es/m/ Backpatch: --- src/backend/storage/aio/read_stream.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/backend/storage/aio/read_stream.c b/src/backend/storage/aio/read_stream.c index 3667d67ab..e28ab5de0 100644 --- a/src/backend/storage/aio/read_stream.c +++ b/src/backend/storage/aio/read_stream.c @@ -931,22 +931,36 @@ read_stream_next_buffer(ReadStream *stream, void **per_buffer_data) { int16 io_index = stream->oldest_io_index; int32 distance; /* wider temporary value, clamped below */ + bool needed_wait; /* Sanity check that we still agree on the buffers. */ Assert(stream->ios[io_index].op.buffers == &stream->buffers[oldest_buffer_index]); - WaitReadBuffers(&stream->ios[io_index].op); + needed_wait = WaitReadBuffers(&stream->ios[io_index].op); Assert(stream->ios_in_progress > 0); stream->ios_in_progress--; if (++stream->oldest_io_index == stream->max_ios) stream->oldest_io_index = 0; - /* Look-ahead distance ramps up rapidly after we do I/O. */ - distance = stream->distance * 2; - distance = Min(distance, stream->max_pinned_buffers); - stream->distance = distance; + /* + * Look-ahead distance ramps up rapidly after we needed to wait for + * IO. We only increase the distance when we needed to wait, to avoid + * increasing the distance further than necessary, as unnecessarily + * pinning many buffers can be costly. + * + * NB: May not increase the distance if we reached the end of the + * stream. + */ + if (stream->distance > 0 && needed_wait) + { + distance = stream->distance * 2; + if (distance && distance < PG_INT16_MAX) + distance++; + distance = Min(distance, stream->max_pinned_buffers); + stream->distance = distance; + } /* * If we've reached the first block of a sequential region we're -- 2.53.0