From 3021eea3905e6979b1eade9669ffba85a41246e7 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 v35 02/21] Delay the reset of UNLOGGED relations

This patch set enables the creation of INIT forks within transactions,
and abort-time cleanup of such forks will be handled by the UNDO log
system introduced in a subsequent commit. Since UNDO logs depend on
WAL, to ensure correct UNDO processing, any operations involving INIT
forks, specifically reinit, must take place after recovery reaches
consistency. To prepare for the introduction of the UNDO log system,
this commit moves the UNLOGGED relation cleanup from before recovery
begins to when the consistency point is reached, or to 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 3ecaf181392..e9d029ebfac 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -5756,14 +5756,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.
@@ -5911,7 +5903,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 31caa49d6c3..278154ad9a0 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -57,6 +57,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"
@@ -2271,6 +2272,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

