PostgreSQL Source Code git master
Loading...
Searching...
No Matches
parsexlog.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include "access/rmgr.h"
#include "access/xact.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 "fe_utils/archive.h"
#include "filemap.h"
#include "pg_rewind.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, decode)    name,
 
#define RmgrName(rmid)
 

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, const char *restoreCommand)
 
XLogRecPtr readOneRecord (const char *datadir, XLogRecPtr ptr, int tliIndex, const char *restoreCommand)
 
void findLastCheckpoint (const char *datadir, XLogRecPtr forkptr, int tliIndex, XLogRecPtr *lastchkptrec, TimeLineID *lastchkpttli, XLogRecPtr *lastchkptredo, const char *restoreCommand)
 

Variables

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

Macro Definition Documentation

◆ PG_RMGR

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

Definition at line 31 of file parsexlog.c.

33 {
34#include "access/rmgrlist.h"
35};
36
37#define RmgrName(rmid) (((rmid) <= RM_MAX_BUILTIN_ID) ? \
38 RmgrNames[rmid] : "custom")
39
40static void extractPageInfo(XLogReaderState *record);
41
42static int xlogreadfd = -1;
43static XLogSegNo xlogreadsegno = 0;
44static char xlogfpath[MAXPGPATH];
45
46typedef struct XLogPageReadPrivate
47{
48 const char *restoreCommand;
49 int tliIndex;
51
54 int reqLen, XLogRecPtr targetRecPtr, char *readBuf);
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 * 'endpoint' is the end of the last record to read. The record starting at
62 * 'endpoint' is the first one that is not read.
63 */
64void
65extractPageMap(const char *datadir, XLogRecPtr startpoint, int tliIndex,
66 XLogRecPtr endpoint, const char *restoreCommand)
67{
68 XLogRecord *record;
70 char *errormsg;
71 XLogPageReadPrivate private;
72
73 private.tliIndex = tliIndex;
74 private.restoreCommand = restoreCommand;
76 XL_ROUTINE(.page_read = &SimpleXLogPageRead),
77 &private);
78 if (xlogreader == NULL)
79 pg_fatal("out of memory while allocating a WAL reading processor");
80
81 XLogBeginRead(xlogreader, startpoint);
82 do
83 {
84 record = XLogReadRecord(xlogreader, &errormsg);
85
86 if (record == NULL)
87 {
89
90 if (errormsg)
91 pg_fatal("could not read WAL record at %X/%08X: %s",
93 errormsg);
94 else
95 pg_fatal("could not read WAL record at %X/%08X",
97 }
98
100 } while (xlogreader->EndRecPtr < endpoint);
101
102 /*
103 * If 'endpoint' didn't point exactly at a record boundary, the caller
104 * messed up.
105 */
107 pg_fatal("end pointer %X/%08X is not a valid end point; expected %X/%08X",
109
111 if (xlogreadfd != -1)
112 {
114 xlogreadfd = -1;
115 }
116}
117
118/*
119 * Reads one WAL record. Returns the end position of the record, without
120 * doing anything with the record itself.
121 */
123readOneRecord(const char *datadir, XLogRecPtr ptr, int tliIndex,
124 const char *restoreCommand)
125{
126 XLogRecord *record;
128 char *errormsg;
129 XLogPageReadPrivate private;
130 XLogRecPtr endptr;
131
132 private.tliIndex = tliIndex;
133 private.restoreCommand = restoreCommand;
135 XL_ROUTINE(.page_read = &SimpleXLogPageRead),
136 &private);
137 if (xlogreader == NULL)
138 pg_fatal("out of memory while allocating a WAL reading processor");
139
141 record = XLogReadRecord(xlogreader, &errormsg);
142 if (record == NULL)
143 {
144 if (errormsg)
145 pg_fatal("could not read WAL record at %X/%08X: %s",
146 LSN_FORMAT_ARGS(ptr), errormsg);
147 else
148 pg_fatal("could not read WAL record at %X/%08X",
149 LSN_FORMAT_ARGS(ptr));
150 }
151 endptr = xlogreader->EndRecPtr;
152
154 if (xlogreadfd != -1)
155 {
157 xlogreadfd = -1;
158 }
159
160 return endptr;
161}
162
163/*
164 * Find the previous checkpoint preceding given WAL location.
165 */
166void
167findLastCheckpoint(const char *datadir, XLogRecPtr forkptr, int tliIndex,
169 XLogRecPtr *lastchkptredo, const char *restoreCommand)
170{
171 /* Walk backwards, starting from the given record */
172 XLogRecord *record;
175 char *errormsg;
176 XLogPageReadPrivate private;
179
180 /*
181 * The given fork pointer points to the end of the last common record,
182 * which is not necessarily the beginning of the next record, if the
183 * previous record happens to end at a page boundary. Skip over the page
184 * header in that case to find the next record.
185 */
186 if (forkptr % XLOG_BLCKSZ == 0)
187 {
190 else
192 }
193
194 private.tliIndex = tliIndex;
195 private.restoreCommand = restoreCommand;
197 XL_ROUTINE(.page_read = &SimpleXLogPageRead),
198 &private);
199 if (xlogreader == NULL)
200 pg_fatal("out of memory while allocating a WAL reading processor");
201
203 for (;;)
204 {
205 uint8 info;
206
208 record = XLogReadRecord(xlogreader, &errormsg);
209
210 if (record == NULL)
211 {
212 if (errormsg)
213 pg_fatal("could not find previous WAL record at %X/%08X: %s",
215 errormsg);
216 else
217 pg_fatal("could not find previous WAL record at %X/%08X",
219 }
220
221 /* Detect if a new WAL file has been opened */
224 {
226
228
229 /* update current values */
232
235
236 /* Track this filename as one to not remove */
238 }
239
240 /*
241 * Check if it is a checkpoint record. This checkpoint record needs to
242 * be the latest checkpoint before WAL forked and not the checkpoint
243 * where the primary has been stopped to be rewound.
244 */
246 if (searchptr < forkptr &&
248 (info == XLOG_CHECKPOINT_SHUTDOWN ||
249 info == XLOG_CHECKPOINT_ONLINE))
250 {
251 CheckPoint checkPoint;
252
253 memcpy(&checkPoint, XLogRecGetData(xlogreader), sizeof(CheckPoint));
255 *lastchkpttli = checkPoint.ThisTimeLineID;
256 *lastchkptredo = checkPoint.redo;
257 break;
258 }
259
260 /* Walk backwards to previous record. */
261 searchptr = record->xl_prev;
262 }
263
265 if (xlogreadfd != -1)
266 {
268 xlogreadfd = -1;
269 }
270}
271
272/* XLogReader callback function, to read a WAL page */
273static int
275 int reqLen, XLogRecPtr targetRecPtr, char *readBuf)
276{
281 int r;
282
286
287 /*
288 * See if we need to switch to a new segment because the requested record
289 * is not in the currently open one.
290 */
291 if (xlogreadfd >= 0 &&
293 {
295 xlogreadfd = -1;
296 }
297
299
300 if (xlogreadfd < 0)
301 {
303
304 /*
305 * Since incomplete segments are copied into next timelines, switch to
306 * the timeline holding the required segment. Assuming this scan can
307 * be done both forward and backward, consider also switching timeline
308 * accordingly.
309 */
310 while (private->tliIndex < targetNentries - 1 &&
312 private->tliIndex++;
313 while (private->tliIndex > 0 &&
315 private->tliIndex--;
316
319
320 snprintf(xlogfpath, MAXPGPATH, "%s/" XLOGDIR "/%s",
322
324
325 if (xlogreadfd < 0)
326 {
327 /*
328 * If we have no restore_command to execute, then exit.
329 */
330 if (private->restoreCommand == NULL)
331 {
332 pg_log_error("could not open file \"%s\": %m", xlogfpath);
333 return -1;
334 }
335
336 /*
337 * Since we have restore_command, then try to retrieve missing WAL
338 * file from the archive.
339 */
341 xlogfname,
342 WalSegSz,
343 private->restoreCommand);
344
345 if (xlogreadfd < 0)
346 return -1;
347 else
348 pg_log_debug("using file \"%s\" restored from archive",
349 xlogfpath);
350 }
351 }
352
353 /*
354 * At this point, we have the right segment open.
355 */
356 Assert(xlogreadfd != -1);
357
358 /* Read the requested page */
360 {
361 pg_log_error("could not seek in file \"%s\": %m", xlogfpath);
362 return -1;
363 }
364
365
366 r = read(xlogreadfd, readBuf, XLOG_BLCKSZ);
367 if (r != XLOG_BLCKSZ)
368 {
369 if (r < 0)
370 pg_log_error("could not read file \"%s\": %m", xlogfpath);
371 else
372 pg_log_error("could not read file \"%s\": read %d of %zu",
374
375 return -1;
376 }
377
379
380 xlogreader->seg.ws_tli = targetHistory[private->tliIndex].tli;
381 return XLOG_BLCKSZ;
382}
383
384/*
385 * Extract information on which blocks the current record modifies.
386 */
387static void
389{
390 int block_id;
391 RmgrId rmid = XLogRecGetRmid(record);
392 uint8 info = XLogRecGetInfo(record);
393 uint8 rminfo = info & ~XLR_INFO_MASK;
394
395 /* Is this a special record type that I recognize? */
396
398 {
399 /*
400 * New databases can be safely ignored. It won't be present in the
401 * source system, so it will be deleted. There's one corner-case,
402 * though: if a new, different, database is also created in the source
403 * system, we'll see that the files already exist and not copy them.
404 * That's OK, though; WAL replay of creating the new database, from
405 * the source systems's WAL, will re-copy the new database,
406 * overwriting the database created in the target system.
407 */
408 }
409 else if (rmid == RM_DBASE_ID && rminfo == XLOG_DBASE_CREATE_WAL_LOG)
410 {
411 /*
412 * New databases can be safely ignored. It won't be present in the
413 * source system, so it will be deleted.
414 */
415 }
416 else if (rmid == RM_DBASE_ID && rminfo == XLOG_DBASE_DROP)
417 {
418 /*
419 * An existing database was dropped. We'll see that the files don't
420 * exist in the target data dir, and copy them in toto from the source
421 * system. No need to do anything special here.
422 */
423 }
424 else if (rmid == RM_SMGR_ID && rminfo == XLOG_SMGR_CREATE)
425 {
426 /*
427 * We can safely ignore these. The file will be removed from the
428 * target, if it doesn't exist in source system. If a file with same
429 * name is created in source system, too, there will be WAL records
430 * for all the blocks in it.
431 */
432 }
433 else if (rmid == RM_SMGR_ID && rminfo == XLOG_SMGR_TRUNCATE)
434 {
435 /*
436 * We can safely ignore these. When we compare the sizes later on,
437 * we'll notice that they differ, and copy the missing tail from
438 * source system.
439 */
440 }
441 else if (rmid == RM_XACT_ID &&
446 {
447 /*
448 * These records can include "dropped rels". We can safely ignore
449 * them, we will see that they are missing and copy them from the
450 * source.
451 */
452 }
453 else if (info & XLR_SPECIAL_REL_UPDATE)
454 {
455 /*
456 * This record type modifies a relation file in some special way, but
457 * we don't recognize the type. That's bad - we don't know how to
458 * track that change.
459 */
460 pg_fatal("WAL record modifies a relation, but record type is not recognized:\n"
461 "lsn: %X/%08X, rmid: %d, rmgr: %s, info: %02X",
463 rmid, RmgrName(rmid), info);
464 }
465
466 for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
467 {
468 RelFileLocator rlocator;
469 ForkNumber forknum;
470 BlockNumber blkno;
471
473 &rlocator, &forknum, &blkno, NULL))
474 continue;
475
476 /* We only care about the main fork; others are copied in toto */
477 if (forknum != MAIN_FORKNUM)
478 continue;
479
480 process_target_wal_block_change(forknum, rlocator, blkno);
481 }
482}
uint32 BlockNumber
Definition block.h:31
uint8_t uint8
Definition c.h:544
#define Assert(condition)
Definition c.h:873
#define PG_BINARY
Definition c.h:1287
uint32_t uint32
Definition c.h:546
size_t Size
Definition c.h:619
#define XLOG_DBASE_CREATE_WAL_LOG
#define XLOG_DBASE_DROP
#define XLOG_DBASE_CREATE_FILE_COPY
void process_target_wal_block_change(ForkNumber forknum, RelFileLocator rlocator, BlockNumber blkno)
Definition filemap.c:353
void keepwal_add_entry(const char *path)
Definition filemap.c:251
#define close(a)
Definition win32.h:12
#define read(a, b, c)
Definition win32.h:13
#define pg_log_error(...)
Definition logging.h:106
#define pg_log_debug(...)
Definition logging.h:133
static int SimpleXLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf)
Definition parsexlog.c:275
static XLogSegNo xlogreadsegno
Definition parsexlog.c:44
void extractPageMap(const char *datadir, XLogRecPtr startpoint, int tliIndex, XLogRecPtr endpoint, const char *restoreCommand)
Definition parsexlog.c:66
static void extractPageInfo(XLogReaderState *record)
Definition parsexlog.c:389
void findLastCheckpoint(const char *datadir, XLogRecPtr forkptr, int tliIndex, XLogRecPtr *lastchkptrec, TimeLineID *lastchkpttli, XLogRecPtr *lastchkptredo, const char *restoreCommand)
Definition parsexlog.c:168
static int xlogreadfd
Definition parsexlog.c:43
XLogRecPtr readOneRecord(const char *datadir, XLogRecPtr ptr, int tliIndex, const char *restoreCommand)
Definition parsexlog.c:124
#define RmgrName(rmid)
Definition parsexlog.c:38
static char xlogfpath[MAXPGPATH]
Definition parsexlog.c:45
#define pg_fatal(...)
#define MAXPGPATH
#define XLOG_CHECKPOINT_SHUTDOWN
Definition pg_control.h:69
#define XLOG_CHECKPOINT_ONLINE
Definition pg_control.h:70
char * datadir
TimeLineHistoryEntry * targetHistory
Definition pg_rewind.c:81
int targetNentries
Definition pg_rewind.c:82
#define snprintf
Definition port.h:260
static int fb(int x)
ForkNumber
Definition relpath.h:56
@ MAIN_FORKNUM
Definition relpath.h:58
uint8 RmgrId
Definition rmgr.h:11
#define XLOG_SMGR_CREATE
#define XLOG_SMGR_TRUNCATE
int WalSegSz
Definition streamutil.c:32
TimeLineID ThisTimeLineID
Definition pg_control.h:39
XLogRecPtr redo
Definition pg_control.h:37
XLogRecPtr begin
Definition timeline.h:28
XLogSegNo ws_segno
Definition xlogreader.h:48
TimeLineID ws_tli
Definition xlogreader.h:49
char ws_dir[MAXPGPATH]
Definition xlogreader.h:55
const char * restoreCommand
Definition parsexlog.c:49
WALSegmentContext segcxt
Definition xlogreader.h:270
XLogRecPtr EndRecPtr
Definition xlogreader.h:206
XLogRecPtr ReadRecPtr
Definition xlogreader.h:205
WALOpenSegment seg
Definition xlogreader.h:271
void * private_data
Definition xlogreader.h:195
XLogRecPtr xl_prev
Definition xlogrecord.h:45
#define XLOG_XACT_COMMIT_PREPARED
Definition xact.h:173
#define XLOG_XACT_COMMIT
Definition xact.h:170
#define XLOG_XACT_OPMASK
Definition xact.h:180
#define XLOG_XACT_ABORT
Definition xact.h:172
#define XLOG_XACT_ABORT_PREPARED
Definition xact.h:174
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
#define MAXFNAMELEN
#define XLOGDIR
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
#define SizeOfXLogShortPHD
#define SizeOfXLogLongPHD
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes)
bool RestoreArchivedFile(char *path, const char *xlogfname, const char *recovername, off_t expectedSize, bool cleanupEnabled)
Definition xlogarchive.c:54
#define LSN_FORMAT_ARGS(lsn)
Definition xlogdefs.h:47
uint64 XLogRecPtr
Definition xlogdefs.h:21
uint32 TimeLineID
Definition xlogdefs.h:63
uint64 XLogSegNo
Definition xlogdefs.h:52
bool XLogRecGetBlockTagExtended(XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum, Buffer *prefetch_buffer)
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition xlogreader.c:107
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition xlogreader.c:390
void XLogReaderFree(XLogReaderState *state)
Definition xlogreader.c:162
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition xlogreader.c:232
#define XLogRecGetInfo(decoder)
Definition xlogreader.h:409
#define XLogRecGetRmid(decoder)
Definition xlogreader.h:410
#define XLogRecGetData(decoder)
Definition xlogreader.h:414
#define XL_ROUTINE(...)
Definition xlogreader.h:117
#define XLogRecMaxBlockId(decoder)
Definition xlogreader.h:417
#define XLR_SPECIAL_REL_UPDATE
Definition xlogrecord.h:82
static XLogReaderState * xlogreader

◆ RmgrName

#define RmgrName (   rmid)
Value:
(((rmid) <= RM_MAX_BUILTIN_ID) ? \
RmgrNames[rmid] : "custom")
static const char *const RmgrNames[RM_MAX_ID+1]
Definition parsexlog.c:34
#define RM_MAX_BUILTIN_ID
Definition rmgr.h:34

Definition at line 38 of file parsexlog.c.

39 : "custom")

