From e3ff0c077cb669f55cf77ab5c7ab86bf201e68a0 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Thu, 1 Aug 2024 11:44:12 +0900
Subject: [PATCH v34 05/16] Prevent orphan storage files after server crash

When a server crashes during a transaction that created tables, newly
created but unused storage files are not removed. This patch prevents
such orphan files by utilizing the UNDO log system for storage files.

The behavior of this feature overlaps with the existing functionality,
which handles the removal of unnecessary files during rollback using
pendingDeletes. Therefore, that part will be removed. On the other
hand, the commit-time file deletions are not within the scope of the
UNDO log functionality, so that part will remain used. As a result,
the isCommit flag of the entries in the pendingDeletes list is now
always true. However, to avoid non-essential changes to the code, the
flag will be retained.
---
 src/backend/access/heap/heapam_handler.c   |  22 +--
 src/backend/access/transam/simpleundolog.c |   1 +
 src/backend/catalog/index.c                |   4 +-
 src/backend/catalog/storage.c              | 179 +++++++++++++++++----
 src/backend/commands/sequence.c            |   4 +-
 src/backend/commands/tablecmds.c           |  19 +--
 src/backend/storage/buffer/bufmgr.c        |   4 +-
 src/backend/storage/file/reinit.c          |  78 +++++++++
 src/backend/storage/smgr/smgr.c            |   9 ++
 src/include/access/rmgrlist.h              |   2 +-
 src/include/catalog/storage.h              |   2 +
 src/include/catalog/storage_ulog.h         |  24 +++
 src/include/storage/reinit.h               |   3 +
 src/include/storage/smgr.h                 |   1 +
 src/tools/pgindent/typedefs.list           |   1 +
 15 files changed, 298 insertions(+), 55 deletions(-)
 create mode 100644 src/include/catalog/storage_ulog.h

diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index 1c6da286d4..e9daa1d59e 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -612,8 +612,7 @@ heapam_relation_set_new_filelocator(Relation rel,
 		Assert(rel->rd_rel->relkind == RELKIND_RELATION ||
 			   rel->rd_rel->relkind == RELKIND_MATVIEW ||
 			   rel->rd_rel->relkind == RELKIND_TOASTVALUE);
-		smgrcreate(srel, INIT_FORKNUM, false);
-		log_smgrcreate(newrlocator, INIT_FORKNUM);
+		RelationCreateFork(srel, INIT_FORKNUM, true, true);
 	}
 
 	smgrclose(srel);
@@ -657,16 +656,17 @@ heapam_relation_copy_data(Relation rel, const RelFileLocator *newrlocator)
 	{
 		if (smgrexists(RelationGetSmgr(rel), forkNum))
 		{
-			smgrcreate(dstrel, forkNum, false);
-
-			/*
-			 * WAL log creation if the relation is persistent, or this is the
-			 * init fork of an unlogged relation.
-			 */
-			if (RelationIsPermanent(rel) ||
+			bool wal_log = RelationIsPermanent(rel) |
 				(rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
-				 forkNum == INIT_FORKNUM))
-				log_smgrcreate(newrlocator, forkNum);
+				 forkNum == INIT_FORKNUM);
+
+			/*
+			 * Usually, we don't use UNDO log for FSM or VM forks, as their
+			 * creation is not transactional. However, we're currently copying
+			 * the entire relation in a transactional manner, which requires
+			 * after-crash cleanup.
+			 */
+			RelationCreateFork(dstrel, forkNum, wal_log, true);
 			RelationCopyStorage(RelationGetSmgr(rel), dstrel, forkNum,
 								rel->rd_rel->relpersistence);
 		}
diff --git a/src/backend/access/transam/simpleundolog.c b/src/backend/access/transam/simpleundolog.c
index 4bba27e340..f8a7fd367f 100644
--- a/src/backend/access/transam/simpleundolog.c
+++ b/src/backend/access/transam/simpleundolog.c
@@ -25,6 +25,7 @@
 #include "access/twophase_rmgr.h"
 #include "access/xact.h"
 #include "access/xlog.h"
