Wrong LSN in WAL decoding error messages

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

Wrong LSN in WAL decoding error messages

От:
rahul@rhyadav.dev
Дата:
Вложения:

Hi,

DecodeXLogRecord() is passed the LSN of the record it decodes, but its
error messages print state->ReadRecPtr instead.  Before the circular
WAL decoding buffer went in (3f1ce97346, v15), the two were the same.
Now records are decoded ahead of the one the reader last returned, so
these messages name an earlier record.  0001 makes them use the lsn
argument.

I noticed this while testing Yuriy's fix for bug #19599 [1], and he
suggested sending it separately.  These checks only see records that
passed the CRC check, so they fire when some code writes a malformed
record, which is when you most want the right LSN.

To trigger one, I set hole_offset to 0 in a full-page image that has
BKPIMAGE_HAS_HOLE set, and recomputed the record CRC.  For that
record, at 0/01C03DC0, master reports:

  pg_waldump: error in WAL record at 0/01C03D38: BKPIMAGE_HAS_HOLE
    set, but hole offset 0 length 4208 block image length 3984 at
    0/01C03D38

  crash recovery: BKPIMAGE_HAS_HOLE set, but hole offset 0 length
    4208 block image length 3984 at 0/01C03CD8

0/01C03D38 is the record just before it and 0/01C03CD8 is three
records back.  With the patches applied, both report 0/01C03DC0, and
crash recovery still ends at the same place.

0002 is separate because it changes long-standing output.
pg_waldump's "error in WAL record at" prefix also uses ReadRecPtr,
which is the last record read successfully rather than where reading
failed.  At the normal end of WAL that gives:

  error in WAL record at 0/017AC370: invalid record length at
  0/017AC3F8: expected at least 24, got 0

With 0002 both LSNs are 0/017AC3F8.  It uses EndRecPtr, as pg_rewind
does.  v14 behaves the same at the end of WAL, so if 0002 is wanted
at all I'd keep it to master.  I can add a check to pg_waldump's TAP
test that the two LSNs match.

0001 applies to REL_19_STABLE as is.  On 15 to 18 the messages still
use %X/%X, so the context needs a small adjustment.

[1] https://postgr.es/m/cd00a0536c674fac9e5dd9f4e27ae868@localhost.localdomain


Regards,
Rahul

Re: Wrong LSN in WAL decoding error messages

От:
surya poondla <suryapoondla4@gmail.com>
Дата:
Hi Rahul,

Thanks for the patches. I reviewed both, and tested 0001 on REL_19_STABLE (3f5bfbce4c4) and 0001+0002 on master.

0001 looks right to me. The other checks in XLogDecodeNextRecord() already report RecPtr; DecodeXLogRecord() is the only place that uses
ReadRecPtr, and that pointer has been unreliable there since 3f1ce97346.
You also correctly left RestoreBlockImage() alone, it only runs on a record that has already been returned (redo, pg_waldump
--save-fullpage, pg_walinspect), so ReadRecPtr really is that record.

Thanks to the reproducing steps, I reproduced it the same way you did. hole_offset set to 0 in an uncompressed FPI, CRC recomputed. I crashed a cluster with kill -9
partway through a transaction after a checkpoint, then corrupted an FPI from that transaction, at 0/01821288.

Crash recovery reported these LSNs (in all cases "redo done at 0/01821248"):

                      recovery_prefetch=try    recovery_prefetch=off
    unpatched         0/0180F608               0/01821248
    patched           0/01821288               0/01821288

With prefetching on, the unpatched message named the redo start point, 45 records (about 71kB) before the bad record.
The prefetcher had decoded that whole span in one go before redo got its second record,
so ReadRecPtr was still at the redo point. So "several records back" in the commit message undersells it a bit. 
With recovery_prefetch on, the LSN can be anywhere in the read-ahead window, and here it was the redo point. It might be worth saying that.

The patched and unpatched clusters ended up the same, i.e same stop point, same rows, and byte-identical relation files (pg_control differed only
in timestamps). So 0001 changes only the message, which makes it an easy backpatch.

One more case for pg_waldump, from a separate WAL segment (an initdb template, not the cluster above) with the same corruption applied to
the FPI at 0/010165F8. When pg_waldump is started at the bad record (-s 0/010165F8), the unpatched build reports

    "could not find a valid record after 0/010165F8: ... at 0/01016568"

which is an LSN before the requested start. With 0001 it names 0/010165F8.

A few comments:
1. The WAL_DEBUG caller in XLogInsertRecord() passes EndPos, so after the patch those messages show the end of the record rather than its
start. As you say, that matches what that code logs. It's still an improvement, because debug_reader is never positioned, so the
messages printed 0/00000000 before. A line in the commit message would make the difference from the other caller clear.

2. Could we have a test? pg_waldump's 001_basic.pl checks the "error in WAL record at" message only for falling off the end of WAL, and
only matches the prefix. Patching two bytes of an FPI and fixing the CRC is easy enough in Perl, and a check that the reported LSN equals
the record's LSN would cover both 0001 and 0002. I'm happy to share the script I used if that helps.

3. pg_walinspect passes the same message through, so users of pg_get_wal_records_info() will benefit from 0001 too.

Reaging the 0002:
+1. It does what pg_rewind's extractPageMap() and pg_walinspect's ReadNextXLogRecord() already do when XLogReadRecord() fails, and with
the prefix and the message agree at the normal end of WAL. I agree about keeping it to master.

Regards,
Surya Poondla

