v17-0014-WIP-read_stream-Only-increase-distance-when-wait.patch

application/octet-stream

Filename: v17-0014-WIP-read_stream-Only-increase-distance-when-wait.patch
Type: application/octet-stream
Part: 5
Message: Re: index prefetching

Patch

Format: format-patch
Series: patch v17-0014
Subject: WIP: read_stream: Only increase distance when waiting for IO
File+
src/backend/storage/aio/read_stream.c 19 5
From eff9872327431526eec59a40952b3824fbf9b9dc Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Tue, 3 Mar 2026 18:00:53 -0500
Subject: [PATCH v17 14/18] 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