PostgreSQL Source Code  git master
parsexlog.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include "pg_rewind.h"
#include "filemap.h"
#include "access/rmgr.h"
#include "access/xlog_internal.h"
#include "access/xlogreader.h"
#include "catalog/pg_control.h"
#include "catalog/storage_xlog.h"
#include "commands/dbcommands_xlog.h"
#include "access/rmgrlist.h"
Include dependency graph for parsexlog.c:

Go to the source code of this file.

Data Structures

struct  XLogPageReadPrivate
 

Macros

#define PG_RMGR(symname, name, redo, desc, identify, startup, cleanup, mask)   name,
 

Typedefs

typedef struct XLogPageReadPrivate XLogPageReadPrivate
 

Functions

static void extractPageInfo (XLogReaderState *record)
 
static int SimpleXLogPageRead (XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf)
 
void extractPageMap (const char *datadir, XLogRecPtr startpoint, int tliIndex, XLogRecPtr endpoint)
 
XLogRecPtr readOneRecord (const char *datadir, XLogRecPtr ptr, int tliIndex)
 
void findLastCheckpoint (const char *datadir, XLogRecPtr forkptr, int tliIndex, XLogRecPtr *lastchkptrec, TimeLineID *lastchkpttli, XLogRecPtr *lastchkptredo)
 

Variables

static const char * RmgrNames [RM_MAX_ID+1]
 
static int xlogreadfd = -1
 
static XLogSegNo xlogreadsegno = -1
 
static char xlogfpath [MAXPGPATH]
 

Macro Definition Documentation

◆ PG_RMGR

#define PG_RMGR (   symname,
  name,
  redo,
  desc,
  identify,
  startup,
  cleanup,
  mask 
)    name,

Definition at line 31 of file parsexlog.c.

Typedef Documentation

◆ XLogPageReadPrivate

Function Documentation

◆ extractPageInfo()

static void extractPageInfo ( XLogReaderState record)
static

Definition at line 327 of file parsexlog.c.

References MAIN_FORKNUM, XLogReaderState::max_block_id, pg_fatal, process_block_change(), XLogReaderState::ReadRecPtr, RmgrNames, XLOG_DBASE_CREATE, XLOG_DBASE_DROP, XLOG_SMGR_CREATE, XLOG_SMGR_TRUNCATE, XLogRecGetBlockTag(), XLogRecGetInfo, XLogRecGetRmid, XLR_INFO_MASK, and XLR_SPECIAL_REL_UPDATE.

Referenced by extractPageMap().

328 {
329  int block_id;
330  RmgrId rmid = XLogRecGetRmid(record);
331  uint8 info = XLogRecGetInfo(record);
332  uint8 rminfo = info & ~XLR_INFO_MASK;
333 
334  /* Is this a special record type that I recognize? */
335 
336  if (rmid == RM_DBASE_ID && rminfo == XLOG_DBASE_CREATE)
337  {
338  /*
339  * New databases can be safely ignored. It won't be present in the
340  * source system, so it will be deleted. There's one corner-case,
341  * though: if a new, different, database is also created in the source
342  * system, we'll see that the files already exist and not copy them.
343  * That's OK, though; WAL replay of creating the new database, from
344  * the source systems's WAL, will re-copy the new database,
345  * overwriting the database created in the target system.
346  */
347  }
348  else if (rmid == RM_DBASE_ID && rminfo == XLOG_DBASE_DROP)
349  {
350  /*
351  * An existing database was dropped. We'll see that the files don't
352  * exist in the target data dir, and copy them in toto from the source
353  * system. No need to do anything special here.
354  */
355  }
356  else if (rmid == RM_SMGR_ID && rminfo == XLOG_SMGR_CREATE)
357  {
358  /*
359  * We can safely ignore these. The file will be removed from the
360  * target, if it doesn't exist in source system. If a file with same
361  * name is created in source system, too, there will be WAL records
362  * for all the blocks in it.
363  */
364  }
365  else if (rmid == RM_SMGR_ID && rminfo == XLOG_SMGR_TRUNCATE)
366  {
367  /*
368  * We can safely ignore these. When we compare the sizes later on,
369  * we'll notice that they differ, and copy the missing tail from
370  * source system.
371  */
372  }
373  else if (info & XLR_SPECIAL_REL_UPDATE)
374  {
375  /*
376  * This record type modifies a relation file in some special way, but
377  * we don't recognize the type. That's bad - we don't know how to
378  * track that change.
379  */
380  pg_fatal("WAL record modifies a relation, but record type is not recognized: "
381  "lsn: %X/%X, rmgr: %s, info: %02X",
382  (uint32) (record->ReadRecPtr >> 32), (uint32) (record->ReadRecPtr),
383  RmgrNames[rmid], info);
384  }
385 
386  for (block_id = 0; block_id <= record->max_block_id; block_id++)
387  {
388  RelFileNode rnode;
389  ForkNumber forknum;
390  BlockNumber blkno;
391 
392  if (!XLogRecGetBlockTag(record, block_id, &rnode, &forknum, &blkno))
393  continue;
394 
395  /* We only care about the main fork; others are copied in toto */
396  if (forknum != MAIN_FORKNUM)
397  continue;
398 
399  process_block_change(forknum, rnode, blkno);
400  }
401 }
#define XLR_SPECIAL_REL_UPDATE
Definition: xlogrecord.h:71
unsigned char uint8
Definition: c.h:356
#define pg_fatal(...)
Definition: pg_rewind.h:43
static const char * RmgrNames[RM_MAX_ID+1]
Definition: parsexlog.c:34
uint32 BlockNumber
Definition: block.h:31
void process_block_change(ForkNumber forknum, RelFileNode rnode, BlockNumber blkno)
Definition: filemap.c:418
#define XLOG_SMGR_CREATE
Definition: storage_xlog.h:30
#define XLOG_DBASE_DROP
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:132
unsigned int uint32
Definition: c.h:358
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:242
ForkNumber
Definition: relpath.h:40
#define XLOG_SMGR_TRUNCATE
Definition: storage_xlog.h:31
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1367
uint8 RmgrId
Definition: rmgr.h:11
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define XLOG_DBASE_CREATE
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:243

