From 9dcd528dd42f689e207d808bee388fb233b2e25e Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Sun, 14 Apr 2024 22:50:10 +0300
Subject: [PATCH 1/2] Expose conflict_xid to caller, for tests

---
 src/backend/access/heap/pruneheap.c | 20 +++++++++++---------
 src/include/access/heapam.h         |  5 +++++
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/src/backend/access/heap/pruneheap.c b/src/backend/access/heap/pruneheap.c
index d2eecaf7ebc..8f4d17f7d08 100644
--- a/src/backend/access/heap/pruneheap.c
+++ b/src/backend/access/heap/pruneheap.c
@@ -366,6 +366,7 @@ heap_page_prune_and_freeze(Relation relation, Buffer buffer,
 	bool		do_hint;
 	bool		hint_bit_fpi;
 	int64		fpi_before = pgWalUsage.wal_fpi;
+	TransactionId conflict_xid = InvalidTransactionId;
 
 	/* Copy parameters to prstate */
 	prstate.vistest = vistest;
@@ -794,7 +795,6 @@ heap_page_prune_and_freeze(Relation relation, Buffer buffer,
 		/*
 		 * Emit a WAL XLOG_HEAP2_PRUNE_FREEZE record showing what we did
 		 */
-		if (RelationNeedsWAL(relation))
 		{
 			/*
 			 * The snapshotConflictHorizon for the whole record should be the
@@ -807,7 +807,6 @@ heap_page_prune_and_freeze(Relation relation, Buffer buffer,
 			 * record will freeze will conflict.
 			 */
 			TransactionId frz_conflict_horizon = InvalidTransactionId;
-			TransactionId conflict_xid;
 
 			/*
 			 * We can use the visibility_cutoff_xid as our cutoff for
@@ -832,13 +831,14 @@ heap_page_prune_and_freeze(Relation relation, Buffer buffer,
 			else
 				conflict_xid = prstate.latest_xid_removed;
 
-			log_heap_prune_and_freeze(relation, buffer,
-									  conflict_xid,
-									  true, reason,
-									  prstate.frozen, prstate.nfrozen,
-									  prstate.redirected, prstate.nredirected,
-									  prstate.nowdead, prstate.ndead,
-									  prstate.nowunused, prstate.nunused);
+			if (RelationNeedsWAL(relation))
+				log_heap_prune_and_freeze(relation, buffer,
+										  conflict_xid,
+										  true, reason,
+										  prstate.frozen, prstate.nfrozen,
+										  prstate.redirected, prstate.nredirected,
+										  prstate.nowdead, prstate.ndead,
+										  prstate.nowunused, prstate.nunused);
 		}
 	}
 
@@ -876,6 +876,8 @@ heap_page_prune_and_freeze(Relation relation, Buffer buffer,
 
 	presult->hastup = prstate.hastup;
 
+	presult->conflict_xid = conflict_xid;
+
 	/*
 	 * For callers planning to update the visibility map, the conflict horizon
 	 * for that record must be the newest xmin on the page.  However, if the
diff --git a/src/include/access/heapam.h b/src/include/access/heapam.h
index 735662dc9df..934b5bfbe50 100644
--- a/src/include/access/heapam.h
+++ b/src/include/access/heapam.h
@@ -255,6 +255,11 @@ typedef struct PruneFreezeResult
 	 */
 	bool		hastup;
 
+	/*
+	 * Recovery conflict XID, if any. This is returned just for white-box tests.
+	 */
+	TransactionId conflict_xid;
+
 	/*
 	 * LP_DEAD items on the page after pruning.  Includes existing LP_DEAD
 	 * items.
-- 
2.39.2

