PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
parsexlog.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include "pg_rewind.h"
#include "filemap.h"
#include "logging.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, TimeLineID *pageTLI)
 
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

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

Definition at line 32 of file parsexlog.c.

Typedef Documentation

Function Documentation

static void extractPageInfo ( XLogReaderState record)
static

Definition at line 329 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().

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 }
#define XLR_SPECIAL_REL_UPDATE
Definition: xlogrecord.h:71
unsigned char uint8
Definition: c.h:256
static const char * RmgrNames[RM_MAX_ID+1]
Definition: parsexlog.c:35
uint32 BlockNumber
Definition: block.h:31
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
void process_block_change(ForkNumber forknum, RelFileNode rnode, BlockNumber blkno)
Definition: filemap.c:343
#define XLOG_SMGR_CREATE
Definition: storage_xlog.h:30
#define XLOG_DBASE_DROP
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:119
unsigned int uint32
Definition: c.h:258
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:222
ForkNumber
Definition: relpath.h:24
#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:1309
uint8 RmgrId
Definition: rmgr.h:11
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define XLOG_DBASE_CREATE
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:223
void extractPageMap ( const char *  datadir,
XLogRecPtr  startpoint,
int  tliIndex,
XLogRecPtr  endpoint 
)

Definition at line 62 of file parsexlog.c.

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

Referenced by main().

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 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:195
XLogRecPtr EndRecPtr
Definition: xlogreader.h:120
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:119
char * datadir
unsigned int uint32
Definition: c.h:258
const char * datadir
Definition: parsexlog.c:47
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:127
XLogReaderState * XLogReaderAllocate(int wal_segment_size, XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:67
static int xlogreadfd
Definition: parsexlog.c:41
static void extractPageInfo(XLogReaderState *record)
Definition: parsexlog.c:329
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int WalSegSz
Definition: pg_standby.c:39
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
void findLastCheckpoint ( const char *  datadir,
XLogRecPtr  forkptr,
int  tliIndex,
XLogRecPtr lastchkptrec,
TimeLineID lastchkpttli,
XLogRecPtr lastchkptredo 
)

Definition at line 157 of file parsexlog.c.

References close, datadir, pg_fatal(), CheckPoint::redo, SimpleXLogPageRead(), SizeOfXLogLongPHD, SizeOfXLogShortPHD, CheckPoint::ThisTimeLineID, 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().

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 }
XLogRecPtr xl_prev
Definition: xlogrecord.h:45
#define XLOG_CHECKPOINT_ONLINE
Definition: pg_control.h:68
unsigned char uint8
Definition: c.h:256
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:195
#define XLogRecGetData(decoder)
Definition: xlogreader.h:226
#define XLOG_CHECKPOINT_SHUTDOWN
Definition: pg_control.h:67
char * datadir
unsigned int uint32
Definition: c.h:258
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:222
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:127
XLogReaderState * XLogReaderAllocate(int wal_segment_size, XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:67
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
static int xlogreadfd
Definition: parsexlog.c:41
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
TimeLineID ThisTimeLineID
Definition: pg_control.h:38
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
XLogRecPtr redo
Definition: pg_control.h:36
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:223
#define SizeOfXLogLongPHD
Definition: xlog_internal.h:72
XLogRecPtr readOneRecord ( const char *  datadir,
XLogRecPtr  ptr,
int  tliIndex 
)

Definition at line 116 of file parsexlog.c.

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

Referenced by main().

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 }
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:195
XLogRecPtr EndRecPtr
Definition: xlogreader.h:120
char * datadir
unsigned int uint32
Definition: c.h:258
const char * datadir
Definition: parsexlog.c:47
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:127
XLogReaderState * XLogReaderAllocate(int wal_segment_size, XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:67
static int xlogreadfd
Definition: parsexlog.c:41
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int WalSegSz
Definition: pg_standby.c:39
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
static int SimpleXLogPageRead ( XLogReaderState xlogreader,
XLogRecPtr  targetPagePtr,
int  reqLen,
XLogRecPtr  targetRecPtr,
char *  readBuf,
TimeLineID pageTLI 
)
static

Definition at line 241 of file parsexlog.c.

References _, Assert, TimeLineHistoryEntry::begin, close, TimeLineHistoryEntry::end, MAXFNAMELEN, MAXPGPATH, PG_BINARY, XLogReaderState::private_data, read, snprintf(), strerror(), targetHistory, targetNentries, TimeLineHistoryEntry::tli, WalSegSz, XLByteInSeg, XLByteToSeg, XLOGDIR, XLogFileName, xlogfpath, xlogreadfd, xlogreadsegno, XLogSegmentOffset, and XLogSegNoOffsetToRecPtr.

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

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 }
TimeLineHistoryEntry * targetHistory
Definition: pg_rewind.c:59
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
TimeLineID tli
Definition: timeline.h:27
void * private_data
Definition: xlogreader.h:113
#define PG_BINARY
Definition: c.h:1044
#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes)
#define MAXPGPATH
uint64 XLogSegNo
Definition: xlogdefs.h:34
#define XLogSegNoOffsetToRecPtr(segno, offset, dest, wal_segsz_bytes)
unsigned int uint32
Definition: c.h:258
#define MAXFNAMELEN
#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
XLogRecPtr end
Definition: timeline.h:29
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:681
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
int WalSegSz
Definition: pg_standby.c:39
XLogRecPtr begin
Definition: timeline.h:28
const char * strerror(int errnum)
Definition: strerror.c:19
#define close(a)
Definition: win32.h:12
#define _(x)
Definition: elog.c:84
#define read(a, b, c)
Definition: win32.h:13
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

Variable Documentation

const char* RmgrNames[RM_MAX_ID+1]
static

Definition at line 35 of file parsexlog.c.

Referenced by extractPageInfo().

char xlogfpath[MAXPGPATH]
static

Definition at line 43 of file parsexlog.c.

Referenced by KeepFileRestoredFromArchive(), and SimpleXLogPageRead().

int xlogreadfd = -1
static

Definition at line 41 of file parsexlog.c.

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

XLogSegNo xlogreadsegno = -1
static

Definition at line 42 of file parsexlog.c.

Referenced by SimpleXLogPageRead().