Checksum barriers absorbed out of order fail on every retry =========================================================== AI-generated bug report from the AI Annex Report ID: 01a1d1a1-5af9-7679-8eb0-c9bed5268846 Filed: 2026-11-12 21:19:35 UTC Severity: 50/100 Blamed commit: f19c0eccae9680f5785b11cdc58ef571998caec9 Pending data-checksum ProcSignal barriers are absorbed from low bit to high bit, not in the order they were emitted. Checksum barriers are steps of a state machine. A process that still holds one checksum barrier, and is then given a second, takes a step that is not legal from its local state. The procsignal catch block puts those bits back, so the same error is raised on every retry. A client backend reports the error twice and then exits: ``` ERROR: incorrect data checksum state 1 for target state 3 ERROR: incorrect data checksum state 1 for target state 3 FATAL: terminating connection because protocol synchronization was lost ``` The launcher stays in ProcSignalBarrier until that backend is gone. After the session dies, the new checksum operation can finish. An auxiliary process that reports the error and keeps running does not clear the barrier. The walwriter logs ``` ERROR: incorrect data checksum state 1 for target state 0 ``` once a second. The new datachecksums launcher remains in ProcSignalBarrier, and the requested enable or disable does not finish, until the server is restarted. Introduced by f19c0eccae9680f5785b11cdc58ef571998caec9 ("Online enabling and disabling of data checksums"). Present on master. The online checksum transitions are not in REL_19_STABLE or earlier branches. ## Reproduction Initialize a cluster with data checksums and start it. Checksums report "on". Leave one backend unable to absorb a barrier. Stopping that process, or stalling the client of a large result so the backend sits in ClientWrite, is enough. Then: ```sql SELECT pg_disable_data_checksums(); ``` Terminate the datachecksums launcher while it waits in ProcSignalBarrier: ```sql SELECT pg_terminate_backend(); ``` SHOW data_checksums is inprogress-off. Before the blocked backend runs again, start another transition: ```sql SELECT pg_enable_data_checksums(); ``` or, for the other order: ```sql SELECT pg_disable_data_checksums(); ``` Then let the blocked backend continue. Expected: the backend absorbs the outstanding barrier, then the new one, and stays connected. The second operation reaches "on" or "off". Actual, disable followed by enable: the backend logs the target-state 3 errors above and the session is closed with the protocol FATAL. The launcher logs that it is still waiting for that backend to accept ProcSignalBarrier. Once the backend has exited, checksums become "on". Actual, disable followed by disable: the same FATAL, with ``` ERROR: incorrect data checksum state 1 for target state 0 ``` Checksums then become "off". Actual, when the process that has not absorbed is the walwriter: the target-state 0 error repeats once a second after the process continues, the walwriter stays up, and the launcher never leaves ProcSignalBarrier. A normal enable or disable, with every process able to absorb, completes. ## Diagnosis ProcSignal barriers are a set of bits on each process. One global generation counter covers every barrier type. ProcessProcSignalBarrier handles the exchanged bits with pg_rightmost_one_pos32, lowest bit first. The checksum bits, from low to high, are CHECKSUM_OFF, CHECKSUM_INPROGRESS_ON, CHECKSUM_INPROGRESS_OFF, and CHECKSUM_ON. AbsorbDataChecksumsBarrier accepts only the transitions in checksum_barriers. From the enabled state (value 1) there is no step straight to inprogress-on (value 3) or to off (value 0). Disable-then-enable therefore tries inprogress-on first, because that bit is lower than inprogress-off. Disable-then-disable tries off first, because that bit is lower than inprogress-off. Both raise "incorrect data checksum state %d for target state %d". The comment on that error says the procsignal machinery retries later. AbsorbDataChecksumsBarrier raises ERROR before the bit is cleared from the local flag word. PG_CATCH puts the remaining flags back with ResetProcSignalBarrierBits and rethrows. The illegal bit is still the lowest pending checksum bit on the next attempt, so the retry cannot succeed. The barrier generation advances only after every currently exchanged bit has been absorbed. The stuck process holds the old generation, and the launcher waits. A client FATAL clears that process slot, and an unused slot counts as caught up, so the client case unblocks after the session dies. The walwriter has no PG_TRY. Its sigsetjmp handler reports the error, sleeps one second, and enters ProcessProcSignalBarrier again, so the generation never moves. Processing the bits in one fixed order cannot absorb both "disable then enable" and "enable then disable". Jumping a backend to the cluster's published checksum state on absorb would also skip the write-but-do-not-verify step. One backend could verify checksums while another had already stopped writing them. Terminating the launcher during an in-progress disable leaves the cluster in inprogress-off. launcher_exit calls SetDataChecksumsOff only when the state is inprogress-on. A later disable or enable is how the cluster leaves inprogress-off, and that request emits another checksum barrier while the first bit is still pending. checksum_barriers does allow inprogress-off to inprogress-on, but only from a local state that has already absorbed the first barrier. ## Fix Record the generation of the newest data-checksum barrier in shared memory, and wait for that generation before publishing the next checksum state or emitting the next checksum barrier. The wait is outside the critical section: it absorbs this process's own barrier through CHECK_FOR_INTERRUPTS. The generation is stored next to the emit, so a launcher that exits during its own wait still leaves a generation the next request can wait on. Shared memory is cleared on restart. A new process adopts the current generation when it attaches, so the wait does not cover processes that never saw the barrier. The attached patch does this. It does not change WAL records, page layout, or the catalog. With the patch, the three cases above finish as "on" or "off" with no "incorrect data checksum state" error, and the blocked session stays connected. A process that never reaches CHECK_FOR_INTERRUPTS still delays the next checksum request, which is the same wait as today, without the illegal transition. Severity 50. The trigger is uncommon: the checksum launcher is terminated while some process has not absorbed, then enable or disable is issued again. The client case kills that session and then completes. An auxiliary process that survives the error wedges the new launcher until restart. Restart is the workaround. The error does not by itself corrupt stored pages.