pgsql: Fix infinite wait when reading a partially written WAL record

From: Alexander Korotkov <akorotkov(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix infinite wait when reading a partially written WAL record
Date: 2025-07-19 12:45:55
Message-ID: E1ud6ws-0075hH-2w@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix infinite wait when reading a partially written WAL record

If a crash occurs while writing a WAL record that spans multiple pages, the
recovery process marks the page with the XLP_FIRST_IS_OVERWRITE_CONTRECORD
flag. However, logical decoding currently attempts to read the full WAL
record based on its expected size before checking this flag, which can lead
to an infinite wait if the remaining data is never written (e.g., no activity
after crash).

This patch updates the logic first to read the page header and check for
the XLP_FIRST_IS_OVERWRITE_CONTRECORD flag before attempting to reconstruct
the full WAL record. If the flag is set, decoding correctly identifies
the record as incomplete and avoids waiting for WAL data that will never
arrive.

Discussion: https://postgr.es/m/CAAKRu_ZCOzQpEumLFgG_%2Biw3FTa%2BhJ4SRpxzaQBYxxM_ZAzWcA%40mail.gmail.com
Discussion: https://postgr.es/m/CALDaNm34m36PDHzsU_GdcNXU0gLTfFY5rzh9GSQv%3Dw6B%2BQVNRQ%40mail.gmail.com
Author: Vignesh C <vignesh21(at)gmail(dot)com>
Reviewed-by: Hayato Kuroda <kuroda(dot)hayato(at)fujitsu(dot)com>
Reviewed-by: Dilip Kumar <dilipbalaut(at)gmail(dot)com>
Reviewed-by: Michael Paquier <michael(at)paquier(dot)xyz>
Reviewed-by: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Backpatch-through: 13

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/b485e1c89a6dd210518eb4e26427df64831cf394

Modified Files
--------------
src/backend/access/transam/xlogreader.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Alexander Korotkov 2025-07-19 12:45:57 pgsql: Fix infinite wait when reading a partially written WAL record
Previous Message Alexander Korotkov 2025-07-19 12:45:42 pgsql: Improve recovery test 046_checkpoint_logical_slot