Re: Offline data checksum changes can cause incorrect checksum state on standbys

Поиск
Список
Период
Сортировка
Искать

Re: Offline data checksum changes can cause incorrect checksum state on standbys

От:
Daniel Gustafsson <daniel@yesql.se>
Дата:
Вложения:
> On 28 Aug 2026, at 07:33, Bertrand Drouvot  wrote:

> Only looking at 0001 here, I've a few comments:

Thanks, I've yet to dig into it completely but below are a few quick questions
to help me along the way.

> === 1
> 
> @@ -9288,17 +9572,33 @@ xlog2_redo(XLogReaderState *record)
> 
>        SpinLockAcquire(&XLogCtl->info_lck);
>        XLogCtl->data_checksum_version = state.new_checksum_state;
> +       SetLocalDataChecksumState(state.new_checksum_state);
>        SpinLockRelease(&XLogCtl->info_lck);
> 
> This applies every XLOG2_CHECKSUMS record encountered during recovery, even when
> the same record was applied before.
> 
> For example, a standby can replay the final "on" record and then stop cleanly
> without advancing its restartpoint beyond that record. If checksums are subsequently
> disabled offline, the next startup begins from the older restartpoint and replays
> the same on record again, overriding the offline disable.

Do you mean that checksums are disabled offline across the cluster on all
nodes, or just on the standby?

> === 2
> 
> +    * checkPoint.dataChecksumState was sampled while holding the WAL insert
> +    * locks, so it is the state in effect at the redo point. 
> .
> .
> .
> +   SpinLockAcquire(&XLogCtl->info_lck);
> +   if (checkPoint.dataChecksumState == XLogCtl->data_checksum_version)
> +       ControlFile->data_checksum_version = checkPoint.dataChecksumState;
> +   SpinLockRelease(&XLogCtl->info_lck);
> 
> I’m not sure the "state in effect at the redo point" is always correct. There is
> a window between inserting the checksum transition record and updating
> XLogCtl->data_checksum_version.
> 
> XLOG2_CHECKSUMS(on) records the target state. The transition can therefore
> proceed as follows:
> 
> 1. The current shared state is inprogress-on.
> 2. XLogChecksums() inserts XLOG2_CHECKSUMS(on) and releases its WAL insertion
>   lock.
> 3. Before the shared state is updated to on, the checkpoint still reads
>   inprogress-on and inserts XLOG_CHECKPOINT_REDO.
> 4. The transition then updates the shared state to on.
> 
> The WAL order is then:
> 
> XLOG2_CHECKSUMS(on)
> XLOG_CHECKPOINT_REDO(inprogress-on)
> 
> If the server crashes after the concurrent checkpoint from step 3 completes,
> but before the enabling operation’s later checkpoint completes, recovery starts
> from that redo point and does not replay the preceding on record. It can
> therefore resolve inprogress-on back to off. The equality check above does
> not repair this ordering.

If this can happen then online checksums wouldn't work at all right?  This
window is happening inside a critical section while DELAY_CHKPT_START is set to
prevent a checkpoint from storing the state and completing to protect against
this.  Have you been able to construct a repro (with injection points) where a
REDO record after a CHECKSUM record carries the wrong state?

--
Daniel Gustafsson



Re: Offline data checksum changes can cause incorrect checksum state on standbys

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Fri, Aug 28, 2026 at 11:04:28AM -0500, Zsolt Parragi wrote:
> > > I'll try to look into what adding the two
> > > patches together looks like, but it most likely combines their size,
> > > as they improve the current master code in different ways.
> >
> > Thanks!
> 
> I looked into this,

Thanks!

> and I was right that if I add the pg_control
> changes to v4 it nearly doubles the size of the actual code changes
> from ~300 to ~550 lines, and fixes all the issues you reported

Yeah, that's what I expected, as v1 included those changes to handle these cases.

> while
> also keeping the existing suite of tests passing. The diff compared to
> v4 is relatively simple, so I don't think that would be an issue by
> itself,

Thanks! I don't see the patch attached. Would you mind sharing it?

> but it's another control version change.

Do you see the control version change as a concern?

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


Re: Offline data checksum changes can cause incorrect checksum state on standbys

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Sat, Aug 29, 2026 at 10:36:14PM +0100, Zsolt Parragi wrote:
> > Thanks! I don't see the patch attached. Would you mind sharing it?
> 
> Sorry, I forgot to attach it to the previous email.

Thanks!

=== 1

The v5-0001 commit message says:

"
The documented procedure for offline changes in a replication setup
becomes the lockstep one: stop all nodes, run pg_checksums on each of
them, then restart.
"

I did some more testing and realized that stopping both nodes is not sufficient
to prevent a mismatch in all cases.