Re: Wrong LSN in WAL decoding error messages

От:
Rahul <rahul@rhyadav.dev>
Дата:
Вложения:
Hi Surya,

Thanks for the review, and for testing on 19 and master.  Your
recovery_prefetch numbers show the problem much better than mine did.

v2 attached:

- 0001's commit message now says that with recovery_prefetch the
  message can name any record in the read-ahead window, with your
  redo start point as the example, and that pg_walinspect reports the
  same messages (your point 3).

- It also says the WAL_DEBUG messages now show the end of the
  inserted record, where they printed 0/00000000 before (point 1).

- 0001 adds a test to pg_waldump's 001_basic.pl (point 2).  It
  updates a row right after a checkpoint, so that the update record
  carries a full-page image with a hole, clears the hole offset in a
  copy of the WAL segment, and recomputes the record's CRC with
  crc32c().  It then checks that pg_waldump names that record, both
  when started before it and when started at it, your second
  pg_waldump case.  0002 extends it to check the "error in WAL record
  at" prefix too.  Without the fixes, those checks fail.

Thanks for offering your script.  Recomputing the CRC with crc32c()
kept the test short, but that function only exists from 18 on, so the
test can't go back further than that with 0001.

Regards,
Rahul Yadav

Re: Wrong LSN in WAL decoding error messages

От:
surya poondla <suryapoondla4@gmail.com>
Дата:
Hi Rahul,

Thanks for v2. It addresses everything I raised.

I tested it on REL_19_STABLE (1a1248818fb) and master (9e7d152488f).
Both patches apply cleanly and build without warnings, and the code
changes are the same as in v1.  The pg_waldump TAP tests pass, and the new checks behave as you described:

All tests pass with v2.                    
   
without 0002's C change:    only the "error in WAL record at" prefix check fails
without both C changes:     both LSN checks fail

Without the fixes, the start-at-the-bad-record case again names an LSN before the requested start point (0/017B2B78 when starting at
0/017B2C00 on master), so the test covers that case too.

The commit messages look good to me.  As you said, the test needs crc32c(), which only exists from 18 on, so it can't go back further
than 18, while the code fix goes back to 15.

The patch looks ready for a Committer.

Regards,
Surya Poondla

Re: Wrong LSN in WAL decoding error messages

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Tue, Oct 13, 2026 at 3:07 AM surya poondla  wrote:
> The patch looks ready for a Committer.

DecodeXLogRecord() changes in 0001 patch look good to me.

But, do we really need a new test for such a small fix? Generating and
locating a record, handling boundaries, modifying WAL, recomputing the CRC,
and copying the segment seem like a lot of complexity and maintenance
overhead just to verify the reported LSN, I'm afraid.

If we really want this, seems it's better to consider it separately as a test
for malformed WAL detection, including the LSN check. I'm just not sure
this additional infrastructure is worth the effort, though..

Regards,


-- 
Fujii Masao


Re: Wrong LSN in WAL decoding error messages

От:
surya poondla <suryapoondla4@gmail.com>
Дата:
Hi Fujii,
 
But, do we really need a new test for such a small fix? 

That's fair. I was the one who asked for the test, but I agree it's a
lot of test code to check an LSN in an error message. I'm happy for 0001 to go in without it.


Also, what do you think about 0002? It makes pg_waldump's "error in WAL record at" prefix show where reading failed, instead of the last
good record. pg_rewind's extractPageMap() and pg_walinspect already
work this way. We could make this master only, since it changes output people may be used to.

Regards,
Surya Poondla 

Re: Wrong LSN in WAL decoding error messages

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Sat, Oct 31, 2026 at 12:52 AM surya poondla  wrote:
> Also, what do you think about 0002? It makes pg_waldump's "error in WAL record at" prefix show where reading failed, instead of the last
> good record.

EndRecPtr is the starting position for the next read, which does not
always correspond to the start of the failing record. No? For example,
if decoding fails on the first record after a page header, EndRecPtr
still points to the start of the page? So if the goal is to report
the exact location of the failing record, isn't this change insufficient?

OTOH, if the goal is consistency with pg_rewind and pg_walinspect,
I'm fine with using EndRecPtr. But, as with 0001, I don't think the test
change in 0002 is necessary.

Regards,

-- 
Fujii Masao


Re: Wrong LSN in WAL decoding error messages

От:
surya poondla <suryapoondla4@gmail.com>
Дата:

Hi Fujii,

EndRecPtr is the starting position for the next read, which does not
always correspond to the start of the failing record. No? For example,
if decoding fails on the first record after a page header, EndRecPtr
still points to the start of the page? 
 
Yes, you're right. When the next record starts right after a page header,
 EndRecPtr points to the start of the page, so the prefix would be off
  by the header size there. The LSN at the end of the message, from
0001, is still exact, since XLogDecodeNextRecord() skips any page header before passing RecPtr to
DecodeXLogRecord().
 
OTOH, if the goal is consistency with pg_rewind and pg_walinspect,
I'm fine with using EndRecPtr. But, as with 0001, I don't think the test
change in 0002 is necessary.
  
0002  is mainly a consistency change with pg_rewind and pg_walinspect. 

Regards,
Surya Poondla

Re: Wrong LSN in WAL decoding error messages

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Mon, Nov 2, 2026 at 1:49 PM surya poondla  wrote:
> 0002  is mainly a consistency change with pg_rewind and pg_walinspect.

I've pushed both patches. Thanks!

Regards,

-- 
Fujii Masao


FAQ