◆ extractPageMap()

void extractPageMap ( const char *  datadir,
XLogRecPtr  startpoint,
int  tliIndex,
XLogRecPtr  endpoint 
)

Definition at line 59 of file parsexlog.c.

References close, XLogReaderState::EndRecPtr, extractPageInfo(), InvalidXLogRecPtr, pg_fatal, XLogReaderState::ReadRecPtr, SimpleXLogPageRead(), XLogPageReadPrivate::tliIndex, WalSegSz, XLogReaderAllocate(), XLogReaderFree(), xlogreadfd, and XLogReadRecord().

Referenced by main().

61 {
62  XLogRecord *record;
63  XLogReaderState *xlogreader;
64  char *errormsg;
65  XLogPageReadPrivate private;
66 
67  private.tliIndex = tliIndex;
69  &private);
70  if (xlogreader == NULL)
71  pg_fatal("out of memory");
72 
73  do
74  {
75  record = XLogReadRecord(xlogreader, startpoint, &errormsg);
76 
77  if (record == NULL)
78  {
79  XLogRecPtr errptr;
80 
81  errptr = startpoint ? startpoint : xlogreader->EndRecPtr;
82 
83  if (errormsg)
84  pg_fatal("could not read WAL record at %X/%X: %s",
85  (uint32) (errptr >> 32), (uint32) (errptr),
86  errormsg);
87  else
88  pg_fatal("could not read WAL record at %X/%X",
89  (uint32) (errptr >> 32), (uint32) (errptr));
90  }
91 
92  extractPageInfo(xlogreader);
93 
94  startpoint = InvalidXLogRecPtr; /* continue reading at next record */
95 
96  } while (xlogreader->ReadRecPtr != endpoint);
97 
98  XLogReaderFree(xlogreader);
99  if (xlogreadfd != -1)
100  {
101  close(xlogreadfd);
102  xlogreadfd = -1;
103  }
104 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define pg_fatal(...)
Definition: pg_rewind.h:43
static int SimpleXLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf)
Definition: parsexlog.c:234
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:237
XLogRecPtr EndRecPtr
Definition: xlogreader.h:133
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:132
char * datadir
unsigned int uint32
Definition: c.h:358
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:133
static int xlogreadfd
Definition: parsexlog.c:40
static void extractPageInfo(XLogReaderState *record)
Definition: parsexlog.c:327
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int WalSegSz
Definition: pg_standby.c:39
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:71
#define close(a)
Definition: win32.h:12

◆ findLastCheckpoint()

