Add test to WAL replay to verify that xl_prev points back to the previous
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 31 May 2005 19:11:28 +0000 (19:11 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 31 May 2005 19:11:28 +0000 (19:11 +0000)
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.

src/backend/access/transam/xlog.c

index b4451ce25c93854ae88195d1d59d93dd3ceee6b5..6348d46c0b217de55e540090bdea6a90db7447c5 100644 (file)
@@ -417,8 +417,8 @@ static char *readRecordBuf = NULL;
 static uint32 readRecordBufSize = 0;
 
 /* State information for XLOG reading */
-static XLogRecPtr ReadRecPtr;
-static XLogRecPtr EndRecPtr;
+static XLogRecPtr ReadRecPtr;                          /* start of last record read */
+static XLogRecPtr EndRecPtr;                           /* end+1 of last record read */
 static XLogRecord *nextRecord = NULL;
 static TimeLineID lastPageTLI = 0;
 
@@ -2525,6 +2525,37 @@ got_record:;
                                         record->xl_rmid, RecPtr->xlogid, RecPtr->xrecoff)));
                goto next_record_is_invalid;
        }
+       if (randAccess)
+       {
+               /*
+                * We can't exactly verify the prev-link, but surely it should be
+                * less than the record's own address.
+                */
+               if (!XLByteLT(record->xl_prev, *RecPtr))
+               {
+                       ereport(emode,
+                                       (errmsg("record with incorrect prev-link %X/%X at %X/%X",
+                                                       record->xl_prev.xlogid, record->xl_prev.xrecoff,
+                                                       RecPtr->xlogid, RecPtr->xrecoff)));
+                       goto next_record_is_invalid;
+               }
+       }
+       else
+       {
+               /*
+                * Record's prev-link should exactly match our previous location.
+                * This check guards against torn WAL pages where a stale but
+                * valid-looking WAL record starts on a sector boundary.
+                */
+               if (!XLByteEQ(record->xl_prev, ReadRecPtr))
+               {
+                       ereport(emode,
+                                       (errmsg("record with incorrect prev-link %X/%X at %X/%X",
+                                                       record->xl_prev.xlogid, record->xl_prev.xrecoff,
+                                                       RecPtr->xlogid, RecPtr->xrecoff)));
+                       goto next_record_is_invalid;
+               }
+       }
 
        /*
         * Compute total length of record including any appended backup