From 7aabff81998ad3f500aa8c6da354ea2e81cee10c Mon Sep 17 00:00:00 2001
From: Peter Geoghegan <pg@bowt.ie>
Date: Fri, 25 Mar 2022 12:51:05 -0700
Subject: [PATCH v12 3/3] vacuumlazy.c: Move resource allocation to
 heap_vacuum_rel().

Finish off work started by commit 73f6ec3d: move remaining resource
allocation and deallocation code from lazy_scan_heap() to its caller,
heap_vacuum_rel().
---
 src/backend/access/heap/vacuumlazy.c | 68 ++++++++++++----------------
 1 file changed, 28 insertions(+), 40 deletions(-)

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index c647d60bc..7e641036e 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -246,7 +246,7 @@ typedef struct LVSavedErrInfo
 
 
 /* non-export function prototypes */
-static void lazy_scan_heap(LVRelState *vacrel, int nworkers);
+static void lazy_scan_heap(LVRelState *vacrel);
 static BlockNumber lazy_scan_skip(LVRelState *vacrel, Buffer *vmbuffer,
 								  BlockNumber next_block,
 								  bool *next_unskippable_allvis,
@@ -519,11 +519,28 @@ heap_vacuum_rel(Relation rel, VacuumParams *params,
 	vacrel->NewRelminMxid = OldestMxact;
 	vacrel->skippedallvis = false;
 
+	/*
+	 * Allocate dead_items array memory using dead_items_alloc.  This handles
+	 * parallel VACUUM initialization as part of allocating shared memory
+	 * space used for dead_items.  (But do a failsafe precheck first, to
+	 * ensure that parallel VACUUM won't be attempted at all when relfrozenxid
+	 * is already dangerously old.)
+	 */
+	lazy_check_wraparound_failsafe(vacrel);
+	dead_items_alloc(vacrel, params->nworkers);
+
 	/*
 	 * Call lazy_scan_heap to perform all required heap pruning, index
 	 * vacuuming, and heap vacuuming (plus related processing)
 	 */
-	lazy_scan_heap(vacrel, params->nworkers);
+	lazy_scan_heap(vacrel);
+
+	/*
+	 * Free resources managed by dead_items_alloc.  This ends parallel mode in
+	 * passing when necessary.
+	 */
+	dead_items_cleanup(vacrel);
+	Assert(!IsInParallelMode());
 
 	/*
 	 * Update pg_class entries for each of rel's indexes where appropriate.
@@ -833,14 +850,14 @@ heap_vacuum_rel(Relation rel, VacuumParams *params,
  *		supply.
  */
 static void
-lazy_scan_heap(LVRelState *vacrel, int nworkers)
+lazy_scan_heap(LVRelState *vacrel)
 {
-	VacDeadItems *dead_items;
 	BlockNumber rel_pages = vacrel->rel_pages,
 				blkno,
 				next_unskippable_block,
-				next_failsafe_block,
-				next_fsm_block_to_vacuum;
+				next_failsafe_block = 0,
+				next_fsm_block_to_vacuum = 0;
+	VacDeadItems *dead_items = vacrel->dead_items;
 	Buffer		vmbuffer = InvalidBuffer;
 	bool		next_unskippable_allvis,
 				skipping_current_range;
@@ -851,23 +868,6 @@ lazy_scan_heap(LVRelState *vacrel, int nworkers)
 	};
 	int64		initprog_val[3];
 
-	/*
-	 * Do failsafe precheck before calling dead_items_alloc.  This ensures
-	 * that parallel VACUUM won't be attempted when relfrozenxid is already
-	 * dangerously old.
-	 */
-	lazy_check_wraparound_failsafe(vacrel);
-	next_failsafe_block = 0;
-
-	/*
-	 * Allocate the space for dead_items.  Note that this handles parallel
-	 * VACUUM initialization as part of allocating shared memory space used
-	 * for dead_items.
-	 */
-	dead_items_alloc(vacrel, nworkers);
-	dead_items = vacrel->dead_items;
-	next_fsm_block_to_vacuum = 0;
-
 	/* Report that we're scanning the heap, advertising total # of blocks */
 	initprog_val[0] = PROGRESS_VACUUM_PHASE_SCAN_HEAP;
 	initprog_val[1] = rel_pages;
@@ -1244,12 +1244,11 @@ lazy_scan_heap(LVRelState *vacrel, int nworkers)
 		}
 	}
 
+	vacrel->blkno = InvalidBlockNumber;
+
 	/* report that everything is now scanned */
 	pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_SCANNED, blkno);
 
-	/* Clear the block number information */
-	vacrel->blkno = InvalidBlockNumber;
-
 	/* now we can compute the new value for pg_class.reltuples */
 	vacrel->new_live_tuples = vac_estimate_reltuples(vacrel->rel, rel_pages,
 													 vacrel->scanned_pages,
@@ -1264,15 +1263,11 @@ lazy_scan_heap(LVRelState *vacrel, int nworkers)
 		vacrel->missed_dead_tuples;
 
 	/*
-	 * Release any remaining pin on visibility map page.
+	 * Do index vacuuming (call each index's ambulkdelete routine), then do
+	 * related heap vacuuming
 	 */
 	if (BufferIsValid(vmbuffer))
-	{
 		ReleaseBuffer(vmbuffer);
-		vmbuffer = InvalidBuffer;
-	}
-
-	/* Perform a final round of index and heap vacuuming */
 	if (dead_items->num_items > 0)
 		lazy_vacuum(vacrel);
 
@@ -1286,16 +1281,9 @@ lazy_scan_heap(LVRelState *vacrel, int nworkers)
 	/* report all blocks vacuumed */
 	pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_VACUUMED, blkno);
 
-	/* Do post-vacuum cleanup */
+	/* Do final index cleanup (call each index's amvacuumcleanup routine) */
 	if (vacrel->nindexes > 0 && vacrel->do_index_cleanup)
 		lazy_cleanup_all_indexes(vacrel);
-
-	/*
-	 * Free resources managed by dead_items_alloc.  This ends parallel mode in
-	 * passing when necessary.
-	 */
-	dead_items_cleanup(vacrel);
-	Assert(!IsInParallelMode());
 }
 
 /*
-- 
2.32.0

