On Mon, Feb 20, 2023 at 04:43:22PM -0800, Nathan Bossart wrote:
> On Mon, Feb 20, 2023 at 05:02:01PM +0900, Michael Paquier wrote:
>> On Fri, Feb 17, 2023 at 02:35:30PM -0800, Nathan Bossart wrote:
>>> I'm happy to move this new test to wherever folks think it should go.  I'll
>>> look around to see if I can find a better place, too.
>> 
>> I think that src/test/recovery/ is the best fit, because this stresses
>> a code path for WAL replay on pg_class for the template db.  The name
>> is not specific enough, though, why not just using something like
>> 0NN_create_database.pl?
> 
> Okay.  I've renamed the test file as suggested in v3.

The test enforces a checkpoint after the table creation on the
template, so what about testing it also without a checkpoint, like the
extended version attached?  I have tweaked a few things in the test,
while on it.
--
Michael
From 4404a480bac81db7df3c1f7c9e49bbeab53f8a0b Mon Sep 17 00:00:00 2001
From: Michael Paquier <mich...@paquier.xyz>
Date: Tue, 21 Feb 2023 15:06:42 +0900
Subject: [PATCH] Fix persistence of database template's pg_class after a crash

---
 src/backend/commands/dbcommands.c          |  2 +-
 src/test/recovery/meson.build              |  1 +
 src/test/recovery/t/034_create_database.pl | 64 ++++++++++++++++++++++
 3 files changed, 66 insertions(+), 1 deletion(-)
 create mode 100644 src/test/recovery/t/034_create_database.pl

diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index ef05633bb0..a0259cc593 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -296,7 +296,7 @@ ScanSourceDatabasePgClass(Oid tbid, Oid dbid, char *srcpath)
 		CHECK_FOR_INTERRUPTS();
 
 		buf = ReadBufferWithoutRelcache(rlocator, MAIN_FORKNUM, blkno,
-										RBM_NORMAL, bstrategy, false);
+										RBM_NORMAL, bstrategy, true);
 
 		LockBuffer(buf, BUFFER_LOCK_SHARE);
 		page = BufferGetPage(buf);
diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build
index 209118a639..59465b97f3 100644
--- a/src/test/recovery/meson.build
+++ b/src/test/recovery/meson.build
@@ -39,6 +39,7 @@ tests += {
       't/031_recovery_conflict.pl',
       't/032_relfilenode_reuse.pl',
       't/033_replay_tsp_drops.pl',
+      't/034_create_database.pl',
     ],
   },
 }
diff --git a/src/test/recovery/t/034_create_database.pl b/src/test/recovery/t/034_create_database.pl
new file mode 100644
index 0000000000..38062662df
--- /dev/null
+++ b/src/test/recovery/t/034_create_database.pl
@@ -0,0 +1,64 @@
+
+# Copyright (c) 2023, PostgreSQL Global Development Group
+
+# Test WAL replay for CREATE DATABASE .. STRATEGY WAL_LOG.
+
+use strict;
+use warnings;
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my $node = PostgreSQL::Test::Cluster->new('node');
+$node->init;
+$node->start;
+
+# This checks that any DDLs run on the template database that modify pg_class
+# are persisted after creating a database from it using WAL_LOG strategy,
+# as a direct copy of the template database's pg_class is used in this case.
+my $db_template = "template1";
+
+# Create table.  It should persist on the template database.
+$node->safe_psql("postgres",
+	"CREATE DATABASE test_db_1 STRATEGY WAL_LOG TEMPLATE $db_template;");
+
+$node->safe_psql($db_template, "CREATE TABLE tab_db_after_create_1 (a INT);");
+$node->safe_psql("postgres",   "CHECKPOINT;");
+
+$node->stop('immediate');
+$node->start;
+my $result = $node->safe_psql($db_template,
+	"SELECT count(*) FROM pg_class WHERE relname LIKE 'tab_db_%';");
+is($result, "1",
+	"check that table exists on template after crash, with checkpoint");
+
+# The new database should have no tables.
+$result = $node->safe_psql("test_db_1",
+	"SELECT count(*) FROM pg_class WHERE relname LIKE 'tab_db_%';");
+is($result, "0",
+	"check that there are no tables from template on new database after crash"
+);
+
+# Again, but without a checkpoint.
+$node->safe_psql("postgres",
+	"CREATE DATABASE test_db_2 STRATEGY WAL_LOG TEMPLATE $db_template;");
+$node->safe_psql($db_template, "CREATE TABLE tab_db_after_create_2 (a INT);");
+
+$node->stop('immediate');
+$node->start;
+
+$result = $node->safe_psql($db_template,
+	"SELECT count(*) FROM pg_class WHERE relname LIKE 'tab_db_%';");
+is($result, "2",
+	"check that all tables exist on template after crash, without checkpoint"
+);
+
+# The table from the first case should also exist on the second, new,
+# database.
+$result = $node->safe_psql("test_db_2",
+	"SELECT count(*) FROM pg_class WHERE relname LIKE 'tab_db_%';");
+is($result, "1",
+	"check that the table from template exists on new database after crash, without checkpoint"
+);
+
+done_testing();
-- 
2.39.2

Attachment: signature.asc
Description: PGP signature

Reply via email to