Typedef Documentation

◆ XLogPageReadPrivate

Function Documentation

◆ extractPageInfo()

static void extractPageInfo ( XLogReaderState record)
static

Definition at line 389 of file parsexlog.c.

390{
391 int block_id;
392 RmgrId rmid = XLogRecGetRmid(record);
393 uint8 info = XLogRecGetInfo(record);
394 uint8 rminfo = info & ~XLR_INFO_MASK;
395
396 /* Is this a special record type that I recognize? */
397
399 {
400 /*
401 * New databases can be safely ignored. It won't be present in the
402 * source system, so it will be deleted. There's one corner-case,
403 * though: if a new, different, database is also created in the source
404 * system, we'll see that the files already exist and not copy them.
405 * That's OK, though; WAL replay of creating the new database, from
406 * the source systems's WAL, will re-copy the new database,
407 * overwriting the database created in the target system.
408 */
409 }
410 else if (rmid == RM_DBASE_ID && rminfo == XLOG_DBASE_CREATE_WAL_LOG)
411 {
412 /*
413 * New databases can be safely ignored. It won't be present in the
414 * source system, so it will be deleted.
415 */
416 }
417 else if (rmid == RM_DBASE_ID && rminfo == XLOG_DBASE_DROP)
418 {
419 /*
420 * An existing database was dropped. We'll see that the files don't
421 * exist in the target data dir, and copy them in toto from the source
422 * system. No need to do anything special here.
423 */
424 }
425 else if (rmid == RM_SMGR_ID && rminfo == XLOG_SMGR_CREATE)
426 {
427 /*
428 * We can safely ignore these. The file will be removed from the
429 * target, if it doesn't exist in source system. If a file with same
430 * name is created in source system, too, there will be WAL records
431 * for all the blocks in it.
432 */
433 }
434 else if (rmid == RM_SMGR_ID && rminfo == XLOG_SMGR_TRUNCATE)
435 {
436 /*
437 * We can safely ignore these. When we compare the sizes later on,
438 * we'll notice that they differ, and copy the missing tail from
439 * source system.
440 */
441 }
442 else if (rmid == RM_XACT_ID &&
447 {
448 /*
449 * These records can include "dropped rels". We can safely ignore
450 * them, we will see that they are missing and copy them from the
451 * source.
452 */
453 }
454 else if (info & XLR_SPECIAL_REL_UPDATE)
455 {
456 /*
457 * This record type modifies a relation file in some special way, but
458 * we don't recognize the type. That's bad - we don't know how to
459 * track that change.
460 */
461 pg_fatal("WAL record modifies a relation, but record type is not recognized:\n"
462 "lsn: %X/%08X, rmid: %d, rmgr: %s, info: %02X",
464 rmid, RmgrName(rmid), info);
465 }
466
467 for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
468 {
469 RelFileLocator rlocator;
470 ForkNumber forknum;
471 BlockNumber blkno;
472
474 &rlocator, &forknum, &blkno, NULL))
475 continue;
476
477 /* We only care about the main fork; others are copied in toto */
478 if (forknum != MAIN_FORKNUM)
479 continue;
480
481 process_target_wal_block_change(forknum, rlocator, blkno);
482 }
483}

