Report
pg_enable_data_checksums() fails with "cannot enable data checksums in a cluster with invalid database" when a DROP DATABASE is merely in progress, because the invalid-database pre-check does not wait out an in-flight drop
- Severity
- 20
- Blamed commit
- 51f55b13a4d3621244b3645a506b006decca0ec2 master
Summary
pg_enable_data_checksums() first scans pg_database and errors out if any database is marked invalid (datconnlimit = -2). DROP DATABASE marks the database invalid with an in-place update, flushes that WAL, and only then removes the files and deletes the pg_database row at commit. The pre-check does not take any lock on the database, so a DROP DATABASE that is in progress at that moment makes the enable request fail with
ERROR: cannot enable data checksums in a cluster with invalid
database "victim"
HINT: Use DROP DATABASE to drop invalid databases.
even though the drop then completes normally and the database is gone. The hint tells the user to drop a database that is already being dropped.
The same commit taught the launcher's DatabaseExists() to take a lock on the database precisely so that a half-finished DROP DATABASE is not mistaken for a database that is there to stay; the up-front check in pg_enable_data_checksums() did not get the same treatment.
Reproduction
Deterministic, with a build configured with injection points:
-- cluster initialized with --no-data-checksums
CREATE EXTENSION injection_points;
CREATE DATABASE victim;
SELECT injection_points_attach('dropdb-after-invalid-marker',
'wait');
-- session 2 (blocks after the invalid marker is written)
DROP DATABASE victim;
-- session 1
SELECT datname, datconnlimit FROM pg_database
WHERE datname = 'victim';
-- victim | -2
SELECT pg_enable_data_checksums();
-- ERROR: cannot enable data checksums in a cluster with invalid
-- database "victim"
-- HINT: Use DROP DATABASE to drop invalid databases.
SELECT injection_points_wakeup('dropdb-after-invalid-marker');
SELECT injection_points_detach('dropdb-after-invalid-marker');
-- session 2: DROP DATABASE completes; "victim" no longer exists
Without injection points, the same error is returned when pg_enable_data_checksums() is called while another session runs a loop of CREATE DATABASE x; DROP DATABASE x WITH (FORCE); on a cluster with data checksums off. In testing it happened on the first attempt in both of two runs.
Expected: the enable request either waits for the in-flight drop to finish (as DatabaseExists() does) or ignores a database whose drop is in progress, and checksum enabling proceeds.
Actual: the request fails with a misleading error and hint. Retrying after the drop finishes succeeds.
Analysis
src/backend/postmaster/datachecksum_state.c, pg_enable_data_checksums() -> ErrorOnInvalidDatabases(): a plain catalog scan that errors on database_is_invalid_form(). dropdb() in dbcommands.c sets datconnlimit = DATCONNLIMIT_INVALID_DB in place (visible immediately, independent of the dropping transaction's outcome), flushes WAL, and then deletes the row, drops buffers, requests a checkpoint and removes the directories before committing. For that whole window the database looks invalid to ErrorOnInvalidDatabases().
dropdb() holds AccessExclusiveLock on the database object for this whole window. DatabaseExists() in the same file takes a conflicting lock "to wait out a drop which is in flight"; ErrorOnInvalidDatabases() takes none.
The check and DatabaseExists()'s locking were both added by 51f55b13a4d3621244b3645a506b006decca0ec2 ("Handle invalid and dropped databases during checksum enable").
Notes
Tested on master 7451263c13c9ddf3758492a74f8503298b919b8f. A database left invalid by an interrupted DROP DATABASE (the case the check targets) is still reported correctly; only a drop that is in progress and will succeed is affected.
Branches
master only; the blamed commit 51f55b13a4d3621244b3645a506b006decca0ec2 is not in any stable branch.
Related
- 01a1eca3-8ff5-79b7-85eb-1e5cf26c5411: pg_enable_data_checksums() right after pg_disable_data_checksums() can fail the launcher.
- 01a1eb8d-b8f5-7103-89b9-50e2eaf9919c: cancelling and re-requesting an enable runs two launchers.