From 754019d478e59220054bf52d9bd2ce216939e8ea Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Fri, 26 Jul 2024 09:40:17 +0900
Subject: [PATCH v35 05/21] Remove function for retaining files on outer
 transaction aborts - step 1/2

The function RelationPreserveStorage() was introduced by commit
b9b8831ad6 in 2010 to retain storage files committed in a
subtransaction at the abort of outer transactions.  However, no use
case for this behavior has emerged since then. Moving the at-commit
removal of storage files from pendingDeletes to the UNDO log system
would require the UNDO system to handle cancellation of already logged
entries, adding unnecessary complexity with no benefit.  Therefore,
remove this feature.
---
 src/backend/catalog/storage.c       | 16 +++++++++++++++
 src/backend/utils/cache/relmapper.c | 30 +++++++++--------------------
 2 files changed, 25 insertions(+), 21 deletions(-)

diff --git a/src/backend/catalog/storage.c b/src/backend/catalog/storage.c
index f56b3cc0f23..bdbed9fba3c 100644
--- a/src/backend/catalog/storage.c
+++ b/src/backend/catalog/storage.c
@@ -254,6 +254,22 @@ RelationPreserveStorage(RelFileLocator rlocator, bool atCommit)
 	PendingRelDelete *prev;
 	PendingRelDelete *next;
 
+	/*
+	 * There is no caller that passes false for atCommit.
+	 *
+	 * The only caller that used to pass false for atCommit was
+	 * write_relmapper_file(), which intended to preserve committed storage
+	 * files for mapped relations if outer transactions aborted. However, this
+	 * has not occurred for more than ten years, and it is unlikely to be
+	 * needed in the future. The code to let storage files committed in
+	 * subtransactions survive after the top transaction aborts makes the UNDO
+	 * log system overly complex and inefficient. Therefore, this feature has
+	 * been removed.  The function signature is left unchanged to make this
+	 * change less invasive and to prevent the function from being mistakenly
+	 * called during transaction aborts.
+	 */
+	Assert (atCommit);
+
 	prev = NULL;
 	for (pending = pendingDeletes; pending != NULL; pending = next)
 	{
diff --git a/src/backend/utils/cache/relmapper.c b/src/backend/utils/cache/relmapper.c
index 48d344ae3ff..89072627120 100644
--- a/src/backend/utils/cache/relmapper.c
+++ b/src/backend/utils/cache/relmapper.c
@@ -1001,29 +1001,17 @@ write_relmap_file(RelMapFile *newmap, bool write_wal, bool send_sinval,
 		CacheInvalidateRelmap(dbid);
 
 	/*
-	 * Make sure that the files listed in the map are not deleted if the outer
-	 * transaction aborts.  This had better be within the critical section
-	 * too: it's not likely to fail, but if it did, we'd arrive at transaction
-	 * abort with the files still vulnerable.  PANICing will leave things in a
-	 * good state on-disk.
+	 * There was a call to RelationPreserveStorage(). It was originally
+	 * intended to ensure that storage files committed in subtransactions would
+	 * survive an outer transaction's abort. This was introduced by commit
+	 * b9b8831ad6 in 2010, but no use case has emerged since then. To simplify
+	 * the UNDO log system, this code has been removed. See
+	 * RelationMapUpdateMap() for more details.  Now, we only check that this
+	 * function is called in a top transaction.
 	 *
-	 * Note: we're cheating a little bit here by assuming that mapped files
-	 * are either in pg_global or the database's default tablespace.
+	 * During boot processing or recovery, the nest level will be zero.
 	 */
-	if (preserve_files)
-	{
-		int32		i;
-
-		for (i = 0; i < newmap->num_mappings; i++)
-		{
-			RelFileLocator rlocator;
-
-			rlocator.spcOid = tsid;
-			rlocator.dbOid = dbid;
-			rlocator.relNumber = newmap->mappings[i].mapfilenumber;
-			RelationPreserveStorage(rlocator, false);
-		}
-	}
+	Assert(!preserve_files || GetCurrentTransactionNestLevel() <= 1);
 
 	/* Critical section done */
 	if (write_wal)
-- 
2.43.5

