incremental backup issue

Started by Fabrice Chapuis9 months ago10 messageshackers
Jump to latest
#1Fabrice Chapuis
fabrice636861@gmail.com

Hi,

I use Postgres 17.7 and I have configured incremental backup

wal_summary_keep_time=10d
show wal_summary_keep_time=on

On the standby, this message is written every 10 seconds by the
walsummarizer process.

2026-02-17 11:40:22.548 CET [544677]: [297-1]
user=,db=,client=,application= ERROR: could not read WAL from timeline 6
at 698/460000A0: invalid record length at 698/460000A0: expected at least
24, got 0
2026-02-17 11:40:32.559 CET [544677]: [298-1]
user=,db=,client=,application= ERROR: could not read WAL from timeline 6
at 698/460000A0: invalid record length at 698/460000A0: expected at least
24, got 0

postgres 544677 544615 0 10:50 ? 00:00:00 postgres: dev_ge:
walsummarizer

Last summary files are:

-rw-------. 1 postgres postgres 1808 Feb 5 18:00
00000007000006998F2205F0000006998F2FCAA8.summary
-rw-------. 1 postgres postgres 1158 Feb 5 18:30
00000007000006998F2FCAA8000006998F328C48.summary
-rw-------. 1 postgres postgres 448 Feb 5 19:00
00000007000006998F328C48000006998F3419C0.summary
-rw-------. 1 postgres postgres 566 Feb 5 19:30
00000007000006998F3419C0000006998F35D2C8.summary
-rw-------. 1 postgres postgres 616 Feb 5 20:00
00000007000006998F35D2C8000006998F379C60.summary
-rw-------. 1 postgres postgres 1108 Feb 5 20:30
00000007000006998F379C60000006998F3A88C8.summary
-rw-------. 1 postgres postgres 330 Feb 5 20:43
00000007000006998F3A88C80000069990000028.summary
-rw-------. 1 postgres postgres 32 Feb 5 20:43
00000007000006999000002800000699900000A0.summary
-rw-------. 1 postgres postgres 32 Feb 5 20:43
0000000800000699900000A000000699900000D8.summary

summary file of the timeline 6

-rw-------. 1 postgres postgres 780 Jan 8 20:10
0000000600000698452AB9680000069846000028.summary
-rw-------. 1 postgres postgres 32 Jan 8 20:10
00000006000006984600002800000698460000A0.summary

pg_waldump 000000060000069800000046
rmgr: XLOG len (rec/tot): 114/ 114, tx: 0, lsn:
698/46000028, prev 698/452D6AA8, desc: CHECKPOINT_SHUTDOWN redo
698/46000028; tli 6; prev tli 6; fpw true; wal_level logical; xid
0:212216474; oid 6953284; multi 86737; offset 217538; oldest xid 16464388
in DB 18769; oldest multi 1 in DB 17257; oldest/newest commit timestamp
xid: 167966575/212216473; oldest running xid 0; shutdown
pg_waldump: error: error in WAL record at 698/46000028: invalid record
length at 698/460000A0: expected at least 24, got 0

On the primary summary process works fine

-rw-------. 1 postgres postgres 2340 Feb 17 10:05
000000090000069A7F9595980000069A7F9B2D08.summary
-rw-------. 1 postgres postgres 1568 Feb 17 10:35
000000090000069A7F9B2D080000069A7F9EF6C8.summary
-rw-------. 1 postgres postgres 5300 Feb 17 11:05
000000090000069A7F9EF6C80000069A7FA47288.summary
-rw-------. 1 postgres postgres 642 Feb 17 11:35
000000090000069A7FA472880000069A7FA6B820.summary

no lag between primary and secondary

Recovery on the standby is active and on time
postgres 544620 544615 0 10:50 ? 00:00:00 postgres: dev_ge:
startup recovering 000000090000069A00000080
postgres 544675 544615 0 10:50 ? 00:00:00 postgres: dev_ge:
walreceiver streaming 69A/800D0808

history files:

cat 00000007.history
1 5F4/BB000C68 no recovery target specified

2 618/9C0000A0 no recovery target specified

3 619/E70000A0 no recovery target specified

4 669/96FFFFD0 no recovery target specified

5 693/CC0000A0 no recovery target specified

6 698/460000A0 no recovery target specified

