Report
Cancelling a data checksums enable and re-requesting it lets two launchers run at once, and the old launcher's exit hook forces "on" straight to "off"
- Severity
- 45
- Blamed commit
- f19c0eccae9680f5785b11cdc58ef571998caec9 REL_19_STABLE, master
Summary
When a running pg_enable_data_checksums() is cancelled (the launcher gets SIGINT, e.g. pg_cancel_backend), the launcher returns from DataChecksumsWorkerLauncherMain() with the cluster still in "inprogress-on". Before returning it clears DataChecksumState->launcher_running. Its on_shmem_exit hook, launcher_exit(), then calls SetDataChecksumsOff(), which takes two fast checkpoints and two procsignal barrier waits.
During that cleanup the shared flag already says no launcher is running. A new pg_enable_data_checksums() starts a second launcher. Both launchers then change the checksum state at the same time. The new one moves the cluster to "inprogress-on" and then to "on". The old one, still in SetDataChecksumsOff(), writes "off" directly over "on", without the "inprogress-off" step.
Results, all reproducible:
- Every process that had absorbed "on" now gets an "off" barrier. on -> off is not in checksum_barriers[], so AbsorbDataChecksumsBarrier() raises "incorrect data checksum state 1 for target state 0". The bit is re-armed and the error repeats. The checkpointer, background writer, walwriter and autovacuum launcher log it once a second until the server is restarted. Idle client sessions get the error and are then closed with "FATAL: terminating connection because protocol synchronization was lost". The logical replication launcher and the new datachecksums launcher exit with code 1.
- The cancelled launcher never finishes. It waits forever in WaitForProcSignalBarrier() inside its exit hook. Its pg_stat_activity entry is already gone, so it is only visible in ps.
- The second enable request is lost. It logged "data checksums are now enabled", but the final state (SHOW data_checksums and pg_control) is "off".
This is not the out-of-order absorption problem in the near match below. Here only one checksum barrier is pending. The bad transition comes from two launchers changing the state at the same time, and no process has to be stalled.
Reproduction
Build with --enable-injection-points (used only to make the timing deterministic by holding the checkpointer). Run initdb without checksums, start the server, then:
CREATE EXTENSION injection_points;
CREATE TABLE big AS
SELECT g, repeat('x', 500) AS pad
FROM generate_series(1, 200000) g;
-- Optional: keep one more idle session open to see its FATAL.
-- 1. Slow enable, so the worker is still busy (launcher L1).
SELECT pg_enable_data_checksums(100, 1);
-- wait until a 'datachecksums worker' shows in pg_stat_activity
-- 2. Hold the checkpointer at the start of its next checkpoint.
SELECT injection_points_attach('create-checkpoint-initial', 'wait');
-- 3. Cancel L1. It aborts and returns. Its exit hook then runs
-- SetDataChecksumsOff() and waits for the held checkpoint.
SELECT pg_cancel_backend(pid) FROM pg_stat_activity
WHERE backend_type = 'datachecksums launcher';
-- SHOW data_checksums -> inprogress-off
-- 4. Ask to enable again. A second launcher (L2) starts, processes
-- all databases and publishes "on".
SELECT pg_enable_data_checksums(0, 100);
-- after a few seconds: SHOW data_checksums -> on
-- (L2 is waiting for its own checkpoint)
-- 5. Let the checkpointer go.
SELECT injection_points_detach('create-checkpoint-initial');
SELECT injection_points_wakeup('create-checkpoint-initial');
Expected: the last request wins. Checksums end up "on", or the second request is refused, and no process logs errors.
Actual:
datachecksums launcher LOG: data checksums processing was aborted in database "postgres"
datachecksums launcher LOG: enabling data checksums requested, starting data checksum calculation
...
walwriter ERROR: incorrect data checksum state 1 for target state 0
checkpointer ERROR: incorrect data checksum state 1 for target state 0
background writer ERROR: incorrect data checksum state 1 for target state 0
autovacuum launcher ERROR: incorrect data checksum state 1 for target state 0
client backend ERROR: incorrect data checksum state 1 for target state 0
datachecksums launcher ERROR: incorrect data checksum state 1 for target state 0
postmaster LOG: background worker "datachecksums launcher" (PID n) exited with exit code 1
(the aux-process errors repeat every second until restart)
The idle session receives the ERROR and then "FATAL: terminating connection because protocol synchronization was lost". SHOW data_checksums and pg_controldata both report off (version 0). The first launcher is still alive in ps but no longer listed in pg_stat_activity.
The injection point only widens the window. A stress run without injection points, doing enable / pg_cancel_backend(launcher) / re-enable with concurrent writes, hit the same overlap on its own. That run logged "cannot set data checksums to "on", current state is not "inprogress-on", disabling" and then "data checksums are now enabled" from the second launcher, which then left the cluster off.
Analysis
In datachecksum_state.c, the normal end of DataChecksumsWorkerLauncherMain() does
launcher_running = false;
DataChecksumState->launcher_running = false;
LWLockRelease(DataChecksumsWorkerLock);
and returns. The abort path (abort_requested -> goto done) reaches this point with the cluster still "inprogress-on". On exit, launcher_exit() runs
if (DataChecksumsInProgressOn())
SetDataChecksumsOff();
at a time when the shared launcher_running is already false. So StartDataChecksumsWorkerLauncher() in another backend registers a new launcher. The state is "inprogress-off", so the "already in desired state" shortcut does not apply. The new launcher passes its launcher_running check because the flag is clear.
The two launchers then interleave state changes in xlog.c. SetDataChecksumsOnInProgress() sets "inprogress-on" without checking the current state. SetDataChecksumsOff() chooses its two-step path from the state it read on entry. After RequestCheckpoint(...WAIT) and WaitForProcSignalBarrier() it writes PG_DATA_CHECKSUM_OFF without checking again, so it overwrites the "on" that the other launcher set in between. The "off" barrier is then illegal for every process already in "on". The ERROR re-arms the bit (see the near match for the retry behaviour), so the exiting launcher never finishes waiting.
Commit cffb4d8bd65e4c05ecd5697d09c4969467596ea5 ("Fix abort and launcher state resets in data checksums launcher") closed this window inside launcher_exit() itself by clearing the shared flag only at the end of the hook. The normal-return path in DataChecksumsWorkerLauncherMain() still clears the flag before the hook's SetDataChecksumsOff() runs. That early clear, and the abort path relying on launcher_exit() for the state reset, were both added by f19c0eccae9680f5785b11cdc58ef571998caec9 ("Online enabling and disabling of data checksums").
Possible directions: do the abort-path SetDataChecksumsOff() inside the main function before clearing launcher_running. Or leave launcher_running set until launcher_exit() is done, as the exit hook already does. Separately, SetDataChecksumsOff() could re-check the shared state before writing "off".
Notes
Tested on master at 11591f273e6a26fb55728b410538fa2627b19b28 (cassert, injection points). The repro is deterministic: 2 of 2 runs with the injection point, plus 1 natural occurrence in about 200 randomized stress trials.
Near match, different root cause (out-of-order absorption of two pending checksum barriers after a launcher is terminated while a backend is stalled; open): https://ai-annex.j-davis.com/report/01a1d1a1-5af9-7679-8eb0-c9bed5268846 The serialization patch attached there would not stop the second launcher from starting, or the exit hook from writing "off" over "on".
Back branches were not tested; REL_19_STABLE is listed because it contains the blamed commit.