For example, start a primary and standby with checksums off, with the standby's
latest replayed checksum transition at L0:

1. Stop the standby.
2. Enable and then disable checksums online on the primary. This writes:

  L1: inprogress-on
  L2: on
  L3: inprogress-off
  L4: off

3. Stop the primary.
4. Run pg_checksums --enable on both stopped nodes.

At this point:

primary: on, watermark L4
standby: on, watermark L0

The standby has not seen L1-L4. When it restarts, each record has an LSN greater
than L0 and is therefore applied. The final XLOG2_CHECKSUMS(off) changes the
standby back to off, while the primary remains on. We get a mismatch despite
both nodes being stopped when pg_checksums ran.

The mismatch remains silent until a later primary checkpoint carrying on is
replayed. FWIW, v1 has the same issue.

Fixing this would probably require recording additional ordering information for
offline changes, adding even more complexity to v5. Another option would be to
document that the standby must be fully caught up before both nodes are stopped
for the offline operation.

> > Do you see the control version change as a concern?
> 
> Yes, it is another non-trivial change in an already complex patch,
> really close to RC1. It's also not an area where we could easily
> implement bug fixes in a minor version, if we discover something
> later.

Yeah, and I think the case above reinforces that concern.

=== 2

+   printf(_("Data checksum watermark:              %X/%08X\n"),
+          LSN_FORMAT_ARGS(ControlFile->data_checksum_lsn));
+   printf(_("Data checksum state is node-local:    %s\n"),
+          (ControlFile->data_checksum_is_local ? _("yes") : _("no")));

That produces pg_upgrade --check against a running source cluster with checksums
enabled to fail with:

"
  old cluster does not use data checksums but the new one does
"

Matching "Data page checksum version:" specifically should fix it.

That makes me realize that we don't have tests for pg_upgrade --check against a
running cluster: I'll open a dedicated thread and submit a patch to add those
new tests.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


Re: Offline data checksum changes can cause incorrect checksum state on standbys

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Вложения:
> Yeah, I see your point that the underlying issue is the non WAL logged change.
> My concern was not about a pg_control change in itself, but that addressing this
> case might require further changes to the current pg_control design, which adds
> risk this close to RC1.

(When I read the emails previously  I completely missed the part that
in this case it remains a mismatch. Sorry, that was before coffee)

I don't think we can get this completely right without both wal and
pg_control changes (and possibly some extra connection-time
communication between the standby and primary to provide early
reporting instead of delayed in the most common cases)
If we want to move toward the error direction in pg20 (which we
should) we will have to log that an offline change happened.

I attached v6 which for now documents the current behavior as-is, and
other than that I reorganized the test files.

Re: Offline data checksum changes can cause incorrect checksum state on standbys

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Вложения:
> Thanks! I did not look in details but it looks like that pg_upgrade --check against
> a running source cluster with checksums enabled will still fail (finding === 2 in
> [0], tested with the test shared in [1]).

Yes, I missed that in the previous version, v7 fixes it.

Re: Offline data checksum changes can cause incorrect checksum state on standbys

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Вложения:
Hi,

On Tue, Sep 01, 2026 at 12:31:42AM +0100, Zsolt Parragi wrote:
> > I found a case where the source's online enable occurs after divergence and was
> > never seen by the target, but replay skips it instead of applying it:
> > ....
> > Maybe the watermark needs timeline context, or pg_rewind needs to adjust it
> > when it comes from the target's divergent history?
> 
> Thanks! v8 adds the latter, with a new test case verifying this scenario.

Thanks!

As far the new test:

=== 1

+# Clean switchover back to A; enable checksums online on it.
+$node_b->stop('fast');
+$node_a->promote;

IIUC, the preceding wait_for_catchup() does not cover the shutdown checkpoint
written by stop(). Therefore, the divergence checkpoint in scenario 2 is not
guaranteed to carry off, as described.

=== 2

+enable_data_checksums($node_b, wait => 'on');
+test_checksum_state($node_b, 'on');
...
+$node_a->wait_for_catchup($node_b, 'replay', $node_a->lsn('insert'));
+test_checksum_state($node_b, 'on');

The target is already "on" before pg_rewind, so the final assertion does not
prove that the source's enable record was replayed.

Please find attached a small patch addressing those two test comments to apply
on top of v8. What do you think?

=== 3

+   /*
+    * End of the newest XLOG2_CHECKSUMS record this node has written or
+    * applied.

and

+    * would skip them as already applied.  Clamp it to the divergence point,
+    * so that every transition record on the source's history takes effect.
+    */
+   if (ControlFile_new.data_checksum_lsn > divergerec)
+       ControlFile_new.data_checksum_lsn = divergerec;

