Skip to content

Commit c9f4e75

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 fd39c3c commit c9f4e75

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/backend/access/transam/xlogreader.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -722,11 +722,12 @@ XLogDecodeNextRecord(XLogReaderState *state, bool nonblocking)
722722
/* Calculate pointer to beginning of next page */
723723
targetPagePtr += XLOG_BLCKSZ;
724724

725-
/* Wait for the next page to become available */
726-
readOff = ReadPageInternal(state, targetPagePtr,
727-
Min(total_len - gotlen + SizeOfXLogShortPHD,
728-
XLOG_BLCKSZ));
729-
725+
/*
726+
* Read the page header before processing the record data, so we
727+
* can handle the case where the previous record ended as being a
728+
* partial one.
729+
*/
730+
readOff = ReadPageInternal(state, targetPagePtr, SizeOfXLogShortPHD);
730731
if (readOff == XLREAD_WOULDBLOCK)
731732
return XLREAD_WOULDBLOCK;
732733
else if (readOff < 0)
@@ -775,6 +776,15 @@ XLogDecodeNextRecord(XLogReaderState *state, bool nonblocking)
775776
goto err;
776777
}
777778

779+
/* Wait for the next page to become available */
780+
readOff = ReadPageInternal(state, targetPagePtr,
781+
Min(total_len - gotlen + SizeOfXLogShortPHD,
782+
XLOG_BLCKSZ));
783+
if (readOff == XLREAD_WOULDBLOCK)
784+
return XLREAD_WOULDBLOCK;
785+
else if (readOff < 0)
786+
goto err;
787+
778788
/* Append the continuation from this page to the buffer */
779789
pageHeaderSize = XLogPageHeaderSize(pageHeader);
780790

0 commit comments

Comments
 (0)