+#include "catalog/storage_ulog.h"
 #include "miscadmin.h"
 #include "storage/fd.h"
 #include "utils/memutils.h"
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 33759056e3..a573f1a702 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -3030,8 +3030,8 @@ index_build(Relation heapRelation,
 	if (indexRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
 		!smgrexists(RelationGetSmgr(indexRelation), INIT_FORKNUM))
 	{
-		smgrcreate(RelationGetSmgr(indexRelation), INIT_FORKNUM, false);
-		log_smgrcreate(&indexRelation->rd_locator, INIT_FORKNUM);
+		RelationCreateFork(RelationGetSmgr(indexRelation),
+						   INIT_FORKNUM, true, true);
 		indexRelation->rd_indam->ambuildempty(indexRelation);
 	}
 
diff --git a/src/backend/catalog/storage.c b/src/backend/catalog/storage.c
index bdbed9fba3..31400c514f 100644
--- a/src/backend/catalog/storage.c
+++ b/src/backend/catalog/storage.c
@@ -19,22 +19,41 @@
 
 #include "postgres.h"
 
+#include "access/amapi.h"
+#include "access/simpleundolog.h"
 #include "access/visibilitymap.h"
 #include "access/xact.h"
 #include "access/xlog.h"
 #include "access/xloginsert.h"
 #include "access/xlogutils.h"
 #include "catalog/storage.h"
+#include "catalog/storage_ulog.h"
 #include "catalog/storage_xlog.h"
+#include "common/file_utils.h"
 #include "miscadmin.h"
 #include "storage/bulk_write.h"
+#include "storage/copydir.h"
+#include "storage/fd.h"
 #include "storage/freespace.h"
 #include "storage/proc.h"
+#include "storage/reinit.h"
 #include "storage/smgr.h"
 #include "utils/hsearch.h"
+#include "utils/inval.h"
 #include "utils/memutils.h"
 #include "utils/rel.h"
 
+/* ULOG gives us high 4 bits (just following xlog) */
+#define ULOG_SMGR_CREATE			0x10
+
+/* undo log entry for storage file creation */
+typedef struct ul_smgr_create
+{
+	RelFileLocator	rlocator;
+	ProcNumber		backend;
+	ForkNumber		forknum;
+} ul_smgr_create;
+
 /* GUC variables */
 int			wal_skip_threshold = 2048;	/* in kilobytes */
 
@@ -76,6 +95,10 @@ typedef struct PendingRelSync
 static PendingRelDelete *pendingDeletes = NULL; /* head of linked list */
 static HTAB *pendingSyncHash = NULL;
 
+/* local functions */
+static void ulog_smgrcreate(SMgrRelation srel, ForkNumber forkNum);
+static void ulog_smgrcreate_redo(SMgrRelation srel, ForkNumber forkNum,
+								 TransactionId xid);
 
 /*
  * AddPendingSync
@@ -147,28 +170,8 @@ RelationCreateStorage(RelFileLocator rlocator, char relpersistence,
 	}
 
 	srel = smgropen(rlocator, procNumber);
-	smgrcreate(srel, MAIN_FORKNUM, false);
 
-	if (needs_wal)
-		log_smgrcreate(&srel->smgr_rlocator.locator, MAIN_FORKNUM);
-
-	/*
-	 * Add the relation to the list of stuff to delete at abort, if we are
-	 * asked to do so.
-	 */
-	if (register_delete)
-	{
-		PendingRelDelete *pending;
-
-		pending = (PendingRelDelete *)
-			MemoryContextAlloc(TopMemoryContext, sizeof(PendingRelDelete));
-		pending->rlocator = rlocator;
-		pending->procNumber = procNumber;
-		pending->atCommit = false;	/* delete if abort */
-		pending->nestLevel = GetCurrentTransactionNestLevel();
-		pending->next = pendingDeletes;
-		pendingDeletes = pending;
-	}
+	RelationCreateFork(srel, MAIN_FORKNUM, needs_wal, register_delete);
 
 	if (relpersistence == RELPERSISTENCE_PERMANENT && !XLogIsNeeded())
 	{
@@ -179,6 +182,32 @@ RelationCreateStorage(RelFileLocator rlocator, char relpersistence,
 	return srel;
 }
 
+/*
+ * RelationCreateFork
+ *		Create physical storage for a fork of a relation.
+ *
+ * This function creates a relation fork in a transactional manner. When
+ * undo_log is true, the creation is UNDO-logged so that in case of transaction
+ * aborts or server crashes later on, the fork will be removed. If the caller
+ * plans to remove the fork in another way, it should pass false. Additionally,
+ * it is WAL-logged if wal_log is true.
+ */
+void
+RelationCreateFork(SMgrRelation srel, ForkNumber forkNum,
+				   bool wal_log, bool undo_log)
+{
+	/* Schedule the removal of this init fork at abort if requested. */
+	if (undo_log)
+		ulog_smgrcreate(srel, forkNum);
+
+	/* WAL-log this creation if requested. */
+	if (wal_log)
+		log_smgrcreate(&srel->smgr_rlocator.locator, forkNum);
+
+	smgrcreate(srel, forkNum, false);
+
+}
+
 /*
  * Perform XLogInsert of an XLOG_SMGR_CREATE record to WAL.
  */
@@ -198,6 +227,38 @@ log_smgrcreate(const RelFileLocator *rlocator, ForkNumber forkNum)
 	XLogInsert(RM_SMGR_ID, XLOG_SMGR_CREATE | XLR_SPECIAL_REL_UPDATE);
 }
 
+/*
+ * Perform SimpleUndoLogWrite of an XLOG_SMGR_CREATE record to UNDO log.
+ */
+void
+ulog_smgrcreate(SMgrRelation srel, ForkNumber forkNum)
+{
+	ul_smgr_create ulrec;
+
+	ulrec.rlocator = srel->smgr_rlocator.locator;
+	ulrec.backend = srel->smgr_rlocator.backend;
+	ulrec.forknum = forkNum;
+	SimpleUndoLogWrite(RM_SMGR_ID, ULOG_SMGR_CREATE,
+					   &ulrec, sizeof(ulrec));
+}
+
+/*
+ * Perform SimpleUndoLogWrite of an XLOG_SMGR_CREATE record to UNDO log during
+ * recovery.
+ */
+void
+ulog_smgrcreate_redo(SMgrRelation srel, ForkNumber forkNum,
+					 TransactionId xid)
+{
+	ul_smgr_create ulrec;
+
+	ulrec.rlocator = srel->smgr_rlocator.locator;
+	ulrec.backend = srel->smgr_rlocator.backend;
+	ulrec.forknum = forkNum;
+	SimpleUndoLogWriteRedo(RM_SMGR_ID, ULOG_SMGR_CREATE,
+						   &ulrec, sizeof(ulrec), xid);
+}
+
 /*
  * RelationDropStorage
  *		Schedule unlinking of physical storage at transaction commit.
@@ -218,13 +279,12 @@ RelationDropStorage(Relation rel)
 	pendingDeletes = pending;
 
 	/*
-	 * NOTE: if the relation was created in this transaction, it will now be
-	 * present in the pending-delete list twice, once with atCommit true and
-	 * once with atCommit false.  Hence, it will be physically deleted at end
-	 * of xact in either case (and the other entry will be ignored by
-	 * smgrDoPendingDeletes, so no error will occur).  We could instead remove
-	 * the existing list entry and delete the physical file immediately, but
-	 * for now I'll keep the logic simple.
+	 * NOTE: If the relation was created in this transaction, it will now be
+	 * present both in the pending-delete list for commit time and in a UNDO
+	 * log file for abort time. Hence, it will be physically deleted at the end
+	 * of the xact in either case. Although we could remove the existing UNDO
+	 * log record, the current UNDO log file format makes it difficult to
+	 * delete individual recoreds for now and maybe in the future.
 	 */
 
 	RelationCloseSmgr(rel);
@@ -967,6 +1027,7 @@ smgr_redo(XLogReaderState *record)
 		SMgrRelation reln;
 
 		reln = smgropen(xlrec->rlocator, INVALID_PROC_NUMBER);
+		ulog_smgrcreate_redo(reln, xlrec->forkNum, XLogRecGetXid(record));
 		smgrcreate(reln, xlrec->forkNum, true);
 	}
 	else if (info == XLOG_SMGR_TRUNCATE)
