v4-0004-Conslidate-FSM-update-code-in-lazy_scan_heap.patch

text/x-patch

Filename: v4-0004-Conslidate-FSM-update-code-in-lazy_scan_heap.patch
Type: text/x-patch
Part: 0
Message: Re: Emit fewer vacuum records by reaping removable tuples during pruning

Patch

Format: format-patch
Series: patch v4-0004
Subject: Conslidate FSM update code in lazy_scan_heap()
File+
src/backend/access/heap/vacuumlazy.c 22 29
From 4a2129de86aee6e1069b381eda47320d3a26f286 Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Fri, 5 Jan 2024 15:18:11 -0500
Subject: [PATCH v4 4/4] Conslidate FSM update code in lazy_scan_heap()

lazy_scan_prune() and lazy_scan_noprune() both require the a FSM update
after returning. Consolidate these FSM updates, being careful not to
call lazy_scan_prune() or lazy_scan_new_or_empty() if
lazy_scan_noprune() has already finished processing the page.

Note that lazy_scan_prune() and lazy_scan_noprune() also both require
vacrel->nonempty_pages to be updated. Consolidate this as well.
---
 src/backend/access/heap/vacuumlazy.c | 51 ++++++++++++----------------
 1 file changed, 22 insertions(+), 29 deletions(-)

diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index 3a22192728b..dd8476b0fd8 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -818,6 +818,7 @@ lazy_scan_heap(LVRelState *vacrel)
 				skipping_current_range;
 	bool		recordfreespace;
 	bool		hastup;
+	bool		do_prune = true;
 	const int	initprog_index[] = {
 		PROGRESS_VACUUM_PHASE,
 		PROGRESS_VACUUM_TOTAL_HEAP_BLKS,
@@ -954,39 +955,30 @@ lazy_scan_heap(LVRelState *vacrel)
 				continue;
 			}
 
-			/* Collect LP_DEAD items in dead_items array, count tuples */
-			if (lazy_scan_noprune(vacrel, buf, blkno, page, &hastup,
-								  &recordfreespace))
-			{
-				Size		freespace = 0;
-
-				/*
-				 * Processed page successfully (without cleanup lock) -- just
-				 * need to perform rel truncation and FSM steps, much like the
-				 * lazy_scan_prune case.  Don't bother trying to match its
-				 * visibility map setting steps, though.
-				 */
-				if (hastup)
-					vacrel->nonempty_pages = blkno + 1;
-				if (recordfreespace)
-					freespace = PageGetHeapFreeSpace(page);
-				UnlockReleaseBuffer(buf);
-				if (recordfreespace)
-					RecordPageWithFreeSpace(vacrel->rel, blkno, freespace);
-				continue;
-			}
+			/*
+			 * Collect LP_DEAD items in dead_items array, count tuples,
+			 * determine if rel truncation is safe. If lazy_scan_noprune()
+			 * returns false, we must get a cleanup lock and call
+			 * lazy_scan_prune().
+			 */
+			do_prune = !lazy_scan_noprune(vacrel, buf, blkno, page,
+										  &hastup, &recordfreespace);
 
 			/*
 			 * lazy_scan_noprune could not do all required processing.  Wait
 			 * for a cleanup lock, and call lazy_scan_prune in the usual way.
 			 */
-			Assert(vacrel->aggressive);
-			LockBuffer(buf, BUFFER_LOCK_UNLOCK);
-			LockBufferForCleanup(buf);
+			if (do_prune)
+			{
+				Assert(vacrel->aggressive);
+				LockBuffer(buf, BUFFER_LOCK_UNLOCK);
+				LockBufferForCleanup(buf);
+			}
 		}
 
-		/* Check for new or empty pages before lazy_scan_prune call */
-		if (lazy_scan_new_or_empty(vacrel, buf, blkno, page, false, vmbuffer))
+		/* Check for new or empty pages before lazy_scan_prune processing */
+		if (do_prune &&
+			lazy_scan_new_or_empty(vacrel, buf, blkno, page, false, vmbuffer))
 		{
 			/* Processed as new/empty page (lock and pin released) */
 			continue;
@@ -1003,9 +995,10 @@ lazy_scan_heap(LVRelState *vacrel)
 		 * were pruned some time earlier.  Also considers freezing XIDs in the
 		 * tuple headers of remaining items with storage.
 		 */
-		lazy_scan_prune(vacrel, buf, blkno, page,
-						vmbuffer, all_visible_according_to_vm,
-						&recordfreespace, &hastup);
+		if (do_prune)
+			lazy_scan_prune(vacrel, buf, blkno, page,
+							vmbuffer, all_visible_according_to_vm,
+							&recordfreespace, &hastup);
 
 		/* Remember the location of the last page with nonremovable tuples */
 		if (hastup)
-- 
2.37.2