From 634904f9b76c8072b14fd123207f66c7196a0c8f Mon Sep 17 00:00:00 2001
From: "Imseih (AWS)" <simseih@88665a22795f.ant.amazon.com>
Date: Tue, 20 Dec 2022 11:17:08 -0600
Subject: [PATCH 1/1] fixed when wraparound failsafe is checked.

Trigger lazy_check_wraparound_failsafe when
FAILSAFE_EVERY_PAGES of heap pages have been scanned
including pages that may have been skipped. This ensures
that lazy_check_wraparound_failsafe is checked
consistently even in cases in which many pages may be
skipped.

Discussion: https://www.postgresql.org/message-id/flat/401CE010-4049-4B94-9961-0B610A5D254D%40amazon.com
---
 src/backend/access/heap/vacuumlazy.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index d59711b7ec..e3833dff75 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -851,7 +851,6 @@ lazy_scan_heap(LVRelState *vacrel)
 	BlockNumber rel_pages = vacrel->rel_pages,
 				blkno,
 				next_unskippable_block,
-				next_failsafe_block = 0,
 				next_fsm_block_to_vacuum = 0;
 	VacDeadItems *dead_items = vacrel->dead_items;
 	Buffer		vmbuffer = InvalidBuffer;
@@ -925,11 +924,8 @@ lazy_scan_heap(LVRelState *vacrel)
 		 * one-pass strategy, and the two-pass strategy with the index_cleanup
 		 * param set to 'off'.
 		 */
-		if (blkno - next_failsafe_block >= FAILSAFE_EVERY_PAGES)
-		{
+		if (vacrel->scanned_pages % FAILSAFE_EVERY_PAGES == 0)
 			lazy_check_wraparound_failsafe(vacrel);
-			next_failsafe_block = blkno;
-		}
 
 		/*
 		 * Consider if we definitely have enough space to process TIDs on page
-- 
2.32.1 (Apple Git-133)