@@ -1060,3 +1121,65 @@ smgr_redo(XLogReaderState *record)
 	else
 		elog(PANIC, "smgr_redo: unknown op code %u", info);
 }
+
+void
+smgr_undo(SimpleUndoLogRecord *record, UndoLogFileState state,
+		  bool isCommit, bool cleanup)
+{
+	uint8	info = record->ul_info & ~ULR_INFO_MASK;
+
+	if (info == ULOG_SMGR_CREATE)
+	{
+		ul_smgr_create *ulrec = (ul_smgr_create *) ULogRecGetData(record);
+
+		if (state != ULOG_FILE_DEFAULT && cleanup)
+		{
+			Assert (!isCommit);
+			/*
+			 * During post-crash cleanup, if the transaction that created the
+			 * fork was already prepared before the crash, the fate of the file
+			 * should be determined by whether the prepared transaction will be
+			 * committed or not. Tell reinit not to reset this relation.
+			 */
+			ResetUnloggedRelationIgnore(ulrec->rlocator,
+										ulrec->backend);
+		}
+		else if (!isCommit)
+		{
+			/* Otherwise, remove the file immediately. */
+			SMgrRelation reln;
+			ForkNumber	forks[3];
+			BlockNumber firstblocks[3] = {0};
+			int			nforks = 0;
+
+			forks[nforks++] = ulrec->forknum;
+
+			/*
+			 * If the MAIN fork was created in the transaction, the rollback
+			 * should remove all forks of this relation.  Although we could
+			 * register an undo record individually for each fork, this may be
+			 * more complex because VM and FSM can be created
+			 * non-transactionally outside the transaction that created the
+			 * MAIN fork.
+			 */
+			if (ulrec->forknum == MAIN_FORKNUM)
+			{
+				forks[nforks++] = VISIBILITYMAP_FORKNUM;
+				forks[nforks++] = FSM_FORKNUM;
+			}
+
+			/*
+			 * Drop buffers, then the files. This can be improved by using
+			 * smgrdounlinkall(), but currently I take the simpler way.
+			 */
+			reln = smgropen(ulrec->rlocator, ulrec->backend);
+			DropRelationBuffers(reln, forks, nforks, firstblocks);
+			for (int i = 0 ; i < nforks ; i++)
+				smgrunlink(reln, forks[i], true);
+
+			smgrclose(reln);
+		}
+	}
+	else
+		elog(PANIC, "smgr_undo: unknown op code %u", info);
+}
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index b37fd688d3..065bfbc1c9 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -31,6 +31,7 @@
 #include "catalog/objectaccess.h"
 #include "catalog/pg_sequence.h"
 #include "catalog/pg_type.h"