void findLastCheckpoint ( const char *  datadir,
XLogRecPtr  forkptr,
int  tliIndex,
XLogRecPtr lastchkptrec,
TimeLineID lastchkpttli,
XLogRecPtr lastchkptredo 
)

Definition at line 151 of file parsexlog.c.

References close, pg_fatal, CheckPoint::redo, SimpleXLogPageRead(), SizeOfXLogLongPHD, SizeOfXLogShortPHD, CheckPoint::ThisTimeLineID, XLogPageReadPrivate::tliIndex, WalSegSz, XLogRecord::xl_prev, XLOG_CHECKPOINT_ONLINE, XLOG_CHECKPOINT_SHUTDOWN, XLogReaderAllocate(), XLogReaderFree(), xlogreadfd, XLogReadRecord(), XLogRecGetData, XLogRecGetInfo, XLogRecGetRmid, XLogSegmentOffset, and XLR_INFO_MASK.

Referenced by main().

154 {
155  /* Walk backwards, starting from the given record */
156  XLogRecord *record;
157  XLogRecPtr searchptr;
158  XLogReaderState *xlogreader;
159  char *errormsg;
160  XLogPageReadPrivate private;
161 
162  /*
163  * The given fork pointer points to the end of the last common record,
164  * which is not necessarily the beginning of the next record, if the
165  * previous record happens to end at a page boundary. Skip over the page
166  * header in that case to find the next record.
167  */
168  if (forkptr % XLOG_BLCKSZ == 0)
169  {
170  if (XLogSegmentOffset(forkptr, WalSegSz) == 0)
171  forkptr += SizeOfXLogLongPHD;
172  else
173  forkptr += SizeOfXLogShortPHD;
174  }
175 
176  private.tliIndex = tliIndex;
178  &private);
179  if (xlogreader == NULL)
180  pg_fatal("out of memory");
181 
182  searchptr = forkptr;
183  for (;;)
184  {
185  uint8 info;
186 
187  record = XLogReadRecord(xlogreader, searchptr, &errormsg);
188 
189  if (record == NULL)
190  {
191  if (errormsg)
192  pg_fatal("could not find previous WAL record at %X/%X: %s",
193  (uint32) (searchptr >> 32), (uint32) (searchptr),
194  errormsg);
195  else
196  pg_fatal("could not find previous WAL record at %X/%X",
197  (uint32) (searchptr >> 32), (uint32) (searchptr));
198  }
199 
200  /*
201  * Check if it is a checkpoint record. This checkpoint record needs to
202  * be the latest checkpoint before WAL forked and not the checkpoint
203  * where the master has been stopped to be rewinded.
204  */
205  info = XLogRecGetInfo(xlogreader) & ~XLR_INFO_MASK;
206  if (searchptr < forkptr &&
207  XLogRecGetRmid(xlogreader) == RM_XLOG_ID &&
208  (info == XLOG_CHECKPOINT_SHUTDOWN ||
209  info == XLOG_CHECKPOINT_ONLINE))
210  {
211  CheckPoint checkPoint;
212 
213  memcpy(&checkPoint, XLogRecGetData(xlogreader), sizeof(CheckPoint));
214  *lastchkptrec = searchptr;
215  *lastchkpttli = checkPoint.ThisTimeLineID;
216  *lastchkptredo = checkPoint.redo;
217  break;
218  }
219 
220  /* Walk backwards to previous record. */
221  searchptr = record->xl_prev;
222  }
223 
224  XLogReaderFree(xlogreader);
225  if (xlogreadfd != -1)
226  {
227  close(xlogreadfd);
228  xlogreadfd = -1;
229  }
230 }
XLogRecPtr xl_prev
Definition: xlogrecord.h:45
#define XLOG_CHECKPOINT_ONLINE
Definition: pg_control.h:68
unsigned char uint8
Definition: c.h:356
#define pg_fatal(...)
Definition: pg_rewind.h:43
static int SimpleXLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf)
Definition: parsexlog.c:234
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:237
#define XLogRecGetData(decoder)
Definition: xlogreader.h:246
#define XLOG_CHECKPOINT_SHUTDOWN
Definition: pg_control.h:67
char * datadir
unsigned int uint32
Definition: c.h:358
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:242
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:133
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
static int xlogreadfd
Definition: parsexlog.c:40
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:55
int WalSegSz
Definition: pg_standby.c:39
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:71
TimeLineID ThisTimeLineID
Definition: pg_control.h:39
#define close(a)
Definition: win32.h:12
XLogRecPtr redo
Definition: pg_control.h:37
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:243
#define SizeOfXLogLongPHD
Definition: xlog_internal.h:72