cat 00000008.history
1 5F4/BB000C68 no recovery target specified

2 618/9C0000A0 no recovery target specified

3 619/E70000A0 no recovery target specified

4 669/96FFFFD0 no recovery target specified

5 693/CC0000A0 no recovery target specified

6 698/460000A0 no recovery target specified

7 699/900000A0 no recovery target specified

cat 00000009.history
1 5F4/BB000C68 no recovery target specified

2 618/9C0000A0 no recovery target specified

3 619/E70000A0 no recovery target specified

4 669/96FFFFD0 no recovery target specified

5 693/CC0000A0 no recovery target specified

6 698/460000A0 no recovery target specified

7 699/900000A0 no recovery target specified

8 699/910000A0 no recovery target specified

In the code:
walsummarizer.c
/*
* Main loop: read xlog records one by one.
*/
while (1)
{
int block_id;
char *errormsg;
XLogRecord *record;
uint8 rmid;

ProcessWalSummarizerInterrupts();

/* We shouldn't go backward. */
Assert(summary_start_lsn <= xlogreader->EndRecPtr);

/* Now read the next record. */
record = XLogReadRecord(xlogreader, &errormsg);
if (record == NULL)
{

xlogreader.c
XLogReadRecord --> XLogReadAhead(XLogReaderState *state, bool nonblocking)
--> result = XLogDecodeNextRecord(state, nonblocking); => error
--> XLogNextRecord

/* There may be no next page if it's too small. */
if (total_len < SizeOfXLogRecord)
{
report_invalid_record(state,
"invalid record length at %X/%08X: expected at least %u, got %u",
LSN_FORMAT_ARGS(RecPtr),
(uint32) SizeOfXLogRecord, total_len);
goto err;

why walsummarizer works fine on the primary and it is stuck on the
secondary?

when the walsummarizer process was stop for a while (24h) and restarted,
it resumes, but the history is lost.

-rw-------. 1 postgres postgres 580 Feb 18 11:09
000000090000069AB9B511680000069AB9B7F260.summary
-rw-------. 1 postgres postgres 1880 Feb 18 11:09
000000090000069AB9B7F2600000069AB9BB6780.summary
-rw-------. 1 postgres postgres 530 Feb 18 11:09
000000090000069ABF2082880000069ABF23C690.summary
-rw-------. 1 postgres postgres 8582 Feb 18 11:35
000000090000069ABF23C6900000069AC01B1EB0.summary

Any clue of what happend will be very appreciated

Fabrice

#2Robert Haas
robertmhaas@gmail.com
In reply to: Fabrice Chapuis (#1)
Re: incremental backup issue

On Wed, Feb 18, 2026 at 5:40 AM Fabrice Chapuis <fabrice636861@gmail.com> wrote:

why walsummarizer works fine on the primary and it is stuck on the secondary?

I don't know. It sounds like a bug, but I'm not exactly sure what the
bug is. In the situation you describe here, it seems like the standby
might have switched from timeline 6 to some newer timeline, most
likely 7, but the WAL summarizer got stuck at the end of timeline 6
instead of following the timeline change. If that were the
explanation, then this would only happen when a new primary is
promoted and a standby starts following it. It could either be the
case that this happens every single time a standby changes timelines,
or it could be that it only happens if the standby changes timelines
and some other unknown condition is also satisfied.

Alternatively, maybe there's some other triggering event altogether.

Any ideas, based on your experience so far?

--
Robert Haas
EDB: http://www.enterprisedb.com

#3Fabrice Chapuis
fabrice636861@gmail.com
In reply to: Robert Haas (#2)
Re: incremental backup issue

I could try to reproduce the problem. I'll keep you in touch

Regards,
Fabrice

On Thu, Feb 19, 2026 at 1:14 AM Robert Haas <robertmhaas@gmail.com> wrote:

Show quoted text

On Wed, Feb 18, 2026 at 5:40 AM Fabrice Chapuis <fabrice636861@gmail.com>
wrote:

why walsummarizer works fine on the primary and it is stuck on the

secondary?

I don't know. It sounds like a bug, but I'm not exactly sure what the
bug is. In the situation you describe here, it seems like the standby
might have switched from timeline 6 to some newer timeline, most
likely 7, but the WAL summarizer got stuck at the end of timeline 6
instead of following the timeline change. If that were the
explanation, then this would only happen when a new primary is
promoted and a standby starts following it. It could either be the
case that this happens every single time a standby changes timelines,
or it could be that it only happens if the standby changes timelines
and some other unknown condition is also satisfied.

Alternatively, maybe there's some other triggering event altogether.

Any ideas, based on your experience so far?

--
Robert Haas
EDB: http://www.enterprisedb.com

#4Nick Ivanov
nick.ivanov@enterprisedb.com
In reply to: Fabrice Chapuis (#3)
Re: incremental backup issue

Hello Fabrice,

I'm wondering if you had a chance to reproduce the problem. I'm seeing
something similar, but not exactly, so I'm curious if this may be another
manifestation of the error I get.

Thanks

Nick

On Thu, Feb 19, 2026 at 8:27 AM Fabrice Chapuis <fabrice636861@gmail.com>
wrote:

I could try to reproduce the problem. I'll keep you in touch

--
Robert Haas
EDB: http://www.enterprisedb.com

I use Postgres 17.7 and I have configured incremental backup

wal_summary_keep_time=10d
show wal_summary_keep_time=on

On the standby, this message is written every 10 seconds by the
walsummarizer process.

2026-02-17 11:40:22.548 CET [544677]: [297-1]
user=,db=,client=,application= ERROR: could not read WAL from timeline 6
at 698/460000A0: invalid record length at 698/460000A0: expected at least
24, got 0
2026-02-17 11:40:32.559 CET [544677]: [298-1]
user=,db=,client=,application= ERROR: could not read WAL from timeline 6
at 698/460000A0: invalid record length at 698/460000A0: expected at least
24, got 0

--
Nick Ivanov
Solutions Architect
www.enterprisedb.com

#5Thom Brown
thom@linux.com
In reply to: Nick Ivanov (#4)
Re: incremental backup issue

On Mon, 13 Jul 2026 at 17:26, Nick Ivanov <nick.ivanov@enterprisedb.com> wrote:

Hello Fabrice,

I'm wondering if you had a chance to reproduce the problem. I'm seeing something similar, but not exactly, so I'm curious if this may be another manifestation of the error I get.

I threw this at Claude which managed to reproduce the issue, identify
the cause, and has produced a fix.

Here's the report:

A user running 17.7 reported that the walsummarizer process on a standby
logged this, over and over, every 10 seconds, and never made progress:

ERROR: could not read WAL from timeline 6 at 698/460000A0: invalid
record length at 698/460000A0: expected at least 24, got 0

698/460000A0 was the switch point from timeline 6 to timeline 7 (per the
history file); on disk, timeline 6 ends there with a shutdown checkpoint
followed by zeroes. The summarizer on the primary was fine. Restarting it
"fixed" things only in the sense that it jumped forward to the current
timeline and silently left a gap in the summaries, breaking the incremental
backup chain across that range.

I looked into it and believe there are two related problems, both in
src/backend/postmaster/walsummarizer.c.

Root cause
----------

SummarizeWAL() reads WAL up to a horizon, private_data->read_upto. On a
standby that comes from GetLatestLSN(), i.e. the greater of the WAL
receiver's flush position and the replay position. That value can sit
past the last *complete* record on the timeline being read: the flush
pointer can be a little ahead of the last good record, and a timeline
that ends at a switch point is followed by zeroed WAL space.

When the summarizer then reads into that zeroed space, XLogReadRecord()
returns the usual end-of-WAL marker (xl_tot_len == 0), reported as
"invalid record length ... got 0". SummarizeWAL() treats that as a fatal
error rather than as end of WAL, so it errors out, sleeps 10 s, and
retries from GetOldestUnsummarizedLSN() -- which returns the same place,
because nothing advanced. And because unsummarized WAL is pinned on disk
(KeepLogSeg() keeps everything >= GetOldestUnsummarizedLSN()), the
offending segment is retained forever, so the condition cannot clear on
its own. That's the 10-second loop.

The primary never hits this because there GetLatestLSN() is
GetFlushRecPtr(), which is always the end of a complete record on the
current timeline.

Separately, and contributing to the above: when SummarizeWAL() is reading
a *historic* timeline, it still sets read_upto = maximum_lsn (the latest
position on the current timeline), which can be far past where the
historic timeline ended. summarizer_read_local_xlog_page() already clamps
read_upto to the switch point when it *discovers* mid-read that a timeline
has gone historic (that's the case cf8a4898360 dealt with), and the
SummarizeWAL() header comment says maximum_lsn "should be the switch point
when reading a historic timeline" -- but the initial setup doesn't
actually clamp it.

Reproduction
------------

The attached TAP test reproduces it deterministically: stream a standby,
promote it at a chosen LSN to fork a new timeline, then overwrite the tail
of the old timeline with zeroes ahead of its switch point, and turn on
summarize_wal. Without the fix the summarizer loops on the error above and
never advances; with it, the summarizer stops cleanly at the end of valid
WAL. (The zeroing stands in for "the timeline's on-disk WAL ends, in
zeroes, before read_upto" -- which is what happens on the reporter's box,
just arrived at through the standby's position reporting rather than by
hand.)

Proposed fix
------------

0001 clamps read_upto to the switch point when reading a historic
timeline, matching the page-read callback and the documented contract.

0002 makes SummarizeWAL() recognise a zeroed record (xl_tot_len == 0, the
canonical end-of-WAL marker) as end of WAL instead of a hard error: it
ends the current summary there and waits, with backoff, for the situation
to change (more WAL, or a timeline switch that the main loop then
follows). This mirrors how recovery treats the same condition on a
standby. Other invalid records (bad CRC, bad rmid, ...) are still reported
as errors, so genuine corruption isn't masked. 0002 also carries the TAP
test.

Both patches pass the existing pg_walsummary and pg_combinebackup suites
(including 008_promote, incremental backup across a promotion).

Open question
-------------

0002 is really hardening the summarizer against a read horizon that points
past valid WAL. The deeper question -- why a standby's reported flush/
replay position ends up past the real end of a timeline in the first place
-- may deserve a separate look on the recovery side. But the summarizer
should not melt down regardless, and today it does, so I think the
hardening is worthwhile on its own.

</claude>

Patches attached.

Regards

Thom

Attachments:

0001-clamp-read-horizon.difftext/x-patch; charset=US-ASCII; name=0001-clamp-read-horizon.diffDownload+14-1
0002-end-of-wal.difftext/x-patch; charset=US-ASCII; name=0002-end-of-wal.diffDownload+210-0
#6Robert Haas
robertmhaas@gmail.com
In reply to: Thom Brown (#5)
Re: incremental backup issue

On Mon, Jul 13, 2026 at 2:14 PM Thom Brown <thom@linux.com> wrote:

0001 clamps read_upto to the switch point when reading a historic
timeline, matching the page-read callback and the documented contract.

IMHO, this is a great example of why AI-generated bug reports and/or
fixes need to be double-checked by knowledgeable humans. claude would
have benefited from reading the comments for SummarizeWAL itself,
which say:

* 'maximum_lsn' identifies the point beyond which we can't count on being
* able to read any more WAL. It should be the switch point when reading a
* historic timeline, or the most-recently-measured end of WAL when reading
* the current timeline.

Which means that the clamping in 0001 shouldn't be necessary, because
the caller should already have done it.

But the question is: how exactly does this scenario arise in the first
place? SummarizeWAL checks before reading each record that the record
it's reading starts before the switch point, and then checks again
after reading it that it ends before the switch point. So if, for
example, you have a primary archiving files on TLI 1 and you promote a
standby and it archives files on TLI 2, nothing will actually go
wrong, I think. The standby trying to follow the timeline switch from
TLI 1 to TLI 2 might read one record past the switchpoint, but then it
will realize what's happened and sort itself out. The problem only
occurs if trying to read one record past the switchpoint results in an
error. In the original scenario and in claude's analysis, that seems
to happen because the tail end of the WAL segment is all zeros... but
how did such a file get archived in the first place?

The only obvious way I can see that happening is if somebody renames
the .partial file to remove that suffix and then causes the resulting
file to get archived. I don't think that's a thing that you're really
supposed to do. That's not to say I don't think we should fix this:
WalSummarizerMain is calling SummarizeWAL with a maximum_lsn that is
not computed in the way that SummarizeWAL says it should be computed,
which is bad, and the result is that this code is less robust than I
would like it to be, which is also bad. But I *think* you have to be
doing something unusual for it to become a problem in practice, which
might be why Fabrice had difficulty reproducing it.

I attach a patch. I don't think we need anything like the 0002 in your
proposal from claude. The read horizon used by the WAL summarizer
*has* to be valid; if we can't achieve that, we're doomed.

--
Robert Haas
EDB: http://www.enterprisedb.com

Attachments:

v2-0001-walsummarizer-Guard-against-WAL-files-whose-tail-.patchapplication/octet-stream; name=v2-0001-walsummarizer-Guard-against-WAL-files-whose-tail-.patchDownload+3-2
#7Thom Brown
thom@linux.com
In reply to: Robert Haas (#6)
Re: incremental backup issue

On Mon, 13 Jul 2026 at 22:43, Robert Haas <robertmhaas@gmail.com> wrote:

On Mon, Jul 13, 2026 at 2:14 PM Thom Brown <thom@linux.com> wrote:

0001 clamps read_upto to the switch point when reading a historic
timeline, matching the page-read callback and the documented contract.

IMHO, this is a great example of why AI-generated bug reports and/or
fixes need to be double-checked by knowledgeable humans. claude would
have benefited from reading the comments for SummarizeWAL itself,
which say:

* 'maximum_lsn' identifies the point beyond which we can't count on being
* able to read any more WAL. It should be the switch point when reading a
* historic timeline, or the most-recently-measured end of WAL when reading
* the current timeline.

Ha, yes, this is certainly a clear example of the confidence AI puts
in its own conclusions not being indicative of reliability.

Which means that the clamping in 0001 shouldn't be necessary, because
the caller should already have done it.

But the question is: how exactly does this scenario arise in the first
place? SummarizeWAL checks before reading each record that the record
it's reading starts before the switch point, and then checks again
after reading it that it ends before the switch point. So if, for
example, you have a primary archiving files on TLI 1 and you promote a
standby and it archives files on TLI 2, nothing will actually go
wrong, I think. The standby trying to follow the timeline switch from
TLI 1 to TLI 2 might read one record past the switchpoint, but then it
will realize what's happened and sort itself out. The problem only
occurs if trying to read one record past the switchpoint results in an
error. In the original scenario and in claude's analysis, that seems
to happen because the tail end of the WAL segment is all zeros... but
how did such a file get archived in the first place?

The only obvious way I can see that happening is if somebody renames
the .partial file to remove that suffix and then causes the resulting
file to get archived. I don't think that's a thing that you're really
supposed to do. That's not to say I don't think we should fix this:
WalSummarizerMain is calling SummarizeWAL with a maximum_lsn that is
not computed in the way that SummarizeWAL says it should be computed,
which is bad, and the result is that this code is less robust than I
would like it to be, which is also bad. But I *think* you have to be
doing something unusual for it to become a problem in practice, which
might be why Fabrice had difficulty reproducing it.

So really, this doesn't sound like this is solved, not that these
changes aren't still necessary.

I attach a patch. I don't think we need anything like the 0002 in your
proposal from claude. The read horizon used by the WAL summarizer
*has* to be valid; if we can't achieve that, we're doomed.

Nick, you said that you saw something "similar" (suggesting that it's
not identical), but you didn't explain what that was. Is there
potentially a separate bug that needs reporting?

Thom

#8Thom Brown
thom@linux.com
In reply to: Thom Brown (#7)
Re: incremental backup issue

On Mon, 13 Jul 2026 at 23:07, Thom Brown <thom@linux.com> wrote:

On Mon, 13 Jul 2026 at 22:43, Robert Haas <robertmhaas@gmail.com> wrote:

On Mon, Jul 13, 2026 at 2:14 PM Thom Brown <thom@linux.com> wrote:

0001 clamps read_upto to the switch point when reading a historic
timeline, matching the page-read callback and the documented contract.

IMHO, this is a great example of why AI-generated bug reports and/or
fixes need to be double-checked by knowledgeable humans. claude would
have benefited from reading the comments for SummarizeWAL itself,
which say:

* 'maximum_lsn' identifies the point beyond which we can't count on being
* able to read any more WAL. It should be the switch point when reading a
* historic timeline, or the most-recently-measured end of WAL when reading
* the current timeline.

Ha, yes, this is certainly a clear example of the confidence AI puts
in its own conclusions not being indicative of reliability.

Which means that the clamping in 0001 shouldn't be necessary, because
the caller should already have done it.

But the question is: how exactly does this scenario arise in the first
place? SummarizeWAL checks before reading each record that the record
it's reading starts before the switch point, and then checks again
after reading it that it ends before the switch point. So if, for
example, you have a primary archiving files on TLI 1 and you promote a
standby and it archives files on TLI 2, nothing will actually go
wrong, I think. The standby trying to follow the timeline switch from
TLI 1 to TLI 2 might read one record past the switchpoint, but then it
will realize what's happened and sort itself out. The problem only
occurs if trying to read one record past the switchpoint results in an
error. In the original scenario and in claude's analysis, that seems
to happen because the tail end of the WAL segment is all zeros... but
how did such a file get archived in the first place?

The only obvious way I can see that happening is if somebody renames
the .partial file to remove that suffix and then causes the resulting
file to get archived. I don't think that's a thing that you're really
supposed to do. That's not to say I don't think we should fix this:
WalSummarizerMain is calling SummarizeWAL with a maximum_lsn that is
not computed in the way that SummarizeWAL says it should be computed,
which is bad, and the result is that this code is less robust than I
would like it to be, which is also bad. But I *think* you have to be
doing something unusual for it to become a problem in practice, which
might be why Fabrice had difficulty reproducing it.

So really, this doesn't sound like this is solved, not that these
changes aren't still necessary.

I attach a patch. I don't think we need anything like the 0002 in your
proposal from claude. The read horizon used by the WAL summarizer
*has* to be valid; if we can't achieve that, we're doomed.

Nick, you said that you saw something "similar" (suggesting that it's
not identical), but you didn't explain what that was. Is there
potentially a separate bug that needs reporting?

Oh, and I'm removing Fabrice from the recipients. It's twice come back
with "Address not found", so I don't think we'll be getting any
updates from them.

Thom

#9Robert Haas
robertmhaas@gmail.com
In reply to: Thom Brown (#7)
Re: incremental backup issue

On Mon, Jul 13, 2026 at 6:07 PM Thom Brown <thom@linux.com> wrote:

Ha, yes, this is certainly a clear example of the confidence AI puts
in its own conclusions not being indicative of reliability.

Yep. And it's an odd oversight, too, since apportioning blame between
SummarizeWAL and its caller seems to be the whole deal here, and the
comments are how you figure out whose fault it is (at least, if they
say anything useful).

So really, this doesn't sound like this is solved, not that these
changes aren't still necessary.

I assume it's likely that Fabrice ended up with a not-totally-valid
WAL file in the archive after some series of manual steps and couldn't
quite remember what had been done well enough to provide an exact
reproducer. That seems like a completely reasonable thing to happen to
somebody. Now it's also possible that there is some as-yet-unknown way
for this to happen without manual fudging, but unless somebody can
figure out what that is exactly, it's hard to say what we ought to do
about it.

I attach a patch. I don't think we need anything like the 0002 in your
proposal from claude. The read horizon used by the WAL summarizer
*has* to be valid; if we can't achieve that, we're doomed.

Nick, you said that you saw something "similar" (suggesting that it's
not identical), but you didn't explain what that was. Is there
potentially a separate bug that needs reporting?

See the "walsummarizer can get stuck when switching timelines" thread.
That case occurs when there's a timeline switch and the file from the
old timeline doesn't make it into the archive at all. This case occurs
where there's a timeline switch and a corrupted file from the old
timeline makes it into the archive. It's possible to reproduce that
case without any manual steps; see the included test case on that
thread. As I say, I can believe that there could be a way to reproduce
this case without manual steps too, but I don't know what it is.

--
Robert Haas
EDB: http://www.enterprisedb.com

#10Robert Haas
robertmhaas@gmail.com
In reply to: Robert Haas (#6)
Re: incremental backup issue

On Mon, Jul 13, 2026 at 5:42 PM Robert Haas <robertmhaas@gmail.com> wrote:

I attach a patch. I don't think we need anything like the 0002 in your
proposal from claude. The read horizon used by the WAL summarizer
*has* to be valid; if we can't achieve that, we're doomed.

I have now committed this patch and back-patched to back to v17, the
first release that contains walsummarizer.

--
Robert Haas
EDB: http://www.enterprisedb.com