From 5655e65ae7eac64e7342b841df2fe82a52cd2594 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Fri, 5 Jul 2024 14:32:46 +0900
Subject: [PATCH v34 02/16] Introduce undo log implementation

This implementation creates UNDO log files, named with XID values and
stored in the pg_ulog directory. These files have a record format
similar to XLOG files, with one file for each sub-transaction if
needed. Each file consists of multiple UNDO records that capture
in-transaction changes requiring cleanup at abort time. If a server
crash occurs while UNDO log files exist, they trigger post-crash
cleanup before entering the REDO loop of crash recovery. The REDO loop
may then generate new UNDO logs. Most of these are removed during the
processing of commit/abort records, but some files may remain after
the REDO loop finishes. These remaining files also trigger abort-time
cleanups. The UNDO log files associated with prepared transactions are
preserved and not processed during recovery. They are processed when
the prepared transactions are finalized.

The creation of UNDO files is tracked in-memory using a linked list of
ActiveULog in a process for quick lookups. For a normal session, all
elements in the list pertain to one top-level transaction and are
removed at commit or abort time. In the startup process, the list may
contain elements for multiple top-level transactions.
---
 src/backend/access/transam/Makefile        |   1 +
 src/backend/access/transam/meson.build     |   1 +
 src/backend/access/transam/rmgr.c          |   2 +-
 src/backend/access/transam/simpleundolog.c | 991 +++++++++++++++++++++
 src/backend/access/transam/twophase.c      |   3 +
 src/backend/access/transam/xact.c          |  23 +
 src/backend/access/transam/xlog.c          |  22 +-
 src/bin/initdb/initdb.c                    |  17 +
 src/bin/pg_rewind/parsexlog.c              |   2 +-
 src/bin/pg_waldump/rmgrdesc.c              |   2 +-
 src/include/access/rmgr.h                  |   2 +-
 src/include/access/rmgrlist.h              |  46 +-
 src/include/access/simpleundolog.h         |  44 +
 src/tools/pgindent/typedefs.list           |   5 +
 14 files changed, 1129 insertions(+), 32 deletions(-)
 create mode 100644 src/backend/access/transam/simpleundolog.c
 create mode 100644 src/include/access/simpleundolog.h

diff --git a/src/backend/access/transam/Makefile b/src/backend/access/transam/Makefile
index 661c55a9db..531505cbbd 100644
--- a/src/backend/access/transam/Makefile
+++ b/src/backend/access/transam/Makefile
@@ -21,6 +21,7 @@ OBJS = \
 	rmgr.o \
 	slru.o \
 	subtrans.o \
+	simpleundolog.o \
 	timeline.o \
 	transam.o \
 	twophase.o \
