Skip to content

Commit c125544

Browse files
committed
Fix init of WAL page header at startup
If the primary is started at an LSN within the first of a 16 MB WAL segment, the "long XLOG page header" at the beginning of the segment was not initialized correctly. That has gone unnnoticed, because under normal circumstances, nothing looks at the page header. The WAL that is streamed to the safekeepers starts at the new record's LSN, not at the beginning of the page, so that bogus page header didn't propagate elsewhere, and a primary server doesn't normally read the WAL its written. Which is good because the contents of the page would be bogus anyway, as it wouldn't contain any of the records before the LSN where the new record is written. Except that in the following cases a primary does read its own WAL: 1. When there are two-phase transactions in prepared state at checkpoint. The checkpointer reads the two-phase state from the XLOG_XACT_PREPARE record, and writes it to a file in pg_twophase/. 2. Logical decoding reads the WAL starting from the replication slot's restart LSN. This PR fixes the problem with two-phase transactions. For that, it's sufficient to initialize the page header correctly. The checkpointer only needs to read XLOG_XACT_PREPARE records that were generated after the server startup, so it's still OK that older WAL is missing / bogus. I have not investigated if we have a problem with logical decoding, however. Let's deal with that separately.
1 parent 3ec6e24 commit c125544

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/backend/access/transam/xlogrecovery.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,25 +1650,37 @@ FinishWalRecovery(void)
16501650
}
16511651
else
16521652
{
1653-
int offs = endOfLog % XLOG_BLCKSZ;
1654-
char *page = palloc0(offs);
1655-
XLogRecPtr pageBeginPtr = endOfLog - offs;
1656-
int lastPageSize = ((pageBeginPtr % wal_segment_size) == 0) ? SizeOfXLogLongPHD : SizeOfXLogShortPHD;
1657-
1658-
XLogPageHeader xlogPageHdr = (XLogPageHeader) (page);
1653+
int offs = endOfLog % XLOG_BLCKSZ;
1654+
XLogRecPtr pageBeginPtr = endOfLog - offs;
1655+
bool isLongHeader = (pageBeginPtr % wal_segment_size) == 0;
1656+
int lastPageSize = isLongHeader ? SizeOfXLogLongPHD : SizeOfXLogShortPHD;
1657+
char *page = palloc0(offs);
1658+
XLogPageHeader xlogPageHdr = (XLogPageHeader) page;
16591659

16601660
xlogPageHdr->xlp_pageaddr = pageBeginPtr;
16611661
xlogPageHdr->xlp_magic = XLOG_PAGE_MAGIC;
16621662
xlogPageHdr->xlp_tli = recoveryTargetTLI;
1663+
xlogPageHdr->xlp_info = 0;
16631664
/*
16641665
* If we start writing with offset from page beginning, pretend in
16651666
* page header there is a record ending where actual data will
16661667
* start.
16671668
*/
16681669
xlogPageHdr->xlp_rem_len = offs - lastPageSize;
1669-
xlogPageHdr->xlp_info = (xlogPageHdr->xlp_rem_len > 0) ? XLP_FIRST_IS_CONTRECORD : 0;
1670+
if (xlogPageHdr->xlp_rem_len > 0)
1671+
xlogPageHdr->xlp_info |= XLP_FIRST_IS_CONTRECORD;
16701672
readOff = XLogSegmentOffset(pageBeginPtr, wal_segment_size);
16711673

1674+
if (isLongHeader)
1675+
{
1676+
XLogLongPageHeader longHdr = (XLogLongPageHeader) page;
1677+
1678+
longHdr->xlp_sysid = GetSystemIdentifier();
1679+
longHdr->xlp_seg_size = wal_segment_size;
1680+
longHdr->xlp_xlog_blcksz = XLOG_BLCKSZ;
1681+
1682+
xlogPageHdr->xlp_info |= XLP_LONG_HEADER;
1683+
}
16721684
result->lastPageBeginPtr = pageBeginPtr;
16731685
result->lastPage = page;
16741686
elog(LOG, "Continue writing WAL at %X/%X", LSN_FORMAT_ARGS(xlogreader->EndRecPtr));

0 commit comments

Comments
 (0)