From a7a4d899f2815e917b4a6a27537c45f63418aaae Mon Sep 17 00:00:00 2001
From: Karina Litskevich <litskevichkarina@gmail.com>
Date: Mon, 24 Apr 2023 11:24:21 +0300
Subject: [PATCH v1] Don't unlink init fork until the next checkpoint

---
 src/backend/storage/smgr/md.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c
index e982a8dd7f..6775b4a8e9 100644
--- a/src/backend/storage/smgr/md.c
+++ b/src/backend/storage/smgr/md.c
@@ -285,11 +285,12 @@ mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
  * Furthermore, it is important to remove the files from disk immediately,
  * because we may be about to reuse the same relfilenumber.
  *
- * All the above applies only to the relation's main fork; other forks can
- * just be removed immediately, since they are not needed to prevent the
- * relfilenumber from being recycled.  Also, we do not carefully
- * track whether other forks have been created or not, but just attempt to
- * unlink them unconditionally; so we should never complain about ENOENT.
+ * All the above applies only to the relation's main and init forks; other forks
+ * can just be removed immediately, since they are not needed to prevent the
+ * relfilenumber from being recycled.  Init fork should be kept as it is used
+ * on startup.  Also, we do not carefully track whether other forks have been
+ * created or not, but just attempt to unlink them unconditionally; so we should
+ * never complain about ENOENT.
  *
  * If isRedo is true, it's unsurprising for the relation to be already gone.
  * Also, we should remove the file immediately instead of queuing a request
@@ -352,8 +353,8 @@ mdunlinkfork(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
 	 * Truncate and then unlink the first segment, or just register a request
 	 * to unlink it later, as described in the comments for mdunlink().
 	 */
-	if (isRedo || IsBinaryUpgrade || forknum != MAIN_FORKNUM ||
-		RelFileLocatorBackendIsTemp(rlocator))
+	if ((forknum != MAIN_FORKNUM && forknum != INIT_FORKNUM) || isRedo ||
+		IsBinaryUpgrade || RelFileLocatorBackendIsTemp(rlocator))
 	{
 		if (!RelFileLocatorBackendIsTemp(rlocator))
 		{
@@ -1594,7 +1595,7 @@ mdunlinkfiletag(const FileTag *ftag, char *path)
 	char	   *p;
 
 	/* Compute the path. */
-	p = relpathperm(ftag->rlocator, MAIN_FORKNUM);
+	p = relpathperm(ftag->rlocator, ftag->forknum);
 	strlcpy(path, p, MAXPGPATH);
 	pfree(p);
 
-- 
2.25.1

