From 6942d141add0d40499d41d341d438de0d6e16b51 Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Thu, 6 Aug 2026 17:49:12 -0400
Subject: [PATCH] Only clear VACUUM's read stream strategy once in failsafe
 mode

112c2683807b4d690 restored failsafe vacuum's abandonment of a buffer
access strategy by clearing the ReadBuffersOperations' strategy
references. But it did so in lazy_scan_heap()'s main loop, meaning it
looped through all the ReadBuffersOperations once per block after
failsafe was engaged. Track it with a local flag and clear the strategy
only once.

Backpatch-through: 18
---
 src/backend/access/heap/vacuumlazy.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index d346344934c..cb9f23436f1 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -1285,6 +1285,7 @@ lazy_scan_heap(LVRelState *vacrel)
 	BlockNumber orig_eager_scan_success_limit =
 		vacrel->eager_scan_remaining_successes; /* for logging */
 	Buffer		vmbuffer = InvalidBuffer;
+	bool		strategy_cleared = false;
 	const int	initprog_index[] = {
 		PROGRESS_VACUUM_PHASE,
 		PROGRESS_VACUUM_TOTAL_HEAP_BLKS,
@@ -1389,10 +1390,14 @@ lazy_scan_heap(LVRelState *vacrel)
 		 * If the wraparound failsafe has engaged -- either via the check
 		 * above or during index vacuuming invoked from this loop -- stop
 		 * using the buffer access strategy so that the rest of the vacuum may
-		 * use all of shared buffers.
+		 * use all of shared buffers.  Failsafe mode stays engaged once
+		 * triggered, so we only need to do this once.
 		 */
-		if (unlikely(VacuumFailsafeActive))
+		if (unlikely(VacuumFailsafeActive) && !strategy_cleared)
+		{
 			read_stream_clear_strategy(stream);
+			strategy_cleared = true;
+		}
 
 		buf = read_stream_next_buffer(stream, &per_buffer_data);
 
-- 
2.47.3

