| From: | Tomas Vondra <tomas(at)vondra(dot)me> |
|---|---|
| To: | Daniel Gustafsson <daniel(at)yesql(dot)se>, Alexander Lakhin <exclusion(at)gmail(dot)com> |
| Cc: | Andres Freund <andres(at)anarazel(dot)de>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, Bernd Helmle <mailings(at)oopsware(dot)de>, Michael Paquier <michael(at)paquier(dot)xyz>, Michael Banck <mbanck(at)gmx(dot)net>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Re: Changing the state of data checksums in a running cluster |
| Date: | 2026-11-21 12:37:32 |
| Message-ID: | 0084a1a8-281f-4482-94f1-08151bbb2db1@vondra.me |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
Apologies for pinging this thread after 2+ months, but I recalled one
thing that puzzled me when reviewing the online checksums patches. I
might have mentioned it off list, but I don't see it in the thread.
Why do we actually need the DELAY_CHKPT_START bits?
START_CRIT_SECTION();
MyProc->delayChkptFlags |= DELAY_CHKPT_START;
... WAL-log the new checksum state
... update control file
... emit barrier
MyProc->delayChkptFlags &= ~DELAY_CHKPT_START;
END_CRIT_SECTION();
But do we actually need it? For a while I thought this is meant to
protect against cases like this:
1. checksums launcher: write XLOG2_CHECKSUMS
2. checkpointer: writes REDO with an old checksum_version value
3. checkpointer: does the flushing etc.
4. checkpointer: writes checkpoint record etc.
5. checksums launcher completes (updates ControlFile ....)
6. ... backends start writing pages without checksums or whatever
If we crash and start from the REDO, we might end up with the "stale"
checksum_version. Which I guess might be annoying (when going back to
"odd") or bad (when going back to "on").
But AFAIK this is impossible thanks to DataChecksumTransitionLock, which
is adding an interlock between exactly these two places (writing the
XLOG2_CHECKSUMS record and REDO record). And indeed, the comments in
those two places confirm that.
So I still don't understand how adding a "wait" between steps 3 and 4
would make any difference? Why do we need to add such wait?
But there's a much bigger issue, completely neutering all of this. The
checkpointer relies on GetVirtualXIDsDelayingChkpt(), which only waits
for backends with valid virtual XID.
But SetDataChecksum functions run in a datachecksum launcher, which has
no transaction. So the checkpointer won't ever wait for any of this
anyway! As for the purpose / need for DELAY_CHKPT_START, proc.h says:
* Setting DELAY_CHKPT_START prevents the system from moving from phase
* 1 to phase 2. This is useful when we are performing a WAL-logged
* modification of data that will be flushed to disk in phase 2. By
* setting this flag before writing WAL and clearing it after we've both
* written WAL and performed the corresponding modification, we ensure
* that if the WAL record is inserted prior to the new redo point, the
* corresponding data changes will also be flushed to disk before the
* checkpoint can complete. (In the extremely common case where the data
* being modified is in shared buffers and we acquire an exclusive
* content lock and MarkBufferDirty() on the relevant buffers before
* writing WAL, this mechanism is not needed, because phase 2 will block
* until we release the content lock and then flush the modified data to
* disk. See transam/README and SyncOneBuffer().)
I may misunderstand this, but it seems like it's exactly the thing we'd
want to enforce a WAL record + update of a ControlFile happens both on
one side of the checkpoint, and I guess that's what we still want? Maybe
it can't happen for some reason, or maybe the race window is so narrow
(relative to the checkpoint durations) that it's effectively impossible.
So it's clearly not doing anything. The big open question is - is that
OK and we can remove the pointless code? Or do we still need some sort
of synchronization with checkpoints? We need to prevent multiple
checkpoint transitions within a single checkpoint cycle, but we do that
by requesting / waiting for checkpoints in a couple places.
So, do we need to do anything?
regards
--
Tomas Vondra
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Amit Kapila | 2026-11-21 12:55:39 | Re: Add a hook for handling logical decoding messages on subscribers. |
| Previous Message | Zhijie Hou | 2026-11-21 12:18:45 | Re: Stop masking visibility information during WAL consistency checking |