From a78690b05849bb9231db5e67de78f524dbe7ae1b Mon Sep 17 00:00:00 2001 From: Jan Nidzwetzki Date: Fri, 4 Dec 2026 14:09:11 +0100 Subject: [PATCH v1 1/4] Advance minRecoveryPoint before removing directories during redo Replay of XLOG_DBASE_DROP and XLOG_TBLSPC_DROP removes directories directly, not through the buffer manager, so nothing advances the minimum recovery point past the record before the directories are gone. For DROP DATABASE and DROP TABLESPACE, replay of the commit record does that, but a standby can crash before it gets there. The next recovery can then reach consistency before it has replayed the drop record a second time. After DROP TABLESPACE, this can fail with PANIC: unexpected directory entry "16384" found in pg_tblspc when replay of an earlier CREATE DATABASE record has recreated the tablespace directory, because the record that removes it has not been replayed yet. Call XLogFlush() before removing the directories, just as xact_redo_commit() and smgr_redo() do before removing or truncating relation files. Discussion: https://postgr.es/m/CANVD+MB_JL5ydxD8NUUGJsdEGB1+nW56-PnS8QTfUtW+w_V9+A@mail.gmail.com --- src/backend/commands/dbcommands.c | 6 +++ src/backend/commands/tablespace.c | 8 +++ src/test/recovery/t/033_replay_tsp_drops.pl | 59 +++++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index 15b93512e4b..ab9b97ac99d 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -3487,6 +3487,12 @@ dbase_redo(XLogReaderState *record) /* Close all smgr fds in all backends. */ WaitForProcSignalBarrier(EmitProcSignalBarrier(PROCSIGNAL_BARRIER_SMGRRELEASE)); + /* + * Update the minimum recovery point to cover this record before + * removing the files, for the same reason as in xact_redo_commit(). + */ + XLogFlush(record->EndRecPtr); + for (i = 0; i < xlrec->ntablespaces; i++) { dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_ids[i]); diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c index 7c40176b7b9..3f9a3ea79dd 100644 --- a/src/backend/commands/tablespace.c +++ b/src/backend/commands/tablespace.c @@ -55,6 +55,7 @@ #include "access/reloptions.h" #include "access/tableam.h" #include "access/xact.h" +#include "access/xlog.h" #include "access/xloginsert.h" #include "access/xlogutils.h" #include "catalog/binary_upgrade.h" @@ -1555,6 +1556,13 @@ tblspc_redo(XLogReaderState *record) /* Close all smgr fds in all backends. */ WaitForProcSignalBarrier(EmitProcSignalBarrier(PROCSIGNAL_BARRIER_SMGRRELEASE)); + /* + * Update the minimum recovery point to cover this record before + * removing the directories, for the same reason as in + * xact_redo_commit(). + */ + XLogFlush(record->EndRecPtr); + /* * If we issued a WAL record for a drop tablespace it implies that * there were no files in it at all when the DROP was done. That means diff --git a/src/test/recovery/t/033_replay_tsp_drops.pl b/src/test/recovery/t/033_replay_tsp_drops.pl index 93c89550511..880d9c859b8 100644 --- a/src/test/recovery/t/033_replay_tsp_drops.pl +++ b/src/test/recovery/t/033_replay_tsp_drops.pl @@ -142,4 +142,63 @@ while ($max_attempts-- >= 0) } ok($max_attempts > 0, "invalid directory creation is detected"); +# Ensure that a standby can restart if it crashes between the replay of +# XLOG_TBLSPC_DROP and the commit record of DROP TABLESPACE. This needs a +# tablespace outside the data directory, as the check for leftover +# directories in pg_tblspc is only a warning with in-place tablespaces. + +my $tempdir = PostgreSQL::Test::Utils::tempdir_short; +mkdir("$tempdir/ts_primary"); + +$node_primary = PostgreSQL::Test::Cluster->new('primary3'); +$node_primary->init(allows_streaming => 1); +$node_primary->start; +$node_primary->safe_psql('postgres', + "CREATE TABLESPACE ts1 LOCATION '$tempdir/ts_primary'"); +$tspoid = $node_primary->safe_psql('postgres', + "SELECT oid FROM pg_tablespace WHERE spcname = 'ts1'"); +$node_primary->backup($backup_name, + backup_options => + ["--tablespace-mapping=$tempdir/ts_primary=$tempdir/ts_backup"]); + +$node_standby = PostgreSQL::Test::Cluster->new('standby4'); +$node_standby->init_from_backup( + $node_primary, $backup_name, + has_streaming => 1, + tablespace_map => { $tspoid => "$tempdir/ts_standby" }); + +# Find the record to stop at. +my $start_lsn = $node_primary->lsn('insert'); +$node_primary->safe_psql( + 'postgres', q[ + CREATE DATABASE db1 WITH TABLESPACE ts1; + DROP DATABASE db1; + DROP TABLESPACE ts1; + ]); +my ($stdout, $stderr) = run_command( + [ + 'pg_waldump', + '--path' => $node_primary->data_dir . '/pg_wal', + '--rmgr' => 'Tablespace', + '--start' => $start_lsn, + '--end' => $node_primary->lsn('flush'), + ]); +$stdout =~ /lsn: ([0-9A-F]+\/[0-9A-F]+), .*desc: DROP / + or die "could not find the XLOG_TBLSPC_DROP record"; + +# Replay up to this record, and crash. +$node_standby->append_conf( + 'postgresql.conf', qq[ +recovery_target_lsn = '$1' +recovery_target_action = 'pause' +]); +$node_standby->start; +$node_standby->poll_query_until('postgres', + "SELECT pg_get_wal_replay_pause_state() = 'paused'"); +$node_standby->stop('immediate'); + +$node_standby->append_conf('postgresql.conf', "recovery_target_lsn = ''"); +is($node_standby->start(fail_ok => 1), + 1, "standby node started after crash in DROP TABLESPACE"); + done_testing(); -- 2.47.3