Re: dbase_redo: XLOG_DBASE_CREATE_FILE_COPY replay empties live database

From: Jan Nidzwetzki <jan(at)planetscale(dot)com>
To: Michael Malis <malis(at)pgrust(dot)com>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: dbase_redo: XLOG_DBASE_CREATE_FILE_COPY replay empties live database
Date: 2026-12-04 16:37:32
Message-ID: c23f9aec-cfa1-4fb0-9585-bf2632056da2@planetscale.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Michael,

On 16.11.26 21:28, Michael Malis wrote:
> dbase_redo() removes the surviving
> destination directory with rmtree(dst_path, true), creates an empty
> placeholder at the missing src_path via recovery_create_dbdir(), and copies
> that empty directory to dst_path.

Thank you for your report and analysis. I faced the same issue with
pg_rewind and base backups, so I created the attached patches to address
all three cases.

For pg_rewind: if the new primary runs ALTER DATABASE ... SET
TABLESPACE, or CREATE DATABASE ... STRATEGY file_copy followed by DROP
DATABASE of the template after the divergence, the database ends up
empty on the rewound node. This happens without a crash or timing issue,
since the node replays the source's WAL from before these commands on a
data directory that already has their result. pg_rewind reports success,
and the rewound node logs no errors.

For base backups, the same issue occurs with a low-level backup if the
commands run between pg_backup_start() and copying the data directory.
Andres reported base backup problems with this replay code in 2015 [1],
and Tomas in 2024 [2]. As far as I can tell, neither thread resulted in
a fix.

> 5. Skipping rmtree(dst_path) only when src_path is ENOENT would not cover
> cases where src_path still exists on disk in a modified state

I agree, and with pg_rewind this case is easy to encounter: create a
database from a template with file_copy on the new primary, change the
template, then rewind. The copy on the rewound node will then have the
template's later rows. This series has two parts. Patches 0001 to 0003
are small and work independently of 0004. They fix the empty database
and can probably be back-patched. Patch 0004 also covers the modified
source, but since it changes how the copy is made, I think it should
only go to master.

- Patch 0001 advances minRecoveryPoint before replay of XLOG_DBASE_DROP
and XLOG_TBLSPC_DROP removes directories. This is a separate bug in the
same code: if a standby crashes between the XLOG_TBLSPC_DROP record and
its commit record, it can fail on restart with: PANIC: unexpected
directory entry "16384" found in pg_tblspc. Fujii-san highlighted this
issue in [3]. 49d9cfc68bf4 added an XLogFlush() to tblspc_redo(), which
was lost with the revert.

- Patch 0002 updates pg_rewind for databases created in the target after
divergence. Usually, such a database does not exist in the source, so
its files are removed. However, if the source has created a different
database with the same OID, the files exist on both sides, and pg_rewind
does not copy them in full. Currently, it relies on replaying the
source's CREATE DATABASE record to copy the database again. With patch
0003, replay keeps an existing directory, so pg_rewind now copies these
files in full. The WAL summarizer uses the same rule for incremental
backups.

- Patch 0003 keeps a non-empty target directory if the source directory
is missing and consistency has not been reached. This fixes the empty
database after a standby crash, after pg_rewind, and with a base backup
that contains the finished target. It also checks for the source before
removing the target, which addresses your point 3. One case changes
without improvement: a base backup that contains part of the target and
no source now keeps that part, whereas it previously got an empty
directory. With 0004 this cannot happen.

- Patch 0004 makes the copy process atomic. CREATE DATABASE, ALTER
DATABASE SET TABLESPACE, and replay now copy into a temporary directory
next to the target, then rename it when the copy is complete. This way,
a database directory is either absent or complete, so replay can keep an
existing one before consistency, regardless of whether the source is
present. The temporary directory is called pgsql_tmp_dbcopy_<oid>, so
base backups and pg_rewind skip it, and it is removed at startup.

Not fixed by this series: a base backup that copied the source after the
command but does not include the target. Replay still copies the source
in its later state, which is the limitation documented in backup.sgml.
If the backup contains neither directory, the target is still created empty.

Regarding the patch series, I have two questions:

- Is it generally acceptable to keep an existing database directory
before consistency? Replay of CREATE TABLESPACE stopped removing an
existing directory in 97ddda8a82ac, for a similar reason [4].

- Should the case where neither directory exists when the record is
replayed become an error once consistency is reached? I left that as it
is. Detecting it would need a remember-and-check mechanism like the one
in 49d9cfc68bf4, which was reverted [3].

[1] https://postgr.es/m/20150120152819.GC24381@alap3.anarazel.de
[2]
https://postgr.es/m/bf9bc731-5cd4-42b0-879b-57d6be2204ac@enterprisedb.com
[3]
https://postgr.es/m/CAEET0ZGx9AvioViLf7nbR_8tH9-=27DN5xWJ2P9-ROH16e4JUA@mail.gmail.com
[4]
https://postgr.es/m/CA+TgmoaLO9ncuwvr2nN-J4VEP5XyAcy=zKiHxQzBbFRxxGxm0w@mail.gmail.com

Best regards
Jan

--
Jan Nidzwetzki
PlanetScale

Attachment Content-Type Size
v1-0001-Advance-minRecoveryPoint-before-removing-director.patch text/plain 5.4 KB
v1-0002-pg_rewind-Copy-databases-created-in-the-target-in.patch text/plain 5.9 KB
v1-0003-Don-t-replace-a-database-with-a-copy-of-a-missing.patch text/plain 7.3 KB
v1-0004-Make-file-copies-of-databases-atomic-and-keep-the.patch text/plain 13.5 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message ZizhuanLiu X-MAN 2026-12-04 16:04:49 Re: BUG #19926: Incorrect join selectivity for <> with NULLs (related to BUG #17545)