From cf650befbbe48a14f212fb847a12aee16c2e826f Mon Sep 17 00:00:00 2001
From: Junwang Zhao <zhjwpku@gmail.com>
Date: Sun, 20 Oct 2024 10:09:59 +0000
Subject: [PATCH v2 2/2] refactor

Signed-off-by: Junwang Zhao <zhjwpku@gmail.com>
---
 src/backend/access/nbtree/nbtree.c | 56 +++++++++---------------------
 1 file changed, 16 insertions(+), 40 deletions(-)

diff --git a/src/backend/access/nbtree/nbtree.c b/src/backend/access/nbtree/nbtree.c
index b6762e9c30..1d493aa680 100644
--- a/src/backend/access/nbtree/nbtree.c
+++ b/src/backend/access/nbtree/nbtree.c
@@ -971,6 +971,8 @@ btvacuumscan(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
 	BlockNumber num_pages;
 	BlockNumber scanblkno;
 	bool		needLock;
+	BlockRangeReadStreamPrivate p;
+	ReadStream *stream = NULL;
 
 	/*
 	 * Reset fields that track information about the entire index now.  This
@@ -1041,44 +1043,6 @@ btvacuumscan(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
 
 	scanblkno = BTREE_METAPAGE + 1;
 
-	{
-		/* streamline reading most of index data */
-		BlockRangeReadStreamPrivate p;
-		ReadStream *stream = NULL;
-		p.current_blocknum = scanblkno;
-		if (needLock)
-			LockRelationForExtension(rel, ExclusiveLock);
-		/* We only streamline number of blocks that are know at the beginning */
-		p.last_exclusive = RelationGetNumberOfBlocks(rel);
-		if (needLock)
-			UnlockRelationForExtension(rel, ExclusiveLock);
-		stream = read_stream_begin_relation(READ_STREAM_FULL,
-											info->strategy,
-											rel,
-											MAIN_FORKNUM,
-											block_range_read_stream_cb,
-											&p,
-											0);
-		for (; scanblkno < p.last_exclusive; scanblkno++)
-		{
-			Buffer buf = read_stream_next_buffer(stream, NULL);
-			/*
-			 * We expect that blocks are returned in order.
-			 * However, we do not depent on it much, and in future ths
-			 * expetation might change.
-			 * Currently, this expectation only matters for progress reporting.
-			 */
-			Assert(BufferGetBlockNumber(buf) == scanblkno);
-			btvacuumpage(&vstate, buf);
-			if (info->report_progress)
-				pgstat_progress_update_param(PROGRESS_SCAN_BLOCKS_DONE,
-											 scanblkno);
-		}
-		Assert(read_stream_next_buffer(stream, NULL) == InvalidBuffer);
-		read_stream_end(stream);
-	}
-
-	/* Now we have to process pages created after streamlined vacuum */
 	for (;;)
 	{
 		/* Get the current relation length */
@@ -1095,16 +1059,28 @@ btvacuumscan(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
 		/* Quit if we've scanned the whole relation */
 		if (scanblkno >= num_pages)
 			break;
+
+		p.current_blocknum = scanblkno;
+		p.last_exclusive = num_pages;
+		stream = read_stream_begin_relation(READ_STREAM_FULL,
+											info->strategy,
+											rel,
+											MAIN_FORKNUM,
+											block_range_read_stream_cb,
+											&p,
+											0);
+
 		/* Iterate over pages, then loop back to recheck length */
 		for (; scanblkno < num_pages; scanblkno++)
 		{
-			Buffer buf = ReadBufferExtended(rel, MAIN_FORKNUM, scanblkno, RBM_NORMAL,
-							 info->strategy);
+			Buffer buf = read_stream_next_buffer(stream, NULL);
 			btvacuumpage(&vstate, buf);
 			if (info->report_progress)
 				pgstat_progress_update_param(PROGRESS_SCAN_BLOCKS_DONE,
 											 scanblkno);
 		}
+		Assert(read_stream_next_buffer(stream, NULL) == InvalidBuffer);
+		read_stream_end(stream);
 	}
 
 	/* Set statistics num_pages field to final size of index */
-- 
2.39.5

