Report
recovery_target_time and recovery_min_apply_delay do not stop before CREATE/DROP DATABASE, ALTER DATABASE SET TABLESPACE and CREATE/DROP TABLESPACE directory changes, which are replayed before the commit record (PITR to just before DROP DATABASE loses the database)
- Severity
- 75
- 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
Recovery targets and recovery_min_apply_delay act only on transaction commit/abort records, but several database and tablespace commands apply their file-system changes in WAL records that precede their commit record: CREATE DATABASE (FILE_COPY), DROP DATABASE, ALTER DATABASE ... SET TABLESPACE, CREATE TABLESPACE and DROP TABLESPACE. Recovery therefore replays those directory changes even when it stops, or is delayed, before the command's commit.
The worst case is DROP DATABASE. Point-in-time recovery with recovery_target_time set to just before an accidental DROP DATABASE replays the XLOG_DBASE_DROP record, which removes the database's directories, and then stops before the commit. The recovered cluster still has the pg_database row (the drop never committed), but it is marked invalid by the replayed in-place update that DROP DATABASE writes first, and the database's files are gone. Likewise, a delayed standby (recovery_min_apply_delay), often kept to guard against such mistakes, removes the files immediately and only delays the commit. Other commands leave orphaned directories or files behind.
Reproduction
- Primary with WAL archiving; take a base backup.
- CREATE DATABASE d; create and fill a table in d.
- Note a timestamp T, wait a moment, then DROP DATABASE d with no other transactions committing in between.
- Restore the backup with recovery_target_time = T (exclusive or inclusive) and recovery_target_action = 'promote'.
- pg_database still lists d, but connecting fails ("cannot connect to invalid database") and its directory under base/ is gone.
(Steps derived from code reading; not run for this report.)
With recovery_target_lsn or recovery_target_xid set before the XLOG_DBASE_DROP record, the database survives.
Analysis
recoveryStopsBefore() and recoveryStopsAfter() (xlogrecovery.c) consider a time or xid target only for RM_XACT_ID commit/abort records, and recoveryApplyDelay() only delays commit records. In dbcommands.c and tablespace.c the commands above emit XLOG_DBASE_CREATE_FILE_COPY, XLOG_DBASE_DROP, XLOG_TBLSPC_CREATE or XLOG_TBLSPC_DROP and perform the directory work before the transaction commits (they cannot run in a transaction block), and dbase_redo()/tblspc_redo() repeat that work immediately at replay. Nothing ties the timestamp of the commit to these earlier records, so a time target stops after them.
Long-standing: present since these commands were first WAL-logged in 50742aed68b7 (2004), the oldest commit identified, which postdates PITR itself.
This differs from the existing reports about minRecoveryPoint not being advanced by DROP DATABASE/DROP TABLESPACE replay and about ALTER DATABASE SET TABLESPACE replay before a restartpoint.
Fix status
A patch is under discussion on the list (CF 7272), not yet committed. It adds a WAL record marking a recovery boundary before these commands so that recovery targets and apply delay can stop there.
Branches: REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master