Skip to content

Commit 762f352

Browse files
committed
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@gmail.com> Reviewed-by: Hayato Kuroda <kuroda.hayato@fujitsu.com> Reviewed-by: Dilip Kumar <dilipbalaut@gmail.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Reviewed-by: Alexander Korotkov <aekorotkov@gmail.com> Backpatch-through: 13
1 parent 9dcd1aa commit 762f352

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/backend/access/transam/xlogreader.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,12 @@ XLogReadRecord(XLogReaderState *state, char **errormsg)
407407
/* Calculate pointer to beginning of next page */
408408
targetPagePtr += XLOG_BLCKSZ;
409409

410-
/* Wait for the next page to become available */
411-
readOff = ReadPageInternal(state, targetPagePtr,
412-
Min(total_len - gotlen + SizeOfXLogShortPHD,
413-
XLOG_BLCKSZ));
414-
410+
/*
411+
* Read the page header before processing the record data, so we
412+
* can handle the case where the previous record ended as being a
413+
* partial one.
414+
*/
415+
readOff = ReadPageInternal(state, targetPagePtr, SizeOfXLogShortPHD);
415416
if (readOff < 0)
416417
goto err;
417418

@@ -458,6 +459,13 @@ XLogReadRecord(XLogReaderState *state, char **errormsg)
458459
goto err;
459460
}
460461

462+
/* Wait for the next page to become available */
463+
readOff = ReadPageInternal(state, targetPagePtr,
464+
Min(total_len - gotlen + SizeOfXLogShortPHD,
465+
XLOG_BLCKSZ));
466+
if (readOff < 0)
467+
goto err;
468+
461469
/* Append the continuation from this page to the buffer */
462470
pageHeaderSize = XLogPageHeaderSize(pageHeader);
463471

0 commit comments

Comments
 (0)