◆ readOneRecord()

XLogRecPtr readOneRecord ( const char *  datadir,
XLogRecPtr  ptr,
int  tliIndex 
)

Definition at line 111 of file parsexlog.c.

References close, XLogReaderState::EndRecPtr, pg_fatal, SimpleXLogPageRead(), XLogPageReadPrivate::tliIndex, WalSegSz, XLogReaderAllocate(), XLogReaderFree(), xlogreadfd, and XLogReadRecord().

Referenced by main().

112 {
113  XLogRecord *record;
114  XLogReaderState *xlogreader;
115  char *errormsg;
116  XLogPageReadPrivate private;
117  XLogRecPtr endptr;
118 
119  private.tliIndex = tliIndex;
121  &private);
122  if (xlogreader == NULL)
123  pg_fatal("out of memory");
124 
125  record = XLogReadRecord(xlogreader, ptr, &errormsg);
126  if (record == NULL)
127  {
128  if (errormsg)
129  pg_fatal("could not read WAL record at %X/%X: %s",
130  (uint32) (ptr >> 32), (uint32) (ptr), errormsg);
131  else
132  pg_fatal("could not read WAL record at %X/%X",
133  (uint32) (ptr >> 32), (uint32) (ptr));
134  }
135  endptr = xlogreader->EndRecPtr;
136 
137  XLogReaderFree(xlogreader);
138  if (xlogreadfd != -1)
139  {
140  close(xlogreadfd);
141  xlogreadfd = -1;
142  }
143 
144  return endptr;
145 }
#define pg_fatal(...)
Definition: pg_rewind.h:43
static int SimpleXLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf)
Definition: parsexlog.c:234
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:237
XLogRecPtr EndRecPtr
Definition: xlogreader.h:133
char * datadir
unsigned int uint32
Definition: c.h:358
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:133
static int xlogreadfd
Definition: parsexlog.c:40
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int WalSegSz
Definition: pg_standby.c:39
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:71
#define close(a)
Definition: win32.h:12

◆ SimpleXLogPageRead()

static int SimpleXLogPageRead ( XLogReaderState xlogreader,
XLogRecPtr  targetPagePtr,
int  reqLen,
XLogRecPtr  targetRecPtr,
char *  readBuf 
)
static

Definition at line 234 of file parsexlog.c.

References Assert, TimeLineHistoryEntry::begin, close, TimeLineHistoryEntry::end, MAXFNAMELEN, MAXPGPATH, PG_BINARY, pg_log_error, XLogReaderState::private_data, read, XLogReaderState::seg, XLogReaderState::segcxt, snprintf, targetHistory, targetNentries, TimeLineHistoryEntry::tli, WalSegSz, WALSegmentContext::ws_dir, WALOpenSegment::ws_tli, XLByteInSeg, XLByteToSeg, XLOGDIR, XLogFileName, xlogfpath, xlogreadfd, xlogreadsegno, XLogSegmentOffset, and XLogSegNoOffsetToRecPtr.

Referenced by extractPageMap(), findLastCheckpoint(), and readOneRecord().

