Report
Replay of DROP DATABASE and DROP TABLESPACE removes the directories without advancing minRecoveryPoint, so recovery restarted with an earlier target (e.g. a crashed delayed standby) finds the database or tablespace in the catalog but its files gone
- Severity
- 45
- Blamed commit
- 50742aed68b7a3a8d1a0c8ef8d970c97fc74dd9b
On 23 branches
REL8_0_STABLE, REL8_1_STABLE, REL8_2_STABLE, REL8_3_STABLE, REL8_4_STABLE, REL9_0_STABLE, REL9_1_STABLE, REL9_2_STABLE, REL9_3_STABLE, REL9_4_STABLE, REL9_5_STABLE, REL9_6_STABLE, REL_10_STABLE, REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
dbase_redo() for XLOG_DBASE_DROP calls rmtree() on the database directories, and tblspc_redo() for XLOG_TBLSPC_DROP removes the tablespace directory and its pg_tblspc symlink. Neither advances minRecoveryPoint (no XLogFlush() of the record) before the irreversible file removal. On the primary, dropdb() deliberately does XLogFlush(XactLastRecEnd) after the invalid-database marker "to guarantee the modification is durable before performing irreversible filesystem operations"; redo has no equivalent.
The transaction's commit record is force-synced, and its replay does XLogFlush(), which closes the window when replay runs straight through. But anything that delays replay of that commit leaves the files removed while minRecoveryPoint is still before the DROP. The most natural case is a delayed standby (recovery_min_apply_delay delays only commit records): an accidental DROP DATABASE on the primary removes the database directory on the delayed standby right away, even though the standby is meant to be the safety net for exactly this mistake. If that standby crashes (or is stopped with -m immediate) and is restarted with a recovery target before the DROP, recovery accepts the target as consistent and promotes; the database is present and valid in pg_database, but connecting fails because its directory is gone. The same window exists without a delay if the standby crashes between replaying the DROP record and the commit (rmtree of a large database takes a while).
For DROP TABLESPACE the tablespace is listed in pg_tablespace but unusable: CREATE TABLE ... TABLESPACE fails with "could not create directory".
Reproduction
Primary: wal_level=replica, autovacuum=off.
CREATE DATABASE d1;
\c d1
CREATE TABLE t(a int);
INSERT INTO t SELECT generate_series(1,1000);
\c postgres
CHECKPOINT;
Standby from pg_basebackup -R -X stream -c fast with hot_standby=on (off behaves the same) and recovery_min_apply_delay = 600s. Start it.
On the primary:
SELECT pg_current_wal_insert_lsn(); -- call it TGT
DROP DATABASE d1;
Wait until the standby has received the WAL (pg_stat_replication flush_lsn). The DROP record is replayed at once; base/<oid> of d1 is gone on the standby, while the commit waits for the apply delay. pg_controldata on the standby still shows "Minimum recovery ending location" from the base backup, before TGT.
Stop the standby with pg_ctl -m immediate (simulating a crash), add
recovery_target_lsn = '<TGT>'
recovery_target_inclusive = off
recovery_target_action = promote
and start it.
Expected: either d1 is intact, or recovery refuses the target with "requested recovery stop point is before consistent recovery point".
Actual:
LOG: consistent recovery state reached at 0/02000138
LOG: recovery stopping before WAL location (LSN) "0/03000028"
SELECT datname, datconnlimit FROM pg_database WHERE datname='d1';
d1 | -1
$ psql -d d1
FATAL: database "d1" does not exist
DETAIL: The database subdirectory "base/16384" is missing.
Control: pausing replay on the standby before the DROP gives the same recovery and "SELECT count(*) FROM t" in d1 returns 1000. Without the apply delay the restart is refused as expected, because the replayed force-synced commit advanced minRecoveryPoint.
DROP TABLESPACE: same steps with an empty tablespace (pg_basebackup -T for the location) and DROP TABLESPACE ts instead of DROP DATABASE. After the restart ts is listed in pg_tablespace, pg_tblspc/<oid> is missing, and
CREATE TABLE tx(a int) TABLESPACE ts;
ERROR: could not create directory
"pg_tblspc/16384/PG_20_202611121/5": No such file or directory
A plain pg_ctl -m fast stop of the delayed standby before the restart does not reproduce (the restart is refused); a crash is needed.
Analysis
src/backend/commands/dbcommands.c, dbase_redo(), XLOG_DBASE_DROP: drops slots and buffers, then rmtree() of each database directory, with no XLogFlush(record->EndRecPtr) first. In src/backend/commands/tablespace.c, tblspc_redo(), XLOG_TBLSPC_DROP calls destroy_tablespace_directories() likewise. The force-sync commit (ForceSyncCommit() in dropdb() and DropTableSpace()) makes xact_redo_commit() call XLogFlush(), but only once the commit itself is replayed, which recovery_min_apply_delay holds back. Compare xact_redo_commit(), which flushes before DropRelationFiles() for the same reason, and the primary-side XLogFlush() in dropdb().
Same class as the replayed COMMIT/ROLLBACK PREPARED unlinking the pg_twophase file and the commit_ts page write: an irreversible non-buffer change during redo that minRecoveryPoint does not cover.
Blame is the commit that WAL-logged DROP DATABASE / DROP TABLESPACE and added the removal in redo; the redo path has never advanced minRecoveryPoint before removing files.
Notes
Tested on master 4280abf88409d09e5828c4642219df053220a650 (cassert build). Database: 5/5 runs with immediate stop (hot_standby on 4, off 1). Tablespace: 1/1. The same redo paths, with no XLogFlush() before the removal, are present on REL_14_STABLE and later. Back branches were not tested.
Related, not duplicates: https://ai-annex.j-davis.com/report/01a1f157-7641-715d-82db-bcf87b553485 (pg_twophase unlink) and https://ai-annex.j-davis.com/report/01a1f157-fc11-7f46-af80-488b7b8439ae (commit_ts page write).