divergerec is not necessarily the end of an XLOG2_CHECKSUMS record, so the
comment no longer describes every value the field may contain. Maybe it should
describe it as the WAL position through which checksum transitions are covered?

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Re: Offline data checksum changes can cause incorrect checksum state on standbys

От:
Daniel Gustafsson <daniel@yesql.se>
Дата:
Вложения:
> On 7 Sep 2026, at 17:57, Heikki Linnakangas  wrote:

>>> I'm still trying to understand all the different states and interactions between online and offline changes. It's really complicated :-(. I know it's a tall order, but is there something we could do to make it simpler?
>> If there was I'd love to try it, but across the many alteratives tried during
>> this open item there hasn't been anyhing less complicated which also solves the
>> problem.  Combining a WAL logged procedure with one that can rewrite the data
>> directory without any WAL entries at all is inherently complicated.
> 
> So, if you can have different state in primary and a replica, there are four combinations:
> 
> 1: Primary on, replica on
> 2: Primary off, replica off
> 
> These are straightforward
> 
> 3: Primary on, replica off
> 
> You end up in this situation, if you turn on run pg_checksums to turn on checksums in primary. You stay that you really really shouldn't stay for long in this state, but why? What's the harm?
> 
> One harm is that it's confusing, but if we have to deal with it anyway, why is it so bad?

Tools like pg_rewind etc rely on the fact that nodes are equal in checksum
state.  What if the replica is promoted?  I'm personally unconvinced that there
is a good usecase for per-node settings, but I've spent X years thinking about
checksums being replicated so I am clearly biased.

Considering how complicated it was to get replicated states right, if we want
to make it per-node I think we need to go back to the drawing board and
re-think properly rather than settle for that it seems to work.  (Not that I
think that's what you're advocating, I just expect getting it work will be
complicated.)

> 4: Primary off, replica on
> 
> Is this possible? Does it make sense?

This is possible using pg_checksums, but like the inverse case I don't think it
makes sense to use different settings across replication.

--
Daniel Gustafsson



Re: Offline data checksum changes can cause incorrect checksum state on standbys

От:
Daniel Gustafsson <daniel@yesql.se>
Дата:
Вложения:
> On 7 Sep 2026, at 13:08, Heikki Linnakangas  wrote:

> Let's add a new 'sect2' for this explanation, and move it after the "Online Enabling of Checksums" section. It's currently placed under "Offline Enabling of Checksums", but it actually goes into a lot of details of how *online* checksumming works, but "Online Enabling of Checksums" is covered in the following paragraph. If you read this in order like a novel, it feels weird.
> 
> I think these paragraphs could use some copy-editing too. It feels like a pretty deep technical explanation, not very accessible to a DBA. Maybe start with "The primary server and replica can have different checksum states".
> 
> (Not new with this patch, but: )
> 
> The placement of the states in the state diagram on that page looks bizarre. I know it's auto-generated so not sure there's much we can do about it.. but could we, please? Maybe it'd get more clear if you leave 'initdb' out of the diagram. Or consider some completely different representation.

The attached v14 rewords and reorganizes the docs, and attempts to tidy uo the
state diagram.  There are no new code changes in this version.

--
Daniel Gustafsson

Re: Offline data checksum changes can cause incorrect checksum state on standbys

От:
Alexander Lakhin <exclusion@gmail.com>
Дата:
Hello Daniel,

14.09.2026 16:48, Daniel Gustafsson wrote:
Pushed to master along with the cleanups from the "Trying to break online
checksums with LLMs" thread.  Not backpatched yet, as I wanted a few builds in
the BF first and also some input on the new open item in the other thread.

BF animal turaco (Raspberry PI, kernel 6.12.75+rpt-rpi-v8) managed to fail
a test added in b52a1c2c8:
[22:51:29.073](0.002s) ok 7 - replay starts at the switchover checkpoint
[22:51:29.087](0.014s) not ok 8 - last common checkpoint is a shutdown checkpoint
[22:51:29.088](0.001s)
[22:51:29.088](0.000s) #   Failed test 'last common checkpoint is a shutdown checkpoint'
#   at t/013_rewind.pl line 161.
[22:51:29.089](0.001s) #                   ''
#     doesn't match '(?^:CHECKPOINT_SHUTDOWN)'
[22:51:29.100](0.011s) ok 9 - rewound node keeps its own checksum state in the control file
...
Test Summary Report
-------------------
t/013_rewind.pl                      (Wstat: 256 (exited 1) Tests: 13 Failed: 1)

I've reproduced this failure with the following modification:
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -3446,2 +3446,3 @@ XLogFileInitInternal(XLogSegNo logsegno, TimeLineID logtli,
         */
+pg_usleep(100000);
        installed_segno = logsegno;

which makes all-zero 000000020000000000000005 appear inside
$node_a->data_dir . '/pg_wal':
tr -d '\000' < src/test/modules/test_checksums/tmp_check/t_013_rewind_node_a_data/pgdata/pg_wal/000000020000000000000005 | wc
      0       0       0

and then if readdir() happens to return this file first (I'm observing
this on ext4):
perl -e 'opendir(my $dh, $ARGV[0]) or die; my @d = grep { length($_) == 24 } readdir($dh); print("@d\n");' \
src/test/modules/test_checksums/tmp_check/t_013_rewind_node_a_data/pgdata/pg_wal
000000020000000000000005 000000020000000000000004 000000010000000000000002 000000020000000000000003 000000010000000000000003

pg_waldump with no explicit segment specification fails:
.../pg_waldump -p src/test/modules/test_checksums/tmp_check/t_013_rewind_node_a_data/pgdata/pg_wal -s 0/03000000
pg_waldump: error: invalid WAL segment size in WAL file "000000020000000000000005" (0 bytes)
pg_waldump: detail: The WAL segment size must be a power of two between 1 MB and 1 GB.


[1] https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=turaco&dt=2026-09-24%2019%3A58%3A27

Best regards,
Alexander

Re: Offline data checksum changes can cause incorrect checksum state on standbys

От:
Nazir Bilal Yavuz <byavuz81@gmail.com>
Дата:
Hi,

On Sat, 26 Sept 2026 at 08:00, Alexander Lakhin  wrote:
>
> 14.09.2026 16:48, Daniel Gustafsson wrote:
>
> BF animal turaco (Raspberry PI, kernel 6.12.75+rpt-rpi-v8) managed to fail
> a test added in b52a1c2c8:
> [22:51:29.073](0.002s) ok 7 - replay starts at the switchover checkpoint
> [22:51:29.087](0.014s) not ok 8 - last common checkpoint is a shutdown checkpoint
> [22:51:29.088](0.001s)
> [22:51:29.088](0.000s) #   Failed test 'last common checkpoint is a shutdown checkpoint'
> #   at t/013_rewind.pl line 161.
> [22:51:29.089](0.001s) #                   ''
> #     doesn't match '(?^:CHECKPOINT_SHUTDOWN)'
> [22:51:29.100](0.011s) ok 9 - rewound node keeps its own checksum state in the control file
> ...
> Test Summary Report
> -------------------
> t/013_rewind.pl                      (Wstat: 256 (exited 1) Tests: 13 Failed: 1)
>
> I've reproduced this failure with the following modification:
> --- a/src/backend/access/transam/xlog.c
> +++ b/src/backend/access/transam/xlog.c
> @@ -3446,2 +3446,3 @@ XLogFileInitInternal(XLogSegNo logsegno, TimeLineID logtli,
>          */
> +pg_usleep(100000);
>         installed_segno = logsegno;
>
> which makes all-zero 000000020000000000000005 appear inside
> $node_a->data_dir . '/pg_wal':
> tr -d '\000' < src/test/modules/test_checksums/tmp_check/t_013_rewind_node_a_data/pgdata/pg_wal/000000020000000000000005 | wc
>       0       0       0
>
> and then if readdir() happens to return this file first (I'm observing
> this on ext4):
> perl -e 'opendir(my $dh, $ARGV[0]) or die; my @d = grep { length($_) == 24 } readdir($dh); print("@d\n");' \
> src/test/modules/test_checksums/tmp_check/t_013_rewind_node_a_data/pgdata/pg_wal
> 000000020000000000000005 000000020000000000000004 000000010000000000000002 000000020000000000000003 000000010000000000000003
>
> pg_waldump with no explicit segment specification fails:
> .../pg_waldump -p src/test/modules/test_checksums/tmp_check/t_013_rewind_node_a_data/pgdata/pg_wal -s 0/03000000
> pg_waldump: error: invalid WAL segment size in WAL file "000000020000000000000005" (0 bytes)
> pg_waldump: detail: The WAL segment size must be a power of two between 1 MB and 1 GB.

Thanks for the report! I think your analysis correct. I encountered
the same problem on my local, there is another thread for fixing this
problem [1].

I can reproduce the problem with your reproducer, though not on the
first try; I needed to run the test a couple of times. Then, I confirm
that the patch in [1] fixes the problem, I run 013_rewind test 100
times and there was no failure.

[1] https://postgr.es/m/CAN55FZ1Yak_xBqMaDQsD7atpBkGLEkF-DKXcs3nLHM1Uq4YRew%40mail.gmail.com


-- 
Regards,
Nazir Bilal Yavuz
Microsoft


FAQ