References fb(), LSN_FORMAT_ARGS, MAIN_FORKNUM, pg_fatal, process_target_wal_block_change(), XLogReaderState::ReadRecPtr, RmgrName, XLOG_DBASE_CREATE_FILE_COPY, XLOG_DBASE_CREATE_WAL_LOG, XLOG_DBASE_DROP, XLOG_SMGR_CREATE, XLOG_SMGR_TRUNCATE, XLOG_XACT_ABORT, XLOG_XACT_ABORT_PREPARED, XLOG_XACT_COMMIT, XLOG_XACT_COMMIT_PREPARED, XLOG_XACT_OPMASK, XLogRecGetBlockTagExtended(), XLogRecGetInfo, XLogRecGetRmid, XLogRecMaxBlockId, and XLR_SPECIAL_REL_UPDATE.

Referenced by extractPageMap().

◆ extractPageMap()

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

Definition at line 66 of file parsexlog.c.

68{
69 XLogRecord *record;
71 char *errormsg;
72 XLogPageReadPrivate private;
73
74 private.tliIndex = tliIndex;
75 private.restoreCommand = restoreCommand;
77 XL_ROUTINE(.page_read = &SimpleXLogPageRead),
78 &private);
79 if (xlogreader == NULL)
80 pg_fatal("out of memory while allocating a WAL reading processor");
81
82 XLogBeginRead(xlogreader, startpoint);
83 do
84 {
85 record = XLogReadRecord(xlogreader, &errormsg);
86
87 if (record == NULL)
88 {
90
91 if (errormsg)
92 pg_fatal("could not read WAL record at %X/%08X: %s",
94 errormsg);
95 else
96 pg_fatal("could not read WAL record at %X/%08X",
98 }
99
101 } while (xlogreader->EndRecPtr < endpoint);
102
103 /*
104 * If 'endpoint' didn't point exactly at a record boundary, the caller
105 * messed up.
106 */
108 pg_fatal("end pointer %X/%08X is not a valid end point; expected %X/%08X",
110
112 if (xlogreadfd != -1)
113 {
115 xlogreadfd = -1;
116 }
117}

References close, datadir, XLogReaderState::EndRecPtr, extractPageInfo(), fb(), LSN_FORMAT_ARGS, pg_fatal, SimpleXLogPageRead(), XLogPageReadPrivate::tliIndex, WalSegSz, XL_ROUTINE, XLogBeginRead(), xlogreader, XLogReaderAllocate(), XLogReaderFree(), xlogreadfd, and XLogReadRecord().

Referenced by main().

◆ findLastCheckpoint()

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

Definition at line 168 of file parsexlog.c.

171{
172 /* Walk backwards, starting from the given record */
173 XLogRecord *record;
176 char *errormsg;
177 XLogPageReadPrivate private;
180
181 /*
182 * The given fork pointer points to the end of the last common record,
183 * which is not necessarily the beginning of the next record, if the
184 * previous record happens to end at a page boundary. Skip over the page
185 * header in that case to find the next record.
186 */
187 if (forkptr % XLOG_BLCKSZ == 0)
188 {
191 else
193 }
194
195 private.tliIndex = tliIndex;
196 private.restoreCommand = restoreCommand;
198 XL_ROUTINE(.page_read = &SimpleXLogPageRead),
199 &private);
200 if (xlogreader == NULL)
201 pg_fatal("out of memory while allocating a WAL reading processor");
202
204 for (;;)
205 {
206 uint8 info;
207
209 record = XLogReadRecord(xlogreader, &errormsg);
210
211 if (record == NULL)
212 {
213 if (errormsg)
214 pg_fatal("could not find previous WAL record at %X/%08X: %s",
216 errormsg);
217 else
218 pg_fatal("could not find previous WAL record at %X/%08X",
220 }
221
222 /* Detect if a new WAL file has been opened */
225 {
227
229
230 /* update current values */
233
236
237 /* Track this filename as one to not remove */
239 }
240
241 /*
242 * Check if it is a checkpoint record. This checkpoint record needs to
243 * be the latest checkpoint before WAL forked and not the checkpoint
244 * where the primary has been stopped to be rewound.
245 */
247 if (searchptr < forkptr &&
249 (info == XLOG_CHECKPOINT_SHUTDOWN ||
250 info == XLOG_CHECKPOINT_ONLINE))
251 {
252 CheckPoint checkPoint;
253
254 memcpy(&checkPoint, XLogRecGetData(xlogreader), sizeof(CheckPoint));
256 *lastchkpttli = checkPoint.ThisTimeLineID;
257 *lastchkptredo = checkPoint.redo;
258 break;
259 }
260
261 /* Walk backwards to previous record. */
262 searchptr = record->xl_prev;
263 }
264
266 if (xlogreadfd != -1)
267 {
269 xlogreadfd = -1;
270 }
271}

References close, datadir, fb(), keepwal_add_entry(), LSN_FORMAT_ARGS, MAXFNAMELEN, pg_fatal, CheckPoint::redo, XLogReaderState::seg, SimpleXLogPageRead(), SizeOfXLogLongPHD, SizeOfXLogShortPHD, snprintf, CheckPoint::ThisTimeLineID, XLogPageReadPrivate::tliIndex, WalSegSz, WALOpenSegment::ws_segno, WALOpenSegment::ws_tli, XLogRecord::xl_prev, XL_ROUTINE, XLOG_CHECKPOINT_ONLINE, XLOG_CHECKPOINT_SHUTDOWN, XLogBeginRead(), XLOGDIR, XLogFileName(), xlogreader, XLogReaderAllocate(), XLogReaderFree(), xlogreadfd, XLogReadRecord(), XLogRecGetData, XLogRecGetInfo, XLogRecGetRmid, and XLogSegmentOffset.

Referenced by main().

◆ readOneRecord()

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

Definition at line 124 of file parsexlog.c.

126{
127 XLogRecord *record;
129 char *errormsg;
130 XLogPageReadPrivate private;
131 XLogRecPtr endptr;
132
133 private.tliIndex = tliIndex;
134 private.restoreCommand = restoreCommand;
136 XL_ROUTINE(.page_read = &SimpleXLogPageRead),
137 &private);
138 if (xlogreader == NULL)
139 pg_fatal("out of memory while allocating a WAL reading processor");
140
142 record = XLogReadRecord(xlogreader, &errormsg);
143 if (record == NULL)
144 {
145 if (errormsg)
146 pg_fatal("could not read WAL record at %X/%08X: %s",
147 LSN_FORMAT_ARGS(ptr), errormsg);
148 else
149 pg_fatal("could not read WAL record at %X/%08X",
150 LSN_FORMAT_ARGS(ptr));
151 }
152 endptr = xlogreader->EndRecPtr;
153
155 if (xlogreadfd != -1)
156 {
158 xlogreadfd = -1;
159 }
160
161 return endptr;
162}

