Skip to content

Commit a91fa39

Browse files
committed
Add test to WAL replay to verify that xl_prev points back to the previous
WAL record; this is necessary to be sure we recognize stale WAL records when a WAL page was only partially written during a system crash.
1 parent 5b3625f commit a91fa39

File tree

1 file changed

+34
-3
lines changed
  • src/backend/access/transam

1 file changed

+34
-3
lines changed

src/backend/access/transam/xlog.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.193 2005/05/20 14:53:25 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.194 2005/05/31 19:10:28 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -417,8 +417,8 @@ static char *readRecordBuf = NULL;
417417
static uint32 readRecordBufSize = 0;
418418

419419
/* State information for XLOG reading */
420-
static XLogRecPtr ReadRecPtr;
421-
static XLogRecPtr EndRecPtr;
420+
static XLogRecPtr ReadRecPtr; /* start of last record read */
421+
static XLogRecPtr EndRecPtr; /* end+1 of last record read */
422422
static XLogRecord *nextRecord = NULL;
423423
static TimeLineID lastPageTLI = 0;
424424

@@ -2525,6 +2525,37 @@ got_record:;
25252525
record->xl_rmid, RecPtr->xlogid, RecPtr->xrecoff)));
25262526
goto next_record_is_invalid;
25272527
}
2528+
if (randAccess)
2529+
{
2530+
/*
2531+
* We can't exactly verify the prev-link, but surely it should be
2532+
* less than the record's own address.
2533+
*/
2534+
if (!XLByteLT(record->xl_prev, *RecPtr))
2535+
{
2536+
ereport(emode,
2537+
(errmsg("record with incorrect prev-link %X/%X at %X/%X",
2538+
record->xl_prev.xlogid, record->xl_prev.xrecoff,
2539+
RecPtr->xlogid, RecPtr->xrecoff)));
2540+
goto next_record_is_invalid;
2541+
}
2542+
}
2543+
else
2544+
{
2545+
/*
2546+
* Record's prev-link should exactly match our previous location.
2547+
* This check guards against torn WAL pages where a stale but
2548+
* valid-looking WAL record starts on a sector boundary.
2549+
*/
2550+
if (!XLByteEQ(record->xl_prev, ReadRecPtr))
2551+
{
2552+
ereport(emode,
2553+
(errmsg("record with incorrect prev-link %X/%X at %X/%X",
2554+
record->xl_prev.xlogid, record->xl_prev.xrecoff,
2555+
RecPtr->xlogid, RecPtr->xrecoff)));
2556+
goto next_record_is_invalid;
2557+
}
2558+
}
25282559

25292560
/*
25302561
* Compute total length of record including any appended backup

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy