From ef5c1a71cbf54de1abcd4984f6b0c7d39af51af2 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Mon, 30 Sep 2024 17:56:46 +0900
Subject: [PATCH v36 02/17] Delay the reset of UNLOGGED relations

This patch set enables the creation of INIT forks within
transactions. Cleanup of such forks after a crash will be managed by
the UNDO log system, which will be introduced in a subsequent
patch. Since the consistency of UNDO logs relies on WAL, operations
involving INIT forks - specifically reinit - must only occur after
recovery has reached consistency.

To prepare for the introduction of the UNDO log system, this commit
adjusts the timing of UNLOGGED relation cleanup. Instead of occurring
before recovery begins, it is now performed either when the
consistency point is reached or at the end of recovery if hot standby
is disabled.
---
 src/backend/access/transam/xlog.c         | 17 +++++++++--------
 src/backend/access/transam/xlogrecovery.c |  9 +++++++++
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 6f58412bcab..62767a4a2b9 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -5761,14 +5761,6 @@ StartupXLOG(void)
 		/* Check that the GUCs used to generate the WAL allow recovery */
 		CheckRequiredParameterValues();
 
-		/*
-		 * We're in recovery, so unlogged relations may be trashed and must be
-		 * reset.  This should be done BEFORE allowing Hot Standby
-		 * connections, so that read-only backends don't try to read whatever
-		 * garbage is left over from before.
-		 */
-		ResetUnloggedRelations(UNLOGGED_RELATION_CLEANUP);
-
 		/*
 		 * Likewise, delete any saved transaction snapshot files that got left
 		 * behind by crashed backends.
@@ -5916,7 +5908,16 @@ StartupXLOG(void)
 	 * end-of-recovery steps fail.
 	 */
 	if (InRecovery)
+	{
+		/*
+		 * Clean up unlogged relations if not already done. If consistency has
+		 * been established, this cleanup would have occurred when entering hot
+		 * standby mode (see CheckRecoveryConsistency for details).
+		 */
+		if (!reachedConsistency)
+			ResetUnloggedRelations(UNLOGGED_RELATION_CLEANUP);
 		ResetUnloggedRelations(UNLOGGED_RELATION_INIT);
+	}
 
 	/*
 	 * Pre-scan prepared transactions to find out the range of XIDs present.
diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index c6994b78282..5ceebce5a19 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -56,6 +56,7 @@
 #include "storage/latch.h"
 #include "storage/pmsignal.h"
 #include "storage/procarray.h"
+#include "storage/reinit.h"
 #include "storage/spin.h"
 #include "utils/datetime.h"
 #include "utils/fmgrprotos.h"
@@ -2260,6 +2261,14 @@ CheckRecoveryConsistency(void)
 		reachedConsistency &&
 		IsUnderPostmaster)
 	{
+		/*
+		 * Unlogged relations may be trashed and must be reset.  This should be
+		 * done BEFORE allowing Hot Standby connections, so that read-only
+		 * backends don't try to read whatever garbage is left over from
+		 * before.
+		 */
+		ResetUnloggedRelations(UNLOGGED_RELATION_CLEANUP);
+
 		SpinLockAcquire(&XLogRecoveryCtl->info_lck);
 		XLogRecoveryCtl->SharedHotStandbyActive = true;
 		SpinLockRelease(&XLogRecoveryCtl->info_lck);
-- 
2.43.5

