From c1c37cfa13240b59210bc2165ee869db3b45505e Mon Sep 17 00:00:00 2001 From: Jan Nidzwetzki Date: Fri, 4 Dec 2026 14:13:44 +0100 Subject: [PATCH v1 3/4] Don't replace a database with a copy of a missing source in redo Replay of XLOG_DBASE_CREATE_FILE_COPY removes the target directory and copies the source directory again. If the source is missing, replay creates an empty directory in its place and copies that, assuming that later records drop both directories. That holds for the source, which is missing because a later record drops it. It does not hold for the target, which can be a database that is still in use. It is then left as an empty directory, and connections fail with FATAL: "base/16398" is not a valid data directory DETAIL: File "base/16398/PG_VERSION" is missing. This occurs when recovery starts before the record on a data directory where the source has already been dropped. That can happen on a standby that crashes before its next restartpoint, after pg_rewind, or with a base backup taken while the commands ran. The commands are CREATE DATABASE with STRATEGY file_copy followed by DROP DATABASE of the template, and ALTER DATABASE SET TABLESPACE. If the source is missing before consistency is reached, keep a non-empty target, since it is the only copy of the database. Also check for the source before removing the target, so that the PANIC for a missing source after consistency leaves the target alone. This does not help if the source still exists but has changed since the record was written. Reported-by: Michael Malis Reported-by: Tom Pang Discussion: https://postgr.es/m/CANVD+MB_JL5ydxD8NUUGJsdEGB1+nW56-PnS8QTfUtW+w_V9+A@mail.gmail.com --- src/backend/commands/dbcommands.c | 36 +++++++++++++++------ src/bin/pg_rewind/t/002_databases.pl | 25 ++++++++++++++ src/test/recovery/t/033_replay_tsp_drops.pl | 11 ++++++- 3 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index ab9b97ac99d..5bfd72b22ec 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -3370,6 +3370,33 @@ dbase_redo(XLogReaderState *record) src_path = GetDatabasePath(xlrec->src_db_id, xlrec->src_tablespace_id); dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id); + /* + * The source directory can be missing if a later record has dropped + * it. Create an empty one so that copydir below doesn't fail. The + * directory will be dropped soon by recovery. Do this before + * removing the target, as it fails once consistency is reached. + */ + if (stat(src_path, &st) < 0 && errno == ENOENT) + { + recovery_create_dbdir(src_path, false); + + /* + * A target that exists is then the only copy of the database. + * Keep it instead of replacing it with an empty directory. + */ + if (!reachedConsistency && + stat(dst_path, &st) == 0 && S_ISDIR(st.st_mode) && + !directory_is_empty(dst_path)) + { + elog(DEBUG1, + "keeping database directory \"%s\" because source directory \"%s\" was missing", + dst_path, src_path); + pfree(src_path); + pfree(dst_path); + return; + } + } + /* * Our theory for replaying a CREATE is to forcibly drop the target * subdirectory if present, then re-copy the source data. This may be @@ -3402,15 +3429,6 @@ dbase_redo(XLogReaderState *record) } pfree(parent_path); - /* - * There's a case where the copy source directory is missing for the - * same reason above. Create the empty source directory so that - * copydir below doesn't fail. The directory will be dropped soon by - * recovery. - */ - if (stat(src_path, &st) < 0 && errno == ENOENT) - recovery_create_dbdir(src_path, false); - /* * Force dirty buffers out to disk, to ensure source database is * up-to-date for the copy. diff --git a/src/bin/pg_rewind/t/002_databases.pl b/src/bin/pg_rewind/t/002_databases.pl index 0740dfd1c84..b25e7621222 100644 --- a/src/bin/pg_rewind/t/002_databases.pl +++ b/src/bin/pg_rewind/t/002_databases.pl @@ -22,6 +22,13 @@ sub run_test primary_psql('CREATE DATABASE inprimary'); primary_psql('CREATE TABLE inprimary_tab (a int)', 'inprimary'); + # Databases for the file-copy operations below. + primary_psql('CREATE DATABASE tmpl'); + primary_psql('CREATE TABLE tmpl_tab AS SELECT 1 AS a', 'tmpl'); + primary_psql('CREATE DATABASE moved'); + primary_psql('CREATE TABLE moved_tab AS SELECT 1 AS a', 'moved'); + primary_psql("CREATE TABLESPACE ts LOCATION ''"); + RewindTest::create_standby($test_mode); # Create another database with another table, the creation is @@ -40,6 +47,17 @@ sub run_test standby_psql('CREATE TABLE standby_promotion_tab (a int)', 'standby_afterpromotion'); + # File-copy operations in the new primary. Their source directories are + # gone afterwards, so replay in the rewound node cannot repeat the copies. + # The old primary has a different database with the same OID. + primary_psql('UPDATE tmpl_tab SET a = 2', 'tmpl'); + primary_psql( + 'CREATE DATABASE cloned OID 99999 TEMPLATE tmpl STRATEGY file_copy'); + standby_psql( + 'CREATE DATABASE cloned OID 99999 TEMPLATE tmpl STRATEGY file_copy'); + standby_psql('DROP DATABASE tmpl'); + standby_psql('ALTER DATABASE moved SET TABLESPACE ts'); + # The clusters are now diverged. RewindTest::run_pg_rewind($test_mode); @@ -48,7 +66,9 @@ sub run_test check_query( 'SELECT datname FROM pg_database ORDER BY 1', qq(beforepromotion +cloned inprimary +moved postgres standby_afterpromotion template0 @@ -56,6 +76,11 @@ template1 ), 'database names'); + is($node_primary->safe_psql('cloned', 'SELECT a FROM tmpl_tab'), + '1', 'cloned database is intact'); + is($node_primary->safe_psql('moved', 'SELECT a FROM moved_tab'), + '1', 'moved database is intact'); + # Permissions on PGDATA should have group permissions SKIP: { diff --git a/src/test/recovery/t/033_replay_tsp_drops.pl b/src/test/recovery/t/033_replay_tsp_drops.pl index 880d9c859b8..36bc6df3bb7 100644 --- a/src/test/recovery/t/033_replay_tsp_drops.pl +++ b/src/test/recovery/t/033_replay_tsp_drops.pl @@ -29,6 +29,8 @@ sub test_tablespace CREATE DATABASE template_db IS_TEMPLATE = true; SELECT pg_create_physical_replication_slot('slot', true); ]); + $node_primary->safe_psql('template_db', + 'CREATE TABLE tt AS SELECT 1 AS a'); my $backup_name = 'my_backup'; $node_primary->backup($backup_name); @@ -63,7 +65,7 @@ sub test_tablespace $query =~ s//$strategy/g; $node_primary->safe_psql('postgres', $query); - $node_primary->wait_for_catchup($node_standby, 'write'); + $node_primary->wait_for_catchup($node_standby, 'replay'); # show "create missing directory" log message $node_standby->safe_psql('postgres', @@ -72,6 +74,13 @@ sub test_tablespace # Should restart ignoring directory creation error. is($node_standby->start(fail_ok => 1), 1, "standby node started for $strategy"); + + # The databases that were not dropped must have survived the replay. + $node_standby->poll_query_until('postgres', 'SELECT 1', '1'); + is($node_standby->safe_psql('newdb', 'SELECT a FROM tt'), + '1', "copied database is intact for $strategy"); + is($node_standby->safe_psql('moveme_db', 'SELECT 1'), + '1', "moved database is intact for $strategy"); $node_standby->stop('immediate'); } -- 2.47.3