Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3f2701c

Browse files
committed
Prevent WAL corruption after a standby promotion.
When a PostgreSQL instance performing archive recovery but not using standby mode is promoted, and the last WAL segment that it attempted to read ended in a partial record, the previous code would create invalid WAL on the new timeline. The WAL from the previously timeline would be copied to the new timeline up until the end of the last valid record, but instead of beginning to write WAL at immediately afterwards, the promoted server would write an overwrite contrecord at the beginning of the next segment. The end of the previous segment would be left as all-zeroes, resulting in failures if anything tried to read WAL from that file. The root of the issue is that ReadRecord() decides whether to set abortedRecPtr and missingContrecPtr based on the value of StandbyMode, but ReadRecord() switches to a new timeline based on the value of ArchiveRecoveryRequested. We shouldn't try to write an overwrite contrecord if we're switching to a new timeline, so change the test in ReadRecod() to check ArchiveRecoveryRequested instead. Code fix by Dilip Kumar. Comments by me incorporating suggested language from Álvaro Herrera. Further review from Kyotaro Horiguchi and Sami Imseih. Discussion: http://postgr.es/m/CAFiTN-t7umki=PK8dT1tcPV=mOUe2vNhHML6b3T7W7qqvvajjg@mail.gmail.com Discussion: http://postgr.es/m/FB0DEA0B-E14E-43A0-811F-C1AE93D00FF3%40amazon.com
1 parent 4079d91 commit 3f2701c

File tree

1 file changed

+19
-5
lines changed
  • src/backend/access/transam

1 file changed

+19
-5
lines changed

src/backend/access/transam/xlog.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4374,12 +4374,18 @@ ReadRecord(XLogReaderState *xlogreader, int emode,
43744374
if (record == NULL)
43754375
{
43764376
/*
4377-
* When not in standby mode we find that WAL ends in an incomplete
4378-
* record, keep track of that record. After recovery is done,
4379-
* we'll write a record to indicate downstream WAL readers that
4380-
* that portion is to be ignored.
4377+
* When we find that WAL ends in an incomplete record, keep track
4378+
* of that record. After recovery is done, we'll write a record to
4379+
* indicate to downstream WAL readers that that portion is to be
4380+
* ignored.
4381+
*
4382+
* However, when ArchiveRecoveryRequested = true, we're going to
4383+
* switch to a new timeline at the end of recovery. We will only
4384+
* copy WAL over to the new timeline up to the end of the last
4385+
* complete record, so if we did this, we would later create an
4386+
* overwrite contrecord in the wrong place, breaking everything.
43814387
*/
4382-
if (!StandbyMode &&
4388+
if (!ArchiveRecoveryRequested &&
43834389
!XLogRecPtrIsInvalid(xlogreader->abortedRecPtr))
43844390
{
43854391
abortedRecPtr = xlogreader->abortedRecPtr;
@@ -7713,6 +7719,14 @@ StartupXLOG(void)
77137719
*/
77147720
if (!XLogRecPtrIsInvalid(missingContrecPtr))
77157721
{
7722+
/*
7723+
* We should only have a missingContrecPtr if we're not switching to
7724+
* a new timeline. When a timeline switch occurs, WAL is copied from
7725+
* the old timeline to the new only up to the end of the last complete
7726+
* record, so there can't be an incomplete WAL record that we need to
7727+
* disregard.
7728+
*/
7729+
Assert(ThisTimeLineID == PrevTimeLineID);
77167730
Assert(!XLogRecPtrIsInvalid(abortedRecPtr));
77177731
EndOfLog = missingContrecPtr;
77187732
}

0 commit comments

Comments
 (0)