+#include "catalog/storage.h"
 #include "catalog/storage_xlog.h"
 #include "commands/defrem.h"
 #include "commands/sequence.h"
@@ -344,8 +345,7 @@ fill_seq_with_data(Relation rel, HeapTuple tuple)
 		SMgrRelation srel;
 
 		srel = smgropen(rel->rd_locator, INVALID_PROC_NUMBER);
-		smgrcreate(srel, INIT_FORKNUM, false);
-		log_smgrcreate(&rel->rd_locator, INIT_FORKNUM);
+		RelationCreateFork(srel, INIT_FORKNUM, true, true);
 		fill_seq_fork_with_data(rel, tuple, INIT_FORKNUM);
 		FlushRelationBuffers(rel);
 		smgrclose(srel);
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index b3cc6f8f69..e9bba3aceb 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -15381,16 +15381,17 @@ index_copy_data(Relation rel, RelFileLocator newrlocator)
 	{
 		if (smgrexists(RelationGetSmgr(rel), forkNum))
 		{
-			smgrcreate(dstrel, forkNum, false);
-
-			/*
-			 * WAL log creation if the relation is persistent, or this is the
-			 * init fork of an unlogged relation.
-			 */
-			if (RelationIsPermanent(rel) ||
+			bool wal_log = RelationIsPermanent(rel) |
 				(rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
-				 forkNum == INIT_FORKNUM))
-				log_smgrcreate(&newrlocator, forkNum);
+				 forkNum == INIT_FORKNUM);
+
+			/*
+			 * Usually, we don't use UNDO log for FSM or VM forks, as their
+			 * creation is not transactional. However, we're currently copying
+			 * the entire relation in a transactional manner, which requires
+			 * after-crash cleanup.
+			 */
+			RelationCreateFork(dstrel, forkNum, wal_log, true);
 			RelationCopyStorage(RelationGetSmgr(rel), dstrel, forkNum,
 								rel->rd_rel->relpersistence);
 		}
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 5cdd2f10fc..2bc60f7295 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -4821,8 +4821,7 @@ CreateAndCopyRelationData(RelFileLocator src_rlocator,
 	/*
 	 * Create and copy all forks of the relation.  During create database we
 	 * have a separate cleanup mechanism which deletes complete database
-	 * directory.  Therefore, each individual relation doesn't need to be
-	 * registered for cleanup.
+	 * directory. Therefore, do not issue an UNDO log for this relation.
 	 */
 	RelationCreateStorage(dst_rlocator, relpersistence, false);
 
@@ -4836,6 +4835,7 @@ CreateAndCopyRelationData(RelFileLocator src_rlocator,
 	{
 		if (smgrexists(src_rel, forkNum))
 		{
+			/* Use smgrcreate() directly as no UNDO log is required. */
 			smgrcreate(dst_rel, forkNum, false);
 
 			/*
diff --git a/src/backend/storage/file/reinit.c b/src/backend/storage/file/reinit.c
index f1cd1a38d9..c00f1aaa8b 100644
--- a/src/backend/storage/file/reinit.c
+++ b/src/backend/storage/file/reinit.c
@@ -34,6 +34,39 @@ typedef struct
 	RelFileNumber relnumber;	/* hash key */
 } unlogged_relation_entry;
 
+static char **ignore_files = NULL;
+static int nignore_elems = 0;
+static int nignore_files = 0;
+
+/*
+ * identify the file should be ignored during resetting unlogged relations.
+ */
+static bool
+reinit_ignore_file(const char *dirname, const char *name)
+{
+	char fnamebuf[MAXPGPATH];
+	int len;
+
+	if (nignore_files == 0)
+		return false;
+
+	strncpy(fnamebuf, dirname, MAXPGPATH - 1);
+	strncat(fnamebuf, "/", MAXPGPATH - 1);
+	strncat(fnamebuf, name, MAXPGPATH - 1);
+	fnamebuf[MAXPGPATH - 1] = 0;
+
+	for (int i = 0 ; i < nignore_files ; i++)
+	{
+		/* match ignoring fork part */
+		len = strlen(ignore_files[i]);
+		if (strncmp(fnamebuf, ignore_files[i], len) == 0 &&
+			(fnamebuf[len] == 0 || fnamebuf[len] == '_'))
+			return true;
+	}
+
+	return false;
+}
+
 /*
  * Reset unlogged relations from before the last restart.
  *
@@ -204,6 +237,10 @@ ResetUnloggedRelationsInDbspaceDir(const char *dbspacedirname, int op)
 													 &forkNum, &segno))
 				continue;
 
+			/* Skip anything that undo log suggested to ignore */
+			if (reinit_ignore_file(dbspacedirname, de->d_name))
+				continue;
+
 			/* Also skip it unless this is the init fork. */
 			if (forkNum != INIT_FORKNUM)
 				continue;
@@ -243,6 +280,10 @@ ResetUnloggedRelationsInDbspaceDir(const char *dbspacedirname, int op)
 													 &forkNum, &segno))
 				continue;
 
+			/* Skip anything that undo log suggested to ignore */
+			if (reinit_ignore_file(dbspacedirname, de->d_name))
+				continue;
+
 			/* We never remove the init fork. */
 			if (forkNum == INIT_FORKNUM)
 				continue;
@@ -294,6 +335,10 @@ ResetUnloggedRelationsInDbspaceDir(const char *dbspacedirname, int op)
 													 &forkNum, &segno))
 				continue;
 
