From 87bf27b2cbb8825c624b6fc7b80155cf762051ce Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Wed, 27 Mar 2024 23:47:24 +0200
Subject: [PATCH v9 20/21] Move 'frz_conflict_horizon' to tighter scope

---
 src/backend/access/heap/pruneheap.c | 38 ++++++++++++++---------------
 1 file changed, 19 insertions(+), 19 deletions(-)

diff --git a/src/backend/access/heap/pruneheap.c b/src/backend/access/heap/pruneheap.c
index 2bd2e858bcd..e1eed42004f 100644
--- a/src/backend/access/heap/pruneheap.c
+++ b/src/backend/access/heap/pruneheap.c
@@ -273,7 +273,6 @@ heap_page_prune_and_freeze(Relation relation, Buffer buffer,
 				maxoff;
 	PruneState	prstate;
 	HeapTupleData tup;
-	TransactionId frz_conflict_horizon;
 	bool		do_freeze;
 	bool		do_prune;
 	bool		do_hint;
@@ -391,7 +390,6 @@ heap_page_prune_and_freeze(Relation relation, Buffer buffer,
 	 * all-visible, so the conflict horizon remains InvalidTransactionId.
 	 */
 	presult->vm_conflict_horizon = prstate.visibility_cutoff_xid = InvalidTransactionId;
-	frz_conflict_horizon = InvalidTransactionId;
 
 	maxoff = PageGetMaxOffsetNumber(page);
 	tup.t_tableOid = RelationGetRelid(relation);
@@ -590,24 +588,7 @@ heap_page_prune_and_freeze(Relation relation, Buffer buffer,
 		}
 
 		if (do_freeze)
-		{
-			/*
-			 * We can use the visibility_cutoff_xid as our cutoff for
-			 * conflicts when the whole page is eligible to become all-frozen
-			 * in the VM once we're done with it.  Otherwise we generate a
-			 * conservative cutoff by stepping back from OldestXmin. This
-			 * avoids false conflicts when hot_standby_feedback is in use.
-			 */
-			if (prstate.all_visible_except_removable && presult->set_all_frozen)
-				frz_conflict_horizon = prstate.visibility_cutoff_xid;
-			else
-			{
-				/* Avoids false conflicts when hot_standby_feedback in use */
-				frz_conflict_horizon = prstate.pagefrz.cutoffs->OldestXmin;
-				TransactionIdRetreat(frz_conflict_horizon);
-			}
 			heap_freeze_prepared_tuples(buffer, prstate.frozen, prstate.nfrozen);
-		}
 
 		MarkBufferDirty(buffer);
 
@@ -626,8 +607,27 @@ heap_page_prune_and_freeze(Relation relation, Buffer buffer,
 			 * on the standby with xids older than the youngest tuple this
 			 * record will freeze will conflict.
 			 */
+			TransactionId frz_conflict_horizon = InvalidTransactionId;
 			TransactionId conflict_xid;
 
+			/*
+			 * We can use the visibility_cutoff_xid as our cutoff for
+			 * conflicts when the whole page is eligible to become all-frozen
+			 * in the VM once we're done with it.  Otherwise we generate a
+			 * conservative cutoff by stepping back from OldestXmin.
+			 */
+			if (do_freeze)
+			{
+				if (prstate.all_visible_except_removable && presult->set_all_frozen)
+					frz_conflict_horizon = prstate.visibility_cutoff_xid;
+				else
+				{
+					/* Avoids false conflicts when hot_standby_feedback in use */
+					frz_conflict_horizon = prstate.pagefrz.cutoffs->OldestXmin;
+					TransactionIdRetreat(frz_conflict_horizon);
+				}
+			}
+
 			if (TransactionIdFollows(frz_conflict_horizon, prstate.latest_xid_removed))
 				conflict_xid = frz_conflict_horizon;
 			else
-- 
2.40.1

