From b26e36ba8614d907a6e15810ed4f684f8f628dd2 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Wed, 20 Mar 2024 14:53:31 +0200
Subject: [PATCH v5 08/26] minor refactoring in log_heap_prune_and_freeze()

Mostly to make local variables more tightly-scoped.
---
 src/backend/access/heap/pruneheap.c    | 80 +++++++++++---------------
 src/backend/access/rmgrdesc/heapdesc.c | 10 ++--
 src/include/access/heapam_xlog.h       |  4 +-
 src/tools/pgindent/typedefs.list       |  2 +
 4 files changed, 44 insertions(+), 52 deletions(-)

diff --git a/src/backend/access/heap/pruneheap.c b/src/backend/access/heap/pruneheap.c
index cd86a6262f5..d8be0f68bf9 100644
--- a/src/backend/access/heap/pruneheap.c
+++ b/src/backend/access/heap/pruneheap.c
@@ -1304,42 +1304,22 @@ log_heap_prune_and_freeze(Relation relation, Buffer buffer,
 {
 	xl_heap_prune xlrec;
 	XLogRecPtr	recptr;
-	xlhp_freeze freeze;
-	xlhp_prune_items redirect_items,
-				dead_items,
-				unused_items;
-
-	int			nplans = 0;
+	int			nplans;
 	xl_heap_freeze_plan plans[MaxHeapTuplesPerPage];
 	OffsetNumber frz_offsets[MaxHeapTuplesPerPage];
-	bool		do_freeze = (nfrozen > 0);
 
 	xlrec.flags = 0;
 
-	if (lp_truncate_only)
-	{
-		xlrec.flags |= XLHP_LP_TRUNCATE_ONLY;
-		Assert(nfrozen == 0 && nredirected == 0 && ndead == 0);
-	}
-
-	if (RelationIsAccessibleInLogicalDecoding(relation))
-		xlrec.flags |= XLHP_IS_CATALOG_REL;
-
-	if (TransactionIdIsValid(conflict_xid))
-		xlrec.flags |= XLHP_HAS_CONFLICT_HORIZON;
-
 	/*
 	 * Prepare deduplicated representation for use in WAL record Destructively
 	 * sorts tuples array in-place.
 	 */
-	if (do_freeze)
+	if (nfrozen > 0)
 		nplans = heap_log_freeze_plan(frozen, nfrozen, plans, frz_offsets);
-	XLogBeginInsert();
-	XLogRegisterData((char *) &xlrec, SizeOfHeapPrune);
-
-	if (TransactionIdIsValid(conflict_xid))
-		XLogRegisterData((char *) &conflict_xid, sizeof(TransactionId));
+	else
+		nplans = 0;
 
+	XLogBeginInsert();
 	XLogRegisterBuffer(0, buffer, REGBUF_STANDARD);
 
 	/*
@@ -1349,45 +1329,40 @@ log_heap_prune_and_freeze(Relation relation, Buffer buffer,
 	 */
 	if (nplans > 0)
 	{
-		xlrec.flags |= XLHP_HAS_FREEZE_PLANS;
-
-		freeze = (xlhp_freeze)
-		{
+		xlhp_freeze_plans freeze_plans = {
 			.nplans = nplans
 		};
 
-		XLogRegisterBufData(0, (char *) &freeze, offsetof(xlhp_freeze, plans));
+		xlrec.flags |= XLHP_HAS_FREEZE_PLANS;
 
+		XLogRegisterBufData(0, (char *) &freeze_plans,
+							offsetof(xlhp_freeze_plans, plans));
 		XLogRegisterBufData(0, (char *) plans,
-							sizeof(xl_heap_freeze_plan) * freeze.nplans);
+							sizeof(xl_heap_freeze_plan) * nplans);
 	}
 
-
 	if (nredirected > 0)
 	{
-		xlrec.flags |= XLHP_HAS_REDIRECTIONS;
-
-		redirect_items = (xlhp_prune_items)
-		{
+		xlhp_prune_items redirect_items = {
 			.ntargets = nredirected
 		};
 
+		xlrec.flags |= XLHP_HAS_REDIRECTIONS;
+
 		XLogRegisterBufData(0, (char *) &redirect_items,
 							offsetof(xlhp_prune_items, data));
-
 		XLogRegisterBufData(0, (char *) redirected,
 							sizeof(OffsetNumber[2]) * nredirected);
 	}
 
 	if (ndead > 0)
 	{
-		xlrec.flags |= XLHP_HAS_DEAD_ITEMS;
-
-		dead_items = (xlhp_prune_items)
-		{
+		xlhp_prune_items dead_items = {
 			.ntargets = ndead
 		};
 
+		xlrec.flags |= XLHP_HAS_DEAD_ITEMS;
+
 		XLogRegisterBufData(0, (char *) &dead_items,
 							offsetof(xlhp_prune_items, data));
 
@@ -1397,13 +1372,12 @@ log_heap_prune_and_freeze(Relation relation, Buffer buffer,
 
 	if (nunused > 0)
 	{
-		xlrec.flags |= XLHP_HAS_NOW_UNUSED_ITEMS;
-
-		unused_items = (xlhp_prune_items)
-		{
+		xlhp_prune_items unused_items = {
 			.ntargets = nunused
 		};
 
+		xlrec.flags |= XLHP_HAS_NOW_UNUSED_ITEMS;
+
 		XLogRegisterBufData(0, (char *) &unused_items,
 							offsetof(xlhp_prune_items, data));
 
@@ -1415,6 +1389,22 @@ log_heap_prune_and_freeze(Relation relation, Buffer buffer,
 		XLogRegisterBufData(0, (char *) frz_offsets,
 							sizeof(OffsetNumber) * nfrozen);
 
+	if (lp_truncate_only)
+	{
+		Assert(nfrozen == 0 && nredirected == 0 && ndead == 0);
+		xlrec.flags |= XLHP_LP_TRUNCATE_ONLY;
+	}
+
+	if (RelationIsAccessibleInLogicalDecoding(relation))
+		xlrec.flags |= XLHP_IS_CATALOG_REL;
+
+	if (TransactionIdIsValid(conflict_xid))
+		xlrec.flags |= XLHP_HAS_CONFLICT_HORIZON;
+
+	XLogRegisterData((char *) &xlrec, SizeOfHeapPrune);
+	if (TransactionIdIsValid(conflict_xid))
+		XLogRegisterData((char *) &conflict_xid, sizeof(TransactionId));
+
 	recptr = XLogInsert(RM_HEAP2_ID, XLOG_HEAP2_PRUNE_FREEZE);
 
 	PageSetLSN(BufferGetPage(buffer), recptr);
diff --git a/src/backend/access/rmgrdesc/heapdesc.c b/src/backend/access/rmgrdesc/heapdesc.c
index ff238d58279..7b5d13ffed3 100644
--- a/src/backend/access/rmgrdesc/heapdesc.c
+++ b/src/backend/access/rmgrdesc/heapdesc.c
@@ -109,14 +109,14 @@ heap_xlog_deserialize_prune_and_freeze(char *cursor, uint8 flags,
 {
 	if (flags & XLHP_HAS_FREEZE_PLANS)
 	{
-		xlhp_freeze *freeze = (xlhp_freeze *) cursor;
+		xlhp_freeze_plans *freeze_plans = (xlhp_freeze_plans *) cursor;
 
-		*nplans = freeze->nplans;
+		*nplans = freeze_plans->nplans;
 		Assert(*nplans > 0);
-		*plans = freeze->plans;
+		*plans = freeze_plans->plans;
 
-		cursor += offsetof(xlhp_freeze, plans);
-		cursor += sizeof(xl_heap_freeze_plan) * freeze->nplans;
+		cursor += offsetof(xlhp_freeze_plans, plans);
+		cursor += sizeof(xl_heap_freeze_plan) * freeze_plans->nplans;
 	}
 
 	if (flags & XLHP_HAS_REDIRECTIONS)
diff --git a/src/include/access/heapam_xlog.h b/src/include/access/heapam_xlog.h
index f0cbd31189e..9d64eaf3933 100644
--- a/src/include/access/heapam_xlog.h
+++ b/src/include/access/heapam_xlog.h
@@ -309,11 +309,11 @@ typedef struct xl_heap_freeze_plan
  * Each such page offset number array corresponds to a single freeze plan
  * (REDO routine freezes corresponding heap tuples using freeze plan).
  */
-typedef struct xlhp_freeze
+typedef struct xlhp_freeze_plans
 {
 	uint16		nplans;
 	xl_heap_freeze_plan plans[FLEXIBLE_ARRAY_MEMBER];
-} xlhp_freeze;
+} xlhp_freeze_plans;
 
 /*
  * Sub-record type contained in block reference 0 of a prune record if
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index e294f8bc4e6..6a46b34c5ca 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -4008,6 +4008,8 @@ xl_xact_stats_items
 xl_xact_subxacts
 xl_xact_twophase
 xl_xact_xinfo
+xlhp_freeze_plans
+xlhp_prune_items
 xmlBuffer
 xmlBufferPtr
 xmlChar
-- 
2.39.2