+			/* Skip anything that undo log suggested to ignore */
+			if (reinit_ignore_file(dbspacedirname, de->d_name))
+				continue;
+
 			/* Also skip it unless this is the init fork. */
 			if (forkNum != INIT_FORKNUM)
 				continue;
@@ -337,6 +382,10 @@ ResetUnloggedRelationsInDbspaceDir(const char *dbspacedirname, int op)
 													 &forkNum, &segno))
 				continue;
 
+			/* Skip anything that undo log suggested to ignore */
+			if (reinit_ignore_file(dbspacedirname, de->d_name))
+				continue;
+
 			/* Also skip it unless this is the init fork. */
 			if (forkNum != INIT_FORKNUM)
 				continue;
@@ -366,6 +415,35 @@ ResetUnloggedRelationsInDbspaceDir(const char *dbspacedirname, int op)
 	}
 }
 
+/*
+ * Record relfilenodes that should be left alone during reinitializing unlogged
+ * relations.
+ */
+void
+ResetUnloggedRelationIgnore(RelFileLocator rloc, ProcNumber backend)
+{
+	RelFileLocatorBackend rbloc;
+
+	if (nignore_files >= nignore_elems)
+	{
+		if (ignore_files == NULL)
+		{
+			nignore_elems = 16;
+			ignore_files = palloc(sizeof(char *) * nignore_elems);
+		}
+		else
+		{
+			nignore_elems *= 2;
+			ignore_files = repalloc(ignore_files,
+									sizeof(char *) * nignore_elems);
+		}
+	}
+
+	rbloc.backend = backend;
+	rbloc.locator = rloc;
+	ignore_files[nignore_files++] = relpath(rbloc, MAIN_FORKNUM);
+}
+
 /*
  * Basic parsing of putative relation filenames.
  *
diff --git a/src/backend/storage/smgr/smgr.c b/src/backend/storage/smgr/smgr.c
index 7b9fa103ef..eb01040772 100644
--- a/src/backend/storage/smgr/smgr.c
+++ b/src/backend/storage/smgr/smgr.c
@@ -791,6 +791,15 @@ smgrimmedsync(SMgrRelation reln, ForkNumber forknum)
 	smgrsw[reln->smgr_which].smgr_immedsync(reln, forknum);
 }
 
+/*
+ * smgrunlink() -- unlink the storage file
+ */
+void
+smgrunlink(SMgrRelation reln, ForkNumber forknum, bool isRedo)
+{
+	smgrsw[reln->smgr_which].smgr_unlink(reln->smgr_rlocator, forknum, isRedo);
+}
+
 /*
  * AtEOXact_SMgr
  *
diff --git a/src/include/access/rmgrlist.h b/src/include/access/rmgrlist.h
index 9afb9bafcc..b856eac024 100644
--- a/src/include/access/rmgrlist.h
+++ b/src/include/access/rmgrlist.h
@@ -27,7 +27,7 @@
 /* symbol name, textual name, redo, desc, identify, startup, cleanup, mask, decode, undo */
 PG_RMGR(RM_XLOG_ID, "XLOG", xlog_redo, xlog_desc, xlog_identify, NULL, NULL, NULL, xlog_decode, NULL)
 PG_RMGR(RM_XACT_ID, "Transaction", xact_redo, xact_desc, xact_identify, NULL, NULL, NULL, xact_decode, NULL)
