From a15c6026b06d4e2d7b7c1629da92bc2b78a64809 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Tue, 27 Aug 2024 10:44:46 +0900
Subject: [PATCH v34 16/16] In-place persistence change to LOGGED

---
 src/backend/commands/tablecmds.c              | 27 +++++++-----
 src/test/recovery/t/044_persistence_change.pl | 43 ++++++++++---------
 2 files changed, 40 insertions(+), 30 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 05364365a7..2f443fd4a6 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -5730,9 +5730,6 @@ RelationChangePersistence(AlteredTableInfo *tab, char persistence,
 			continue;
 		}
 
-		/* Currently, only allowing changes to UNLOGGED. */
-		Assert(!persistent);
-
 		RelationAssumePersistenceChange(r);
 
 		/* switch buffer persistence */
@@ -5740,11 +5737,22 @@ RelationChangePersistence(AlteredTableInfo *tab, char persistence,
 		log_smgrbufpersistence(srel->smgr_rlocator.locator, persistent);
 		SetRelationBuffersPersistence(srel, persistent);
 
-		/* then create the init fork */
-		is_index = (r->rd_rel->relkind == RELKIND_INDEX);
-		RelationCreateFork(srel, INIT_FORKNUM, !is_index, true);
-		if (is_index)
-			r->rd_indam->ambuildempty(r);
+		/* then create or drop the init fork */
+		if (persistent)
+			RelationDropInitFork(srel);
+		else
+		{
+			is_index = (r->rd_rel->relkind == RELKIND_INDEX);
+
+			/*
+			 * If it is an index, have access methods initialize the file. In
+			 * that case, WAL-logging is expected to performed by the
+			 * ambuildempty() method.
+			 */
+			RelationCreateFork(srel, INIT_FORKNUM, !is_index, true);
+			if (is_index)
+				r->rd_indam->ambuildempty(r);
+		}
 
 		/* Update catalog */
 		tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(reloid));
@@ -5903,8 +5911,7 @@ ATRewriteTables(AlterTableStmt *parsetree, List **wqueue, LOCKMODE lockmode,
 										 tab->relid,
 										 tab->rewrite);
 
-			if (tab->rewrite == AT_REWRITE_ALTER_PERSISTENCE &&
-				persistence == RELPERSISTENCE_UNLOGGED)
+			if (tab->rewrite == AT_REWRITE_ALTER_PERSISTENCE)
 			{
 				/* Make in-place persistence change. */
 				RelationChangePersistence(tab, persistence, lockmode);
diff --git a/src/test/recovery/t/044_persistence_change.pl b/src/test/recovery/t/044_persistence_change.pl
index 7f5ef84667..2356d61251 100644
--- a/src/test/recovery/t/044_persistence_change.pl
+++ b/src/test/recovery/t/044_persistence_change.pl
@@ -100,8 +100,8 @@ max_prepared_transactions = 2
 
 	# Check if SET LOGGED didn't change relfilenumbers and data survive a crash
 	my $relfilenodes3 = getrelfilenodes($node, \@relnames);
-	ok (!checkrelfilenodes($relfilenodes2, $relfilenodes3),
-		"crashed SET-LOGGED relations have sane relfilenodes transition");
+	ok (checkrelfilenodes($relfilenodes2, $relfilenodes3),
+	   "crashed SET-LOGGED relations have sane relfilenodes transition");
 
 	is ($node->safe_psql('postgres', "SELECT count(*) FROM t;"), 2000,
 		"crashed SET-LOGGED table does not lose data");
@@ -147,34 +147,35 @@ max_prepared_transactions = 2
 		"storages are reverted to logged state");
 
 	### Subtransactions
-	ok ($node->psql('postgres',
+	my ($ret, $stdout, $stderr) =
+	  $node->psql('postgres',
 				qq(
 				BEGIN;
 				ALTER TABLE t SET UNLOGGED;	-- committed
 				SAVEPOINT a;
-				ALTER TABLE t SET LOGGED;   -- aborted
+				ALTER TABLE t SET LOGGED;   -- ERROR
 				SAVEPOINT b;
 				ROLLBACK TO a;
 				COMMIT;
-				)) != 3,
-	   "command succeeds 1");
-
-	is ($node->safe_psql('postgres', "SELECT count(*) FROM t;"), 2000,
-	   "table data is not changed 1");
-	ok (check_storage_state(\&is_unlogged_state, $node, \@relnames),
-		"storages are changed to unlogged state");
+				));
+	ok ($stderr =~ m/persistence of this relation has been already changed/,
+		"errors out when double flip occured in a single transaction");
+	ok (check_storage_state(\&is_logged_state, $node, \@relnames),
+		"storages stay in logged state");
 
 	ok ($node->psql('postgres',
 				qq(
+				ALTER TABLE t SET UNLOGGED;
 				BEGIN;
+				SAVEPOINT a;
 				ALTER TABLE t SET LOGGED;	-- aborted
+				ROLLBACK TO a;
 				SAVEPOINT a;
-				ALTER TABLE t SET UNLOGGED;   -- aborted
-				SAVEPOINT b;
+				ALTER TABLE t SET LOGGED;   -- no error
 				RELEASE a;
 				ROLLBACK;
 				)) != 3,
-		"command succeeds 2");
+		"rolled-back persistence flip doesn't prevent subsequent flips");
 
 	is ($node->safe_psql('postgres', "SELECT count(*) FROM t;"), 2000,
 		"table data is not changed 2");
@@ -182,7 +183,7 @@ max_prepared_transactions = 2
 		"storages stay in unlogged state");
 
 	### Prepared transactions
-	my ($ret, $stdout, $stderr) =
+	($ret, $stdout, $stderr) =
 	  $node->psql('postgres',
 				  qq(
 					ALTER TABLE t SET LOGGED;
@@ -208,16 +209,17 @@ max_prepared_transactions = 2
 	ok ($stderr =~ m/cannot prepare transaction if persistence change/,
 		"errors out when persistence-flipped xact is prepared 2");
 	ok (check_storage_state(\&is_logged_state, $node, \@relnames),
-		"storages stay in logged state");
+		"storages stay in logged state 2");
 
 	### Error out DML
-	$node->psql('postgres',
+	ok($node->psql('postgres',
 				qq(
 					BEGIN;
-				  	ALTER TABLE t SET LOGGED;
+				  	ALTER TABLE t SET LOGGED;  -- no effect
 				  	INSERT INTO t VALUES(1); -- Succeeds
 				  	COMMIT;
-		  		  	));
+		  		  	)) != 3,
+	   "ineffective persistence change doesn't prevent DML");
 
 	($ret, $stdout, $stderr) =
 	  $node->psql('postgres',
@@ -233,7 +235,7 @@ max_prepared_transactions = 2
 				qq(
 				BEGIN;
 				SAVEPOINT a;
-	  			ALTER TABLE t SET UNLOGGED;
+				ALTER TABLE t SET LOGGED;
 				ROLLBACK TO a;
 				INSERT INTO t VALUES(3); -- Succeeds
 				COMMIT;
@@ -243,6 +245,7 @@ max_prepared_transactions = 2
 	($ret, $stdout, $stderr) =
 	  $node->psql('postgres',
 				  qq(
+				  ALTER TABLE t SET LOGGED;
 				  BEGIN;
 				  SAVEPOINT a;
 				  ALTER TABLE t SET UNLOGGED;
-- 
2.43.5

