v12-0018-WIP-read_stream-Only-increase-distance-when-wait.patch
application/octet-stream
Filename: v12-0018-WIP-read_stream-Only-increase-distance-when-wait.patch
Type: application/octet-stream
Part: 7
Message:
Re: index prefetching
Patch
Format: format-patch
Series: patch v12-0018
Subject: WIP: read_stream: Only increase distance when waiting for IO
| File | + | − |
|---|---|---|
| src/backend/storage/aio/read_stream.c | 19 | 7 |
From ddcdea4d4b67e600cd05dd55e134f8c0b9bd3b9b Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Tue, 3 Mar 2026 18:00:53 -0500
Subject: [PATCH v12 18/23] 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 | 26 +++++++++++++++++++-------
1 file changed, 19 insertions(+), 7 deletions(-)
diff --git a/src/backend/storage/aio/read_stream.c b/src/backend/storage/aio/read_stream.c
index 80067b6e6..e28ab5de0 100644
--- a/src/backend/storage/aio/read_stream.c
+++ b/src/backend/storage/aio/read_stream.c
@@ -931,24 +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;
- if (distance && distance < PG_INT16_MAX)
- distance++;
- 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