-PG_RMGR(RM_SMGR_ID, "Storage", smgr_redo, smgr_desc, smgr_identify, NULL, NULL, NULL, NULL, NULL)
+PG_RMGR(RM_SMGR_ID, "Storage", smgr_redo, smgr_desc, smgr_identify, NULL, NULL, NULL, NULL, smgr_undo)
 PG_RMGR(RM_CLOG_ID, "CLOG", clog_redo, clog_desc, clog_identify, NULL, NULL, NULL, NULL, NULL)
 PG_RMGR(RM_DBASE_ID, "Database", dbase_redo, dbase_desc, dbase_identify, NULL, NULL, NULL, NULL, NULL)
 PG_RMGR(RM_TBLSPC_ID, "Tablespace", tblspc_redo, tblspc_desc, tblspc_identify, NULL, NULL, NULL, NULL, NULL)
diff --git a/src/include/catalog/storage.h b/src/include/catalog/storage.h
index 72ef3ee92c..3451d6ac80 100644
--- a/src/include/catalog/storage.h
+++ b/src/include/catalog/storage.h
@@ -25,6 +25,8 @@ extern PGDLLIMPORT int wal_skip_threshold;
 extern SMgrRelation RelationCreateStorage(RelFileLocator rlocator,
 										  char relpersistence,
 										  bool register_delete);
