LCOV - code coverage report
Current view: top level - src/bin/pg_rewind - parsexlog.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta2 Lines: 80 120 66.7 %
Date: 2019-06-18 07:06:57 Functions: 4 5 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * parsexlog.c
       4             :  *    Functions for reading Write-Ahead-Log
       5             :  *
       6             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *-------------------------------------------------------------------------
      10             :  */
      11             : 
      12             : #include "postgres_fe.h"
      13             : 
      14             : #include <unistd.h>
      15             : 
      16             : #include "pg_rewind.h"
      17             : #include "filemap.h"
      18             : 
      19             : #include "access/rmgr.h"
      20             : #include "access/xlog_internal.h"
      21             : #include "access/xlogreader.h"
      22             : #include "catalog/pg_control.h"
      23             : #include "catalog/storage_xlog.h"
      24             : #include "commands/dbcommands_xlog.h"
      25             : 
      26             : 
      27             : /*
      28             :  * RmgrNames is an array of resource manager names, to make error messages
      29             :  * a bit nicer.
      30             :  */
      31             : #define PG_RMGR(symname,name,redo,desc,identify,startup,cleanup,mask) \
      32             :   name,
      33             : 
      34             : static const char *RmgrNames[RM_MAX_ID + 1] = {
      35             : #include "access/rmgrlist.h"
      36             : };
      37             : 
      38             : static void extractPageInfo(XLogReaderState *record);
      39             : 
      40             : static int  xlogreadfd = -1;
      41             : static XLogSegNo xlogreadsegno = -1;
      42             : static char xlogfpath[MAXPGPATH];
      43             : 
      44             : typedef struct XLogPageReadPrivate
      45             : {
      46             :     const char *datadir;
      47             :     int         tliIndex;
      48             : } XLogPageReadPrivate;
      49             : 
      50             : static int  SimpleXLogPageRead(XLogReaderState *xlogreader,
      51             :                                XLogRecPtr targetPagePtr,
      52             :                                int reqLen, XLogRecPtr targetRecPtr, char *readBuf,
      53             :                                TimeLineID *pageTLI);
      54             : 
      55             : /*
      56             :  * Read WAL from the datadir/pg_wal, starting from 'startpoint' on timeline
      57             :  * index 'tliIndex' in target timeline history, until 'endpoint'. Make note of
      58             :  * the data blocks touched by the WAL records, and return them in a page map.
      59             :  */
      60             : void
      61          16 : extractPageMap(const char *datadir, XLogRecPtr startpoint, int tliIndex,
      62             :                XLogRecPtr endpoint)
      63             : {
      64             :     XLogRecord *record;
      65             :     XLogReaderState *xlogreader;
      66             :     char       *errormsg;
      67             :     XLogPageReadPrivate private;
      68             : 
      69          16 :     private.datadir = datadir;
      70          16 :     private.tliIndex = tliIndex;
      71          16 :     xlogreader = XLogReaderAllocate(WalSegSz, &SimpleXLogPageRead,
      72             :                                     &private);
      73          16 :     if (xlogreader == NULL)
      74           0 :         pg_fatal("out of memory");
      75             : 
      76             :     do
      77             :     {
      78       81832 :         record = XLogReadRecord(xlogreader, startpoint, &errormsg);
      79             : 
      80       81832 :         if (record == NULL)
      81             :         {
      82             :             XLogRecPtr  errptr;
      83             : 
      84           0 :             errptr = startpoint ? startpoint : xlogreader->EndRecPtr;
      85             : 
      86           0 :             if (errormsg)
      87           0 :                 pg_fatal("could not read WAL record at %X/%X: %s",
      88             :                          (uint32) (errptr >> 32), (uint32) (errptr),
      89             :                          errormsg);
      90             :             else
      91           0 :                 pg_fatal("could not read WAL record at %X/%X",
      92             :                          (uint32) (startpoint >> 32),
      93             :                          (uint32) (startpoint));
      94             :         }
      95             : 
      96       81832 :         extractPageInfo(xlogreader);
      97             : 
      98       81832 :         startpoint = InvalidXLogRecPtr; /* continue reading at next record */
      99             : 
     100       81832 :     } while (xlogreader->ReadRecPtr != endpoint);
     101             : 
     102          16 :     XLogReaderFree(xlogreader);
     103          16 :     if (xlogreadfd != -1)
     104             :     {
     105          16 :         close(xlogreadfd);
     106          16 :         xlogreadfd = -1;
     107             :     }
     108          16 : }
     109             : 
     110             : /*
     111             :  * Reads one WAL record. Returns the end position of the record, without
     112             :  * doing anything with the record itself.
     113             :  */
     114             : XLogRecPtr
     115           0 : readOneRecord(const char *datadir, XLogRecPtr ptr, int tliIndex)
     116             : {
     117             :     XLogRecord *record;
     118             :     XLogReaderState *xlogreader;
     119             :     char       *errormsg;
     120             :     XLogPageReadPrivate private;
     121             :     XLogRecPtr  endptr;
     122             : 
     123           0 :     private.datadir = datadir;
     124           0 :     private.tliIndex = tliIndex;
     125           0 :     xlogreader = XLogReaderAllocate(WalSegSz, &SimpleXLogPageRead,
     126             :                                     &private);
     127           0 :     if (xlogreader == NULL)
     128           0 :         pg_fatal("out of memory");
     129             : 
     130           0 :     record = XLogReadRecord(xlogreader, ptr, &errormsg);
     131           0 :     if (record == NULL)
     132             :     {
     133           0 :         if (errormsg)
     134           0 :             pg_fatal("could not read WAL record at %X/%X: %s",
     135             :                      (uint32) (ptr >> 32), (uint32) (ptr), errormsg);
     136             :         else
     137           0 :             pg_fatal("could not read WAL record at %X/%X",
     138             :                      (uint32) (ptr >> 32), (uint32) (ptr));
     139             :     }
     140           0 :     endptr = xlogreader->EndRecPtr;
     141             : 
     142           0 :     XLogReaderFree(xlogreader);
     143           0 :     if (xlogreadfd != -1)
     144             :     {
     145           0 :         close(xlogreadfd);
     146           0 :         xlogreadfd = -1;
     147             :     }
     148             : 
     149           0 :     return endptr;
     150             : }
     151             : 
     152             : /*
     153             :  * Find the previous checkpoint preceding given WAL location.
     154             :  */
     155             : void
     156          16 : findLastCheckpoint(const char *datadir, XLogRecPtr forkptr, int tliIndex,
     157             :                    XLogRecPtr *lastchkptrec, TimeLineID *lastchkpttli,
     158             :                    XLogRecPtr *lastchkptredo)
     159             : {
     160             :     /* Walk backwards, starting from the given record */
     161             :     XLogRecord *record;
     162             :     XLogRecPtr  searchptr;
     163             :     XLogReaderState *xlogreader;
     164             :     char       *errormsg;
     165             :     XLogPageReadPrivate private;
     166             : 
     167             :     /*
     168             :      * The given fork pointer points to the end of the last common record,
     169             :      * which is not necessarily the beginning of the next record, if the
     170             :      * previous record happens to end at a page boundary. Skip over the page
     171             :      * header in that case to find the next record.
     172             :      */
     173          16 :     if (forkptr % XLOG_BLCKSZ == 0)
     174             :     {
     175           4 :         if (XLogSegmentOffset(forkptr, WalSegSz) == 0)
     176           4 :             forkptr += SizeOfXLogLongPHD;
     177             :         else
     178           0 :             forkptr += SizeOfXLogShortPHD;
     179             :     }
     180             : 
     181          16 :     private.datadir = datadir;
     182          16 :     private.tliIndex = tliIndex;
     183          16 :     xlogreader = XLogReaderAllocate(WalSegSz, &SimpleXLogPageRead,
     184             :                                     &private);
     185          16 :     if (xlogreader == NULL)
     186           0 :         pg_fatal("out of memory");
     187             : 
     188          16 :     searchptr = forkptr;
     189             :     for (;;)
     190         208 :     {
     191             :         uint8       info;
     192             : 
     193         224 :         record = XLogReadRecord(xlogreader, searchptr, &errormsg);
     194             : 
     195         224 :         if (record == NULL)
     196             :         {
     197           0 :             if (errormsg)
     198           0 :                 pg_fatal("could not find previous WAL record at %X/%X: %s",
     199             :                          (uint32) (searchptr >> 32), (uint32) (searchptr),
     200             :                          errormsg);
     201             :             else
     202           0 :                 pg_fatal("could not find previous WAL record at %X/%X",
     203             :                          (uint32) (searchptr >> 32), (uint32) (searchptr));
     204             :         }
     205             : 
     206             :         /*
     207             :          * Check if it is a checkpoint record. This checkpoint record needs to
     208             :          * be the latest checkpoint before WAL forked and not the checkpoint
     209             :          * where the master has been stopped to be rewinded.
     210             :          */
     211         224 :         info = XLogRecGetInfo(xlogreader) & ~XLR_INFO_MASK;
     212         432 :         if (searchptr < forkptr &&
     213         240 :             XLogRecGetRmid(xlogreader) == RM_XLOG_ID &&
     214          32 :             (info == XLOG_CHECKPOINT_SHUTDOWN ||
     215             :              info == XLOG_CHECKPOINT_ONLINE))
     216             :         {
     217             :             CheckPoint  checkPoint;
     218             : 
     219          16 :             memcpy(&checkPoint, XLogRecGetData(xlogreader), sizeof(CheckPoint));
     220          16 :             *lastchkptrec = searchptr;
     221          16 :             *lastchkpttli = checkPoint.ThisTimeLineID;
     222          16 :             *lastchkptredo = checkPoint.redo;
     223          16 :             break;
     224             :         }
     225             : 
     226             :         /* Walk backwards to previous record. */
     227         208 :         searchptr = record->xl_prev;
     228             :     }
     229             : 
     230          16 :     XLogReaderFree(xlogreader);
     231          16 :     if (xlogreadfd != -1)
     232             :     {
     233          16 :         close(xlogreadfd);
     234          16 :         xlogreadfd = -1;
     235             :     }
     236          16 : }
     237             : 
     238             : /* XLogreader callback function, to read a WAL page */
     239             : static int
     240        1360 : SimpleXLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr,
     241             :                    int reqLen, XLogRecPtr targetRecPtr, char *readBuf,
     242             :                    TimeLineID *pageTLI)
     243             : {
     244        1360 :     XLogPageReadPrivate *private = (XLogPageReadPrivate *) xlogreader->private_data;
     245             :     uint32      targetPageOff;
     246             :     XLogRecPtr  targetSegEnd;
     247             :     XLogSegNo   targetSegNo;
     248             :     int         r;
     249             : 
     250        1360 :     XLByteToSeg(targetPagePtr, targetSegNo, WalSegSz);
     251        1360 :     XLogSegNoOffsetToRecPtr(targetSegNo + 1, 0, WalSegSz, targetSegEnd);
     252        1360 :     targetPageOff = XLogSegmentOffset(targetPagePtr, WalSegSz);
     253             : 
     254             :     /*
     255             :      * See if we need to switch to a new segment because the requested record
     256             :      * is not in the currently open one.
     257             :      */
     258        2688 :     if (xlogreadfd >= 0 &&
     259        1328 :         !XLByteInSeg(targetPagePtr, xlogreadsegno, WalSegSz))
     260             :     {
     261           8 :         close(xlogreadfd);
     262           8 :         xlogreadfd = -1;
     263             :     }
     264             : 
     265        1360 :     XLByteToSeg(targetPagePtr, xlogreadsegno, WalSegSz);
     266             : 
     267        1360 :     if (xlogreadfd < 0)
     268             :     {
     269             :         char        xlogfname[MAXFNAMELEN];
     270             : 
     271             :         /*
     272             :          * Since incomplete segments are copied into next timelines, switch to
     273             :          * the timeline holding the required segment. Assuming this scan can
     274             :          * be done both forward and backward, consider also switching timeline
     275             :          * accordingly.
     276             :          */
     277          80 :         while (private->tliIndex < targetNentries - 1 &&
     278           0 :                targetHistory[private->tliIndex].end < targetSegEnd)
     279           0 :             private->tliIndex++;
     280          80 :         while (private->tliIndex > 0 &&
     281           0 :                targetHistory[private->tliIndex].begin >= targetSegEnd)
     282           0 :             private->tliIndex--;
     283             : 
     284          40 :         XLogFileName(xlogfname, targetHistory[private->tliIndex].tli,
     285             :                      xlogreadsegno, WalSegSz);
     286             : 
     287          40 :         snprintf(xlogfpath, MAXPGPATH, "%s/" XLOGDIR "/%s", private->datadir, xlogfname);
     288             : 
     289          40 :         xlogreadfd = open(xlogfpath, O_RDONLY | PG_BINARY, 0);
     290             : 
     291          40 :         if (xlogreadfd < 0)
     292             :         {
     293           0 :             pg_log_error("could not open file \"%s\": %m", xlogfpath);
     294           0 :             return -1;
     295             :         }
     296             :     }
     297             : 
     298             :     /*
     299             :      * At this point, we have the right segment open.
     300             :      */
     301             :     Assert(xlogreadfd != -1);
     302             : 
     303             :     /* Read the requested page */
     304        1360 :     if (lseek(xlogreadfd, (off_t) targetPageOff, SEEK_SET) < 0)
     305             :     {
     306           0 :         pg_log_error("could not seek in file \"%s\": %m", xlogfpath);
     307           0 :         return -1;
     308             :     }
     309             : 
     310             : 
     311        1360 :     r = read(xlogreadfd, readBuf, XLOG_BLCKSZ);
     312        1360 :     if (r != XLOG_BLCKSZ)
     313             :     {
     314           0 :         if (r < 0)
     315           0 :             pg_log_error("could not read file \"%s\": %m", xlogfpath);
     316             :         else
     317           0 :             pg_log_error("could not read file \"%s\": read %d of %zu",
     318             :                          xlogfpath, r, (Size) XLOG_BLCKSZ);
     319             : 
     320           0 :         return -1;
     321             :     }
     322             : 
     323             :     Assert(targetSegNo == xlogreadsegno);
     324             : 
     325        1360 :     *pageTLI = targetHistory[private->tliIndex].tli;
     326        1360 :     return XLOG_BLCKSZ;
     327             : }
     328             : 
     329             : /*
     330             :  * Extract information on which blocks the current record modifies.
     331             :  */
     332             : static void
     333       81832 : extractPageInfo(XLogReaderState *record)
     334             : {
     335             :     int         block_id;
     336       81832 :     RmgrId      rmid = XLogRecGetRmid(record);
     337       81832 :     uint8       info = XLogRecGetInfo(record);
     338       81832 :     uint8       rminfo = info & ~XLR_INFO_MASK;
     339             : 
     340             :     /* Is this a special record type that I recognize? */
     341             : 
     342       81832 :     if (rmid == RM_DBASE_ID && rminfo == XLOG_DBASE_CREATE)
     343             :     {
     344             :         /*
     345             :          * New databases can be safely ignored. It won't be present in the
     346             :          * source system, so it will be deleted. There's one corner-case,
     347             :          * though: if a new, different, database is also created in the source
     348             :          * system, we'll see that the files already exist and not copy them.
     349             :          * That's OK, though; WAL replay of creating the new database, from
     350             :          * the source systems's WAL, will re-copy the new database,
     351             :          * overwriting the database created in the target system.
     352             :          */
     353             :     }
     354       81828 :     else if (rmid == RM_DBASE_ID && rminfo == XLOG_DBASE_DROP)
     355             :     {
     356             :         /*
     357             :          * An existing database was dropped. We'll see that the files don't
     358             :          * exist in the target data dir, and copy them in toto from the source
     359             :          * system. No need to do anything special here.
     360             :          */
     361             :     }
     362       81828 :     else if (rmid == RM_SMGR_ID && rminfo == XLOG_SMGR_CREATE)
     363             :     {
     364             :         /*
     365             :          * We can safely ignore these. The file will be removed from the
     366             :          * target, if it doesn't exist in source system. If a file with same
     367             :          * name is created in source system, too, there will be WAL records
     368             :          * for all the blocks in it.
     369             :          */
     370             :     }
     371       81820 :     else if (rmid == RM_SMGR_ID && rminfo == XLOG_SMGR_TRUNCATE)
     372             :     {
     373             :         /*
     374             :          * We can safely ignore these. When we compare the sizes later on,
     375             :          * we'll notice that they differ, and copy the missing tail from
     376             :          * source system.
     377             :          */
     378             :     }
     379       81816 :     else if (info & XLR_SPECIAL_REL_UPDATE)
     380             :     {
     381             :         /*
     382             :          * This record type modifies a relation file in some special way, but
     383             :          * we don't recognize the type. That's bad - we don't know how to
     384             :          * track that change.
     385             :          */
     386           0 :         pg_fatal("WAL record modifies a relation, but record type is not recognized: "
     387             :                  "lsn: %X/%X, rmgr: %s, info: %02X",
     388             :                  (uint32) (record->ReadRecPtr >> 32), (uint32) (record->ReadRecPtr),
     389             :                  RmgrNames[rmid], info);
     390             :     }
     391             : 
     392      163880 :     for (block_id = 0; block_id <= record->max_block_id; block_id++)
     393             :     {
     394             :         RelFileNode rnode;
     395             :         ForkNumber  forknum;
     396             :         BlockNumber blkno;
     397             : 
     398       82048 :         if (!XLogRecGetBlockTag(record, block_id, &rnode, &forknum, &blkno))
     399         368 :             continue;
     400             : 
     401             :         /* We only care about the main fork; others are copied in toto */
     402       82048 :         if (forknum != MAIN_FORKNUM)
     403         368 :             continue;
     404             : 
     405       81680 :         process_block_change(forknum, rnode, blkno);
     406             :     }
     407       81832 : }

Generated by: LCOV version 1.13