Handle interleavings between CREATE DATABASE steps and base backup.
authorNoah Misch <noah@leadboat.com>
Thu, 1 Feb 2024 21:44:19 +0000 (13:44 -0800)
committerNoah Misch <noah@leadboat.com>
Thu, 1 Feb 2024 21:44:19 +0000 (13:44 -0800)
commitdf220714e50f2e924c57f4a8d9f578cf6c92a73f
treeffaaf7c3b27a459358f56f6406a60e7479f9dad9
parent272a7c3034925162deb4395bf925bcf60dc2d061
Handle interleavings between CREATE DATABASE steps and base backup.

Restoring a base backup taken in the middle of CreateDirAndVersionFile()
or write_relmap_file() would lose the function's effects.  The symptom
was absence of the database directory, PG_VERSION file, or
pg_filenode.map.  If missing the directory, recovery would fail.  Either
missing file would not fail recovery but would render the new database
unusable.  Fix CreateDirAndVersionFile() with the transam/README "action
first and then write a WAL entry" strategy.  That has a side benefit of
moving filesystem mutations out of a critical section, reducing the ways
to PANIC.  Fix the write_relmap_file() call with a lock acquisition, so
it interacts with checkpoints like non-CREATE DATABASE calls do.
Back-patch to v15, where commit 9c08aea6a3090a396be334cc58c511edab05776a
introduced STRATEGY=WAL_LOG and made it the default.

Discussion: https://postgr.es/m/20240130195003.0a.nmisch@google.com
src/backend/commands/dbcommands.c
src/backend/utils/cache/relmapper.c