+extern void RelationCreateFork(SMgrRelation srel, ForkNumber forkNum,
+							   bool wal_log, bool undo_log);
 extern void RelationDropStorage(Relation rel);
 extern void RelationPreserveStorage(RelFileLocator rlocator, bool atCommit);
 extern void RelationPreTruncate(Relation rel);
diff --git a/src/include/catalog/storage_ulog.h b/src/include/catalog/storage_ulog.h
new file mode 100644
index 0000000000..cc3d623afd
--- /dev/null
+++ b/src/include/catalog/storage_ulog.h
@@ -0,0 +1,24 @@
+/*-------------------------------------------------------------------------
+ *
+ * storage_ulog.h
+ *	  prototypes for Undo Log support for backend/catalog/storage.c
+ *
+ *
+ * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/catalog/storage_ulog.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef STORAGE_ULOG_H
+#define STORAGE_ULOG_H
+
+#include "access/simpleundolog.h"
+#include "storage/smgr.h"
+
+extern void smgr_undo(SimpleUndoLogRecord *record, UndoLogFileState prepared,
+					  bool isCommit, bool cleanup);
+#define ULogRecGetData(record) ((char *)record + sizeof(SimpleUndoLogRecord))
+
+#endif							/* STORAGE_XLOG_H */
diff --git a/src/include/storage/reinit.h b/src/include/storage/reinit.h
index 1373d509df..108cee160e 100644
--- a/src/include/storage/reinit.h
+++ b/src/include/storage/reinit.h
@@ -16,9 +16,12 @@
 #define REINIT_H
 
 #include "common/relpath.h"
+#include "storage/relfilelocator.h"
 
 
 extern void ResetUnloggedRelations(int op);
+extern void ResetUnloggedRelationIgnore(RelFileLocator rloc,
+										ProcNumber backend);
 extern bool parse_filename_for_nontemp_relation(const char *name,
 												RelFileNumber *relnumber,
 												ForkNumber *fork,
diff --git a/src/include/storage/smgr.h b/src/include/storage/smgr.h
index e15b20a566..e867ff92ab 100644
--- a/src/include/storage/smgr.h
+++ b/src/include/storage/smgr.h
@@ -107,6 +107,7 @@ extern void smgrtruncate(SMgrRelation reln, ForkNumber *forknum,
 						 int nforks, BlockNumber *nblocks);
 extern void smgrimmedsync(SMgrRelation reln, ForkNumber forknum);
 extern void smgrregistersync(SMgrRelation reln, ForkNumber forknum);
+extern void smgrunlink(SMgrRelation reln, ForkNumber forknum, bool isRedo);
 extern void AtEOXact_SMgr(void);
 extern bool ProcessBarrierSmgrRelease(void);
 
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 04f3eca550..c182a65d4d 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -4007,6 +4007,7 @@ uint8
 uint8_t
 uint8x16_t
 uintptr_t
+ul_smgr_create
 unicodeStyleBorderFormat
 unicodeStyleColumnFormat
 unicodeStyleFormat
-- 
2.43.5