References close, datadir, XLogReaderState::EndRecPtr, fb(), LSN_FORMAT_ARGS, pg_fatal, SimpleXLogPageRead(), XLogPageReadPrivate::tliIndex, WalSegSz, XL_ROUTINE, XLogBeginRead(), xlogreader, XLogReaderAllocate(), XLogReaderFree(), xlogreadfd, and XLogReadRecord().

Referenced by main().

◆ SimpleXLogPageRead()

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

Definition at line 275 of file parsexlog.c.

277{
282 int r;
283
287
288 /*
289 * See if we need to switch to a new segment because the requested record
290 * is not in the currently open one.
291 */
292 if (xlogreadfd >= 0 &&
294 {
296 xlogreadfd = -1;
297 }
298
300
301 if (xlogreadfd < 0)
302 {
304
305 /*
306 * Since incomplete segments are copied into next timelines, switch to
307 * the timeline holding the required segment. Assuming this scan can
308 * be done both forward and backward, consider also switching timeline
309 * accordingly.
310 */
311 while (private->tliIndex < targetNentries - 1 &&
313 private->tliIndex++;
314 while (private->tliIndex > 0 &&
316 private->tliIndex--;
317
320
321 snprintf(xlogfpath, MAXPGPATH, "%s/" XLOGDIR "/%s",
323
325
326 if (xlogreadfd < 0)
327 {
328 /*
329 * If we have no restore_command to execute, then exit.
330 */
331 if (private->restoreCommand == NULL)
332 {
333 pg_log_error("could not open file \"%s\": %m", xlogfpath);
334 return -1;
335 }
336
337 /*
338 * Since we have restore_command, then try to retrieve missing WAL
339 * file from the archive.
340 */
342 xlogfname,
343 WalSegSz,
344 private->restoreCommand);
345
346 if (xlogreadfd < 0)
347 return -1;
348 else
349 pg_log_debug("using file \"%s\" restored from archive",
350 xlogfpath);
351 }
352 }
353
354 /*
355 * At this point, we have the right segment open.
356 */
357 Assert(xlogreadfd != -1);
358
359 /* Read the requested page */
361 {
362 pg_log_error("could not seek in file \"%s\": %m", xlogfpath);
363 return -1;
364 }
365
366
367 r = read(xlogreadfd, readBuf, XLOG_BLCKSZ);
368 if (r != XLOG_BLCKSZ)
369 {
370 if (r < 0)
371 pg_log_error("could not read file \"%s\": %m", xlogfpath);
372 else
373 pg_log_error("could not read file \"%s\": read %d of %zu",
375
376 return -1;
377 }
378
380
381 xlogreader->seg.ws_tli = targetHistory[private->tliIndex].tli;
382 return XLOG_BLCKSZ;
383}

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

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

Variable Documentation

◆ RmgrNames

const char* const RmgrNames[RM_MAX_ID+1]
static
Initial value:
= {
}

Definition at line 34 of file parsexlog.c.

34 {
35#include "access/rmgrlist.h"
36};

◆ xlogfpath

char xlogfpath[MAXPGPATH]
static

Definition at line 45 of file parsexlog.c.

Referenced by KeepFileRestoredFromArchive(), and SimpleXLogPageRead().

◆ xlogreadfd

int xlogreadfd = -1
static

Definition at line 43 of file parsexlog.c.

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

◆ xlogreadsegno

XLogSegNo xlogreadsegno = 0
static

Definition at line 44 of file parsexlog.c.

Referenced by SimpleXLogPageRead().