diff --git a/src/backend/access/transam/meson.build b/src/backend/access/transam/meson.build
index 8a3522557c..c1225636b5 100644
--- a/src/backend/access/transam/meson.build
+++ b/src/backend/access/transam/meson.build
@@ -9,6 +9,7 @@ backend_sources += files(
   'rmgr.c',
   'slru.c',
   'subtrans.c',
+  'simpleundolog.c',
   'timeline.c',
   'transam.c',
   'twophase.c',
diff --git a/src/backend/access/transam/rmgr.c b/src/backend/access/transam/rmgr.c
index 1b7499726e..81206a64f8 100644
--- a/src/backend/access/transam/rmgr.c
+++ b/src/backend/access/transam/rmgr.c
@@ -44,7 +44,7 @@
 
 
 /* must be kept in sync with RmgrData definition in xlog_internal.h */
-#define PG_RMGR(symname,name,redo,desc,identify,startup,cleanup,mask,decode) \
+#define PG_RMGR(symname,name,redo,desc,identify,startup,cleanup,mask,decode,undo) \
 	{ name, redo, desc, identify, startup, cleanup, mask, decode },
 
 RmgrData	RmgrTable[RM_MAX_ID + 1] = {
diff --git a/src/backend/access/transam/simpleundolog.c b/src/backend/access/transam/simpleundolog.c
new file mode 100644
index 0000000000..4bba27e340
--- /dev/null
+++ b/src/backend/access/transam/simpleundolog.c
@@ -0,0 +1,991 @@
+/*-------------------------------------------------------------------------
+ *
+ * simpleundolog.c
+ *		Simple implementation of PostgreSQL transaction-undo-log manager
+ *
+ * This module logs the cleanup procedures required during a transaction abort.
+ * The information is recorded in files to ensure post-crash recovery runs the
+ * necessary cleanup procedures.
+ *
+ * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/backend/access/transam/simpleundolog.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include <sys/stat.h>
+
+#include "lib/stringinfo.h"
+#include "access/parallel.h"
+#include "access/simpleundolog.h"
+#include "access/twophase_rmgr.h"
+#include "access/xact.h"
+#include "access/xlog.h"
+#include "miscadmin.h"
+#include "storage/fd.h"
+#include "utils/memutils.h"
+
+
+#define ULOG_FILE_MAGIC 0x474f4c55	/* 'ULOG' in big-endian */
+
+/*
+ * Struct for undo-log disk data
+ *
+ * Each undo-log file is named in the format "<topxid>-<subxid>" and contains
+ * undo data for a subtransaction. The file begins with a header followed by
+ * undo log records. An undo file is created when the first undo log is issued
+ * during a transaction and is removed upon the top transaction's commit or
+ * processed then removed during each subtransaction's rollback. When a
+ * transaction is prepared, this state is marked in the file header. The
+ * prepared undo files are processed by subsequent COMMIT/ROLLBACK PREPARED
+ * commands in the same manner as non-prepared files. In the event of a server
+ * crash during a transaction, non-prepared undo files left behind are handled
+ * before recovery starts. The recovery process may create new undo files,
+ * which are processed at the end of recovery. Prepared undo files are
+ * preserved throughout the recovery process.
+ */
+typedef struct UndoLogFileHeader
+{
+	int32 magic;			/* fixed ULOG file magic number */
+	UndoLogFileState state;	/* state of this file */
+	/* SimpleUndoLogRecord follows */
+} UndoLogFileHeader;
+
+typedef struct UndoDescData
+{
+	const char *rm_name;
+	void	(*rm_undo) (SimpleUndoLogRecord *record,
+						UndoLogFileState state, bool isCommit, bool cleanup);
+} UndoDescData;
+
+#define PG_RMGR(symname,name,redo,desc,identify,startup,cleanup,mask,decode,undo) \
+	{ name, undo },
+
+UndoDescData	UndoRoutines[RM_MAX_ID + 1] = {
+#include "access/rmgrlist.h"
+};
+#undef PG_RMGR
+
+/*
+ * During a transaction, all undo logs are managed by a linked list. This
+ * linked list is used for quick lookup of existing undo log files. We expect
+ * the number of files to be relatively small, so more efficient algorithms are
+ * not used here. During recovery, this list may contain undo log files for
+ * multiple top transactions.
+ */
+/* Undo log wroking state */
+typedef struct ActiveULog
+{
+	TransactionId xid;
+	struct ActiveULog *next;
+} ActiveULog;
+
+/*
+ * Struct for top-level management variables.
+ *
+ * active_ulogs holds the xid-subxid pairs for all subtransactions that issued
+ * undo logs during a top-level transaction.  Once an undo log file is opened,
+ * topxid, subxid, filename, and fd are set according to the currently open
+ * file.
+ */
+typedef struct ULogStateData
+{
+	ActiveULog *active_ulogs;	/* list of subxacts with ulogs */
+	ActiveULog *current_ulog;	/* current open entry */
+	ActiveULog *prev_ulog;		/* previous entry for removal */
+	TransactionId xid;			/* xid of the current ulog */
+	char file_name[MAXPGPATH];	/* current ulog file name */
+	int	fd;						/* file descriptor */
+	UndoLogFileHeader file_header;	/* current ulog file header */
+} ULogStateData;
+
+static ULogStateData ULogState =
+{NULL, NULL, NULL, InvalidTransactionId, "", -1, {0}};
+
+/* ULOG uses the same sync mode as XLOG, except for the PG_O_DIRECT bit. */
+static int
+ULogGetSyncBit(void)
+{
+	return XLogGetSyncBit() & ~PG_O_DIRECT;
+}
+
+/*
+ * undolog_set_filename()
+ *
+ * Generates undo log file name for the xid pair.
+ */
+static void
+undolog_set_filename(char *buf, TransactionId xid)
+{
+	snprintf(buf, MAXPGPATH, "%s/%08x", SIMPLE_UNDOLOG_DIR, xid);
+}
+
+
+/*
+ * undolog_load_file_header()
+ *
+ * Loads the header of the currently open file into the global buffer. The file
+ * pointer will point to the beginning of the first record after this function
+ * returns.
+ */
+static void
+undolog_load_file_header(void)
+{
+	if (lseek(ULogState.fd, 0, SEEK_SET) < 0)
+		ereport(ERROR,
+					errcode_for_file_access(),
+				errmsg("could not seek undolog file \"%s\": %m",
+					   ULogState.file_name));
+
+	if (read(ULogState.fd,
+			 &ULogState.file_header, sizeof(ULogState.file_header)) < 0)
+		ereport(ERROR,
+				errcode_for_file_access(),
+				errmsg("could not read undolog file \"%s\": %m",
+					   ULogState.file_name));
+	if (ULogState.file_header.magic != ULOG_FILE_MAGIC)
+		ereport(ERROR,
+				errcode_for_file_access(),
+				errmsg("invalid undolog file \"%s\": magic don't match",
+					   ULogState.file_name));
+}
+
+/*
+ * undolog_write_file_header()
+ *
+ * Writes the header of the currently open file from the global buffer. The file
+ * pointer will point to the beginning of the first record after this function
+ * returns.
+ */
+static void
+undolog_write_file_header(void)
+{
+	if (lseek(ULogState.fd, 0, SEEK_SET) < 0)
+		ereport(ERROR,
+					errcode_for_file_access(),
+				errmsg("could not seek undolog file \"%s\": %m",
+					   ULogState.file_name));
+
+	if (write(ULogState.fd,
+			  &ULogState.file_header, sizeof(ULogState.file_header)) < 0)
+		ereport(ERROR,
+				errcode_for_file_access(),
+				errmsg("could not write undolog file \"%s\": %m",
+					   ULogState.file_name));
+}
+
+/*
+ * undolog_sync_file()
+ *
+ * Sync the currently open ULOG file.
+ */
+static void
+undolog_sync_file(void)
+{
+	const char *msg;
+
+	/*
+	 * This function counts ULOG sync operation stats as part of WAL
+	 * operations.  In the future, we may want to separate ULOG stats from WAL
+	 * stats.
+	 */
+	if (!XLogFsyncFile(ULogState.fd, &msg))
+	{
+		ereport(ERROR,
+				(errcode_for_file_access(),
+				 errmsg(msg, ULogState.file_name)));
+	}
+}
+
+/*
+ * undolog_select_entry()
+ *
+ * Finds and selects the in-memory entry for the given xid pair.
+ *
+ * If create is false, returns false if not found, and no entry is selected.
+ * If create is true, returns true if found; otherwise, create one and returns
+ * false.
+ */
+static bool
+undolog_select_entry(TransactionId xid, bool create)
+{
+	ActiveULog *prev;
+
+	Assert (TransactionIdIsValid(xid));
+
+	/* short cut when the entry is already selected */
+	if (ULogState.current_ulog &&
+		ULogState.current_ulog->xid == xid)
+		return true;
+
+	ULogState.current_ulog = ULogState.prev_ulog = NULL;
+
+	/* we no longer use this file, close it */
+	if (ULogState.fd >= 0)
+	{
+		/* Switched between subtransactions, close the current file */
+		if (close(ULogState.fd) != 0)
+			ereport(ERROR, errcode_for_file_access(),
+					errmsg("could not close file \"%s\": %m",
+						   ULogState.file_name));
+
+		ULogState.xid = InvalidTransactionId;
+		ULogState.fd = -1;
+		ReleaseExternalFD();
+	}
+
+	/* search for the existing entry */
+	prev = NULL;
+	for (ActiveULog *p = ULogState.active_ulogs ; p ; p = p->next)
+	{
+		if (p->xid == xid)
+		{
+			ULogState.current_ulog = p;
+			ULogState.prev_ulog = prev;
+			return true;
+		}
+
+		prev = p;
+	}
+
+	/* no existing entry found; create a new one */
+	if (create)
+	{
+		ActiveULog *newlog = (ActiveULog *)
+			MemoryContextAlloc(TopMemoryContext, sizeof(ActiveULog));
+
+		newlog->xid = xid;
+		newlog->next = ULogState.active_ulogs;
+		ULogState.active_ulogs = newlog;
+		ULogState.current_ulog = newlog;
+		ULogState.prev_ulog = NULL;
+	}
+
+	return false;
+}
+
+/*
+ * undolog_remove_entry()
+ *
+ * Removes the currently selected entry.
+ *
+ * The entry to be deleted must have been previously selected using
+ * undolog_select_entry(), and the corresponding log file is expected to have
+ * already been deleted if it exists.
+ */
+static bool
+undolog_remove_entry()
+{
+	ActiveULog *p = ULogState.current_ulog;
+
+	if (ULogState.prev_ulog)
+		ULogState.prev_ulog = p->next;
+	else
+		ULogState.active_ulogs = p->next;
+
+	ULogState.current_ulog = ULogState.prev_ulog = NULL;
+
+	pfree(p);
+
+	return true;
+}
+
+/*
+ * undolog_init_file()
+ *
+ * Sets the initial header of an already-opened ulog file.
+ *
+ * The file pointer will point to just after the header after this function
+ * returns.
+ */
+static void
+undolog_init_file(void)
+{
+	ULogState.file_header.magic = ULOG_FILE_MAGIC;
+	ULogState.file_header.state = ULOG_FILE_DEFAULT;
+
+	if (write(ULogState.fd, &ULogState.file_header,
+			  sizeof(ULogState.file_header)) < 0)
+	ereport(ERROR,
+			errcode_for_file_access(),
+			errmsg("could not write undolog file \"%s\": %m",
+				   ULogState.file_name));
+}
+
+/*
+ * undolog_open_file_by_name()
+ *
+ * Opens a ULOG file specified by ULogState.file_name.
+ *
+ * Returns true if the file was found, and false if not found.
+ *
+ * If create is true, this function errors out if the file already
+ * exists. During recovery, this function may attempt to create
+ * already-existing ULOG file for an uncommitted prepared transaction.  In this
+ * case, the existing file is opened instead of causing an error.
+ *
+ * Note that xid values in ULogState are set to invalid even after a sccessful
+ * return. They will be set by undolog_open_fild().
+ */
+static bool
+undolog_open_file_by_name(bool create)
+{
+	int omode = 0;
+	int cmode = 0;
+
+	Assert (ULogState.fd < 0);
+
+	omode = PG_BINARY | O_RDWR | ULogGetSyncBit();
+
+	if (create)
+		cmode = O_CREAT | O_EXCL;
+
+	ULogState.fd = BasicOpenFile(ULogState.file_name, omode | cmode);
+
+	if (ULogState.fd < 0)
+	{
+		if (!create)
+		{
+			if (errno == ENOENT)
+				return false;
+
+			ereport(ERROR,
+					errcode_for_file_access(),
+					errmsg("could not open undolog file \"%s\": %m",
+						   ULogState.file_name));
+		}
+
+		/*
+		 * ULOG files for prepared transactions are preserved throughout the
+		 * recovery process. Therefore, recovery may attempt to create an
+		 * already existing file. If the file is confirmed to be prepared, we
+		 * should continue the recovery and will ignore all ULOG writes to this
+		 * file. See UndoLogCleanup() for details.
+
+		 */
+		if (errno == EEXIST && RecoveryInProgress())
+		{
+			ULogState.fd = BasicOpenFile(ULogState.file_name, omode);
+			if (ULogState.fd >= 0)
+			{
+				undolog_load_file_header();
+				if (ULogState.file_header.state == ULOG_FILE_DEFAULT)
+					elog(PANIC, "non prepared file found: %s",
+						 ULogState.file_name);
+
+				elog(LOG, "ulog file for prepared transaction found: %s",
+					 ULogState.file_name);
+			}
+
+			/* restore the orignal error number */
+			errno = EEXIST;
+		}
+
+		if (ULogState.fd < 0)
+			ereport(ERROR,
+					errcode_for_file_access(),
+					errmsg("could not create undolog file \"%s\": %m",
+						   ULogState.file_name));
+	}
+	else if (create)
+	{
+		undolog_init_file();
+		undolog_sync_file();
+	}
+	else
+		undolog_load_file_header();
+
+	ReserveExternalFD();
+	ULogState.xid = InvalidTransactionId;
+
+	/* in create mode, return false since the file was not found */
+	if (create)
+		return false;
+
+	return true;
+}
+
+/*
+ * undolog_open_file() - Opens a ulog file for the specified xid pair.
+ *
+ * See undolog_open_file_by_name() for more details.
+ *
+ * XID values and file_name in ULogState are set after a successful
+ * return. Otherwise, they are set to invalid values.
+ */
+static bool
+undolog_open_file(TransactionId xid, bool create)
+{
+	bool ret;
+
+	/* shortcut for repeated usage of the same file */
+	if (ULogState.xid == xid)
+	{
+		Assert(ULogState.fd >= 0);
+		return true;
+	}
+
+	/* Switched between subtransactions, close the current file if any */
+	if  (ULogState.fd >= 0)
+	{
+		if (close(ULogState.fd) != 0)
+			ereport(ERROR, errcode_for_file_access(),
+					errmsg("could not close file \"%s\": %m",
+						   ULogState.file_name));
+
+		ULogState.xid = InvalidTransactionId;
+		ULogState.fd = -1;
+		ReleaseExternalFD();
+	}
+
+	Assert (ULogState.xid == InvalidTransactionId &&
+			ULogState.fd == -1);
+
+	/* Set the file name */
+	undolog_set_filename(ULogState.file_name, xid);
+
+	/* Do the task */
+	ret = undolog_open_file_by_name(create);
+
+	/* Set the xid pair for this file if opened */
+	if (ret || create)
+		ULogState.xid = xid;
+
+	return ret;
+}
+
+/*
+ * undolog_close_file() - Closes the curerntly opened ulog file, if any.
+ */
+static void
+undolog_close_file(void)
+{
+	if (ULogState.fd < 0)
+		return;
+
+	if (close(ULogState.fd) != 0)
+		ereport(ERROR, errcode_for_file_access(),
+				errmsg("could not close file \"%s\": %m",
+					   ULogState.file_name));
+
+	ULogState.xid = InvalidTransactionId;
+	ULogState.fd = -1;
+	ReleaseExternalFD();
+}
+
+/*
+ * undolog_remove_file() - Removes a file specified by ULogState.file_name.
+ *
+ * The file must already be closed.
+ */
+static void
+undolog_remove_file(void)
+{
+	durable_unlink(ULogState.file_name, FATAL);
+	ULogState.file_name[0] = 0;
+}
+
+/*
+ * undolog_remove_file_by_xid()
+ *
+ * Removes a file specified by an xid pair. Closes the file if it is open.
+ */
+static void
+undolog_remove_file_by_xid(TransactionId xid)
+{
+	char file_name[MAXPGPATH];
+
+	if (ULogState.xid == xid)
+		undolog_close_file();
+
+	undolog_set_filename(file_name, xid);
+	durable_unlink(file_name, FATAL);
+}
+
+/*
+ * SimpleUndoLogWrite() - Write an undolog record using current xid
+ */
+void
+SimpleUndoLogWrite(RmgrId rmgr, uint8 info, void *data, int len)
+{
+	/*
+	 *  The following lines may assign a new transaction ID. This is somewhat
+	 *  clumsy, but the caller needs to assign it soon.
+	 */
+	TransactionId xid = GetCurrentTransactionId();
+
+	SimpleUndoLogWriteRedo(rmgr, info, data, len, xid);
+}
+
+/*
+ * SimpleUndoLogWriteRedo() -  Writes an undolog record
+ *
+ * topxid is the XID of the top-level transaction. subxid is the assigned
+ * TransactionId of the current transaction, not a SubTransactionId.
+ * InvalidTransactionId indicates that the current transaction is the top-level
+ * transaction.  See SimpleUndoLogWrite().
+ *
+ * This function is exposed for use during recovery.
+ */
+void
+SimpleUndoLogWriteRedo(RmgrId rmgr, uint8 info, void *data, int len,
+					   TransactionId xid)
+{
+	int reclen = sizeof(SimpleUndoLogRecord) + len;
+	SimpleUndoLogRecord *rec;
+	pg_crc32c	undodata_crc;
+
+	Assert(!IsParallelWorker());
+
+	/* Inacitvate undo system during bootprocessing mode */
+	if (IsBootstrapProcessingMode())
+		return;
+
+	/* We must be in xid-assigned transactions */
+	Assert(TransactionIdIsValid(xid));
+
+	/* The caller can set rmgr bits only. */
+	if ((info & ~ULR_RMGR_INFO_MASK) != 0)
+		elog(PANIC, "invalid ulog info mask %02X", info);
+
+	if (!undolog_select_entry(xid, true))
+	{
+		/* new entry created, create the corresponding file */
+		undolog_open_file(xid, true);
+	}
+	else
+	{
+		/* entry exists, open existing file */
+		if (!undolog_open_file(xid, false))
+			ereport(ERROR,
+					errcode_for_file_access(),
+					errmsg("could not open undolog file \"%s\": %m",
+						   ULogState.file_name));
+	}
+
+	/*
+	 * Writes to files for prepared transactions are ignored during
+	 * recovery. See undolog_open_file_by_name() for more details.
+	 */
+	if (ULogState.file_header.state != ULOG_FILE_DEFAULT)
+	{
+		Assert (RecoveryInProgress());
+		return;
+	}
+
+	rec = palloc(reclen);
+	rec->ul_tot_len = reclen;
+	rec->ul_rmid = rmgr;
+	rec->ul_info = info;
+
+	memcpy((char *)rec + sizeof(SimpleUndoLogRecord), data, len);
+
+	/* Calculate CRC of the data */
+	INIT_CRC32C(undodata_crc);
+	COMP_CRC32C(undodata_crc, &rec->ul_rmid,
+				reclen - offsetof(SimpleUndoLogRecord, ul_rmid));
+	rec->ul_crc = undodata_crc;
+
+
+	if (write(ULogState.fd, rec, reclen) < 0)
+				ereport(ERROR,
+						errcode_for_file_access(),
+						errmsg("could not write to undolog file \"%s\": %m",
+							   ULogState.file_name));
+
+	pfree(rec);
+	undolog_sync_file();
+}
+
+/*
+ * ulog_process_ulogfile() - Processes the currently open undo log.
+ *
+ * The file must be opened beforehand.
+ * If cleanup is true, this function informs the undo callback functions that
+ * it is called during recovery cleanup and the transaction is prepared.  In
+ * this case, the undo callback may need to behave differently.
+ */
+#define ULOG_READBUF_INIT_SIZE 32
+static void
+undolog_process_ulogfile(bool isCommit, bool cleanup, MemoryContext outercxt)
+{
+	static int		bufsize = 0;
+	static char	   *buf = NULL;
+	int				ret;
+
+	StaticAssertDecl(sizeof(SimpleUndoLogRecord) <= ULOG_READBUF_INIT_SIZE,
+					 "initial buffer size too small");
+
+	Assert (ULogState.fd >= 0);
+	Assert (outercxt);
+
+	undolog_load_file_header();
+
+	bufsize = ULOG_READBUF_INIT_SIZE;
+	buf = palloc(bufsize);
+
+	while ((ret = read(ULogState.fd, buf, sizeof(SimpleUndoLogRecord))) ==
+		   sizeof(SimpleUndoLogRecord))
+	{
+		SimpleUndoLogRecord *rec = (SimpleUndoLogRecord *) buf;
+		int readlen = rec->ul_tot_len - sizeof(SimpleUndoLogRecord);
+		MemoryContext	oldcxt;
+		pg_crc32c	undodata_crc;
+
+		if (rec->ul_tot_len > bufsize)
+		{
+			bufsize *= 2;
+			buf = repalloc(buf, bufsize);
+			rec = (SimpleUndoLogRecord *) buf;
+		}
+
+		ret = read(ULogState.fd,
+				   buf + sizeof(SimpleUndoLogRecord), readlen);
+		if (ret != readlen)
+		{
+			if (ret < 0)
+				ereport(ERROR,
+						errcode_for_file_access(),
+						errmsg("could not read undo log file \"%s\": %m",
+							   ULogState.file_name));
+
+			ereport(ERROR,
+					errcode_for_file_access(),
+					errmsg("reading undo log expected %d bytes, but actually %d: %s",
+						   readlen, ret, ULogState.file_name));
+
+		}
+
+		/* CRC check */
+		INIT_CRC32C(undodata_crc);
+		COMP_CRC32C(undodata_crc, &rec->ul_rmid,
+					rec->ul_tot_len - offsetof(SimpleUndoLogRecord, ul_rmid));
+		if (!EQ_CRC32C(rec->ul_crc, undodata_crc))
+		{
+			/*
+			 * The location is the byte immediately following the just-read
+			 * record. We cannot issue ERROR because this function is called
+			 * during abort processing.
+			 */
+			off_t off = lseek(ULogState.fd, 0, SEEK_CUR);
+			ereport(WARNING,
+					errmsg("incorrect undolog record checksum at %lld in %s",
+						   (long long int) off, ULogState.file_name),
+					errdetail("Aborted undo processing of the corresponding transaction."));
+		}
+
+		/* The undo routines may want to allcoate memory in the outer context */
+		oldcxt = MemoryContextSwitchTo(outercxt);
+		UndoRoutines[rec->ul_rmid].rm_undo(rec,
+										   ULogState.file_header.state,
+										   isCommit, cleanup);
+		MemoryContextSwitchTo(oldcxt);
+	}
+
+	if (ret != 0)
+	{
+		if (ret < 0)
+				ereport(ERROR,
+						errcode_for_file_access(),
+						errmsg("could not read undo log file \"%s\": %m",
+							   ULogState.file_name));
+		if (ret != sizeof(SimpleUndoLogRecord))
+			ereport(ERROR,
+					errcode_for_file_access(),
+					errmsg("reading undo log expected %d bytes, but actually %d: %s",
+						   (int) sizeof(SimpleUndoLogRecord), ret, ULogState.file_name));
+	}
+}
+
+/*
+ * ulog_undo() - Processes undo log for the specified xid pair
+ *
+ * The undo log file for the xid pair is removed before this function returns,
+ * regardless whether it is processed or not. Therefore, the in-memory entry
+ * for this xid pair must be removed afterwards, if any.
+ */
+static void
+undolog_undo(bool isCommit, TransactionId xid)
+{
+	Assert(!IsParallelWorker());
+
+	/* Return if no undo log exists for the xid pair */
+	if (!undolog_open_file(xid, false))
+		return;
+
+	undolog_process_ulogfile(isCommit, false, CurrentMemoryContext);
+
+	undolog_remove_file_by_xid(xid);
+}
+
+/*
+ * ulog_exists() - Return true if the log file for the xid pair exists.
+ *
+ * This function has no side effects.
+ */
+static bool
+undolog_exists(TransactionId xid)
+{
+	char fname[MAXPGPATH];
+	struct stat statbuf;
+
+	undolog_set_filename(fname, xid);
+
+	if (stat(fname, &statbuf) < 0)
+	{
+		if (errno == ENOENT)
+			return false;
+		ereport(ERROR, errmsg("stat failed for undo file \"%s\": %m", fname));
+	}
+
+	return true;
+}
+
+/*
+ * SimpleUndoLog_UndoByXid()
+ *
+ * Processes undo logs for the specified transaction, intended for usein
+ * finishing prepared transactins or recovery.
+ *
+ * children is the list of subtransaction IDs of the topxid, with a length of
+ * nchildren.
+ */
+void
+SimpleUndoLog_UndoByXid(bool isCommit, TransactionId xid,
+							 int nchildren, TransactionId *children)
+{
+	ActiveULog *p;
+	ActiveULog *prev;
+
+	Assert (RecoveryInProgress() || ULogState.active_ulogs == NULL);
+
+	/* process undo logs */
+	if (undolog_exists(xid))
+		undolog_undo(isCommit, xid);
+
+	for (int i = 0 ; i < nchildren ; i++)
+	{
+		if (undolog_exists(children[i]))
+			undolog_undo(isCommit, children[i]);
+	}
+
+	/*
+	 * Remove in-memory entries for this transaction tree if any.
+	 * We have these entries only during recovery.
+	 */
+	Assert (RecoveryInProgress() || ULogState.active_ulogs == NULL);
+	prev = NULL;
+	for (p = ULogState.active_ulogs ; p ; p = p->next)
+	{
+		bool match = false;
+
+		if (p->xid == xid)
+			match = true;
+		else
+		{
+			for (int i = 0 ; i < nchildren ; i++)
+			{
+				if (p->xid == children[i])
+				{
+					match = true;
+					break;
+				}
+			}
+		}
+
+		if (!match)
+		{
+			prev = p;
+			continue;
+		}
+
+		/* remove this entry */
+		if (prev)
+			prev->next = p->next;
+		else
+			ULogState.active_ulogs = p->next;
+		pfree(p);
+
+		break;
+	}
+}
+
+/*
+ * AtEOXact_SimpleUndoLog() - At end-of-xact processing of undo logs.
+ *
+ * Processes all existing undo log files, leaving none remaining after this
+ * function returns.
+ */
+void
+AtEOXact_SimpleUndoLog(bool isCommit)
+{
+	ActiveULog *p = ULogState.active_ulogs;
+
+	if (!p)
+		return;
+
+	while(p)
+	{
+		ActiveULog *prev = p;
+
+		undolog_undo(isCommit, p->xid);
+
+		ULogState.active_ulogs = p = p->next;
+		pfree(prev);
+	}
+
+	ULogState.active_ulogs = ULogState.current_ulog = NULL;
+}
+
+/*
+ * AtEOXact_SimpleUndoLog() - At end-of-subxact processing of undo logs.
+ *
+ * The undo log for the subtransaction will be removed on abort. It will remain
+ * on commit and be processed at the end of the top-level transaction.
+ */
+void
+AtEOSubXact_SimpleUndoLog(bool isCommit)
+{
+	ActiveULog *p = ULogState.current_ulog;
+	TransactionId xid;
+
+	/*
+	 * Undo logs of committed subtransactions are processed at the end of the
+	 * top-level transaction.
+	 */
+	if (isCommit || !p)
+		return;
+
+	xid = GetCurrentTransactionIdIfAny();
+
+	/* Return if the innermost subxid is not assigned. */
+	if (!TransactionIdIsValid(xid))
+		return;
+
+	if (!undolog_select_entry(xid, false))
+		return;
+
+	undolog_undo(isCommit, xid);
+	undolog_remove_entry();
+}
+
+/*
+ * UndoLogCleanup() - On-recovery cleanup of undo log
+ *
+ * This function is called once before the redo process of recovery to remove
+ * files created by uncommitted transactions before the server crash. It is
+ * then called again after the redo process to clean up any leftover garbage
+ * after the redo process.
+ */
+void
+UndoLogCleanup(void)
+{
+	DIR		   *dirdesc;
+	struct dirent *de;
+	MemoryContext mcxt, outercxt;
+	ActiveULog	*next;
+
+	/*
+	 * Some memory allocation occurs during this process. Use a separate memory
+	 * context to avoid memory leaks.
+	 */
+	mcxt = AllocSetContextCreate(CurrentMemoryContext,
+								 "UndologContext",
+								 ALLOCSET_DEFAULT_SIZES);
+	outercxt = MemoryContextSwitchTo(mcxt);
+
+	undolog_close_file();
+
+	/* scan through all undo log files */
+	dirdesc = AllocateDir(SIMPLE_UNDOLOG_DIR);
+	while ((de = ReadDir(dirdesc, SIMPLE_UNDOLOG_DIR)) != NULL)
+	{
+		if (strspn(de->d_name, "01234567890abcdef") < strlen(de->d_name))
+			continue;
+
+		snprintf(ULogState.file_name, MAXPGPATH, "%s/%s",
+				 SIMPLE_UNDOLOG_DIR, de->d_name);
+
+		undolog_open_file_by_name(false);
+		undolog_process_ulogfile(false, true, outercxt);
+
+		/* Mark this log as crashed after prepared if not yet done */
+		if (ULogState.file_header.state == ULOG_FILE_PREPARED)
+		{
+			ULogState.file_header.state = ULOG_FILE_CRASH_AFTER_PREPARED;
+			undolog_write_file_header();
+			undolog_sync_file();
+		}
+		undolog_close_file();
+
+		/*
+		 * Do not remove ULOG files for prepared transactions.  We cannot
+		 * remove them and let recovery recreate them, because an existing file
+		 * for a prepared transaction may contain logs from before the latest
+		 * checkpoint, which would be lost in the newly created ulog files.
+		 */
+		if (ULogState.file_header.state == ULOG_FILE_DEFAULT)
+			undolog_remove_file();
+	}
+
+	MemoryContextSwitchTo(outercxt);
+	MemoryContextDelete(mcxt);
+
+	/* Clean up in-memory data */
+	for (ActiveULog *p = ULogState.active_ulogs ; p ; p = next)
+	{
+		next = p->next;
+		pfree(p);
+	}
+
+	ULogState.active_ulogs = ULogState.current_ulog = NULL;
+	ULogState.xid = InvalidTransactionId;
+	ULogState.file_name[0] = 0;
+}
+
+/*
+ * AtPrepare_SimpleUndoLog()
+ *
+ * Mark all undo logs as prepared.
+ *
+ * This mark is referenced by crash recovery to determine that each UNDO log
+ * file needs to be preserved.
+ */
+void
+AtPrepare_SimpleUndoLog(void)
+{
+	ActiveULog *p = ULogState.active_ulogs;
+	ActiveULog *prev = NULL;
+	ActiveULog *tmp;
+
+	Assert (!RecoveryInProgress());
+
+	while(p)
+	{
+		/* return no undo log exists for the transaction */
+		if (!undolog_open_file(p->xid, false))
+			ereport(ERROR,
+					errcode_for_file_access(),
+					errmsg("failed to open undolog file \"%s\": %m",
+						   ULogState.file_name));
+
+		undolog_load_file_header();
+		ULogState.file_header.state = ULOG_FILE_PREPARED;
+		undolog_write_file_header();
+		undolog_sync_file();
+		undolog_close_file();
+
+		if (prev)
+			prev->next = p->next;
+		else
+			ULogState.active_ulogs = p->next;
+
+		tmp = p->next;
+		pfree(p);
+		p = tmp;
+	}
+}
diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c
index e98286d768..bd31b77906 100644
--- a/src/backend/access/transam/twophase.c
+++ b/src/backend/access/transam/twophase.c
@@ -78,6 +78,7 @@
 
 #include "access/commit_ts.h"
 #include "access/htup_details.h"
+#include "access/simpleundolog.h"
 #include "access/subtrans.h"
 #include "access/transam.h"
 #include "access/twophase.h"
@@ -1587,6 +1588,8 @@ FinishPreparedTransaction(const char *gid, bool isCommit)
 									   abortstats,
 									   gid);
 
+	SimpleUndoLog_UndoByXid(isCommit, xid, hdr->nsubxacts, children);
+
 	ProcArrayRemove(proc, latestXid);
 
 	/*
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 0fe1630fca..19223d35de 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -24,6 +24,7 @@
 #include "access/multixact.h"
 #include "access/parallel.h"
 #include "access/subtrans.h"
+#include "access/simpleundolog.h"
 #include "access/transam.h"
 #include "access/twophase.h"
 #include "access/xact.h"
@@ -2418,6 +2419,14 @@ CommitTransaction(void)
 
 	AtEOXact_MultiXact();
 
+	/*
+	 * Drop storage files. This has to happen after buffer pins are dropped,
+	 * required by DropRelationBuffers(). This is mainly for a requirement by
+	 * abort-time cleanup, but place this at the same place for commit for
+	 * consistency.
+	 */
+	AtEOXact_SimpleUndoLog(true);
+
 	ResourceOwnerRelease(TopTransactionResourceOwner,
 						 RESOURCE_RELEASE_LOCKS,
 						 true, true);
@@ -2656,6 +2665,7 @@ PrepareTransaction(void)
 	AtPrepare_PgStat();
 	AtPrepare_MultiXact();
 	AtPrepare_RelationMap();
+	AtPrepare_SimpleUndoLog();
 
 	/*
 	 * Here is where we really truly prepare.
@@ -2953,6 +2963,13 @@ AbortTransaction(void)
 		AtEOXact_RelationCache(false);
 		AtEOXact_Inval(false);
 		AtEOXact_MultiXact();
+
+		/*
+		 * Drop storage files. This has to happen after buffer pins are
+		 * dropped, required by DropRelationBuffers().
+		 */
+		AtEOXact_SimpleUndoLog(false);
+
 		ResourceOwnerRelease(TopTransactionResourceOwner,
 							 RESOURCE_RELEASE_LOCKS,
 							 false, true);
@@ -5155,6 +5172,7 @@ CommitSubTransaction(void)
 							  s->parent->subTransactionId);
 	AtEOSubXact_Inval(true);
 	AtSubCommit_smgr();
