]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
walsummarizer: Guard against WAL files whose tail ends are not valid. master github/master
authorRobert Haas <rhaas@postgresql.org>
Wed, 22 Jul 2026 12:47:44 +0000 (08:47 -0400)
committerRobert Haas <rhaas@postgresql.org>
Wed, 22 Jul 2026 12:47:44 +0000 (08:47 -0400)
SummarizeWAL documents that maximum_lsn should be passed as "the switch
point when reading a historic timeline, or the most-recently-measured end of
WAL when reading the current timeline." But the caller always passed the
most recently measured end-of-WAL even when reading from a historic
timeline, due to an oversight on my part. Fix that.

As far as I can determine, for this to become an issue in practice, it's
necessary to have a corrupted WAL file in the archive.  SummarizeWAL checks
that every record it processes both starts and ends before switch_lsn; so if
all the WAL files in the archive are valid, SummarizeWAL will still discover
where it should stop summarizing and do the right thing.  However, if
there's a corrupted file in the WAL archive, and if it is also the case that
the end of the current timeline has advanced past the switch point, then the
incorrect maximum_lsn value can result in trying to read an invalid record
and erroring out, which leads repeatedly retrying and failing with an error
every time.

One way this could occur is if a new primary is promoted and creates a
.partial file, and the user manually renames that file to remove the suffix,
and it is then archived. In that situation, the tail end of the file need
not be valid WAL, and that could lead to a stuck WAL summarizer.

Reported-by: Fabrice Chapuis <fabrice636861@gmail.com>
Analyzed-by: Thom Brown <thom@linux.com> (using claude)
Discussion: http://postgr.es/m/CAA5-nLDdvGMkN6Z-GaHGHG5T7QWEgv4YoHO7XvOJbeD00cghNg@mail.gmail.com
Backpatch-through: 17

src/backend/postmaster/walsummarizer.c

index 4f12eaf2c8527f400dc6913904686eaf5db2a8af..8b429cb51d75f6369ff51ec7a0366cd8b7a76b10 100644 (file)
@@ -349,6 +349,7 @@ WalSummarizerMain(const void *startup_data, size_t startup_data_len)
        {
                XLogRecPtr      latest_lsn;
                TimeLineID      latest_tli;
+               XLogRecPtr      maximum_lsn;
                XLogRecPtr      end_of_summary_lsn;
 
                /* Flush any leaked data in the top-level context */
@@ -413,9 +414,10 @@ WalSummarizerMain(const void *startup_data, size_t startup_data_len)
                }
 
                /* Summarize WAL. */
+               maximum_lsn = XLogRecPtrIsValid(switch_lsn) ? switch_lsn : latest_lsn;
                end_of_summary_lsn = SummarizeWAL(current_tli,
                                                                                  current_lsn, exact,
-                                                                                 switch_lsn, latest_lsn);
+                                                                                 switch_lsn, maximum_lsn);
                Assert(XLogRecPtrIsValid(end_of_summary_lsn));
                Assert(end_of_summary_lsn >= current_lsn);