236 {
237  XLogPageReadPrivate *private = (XLogPageReadPrivate *) xlogreader->private_data;
238  uint32 targetPageOff;
239  XLogRecPtr targetSegEnd;
240  XLogSegNo targetSegNo;
241  int r;
242 
243  XLByteToSeg(targetPagePtr, targetSegNo, WalSegSz);
244  XLogSegNoOffsetToRecPtr(targetSegNo + 1, 0, WalSegSz, targetSegEnd);
245  targetPageOff = XLogSegmentOffset(targetPagePtr, WalSegSz);
246 
247  /*
248  * See if we need to switch to a new segment because the requested record
249  * is not in the currently open one.
250  */
251  if (xlogreadfd >= 0 &&
252  !XLByteInSeg(targetPagePtr, xlogreadsegno, WalSegSz))
253  {
254  close(xlogreadfd);
255  xlogreadfd = -1;
256  }
257 
258  XLByteToSeg(targetPagePtr, xlogreadsegno, WalSegSz);
259 
260  if (xlogreadfd < 0)
261  {
262  char xlogfname[MAXFNAMELEN];
263 
264  /*
265  * Since incomplete segments are copied into next timelines, switch to
266  * the timeline holding the required segment. Assuming this scan can
267  * be done both forward and backward, consider also switching timeline
268  * accordingly.
269  */
270  while (private->tliIndex < targetNentries - 1 &&
271  targetHistory[private->tliIndex].end < targetSegEnd)
272  private->tliIndex++;
273  while (private->tliIndex > 0 &&
274  targetHistory[private->tliIndex].begin >= targetSegEnd)
275  private->tliIndex--;
276 
277  XLogFileName(xlogfname, targetHistory[private->tliIndex].tli,
279 
280  snprintf(xlogfpath, MAXPGPATH, "%s/" XLOGDIR "/%s",
281  xlogreader->segcxt.ws_dir, xlogfname);
282 
283  xlogreadfd = open(xlogfpath, O_RDONLY | PG_BINARY, 0);
284 
285  if (xlogreadfd < 0)
286  {
287  pg_log_error("could not open file \"%s\": %m", xlogfpath);
288  return -1;
289  }
290  }
291 
292  /*
293  * At this point, we have the right segment open.
294  */
295  Assert(xlogreadfd != -1);
296 
297  /* Read the requested page */
298  if (lseek(xlogreadfd, (off_t) targetPageOff, SEEK_SET) < 0)
299  {
300  pg_log_error("could not seek in file \"%s\": %m", xlogfpath);
301  return -1;
302  }
303 
304 
305  r = read(xlogreadfd, readBuf, XLOG_BLCKSZ);
306  if (r != XLOG_BLCKSZ)
307  {
308  if (r < 0)
309  pg_log_error("could not read file \"%s\": %m", xlogfpath);
310  else
311  pg_log_error("could not read file \"%s\": read %d of %zu",
312  xlogfpath, r, (Size) XLOG_BLCKSZ);
313 
314  return -1;
315  }
316 
317  Assert(targetSegNo == xlogreadsegno);
318 
319  xlogreader->seg.ws_tli = targetHistory[private->tliIndex].tli;
320  return XLOG_BLCKSZ;
321 }
char ws_dir[MAXPGPATH]
Definition: xlogreader.h:46
TimeLineHistoryEntry * targetHistory
Definition: pg_rewind.c:64
#define pg_log_error(...)
Definition: logging.h:79
TimeLineID tli
Definition: timeline.h:27
void * private_data
Definition: xlogreader.h:126
#define PG_BINARY
Definition: c.h:1191
#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes)
WALOpenSegment seg
Definition: xlogreader.h:170
#define MAXPGPATH
uint64 XLogSegNo
Definition: xlogdefs.h:41
unsigned int uint32
Definition: c.h:358
#define MAXFNAMELEN
#define XLOGDIR
int targetNentries
Definition: pg_rewind.c:65
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
static int xlogreadfd
Definition: parsexlog.c:40
static XLogSegNo xlogreadsegno
Definition: parsexlog.c:41
static char xlogfpath[MAXPGPATH]
Definition: parsexlog.c:42
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
XLogRecPtr end
Definition: timeline.h:29
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:732
size_t Size
Definition: c.h:466
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
TimeLineID ws_tli
Definition: xlogreader.h:40
int WalSegSz
Definition: pg_standby.c:39
XLogRecPtr begin
Definition: timeline.h:28
WALSegmentContext segcxt
Definition: xlogreader.h:169
#define close(a)
Definition: win32.h:12
#define snprintf
Definition: port.h:192
#define read(a, b, c)
Definition: win32.h:13
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

Variable Documentation

◆ RmgrNames

const char* RmgrNames[RM_MAX_ID+1]
static

Definition at line 34 of file parsexlog.c.

Referenced by extractPageInfo().

◆ xlogfpath

char xlogfpath[MAXPGPATH]
static

Definition at line 42 of file parsexlog.c.

Referenced by KeepFileRestoredFromArchive(), and SimpleXLogPageRead().

◆ xlogreadfd

int xlogreadfd = -1
static

Definition at line 40 of file parsexlog.c.

Referenced by extractPageMap(), findLastCheckpoint(), readOneRecord(), and SimpleXLogPageRead().

◆ xlogreadsegno

XLogSegNo xlogreadsegno = -1
static

Definition at line 41 of file parsexlog.c.

Referenced by SimpleXLogPageRead().