+	AtEOSubXact_SimpleUndoLog(true);
 
 	/*
 	 * The only lock we actually release here is the subtransaction XID lock.
@@ -5336,6 +5354,7 @@ AbortSubTransaction(void)
 							 RESOURCE_RELEASE_AFTER_LOCKS,
 							 false, false);
 		AtSubAbort_smgr();
+		AtEOSubXact_SimpleUndoLog(false);
 
 		AtEOXact_GUC(false, s->gucNestLevel);
 		AtEOSubXact_SPI(false, s->subTransactionId);
@@ -6226,6 +6245,8 @@ xact_redo_commit(xl_xact_parsed_commit *parsed,
 		DropRelationFiles(parsed->xlocators, parsed->nrels, true);
 	}
 
+	SimpleUndoLog_UndoByXid(true, xid, parsed->nsubxacts, parsed->subxacts);
+
 	if (parsed->nstats > 0)
 	{
 		/* see equivalent call for relations above */
@@ -6337,6 +6358,8 @@ xact_redo_abort(xl_xact_parsed_abort *parsed, TransactionId xid,
 		DropRelationFiles(parsed->xlocators, parsed->nrels, true);
 	}
 
+	SimpleUndoLog_UndoByXid(false, xid, parsed->nsubxacts, parsed->subxacts);
+
 	if (parsed->nstats > 0)
 	{
 		/* see equivalent call for relations above */
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index b3d38cfaf8..2809ce5014 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -51,6 +51,7 @@
 #include "access/heaptoast.h"
 #include "access/multixact.h"
 #include "access/rewriteheap.h"
+#include "access/simpleundolog.h"
 #include "access/subtrans.h"
 #include "access/timeline.h"
 #include "access/transam.h"
@@ -5732,6 +5733,13 @@ StartupXLOG(void)
 		/* Check that the GUCs used to generate the WAL allow recovery */
 		CheckRequiredParameterValues();
 
+		/*
+		 * Perform undo processing. To prevent uncommitted INIT forks from
+		 * mistakenly deleting data, this must be done before resetting
+		 * UNLOGGED relations.
+		 */
+		UndoLogCleanup();
+
 		/*
 		 * We're in recovery, so unlogged relations may be trashed and must be
 		 * reset.  This should be done BEFORE allowing Hot Standby
@@ -5880,14 +5888,18 @@ StartupXLOG(void)
 	}
 
 	/*
-	 * Reset unlogged relations to the contents of their INIT fork. This is
-	 * done AFTER recovery is complete so as to include any unlogged relations
-	 * created during recovery, but BEFORE recovery is marked as having
-	 * completed successfully. Otherwise we'd not retry if any of the post
-	 * end-of-recovery steps fail.
+	 * Process undo logs left ater recovery to clean up uncommitted storage
+	 * files, including INIT forks, then reset unlogged relations to the
+	 * contents of their INIT fork. This is done AFTER recovery is complete so
+	 * as to include any file creations during recovery, but BEFORE recovery is
+	 * marked as having completed successfully. Otherwise we'd not retry if any
+	 * of the post end-of-recovery steps fail.
 	 */
 	if (InRecovery)
+	{
+		UndoLogCleanup();
 		ResetUnloggedRelations(UNLOGGED_RELATION_INIT);
+	}
 
 	/*
 	 * Pre-scan prepared transactions to find out the range of XIDs present.
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index f00718a015..1881ef06ff 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -307,6 +307,7 @@ void		setup_signals(void);
 void		setup_text_search(void);
 void		create_data_directory(void);
 void		create_xlog_or_symlink(void);
+void		create_ulog(void);
 void		warn_on_mount_point(int error);
 void		initialize_data_directory(void);
 
@@ -2958,6 +2959,21 @@ create_xlog_or_symlink(void)
 	free(subdirloc);
 }
 
+/* Create undo log directory */
+void
+create_ulog(void)
+{
+	char	   *subdirloc;
+
+	/* form name of the place for the subdirectory */
+	subdirloc = psprintf("%s/pg_ulog", pg_data);
+
+	if (mkdir(subdirloc, pg_dir_create_mode) < 0)
+		pg_fatal("could not create directory \"%s\": %m",
+				 subdirloc);
+
+	free(subdirloc);
+}
 
 void
 warn_on_mount_point(int error)
@@ -2992,6 +3008,7 @@ initialize_data_directory(void)
 	create_data_directory();
 
 	create_xlog_or_symlink();
+	create_ulog();
 
 	/* Create required subdirectories (other than pg_wal) */
 	printf(_("creating subdirectories ... "));
diff --git a/src/bin/pg_rewind/parsexlog.c b/src/bin/pg_rewind/parsexlog.c
index 22f7351fdc..525b98899f 100644
--- a/src/bin/pg_rewind/parsexlog.c
+++ b/src/bin/pg_rewind/parsexlog.c
@@ -28,7 +28,7 @@
  * RmgrNames is an array of the built-in resource manager names, to make error
  * messages a bit nicer.
  */
-#define PG_RMGR(symname,name,redo,desc,identify,startup,cleanup,mask,decode) \
+#define PG_RMGR(symname,name,redo,desc,identify,startup,cleanup,mask,decode,undo) \
   name,
 
 static const char *const RmgrNames[RM_MAX_ID + 1] = {
diff --git a/src/bin/pg_waldump/rmgrdesc.c b/src/bin/pg_waldump/rmgrdesc.c
index 6b8c17bb4c..a21009c5b8 100644
--- a/src/bin/pg_waldump/rmgrdesc.c
+++ b/src/bin/pg_waldump/rmgrdesc.c
@@ -32,7 +32,7 @@
 #include "storage/standbydefs.h"
 #include "utils/relmapper.h"
 
-#define PG_RMGR(symname,name,redo,desc,identify,startup,cleanup,mask,decode) \
+#define PG_RMGR(symname,name,redo,desc,identify,startup,cleanup,mask,decode,undo) \
 	{ name, desc, identify},
 
 static const RmgrDescData RmgrDescTable[RM_N_BUILTIN_IDS] = {
diff --git a/src/include/access/rmgr.h b/src/include/access/rmgr.h
index 3b6a497e1b..d705de9256 100644
--- a/src/include/access/rmgr.h
+++ b/src/include/access/rmgr.h
@@ -19,7 +19,7 @@ typedef uint8 RmgrId;
  * Note: RM_MAX_ID must fit in RmgrId; widening that type will affect the XLOG
  * file format.
  */
-#define PG_RMGR(symname,name,redo,desc,identify,startup,cleanup,mask,decode) \
+#define PG_RMGR(symname,name,redo,desc,identify,startup,cleanup,mask,decode,undo) \
 	symname,
 
 typedef enum RmgrIds
diff --git a/src/include/access/rmgrlist.h b/src/include/access/rmgrlist.h
index 78e6b908c6..9afb9bafcc 100644
--- a/src/include/access/rmgrlist.h
+++ b/src/include/access/rmgrlist.h
@@ -24,26 +24,26 @@
  * Changes to this list possibly need an XLOG_PAGE_MAGIC bump.
  */
 
-/* symbol name, textual name, redo, desc, identify, startup, cleanup, mask, decode */
-PG_RMGR(RM_XLOG_ID, "XLOG", xlog_redo, xlog_desc, xlog_identify, NULL, NULL, NULL, xlog_decode)
-PG_RMGR(RM_XACT_ID, "Transaction", xact_redo, xact_desc, xact_identify, NULL, NULL, NULL, xact_decode)
-PG_RMGR(RM_SMGR_ID, "Storage", smgr_redo, smgr_desc, smgr_identify, NULL, NULL, NULL, NULL)
-PG_RMGR(RM_CLOG_ID, "CLOG", clog_redo, clog_desc, clog_identify, NULL, NULL, NULL, NULL)
-PG_RMGR(RM_DBASE_ID, "Database", dbase_redo, dbase_desc, dbase_identify, NULL, NULL, NULL, NULL)
-PG_RMGR(RM_TBLSPC_ID, "Tablespace", tblspc_redo, tblspc_desc, tblspc_identify, NULL, NULL, NULL, NULL)
-PG_RMGR(RM_MULTIXACT_ID, "MultiXact", multixact_redo, multixact_desc, multixact_identify, NULL, NULL, NULL, NULL)
-PG_RMGR(RM_RELMAP_ID, "RelMap", relmap_redo, relmap_desc, relmap_identify, NULL, NULL, NULL, NULL)
-PG_RMGR(RM_STANDBY_ID, "Standby", standby_redo, standby_desc, standby_identify, NULL, NULL, NULL, standby_decode)
-PG_RMGR(RM_HEAP2_ID, "Heap2", heap2_redo, heap2_desc, heap2_identify, NULL, NULL, heap_mask, heap2_decode)
-PG_RMGR(RM_HEAP_ID, "Heap", heap_redo, heap_desc, heap_identify, NULL, NULL, heap_mask, heap_decode)
-PG_RMGR(RM_BTREE_ID, "Btree", btree_redo, btree_desc, btree_identify, btree_xlog_startup, btree_xlog_cleanup, btree_mask, NULL)
-PG_RMGR(RM_HASH_ID, "Hash", hash_redo, hash_desc, hash_identify, NULL, NULL, hash_mask, NULL)
-PG_RMGR(RM_GIN_ID, "Gin", gin_redo, gin_desc, gin_identify, gin_xlog_startup, gin_xlog_cleanup, gin_mask, NULL)
-PG_RMGR(RM_GIST_ID, "Gist", gist_redo, gist_desc, gist_identify, gist_xlog_startup, gist_xlog_cleanup, gist_mask, NULL)
-PG_RMGR(RM_SEQ_ID, "Sequence", seq_redo, seq_desc, seq_identify, NULL, NULL, seq_mask, NULL)
-PG_RMGR(RM_SPGIST_ID, "SPGist", spg_redo, spg_desc, spg_identify, spg_xlog_startup, spg_xlog_cleanup, spg_mask, NULL)
-PG_RMGR(RM_BRIN_ID, "BRIN", brin_redo, brin_desc, brin_identify, NULL, NULL, brin_mask, NULL)
-PG_RMGR(RM_COMMIT_TS_ID, "CommitTs", commit_ts_redo, commit_ts_desc, commit_ts_identify, NULL, NULL, NULL, NULL)
-PG_RMGR(RM_REPLORIGIN_ID, "ReplicationOrigin", replorigin_redo, replorigin_desc, replorigin_identify, NULL, NULL, NULL, NULL)
-PG_RMGR(RM_GENERIC_ID, "Generic", generic_redo, generic_desc, generic_identify, NULL, NULL, generic_mask, NULL)
-PG_RMGR(RM_LOGICALMSG_ID, "LogicalMessage", logicalmsg_redo, logicalmsg_desc, logicalmsg_identify, NULL, NULL, NULL, logicalmsg_decode)
+/* 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_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)
+PG_RMGR(RM_MULTIXACT_ID, "MultiXact", multixact_redo, multixact_desc, multixact_identify, NULL, NULL, NULL, NULL, NULL)
+PG_RMGR(RM_RELMAP_ID, "RelMap", relmap_redo, relmap_desc, relmap_identify, NULL, NULL, NULL, NULL, NULL)
+PG_RMGR(RM_STANDBY_ID, "Standby", standby_redo, standby_desc, standby_identify, NULL, NULL, NULL, standby_decode, NULL)
+PG_RMGR(RM_HEAP2_ID, "Heap2", heap2_redo, heap2_desc, heap2_identify, NULL, NULL, heap_mask, heap2_decode, NULL)
+PG_RMGR(RM_HEAP_ID, "Heap", heap_redo, heap_desc, heap_identify, NULL, NULL, heap_mask, heap_decode, NULL)
+PG_RMGR(RM_BTREE_ID, "Btree", btree_redo, btree_desc, btree_identify, btree_xlog_startup, btree_xlog_cleanup, btree_mask, NULL, NULL)
+PG_RMGR(RM_HASH_ID, "Hash", hash_redo, hash_desc, hash_identify, NULL, NULL, hash_mask, NULL, NULL)
+PG_RMGR(RM_GIN_ID, "Gin", gin_redo, gin_desc, gin_identify, gin_xlog_startup, gin_xlog_cleanup, gin_mask, NULL, NULL)
+PG_RMGR(RM_GIST_ID, "Gist", gist_redo, gist_desc, gist_identify, gist_xlog_startup, gist_xlog_cleanup, gist_mask, NULL, NULL)
+PG_RMGR(RM_SEQ_ID, "Sequence", seq_redo, seq_desc, seq_identify, NULL, NULL, seq_mask, NULL, NULL)
+PG_RMGR(RM_SPGIST_ID, "SPGist", spg_redo, spg_desc, spg_identify, spg_xlog_startup, spg_xlog_cleanup, spg_mask, NULL, NULL)
+PG_RMGR(RM_BRIN_ID, "BRIN", brin_redo, brin_desc, brin_identify, NULL, NULL, brin_mask, NULL, NULL)
+PG_RMGR(RM_COMMIT_TS_ID, "CommitTs", commit_ts_redo, commit_ts_desc, commit_ts_identify, NULL, NULL, NULL, NULL, NULL)
+PG_RMGR(RM_REPLORIGIN_ID, "ReplicationOrigin", replorigin_redo, replorigin_desc, replorigin_identify, NULL, NULL, NULL, NULL, NULL)
+PG_RMGR(RM_GENERIC_ID, "Generic", generic_redo, generic_desc, generic_identify, NULL, NULL, generic_mask, NULL, NULL)
+PG_RMGR(RM_LOGICALMSG_ID, "LogicalMessage", logicalmsg_redo, logicalmsg_desc, logicalmsg_identify, NULL, NULL, NULL, logicalmsg_decode, NULL)
diff --git a/src/include/access/simpleundolog.h b/src/include/access/simpleundolog.h
new file mode 100644
index 0000000000..93aacfb73c
--- /dev/null
+++ b/src/include/access/simpleundolog.h
@@ -0,0 +1,44 @@
+#ifndef SIMPLE_UNDOLOG_H
+#define SIMPLE_UNDOLOG_H
+
+#include "access/rmgr.h"
+#include "port/pg_crc32c.h"
+
+#define SIMPLE_UNDOLOG_DIR	"pg_ulog"
+
+typedef struct SimpleUndoLogRecord
+{
+	uint32		ul_tot_len;		/* total length of entire record */
+	pg_crc32c	ul_crc;			/* CRC for this record */
+	RmgrId		ul_rmid;		/* resource manager for this record */
+	uint8		ul_info;		/* record info */
+	/* rmgr-specific data follow, no padding */
+} SimpleUndoLogRecord;
+
+/* State of the undo log file */
+typedef enum UndoLogFileState
+{
+	ULOG_FILE_DEFAULT,				/* normal state */
+	ULOG_FILE_PREPARED,				/* ulog file is prepared */
+	ULOG_FILE_CRASH_AFTER_PREPARED	/* experienced a crash after prepared */
+} UndoLogFileState;
+
+/*
+ * The high 4 bits in ul_info may be used freely by rmgr. The lower 4 bits are
+ * not used for now.
+ */
+#define ULR_INFO_MASK			0x0F
+#define ULR_RMGR_INFO_MASK		0xF0
+
+extern void SimpleUndoLogWrite(RmgrId rmgr, uint8 info, void *data, int len);
+extern void SimpleUndoLogWriteRedo(RmgrId rmgr, uint8 info, void *data, int len,
+								   TransactionId xid);
+extern void SimpleUndoLogSetPreparedRedo(void);
+extern void AtEOXact_SimpleUndoLog(bool isCommit);
+extern void AtEOSubXact_SimpleUndoLog(bool isCommit);
+extern void AtPrepare_SimpleUndoLog(void);
+extern void SimpleUndoLog_UndoByXid(bool isCommit, TransactionId xid,
+									int nchildren, TransactionId *children);
+extern void UndoLogCleanup(void);
+
+#endif							/* SIMPLE_UNDOLOG_H */
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 9e951a9e6f..04f3eca550 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -25,6 +25,7 @@ AclResult
 AcquireSampleRowsFunc
 ActionList
 ActiveSnapshotElt
+ActiveULog
 AddForeignUpdateTargets_function
 AddrInfo
 AffixNode
@@ -2658,6 +2659,7 @@ SimplePtrListCell
 SimpleStats
 SimpleStringList
 SimpleStringListCell
+SimpleUndoLogRecord
 SingleBoundSortItem
 Size
 SkipPages
@@ -3026,8 +3028,11 @@ UINT
 ULARGE_INTEGER
 ULONG
 ULONG_PTR
+ULogStateData
 UV
 UVersionInfo
+UndoDescData
+UndoLogFileHeader
 UnicodeNormalizationForm
 UnicodeNormalizationQC
 Unique
-- 
2.43.5

