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