PostgreSQL Source Code  git master
pg_waldump.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_waldump.c - decode and display WAL
4  *
5  * Copyright (c) 2013-2020, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  * src/bin/pg_waldump/pg_waldump.c
9  *-------------------------------------------------------------------------
10  */
11 
12 #define FRONTEND 1
13 #include "postgres.h"
14 
15 #include <dirent.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 
19 #include "access/transam.h"
20 #include "access/xlog_internal.h"
21 #include "access/xlogreader.h"
22 #include "access/xlogrecord.h"
23 #include "common/fe_memutils.h"
24 #include "common/logging.h"
25 #include "getopt_long.h"
26 #include "rmgrdesc.h"
27 
28 static const char *progname;
29 
30 static int WalSegSz;
31 
32 typedef struct XLogDumpPrivate
33 {
39 
40 typedef struct XLogDumpConfig
41 {
42  /* display options */
46  bool follow;
47  bool stats;
49 
50  /* filter options */
55 
56 typedef struct Stats
57 {
58  uint64 count;
59  uint64 rec_len;
60  uint64 fpi_len;
61 } Stats;
62 
63 #define MAX_XLINFO_TYPES 16
64 
65 typedef struct XLogDumpStats
66 {
67  uint64 count;
68  Stats rmgr_stats[RM_NEXT_ID];
71 
72 #define fatal_error(...) do { pg_log_fatal(__VA_ARGS__); exit(EXIT_FAILURE); } while(0)
73 
74 static void
76 {
77  int i;
78 
79  for (i = 0; i <= RM_MAX_ID; i++)
80  {
81  printf("%s\n", RmgrDescTable[i].rm_name);
82  }
83 }
84 
85 /*
86  * Check whether directory exists and whether we can open it. Keep errno set so
87  * that the caller can report errors somewhat more accurately.
88  */
89 static bool
91 {
92  DIR *dir = opendir(directory);
93 
94  if (dir == NULL)
95  return false;
96  closedir(dir);
97  return true;
98 }
99 
100 /*
101  * Split a pathname as dirname(1) and basename(1) would.
102  *
103  * XXX this probably doesn't do very well on Windows. We probably need to
104  * apply canonicalize_path(), at the very least.
105  */
106 static void
107 split_path(const char *path, char **dir, char **fname)
108 {
109  char *sep;
110 
111  /* split filepath into directory & filename */
112  sep = strrchr(path, '/');
113 
114  /* directory path */
115  if (sep != NULL)
116  {
117  *dir = pnstrdup(path, sep - path);
118  *fname = pg_strdup(sep + 1);
119  }
120  /* local directory */
121  else
122  {
123  *dir = NULL;
124  *fname = pg_strdup(path);
125  }
126 }
127 
128 /*
129  * Open the file in the valid target directory.
130  *
131  * return a read only fd
132  */
133 static int
134 open_file_in_directory(const char *directory, const char *fname)
135 {
136  int fd = -1;
137  char fpath[MAXPGPATH];
138 
139  Assert(directory != NULL);
140 
141  snprintf(fpath, MAXPGPATH, "%s/%s", directory, fname);
142  fd = open(fpath, O_RDONLY | PG_BINARY, 0);
143 
144  if (fd < 0 && errno != ENOENT)
145  fatal_error("could not open file \"%s\": %m", fname);
146  return fd;
147 }
148 
149 /*
150  * Try to find fname in the given directory. Returns true if it is found,
151  * false otherwise. If fname is NULL, search the complete directory for any
152  * file with a valid WAL file name. If file is successfully opened, set the
153  * wal segment size.
154  */
155 static bool
156 search_directory(const char *directory, const char *fname)
157 {
158  int fd = -1;
159  DIR *xldir;
160 
161  /* open file if valid filename is provided */
162  if (fname != NULL)
163  fd = open_file_in_directory(directory, fname);
164 
165  /*
166  * A valid file name is not passed, so search the complete directory. If
167  * we find any file whose name is a valid WAL file name then try to open
168  * it. If we cannot open it, bail out.
169  */
170  else if ((xldir = opendir(directory)) != NULL)
171  {
172  struct dirent *xlde;
173 
174  while ((xlde = readdir(xldir)) != NULL)
175  {
176  if (IsXLogFileName(xlde->d_name))
177  {
178  fd = open_file_in_directory(directory, xlde->d_name);
179  fname = xlde->d_name;
180  break;
181  }
182  }
183 
184  closedir(xldir);
185  }
186 
187  /* set WalSegSz if file is successfully opened */
188  if (fd >= 0)
189  {
191  int r;
192 
193  r = read(fd, buf.data, XLOG_BLCKSZ);
194  if (r == XLOG_BLCKSZ)
195  {
197 
198  WalSegSz = longhdr->xlp_seg_size;
199 
201  fatal_error(ngettext("WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" header specifies %d byte",
202  "WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" header specifies %d bytes",
203  WalSegSz),
204  fname, WalSegSz);
205  }
206  else
207  {
208  if (errno != 0)
209  fatal_error("could not read file \"%s\": %m",
210  fname);
211  else
212  fatal_error("could not read file \"%s\": read %d of %zu",
213  fname, r, (Size) XLOG_BLCKSZ);
214  }
215  close(fd);
216  return true;
217  }
218 
219  return false;
220 }
221 
222 /*
223  * Identify the target directory.
224  *
225  * Try to find the file in several places:
226  * if directory != NULL:
227  * directory /
228  * directory / XLOGDIR /
229  * else
230  * .
231  * XLOGDIR /
232  * $PGDATA / XLOGDIR /
233  *
234  * The valid target directory is returned.
235  */
236 static char *
238 {
239  char fpath[MAXPGPATH];
240 
241  if (directory != NULL)
242  {
243  if (search_directory(directory, fname))
244  return pg_strdup(directory);
245 
246  /* directory / XLOGDIR */
247  snprintf(fpath, MAXPGPATH, "%s/%s", directory, XLOGDIR);
248  if (search_directory(fpath, fname))
249  return pg_strdup(fpath);
250  }
251  else
252  {
253  const char *datadir;
254 
255  /* current directory */
256  if (search_directory(".", fname))
257  return pg_strdup(".");
258  /* XLOGDIR */
259  if (search_directory(XLOGDIR, fname))
260  return pg_strdup(XLOGDIR);
261 
262  datadir = getenv("PGDATA");
263  /* $PGDATA / XLOGDIR */
264  if (datadir != NULL)
265  {
266  snprintf(fpath, MAXPGPATH, "%s/%s", datadir, XLOGDIR);
267  if (search_directory(fpath, fname))
268  return pg_strdup(fpath);
269  }
270  }
271 
272  /* could not locate WAL file */
273  if (fname)
274  fatal_error("could not locate WAL file \"%s\"", fname);
275  else
276  fatal_error("could not find any WAL file");
277 
278  return NULL; /* not reached */
279 }
280 
281 /* pg_waldump's openSegment callback for WALRead */
282 static int
284  TimeLineID *tli_p)
285 {
286  TimeLineID tli = *tli_p;
287  char fname[MAXPGPATH];
288  int fd;
289  int tries;
290 
291  XLogFileName(fname, tli, nextSegNo, segcxt->ws_segsize);
292 
293  /*
294  * In follow mode there is a short period of time after the server has
295  * written the end of the previous file before the new file is available.
296  * So we loop for 5 seconds looking for the file to appear before giving
297  * up.
298  */
299  for (tries = 0; tries < 10; tries++)
300  {
301  fd = open_file_in_directory(segcxt->ws_dir, fname);
302  if (fd >= 0)
303  return fd;
304  if (errno == ENOENT)
305  {
306  int save_errno = errno;
307 
308  /* File not there yet, try again */
309  pg_usleep(500 * 1000);
310 
311  errno = save_errno;
312  continue;
313  }
314  /* Any other error, fall through and fail */
315  break;
316  }
317 
318  fatal_error("could not find file \"%s\": %m", fname);
319  return -1; /* keep compiler quiet */
320 }
321 
322 /*
323  * XLogReader read_page callback
324  */
325 static int
326 WALDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen,
327  XLogRecPtr targetPtr, char *readBuff)
328 {
329  XLogDumpPrivate *private = state->private_data;
330  int count = XLOG_BLCKSZ;
331  WALReadError errinfo;
332 
333  if (private->endptr != InvalidXLogRecPtr)
334  {
335  if (targetPagePtr + XLOG_BLCKSZ <= private->endptr)
336  count = XLOG_BLCKSZ;
337  else if (targetPagePtr + reqLen <= private->endptr)
338  count = private->endptr - targetPagePtr;
339  else
340  {
341  private->endptr_reached = true;
342  return -1;
343  }
344  }
345 
346  if (!WALRead(readBuff, targetPagePtr, count, private->timeline,
347  &state->seg, &state->segcxt, WALDumpOpenSegment, &errinfo))
348  {
349  WALOpenSegment *seg = &errinfo.wre_seg;
350  char fname[MAXPGPATH];
351 
352  XLogFileName(fname, seg->ws_tli, seg->ws_segno,
353  state->segcxt.ws_segsize);
354 
355  if (errinfo.wre_errno != 0)
356  {
357  errno = errinfo.wre_errno;
358  fatal_error("could not read from file %s, offset %u: %m",
359  fname, errinfo.wre_off);
360  }
361  else
362  fatal_error("could not read from file %s, offset %u: read %d of %zu",
363  fname, errinfo.wre_off, errinfo.wre_read,
364  (Size) errinfo.wre_req);
365  }
366 
367  return count;
368 }
369 
370 /*
371  * Calculate the size of a record, split into !FPI and FPI parts.
372  */
373 static void
374 XLogDumpRecordLen(XLogReaderState *record, uint32 *rec_len, uint32 *fpi_len)
375 {
376  int block_id;
377 
378  /*
379  * Calculate the amount of FPI data in the record.
380  *
381  * XXX: We peek into xlogreader's private decoded backup blocks for the
382  * bimg_len indicating the length of FPI data. It doesn't seem worth it to
383  * add an accessor macro for this.
384  */
385  *fpi_len = 0;
386  for (block_id = 0; block_id <= record->max_block_id; block_id++)
387  {
388  if (XLogRecHasBlockImage(record, block_id))
389  *fpi_len += record->blocks[block_id].bimg_len;
390  }
391 
392  /*
393  * Calculate the length of the record as the total length - the length of
394  * all the block images.
395  */
396  *rec_len = XLogRecGetTotalLen(record) - *fpi_len;
397 }
398 
399 /*
400  * Store per-rmgr and per-record statistics for a given record.
401  */
402 static void
404  XLogReaderState *record)
405 {
406  RmgrId rmid;
407  uint8 recid;
408  uint32 rec_len;
409  uint32 fpi_len;
410 
411  stats->count++;
412 
413  rmid = XLogRecGetRmid(record);
414 
415  XLogDumpRecordLen(record, &rec_len, &fpi_len);
416 
417  /* Update per-rmgr statistics */
418 
419  stats->rmgr_stats[rmid].count++;
420  stats->rmgr_stats[rmid].rec_len += rec_len;
421  stats->rmgr_stats[rmid].fpi_len += fpi_len;
422 
423  /*
424  * Update per-record statistics, where the record is identified by a
425  * combination of the RmgrId and the four bits of the xl_info field that
426  * are the rmgr's domain (resulting in sixteen possible entries per
427  * RmgrId).
428  */
429 
430  recid = XLogRecGetInfo(record) >> 4;
431 
432  stats->record_stats[rmid][recid].count++;
433  stats->record_stats[rmid][recid].rec_len += rec_len;
434  stats->record_stats[rmid][recid].fpi_len += fpi_len;
435 }
436 
437 /*
438  * Print a record to stdout
439  */
440 static void
442 {
443  const char *id;
444  const RmgrDescData *desc = &RmgrDescTable[XLogRecGetRmid(record)];
445  uint32 rec_len;
446  uint32 fpi_len;
447  RelFileNode rnode;
448  ForkNumber forknum;
449  BlockNumber blk;
450  int block_id;
451  uint8 info = XLogRecGetInfo(record);
452  XLogRecPtr xl_prev = XLogRecGetPrev(record);
453  StringInfoData s;
454 
455  XLogDumpRecordLen(record, &rec_len, &fpi_len);
456 
457  printf("rmgr: %-11s len (rec/tot): %6u/%6u, tx: %10u, lsn: %X/%08X, prev %X/%08X, ",
458  desc->rm_name,
459  rec_len, XLogRecGetTotalLen(record),
460  XLogRecGetXid(record),
461  (uint32) (record->ReadRecPtr >> 32), (uint32) record->ReadRecPtr,
462  (uint32) (xl_prev >> 32), (uint32) xl_prev);
463 
464  id = desc->rm_identify(info);
465  if (id == NULL)
466  printf("desc: UNKNOWN (%x) ", info & ~XLR_INFO_MASK);
467  else
468  printf("desc: %s ", id);
469 
470  initStringInfo(&s);
471  desc->rm_desc(&s, record);
472  printf("%s", s.data);
473  pfree(s.data);
474 
475  if (!config->bkp_details)
476  {
477  /* print block references (short format) */
478  for (block_id = 0; block_id <= record->max_block_id; block_id++)
479  {
480  if (!XLogRecHasBlockRef(record, block_id))
481  continue;
482 
483  XLogRecGetBlockTag(record, block_id, &rnode, &forknum, &blk);
484  if (forknum != MAIN_FORKNUM)
485  printf(", blkref #%u: rel %u/%u/%u fork %s blk %u",
486  block_id,
487  rnode.spcNode, rnode.dbNode, rnode.relNode,
488  forkNames[forknum],
489  blk);
490  else
491  printf(", blkref #%u: rel %u/%u/%u blk %u",
492  block_id,
493  rnode.spcNode, rnode.dbNode, rnode.relNode,
494  blk);
495  if (XLogRecHasBlockImage(record, block_id))
496  {
497  if (XLogRecBlockImageApply(record, block_id))
498  printf(" FPW");
499  else
500  printf(" FPW for WAL verification");
501  }
502  }
503  putchar('\n');
504  }
505  else
506  {
507  /* print block references (detailed format) */
508  putchar('\n');
509  for (block_id = 0; block_id <= record->max_block_id; block_id++)
510  {
511  if (!XLogRecHasBlockRef(record, block_id))
512  continue;
513 
514  XLogRecGetBlockTag(record, block_id, &rnode, &forknum, &blk);
515  printf("\tblkref #%u: rel %u/%u/%u fork %s blk %u",
516  block_id,
517  rnode.spcNode, rnode.dbNode, rnode.relNode,
518  forkNames[forknum],
519  blk);
520  if (XLogRecHasBlockImage(record, block_id))
521  {
522  if (record->blocks[block_id].bimg_info &
524  {
525  printf(" (FPW%s); hole: offset: %u, length: %u, "
526  "compression saved: %u",
527  XLogRecBlockImageApply(record, block_id) ?
528  "" : " for WAL verification",
529  record->blocks[block_id].hole_offset,
530  record->blocks[block_id].hole_length,
531  BLCKSZ -
532  record->blocks[block_id].hole_length -
533  record->blocks[block_id].bimg_len);
534  }
535  else
536  {
537  printf(" (FPW%s); hole: offset: %u, length: %u",
538  XLogRecBlockImageApply(record, block_id) ?
539  "" : " for WAL verification",
540  record->blocks[block_id].hole_offset,
541  record->blocks[block_id].hole_length);
542  }
543  }
544  putchar('\n');
545  }
546  }
547 }
548 
549 /*
550  * Display a single row of record counts and sizes for an rmgr or record.
551  */
552 static void
553 XLogDumpStatsRow(const char *name,
554  uint64 n, uint64 total_count,
555  uint64 rec_len, uint64 total_rec_len,
556  uint64 fpi_len, uint64 total_fpi_len,
557  uint64 tot_len, uint64 total_len)
558 {
559  double n_pct,
560  rec_len_pct,
561  fpi_len_pct,
562  tot_len_pct;
563 
564  n_pct = 0;
565  if (total_count != 0)
566  n_pct = 100 * (double) n / total_count;
567 
568  rec_len_pct = 0;
569  if (total_rec_len != 0)
570  rec_len_pct = 100 * (double) rec_len / total_rec_len;
571 
572  fpi_len_pct = 0;
573  if (total_fpi_len != 0)
574  fpi_len_pct = 100 * (double) fpi_len / total_fpi_len;
575 
576  tot_len_pct = 0;
577  if (total_len != 0)
578  tot_len_pct = 100 * (double) tot_len / total_len;
579 
580  printf("%-27s "
581  "%20" INT64_MODIFIER "u (%6.02f) "
582  "%20" INT64_MODIFIER "u (%6.02f) "
583  "%20" INT64_MODIFIER "u (%6.02f) "
584  "%20" INT64_MODIFIER "u (%6.02f)\n",
585  name, n, n_pct, rec_len, rec_len_pct, fpi_len, fpi_len_pct,
586  tot_len, tot_len_pct);
587 }
588 
589 
590 /*
591  * Display summary statistics about the records seen so far.
592  */
593 static void
595 {
596  int ri,
597  rj;
598  uint64 total_count = 0;
599  uint64 total_rec_len = 0;
600  uint64 total_fpi_len = 0;
601  uint64 total_len = 0;
602  double rec_len_pct,
603  fpi_len_pct;
604 
605  /* ---
606  * Make a first pass to calculate column totals:
607  * count(*),
608  * sum(xl_len+SizeOfXLogRecord),
609  * sum(xl_tot_len-xl_len-SizeOfXLogRecord), and
610  * sum(xl_tot_len).
611  * These are used to calculate percentages for each record type.
612  * ---
613  */
614 
615  for (ri = 0; ri < RM_NEXT_ID; ri++)
616  {
617  total_count += stats->rmgr_stats[ri].count;
618  total_rec_len += stats->rmgr_stats[ri].rec_len;
619  total_fpi_len += stats->rmgr_stats[ri].fpi_len;
620  }
621  total_len = total_rec_len + total_fpi_len;
622 
623  /*
624  * 27 is strlen("Transaction/COMMIT_PREPARED"), 20 is strlen(2^64), 8 is
625  * strlen("(100.00%)")
626  */
627 
628  printf("%-27s %20s %8s %20s %8s %20s %8s %20s %8s\n"
629  "%-27s %20s %8s %20s %8s %20s %8s %20s %8s\n",
630  "Type", "N", "(%)", "Record size", "(%)", "FPI size", "(%)", "Combined size", "(%)",
631  "----", "-", "---", "-----------", "---", "--------", "---", "-------------", "---");
632 
633  for (ri = 0; ri < RM_NEXT_ID; ri++)
634  {
635  uint64 count,
636  rec_len,
637  fpi_len,
638  tot_len;
639  const RmgrDescData *desc = &RmgrDescTable[ri];
640 
641  if (!config->stats_per_record)
642  {
643  count = stats->rmgr_stats[ri].count;
644  rec_len = stats->rmgr_stats[ri].rec_len;
645  fpi_len = stats->rmgr_stats[ri].fpi_len;
646  tot_len = rec_len + fpi_len;
647 
649  count, total_count, rec_len, total_rec_len,
650  fpi_len, total_fpi_len, tot_len, total_len);
651  }
652  else
653  {
654  for (rj = 0; rj < MAX_XLINFO_TYPES; rj++)
655  {
656  const char *id;
657 
658  count = stats->record_stats[ri][rj].count;
659  rec_len = stats->record_stats[ri][rj].rec_len;
660  fpi_len = stats->record_stats[ri][rj].fpi_len;
661  tot_len = rec_len + fpi_len;
662 
663  /* Skip undefined combinations and ones that didn't occur */
664  if (count == 0)
665  continue;
666 
667  /* the upper four bits in xl_info are the rmgr's */
668  id = desc->rm_identify(rj << 4);
669  if (id == NULL)
670  id = psprintf("UNKNOWN (%x)", rj << 4);
671 
672  XLogDumpStatsRow(psprintf("%s/%s", desc->rm_name, id),
673  count, total_count, rec_len, total_rec_len,
674  fpi_len, total_fpi_len, tot_len, total_len);
675  }
676  }
677  }
678 
679  printf("%-27s %20s %8s %20s %8s %20s %8s %20s\n",
680  "", "--------", "", "--------", "", "--------", "", "--------");
681 
682  /*
683  * The percentages in earlier rows were calculated against the column
684  * total, but the ones that follow are against the row total. Note that
685  * these are displayed with a % symbol to differentiate them from the
686  * earlier ones, and are thus up to 9 characters long.
687  */
688 
689  rec_len_pct = 0;
690  if (total_len != 0)
691  rec_len_pct = 100 * (double) total_rec_len / total_len;
692 
693  fpi_len_pct = 0;
694  if (total_len != 0)
695  fpi_len_pct = 100 * (double) total_fpi_len / total_len;
696 
697  printf("%-27s "
698  "%20" INT64_MODIFIER "u %-9s"
699  "%20" INT64_MODIFIER "u %-9s"
700  "%20" INT64_MODIFIER "u %-9s"
701  "%20" INT64_MODIFIER "u %-6s\n",
702  "Total", stats->count, "",
703  total_rec_len, psprintf("[%.02f%%]", rec_len_pct),
704  total_fpi_len, psprintf("[%.02f%%]", fpi_len_pct),
705  total_len, "[100%]");
706 }
707 
708 static void
709 usage(void)
710 {
711  printf(_("%s decodes and displays PostgreSQL write-ahead logs for debugging.\n\n"),
712  progname);
713  printf(_("Usage:\n"));
714  printf(_(" %s [OPTION]... [STARTSEG [ENDSEG]]\n"), progname);
715  printf(_("\nOptions:\n"));
716  printf(_(" -b, --bkp-details output detailed information about backup blocks\n"));
717  printf(_(" -e, --end=RECPTR stop reading at WAL location RECPTR\n"));
718  printf(_(" -f, --follow keep retrying after reaching end of WAL\n"));
719  printf(_(" -n, --limit=N number of records to display\n"));
720  printf(_(" -p, --path=PATH directory in which to find log segment files or a\n"
721  " directory with a ./pg_wal that contains such files\n"
722  " (default: current directory, ./pg_wal, $PGDATA/pg_wal)\n"));
723  printf(_(" -r, --rmgr=RMGR only show records generated by resource manager RMGR;\n"
724  " use --rmgr=list to list valid resource manager names\n"));
725  printf(_(" -s, --start=RECPTR start reading at WAL location RECPTR\n"));
726  printf(_(" -t, --timeline=TLI timeline from which to read log records\n"
727  " (default: 1 or the value used in STARTSEG)\n"));
728  printf(_(" -V, --version output version information, then exit\n"));
729  printf(_(" -x, --xid=XID only show records with transaction ID XID\n"));
730  printf(_(" -z, --stats[=record] show statistics instead of records\n"
731  " (optionally, show per-record statistics)\n"));
732  printf(_(" -?, --help show this help, then exit\n"));
733  printf(_("\nReport bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
734 }
735 
736 int
737 main(int argc, char **argv)
738 {
739  uint32 xlogid;
740  uint32 xrecoff;
741  XLogReaderState *xlogreader_state;
742  XLogDumpPrivate private;
743  XLogDumpConfig config;
744  XLogDumpStats stats;
745  XLogRecord *record;
746  XLogRecPtr first_record;
747  char *waldir = NULL;
748  char *errormsg;
749 
750  static struct option long_options[] = {
751  {"bkp-details", no_argument, NULL, 'b'},
752  {"end", required_argument, NULL, 'e'},
753  {"follow", no_argument, NULL, 'f'},
754  {"help", no_argument, NULL, '?'},
755  {"limit", required_argument, NULL, 'n'},
756  {"path", required_argument, NULL, 'p'},
757  {"rmgr", required_argument, NULL, 'r'},
758  {"start", required_argument, NULL, 's'},
759  {"timeline", required_argument, NULL, 't'},
760  {"xid", required_argument, NULL, 'x'},
761  {"version", no_argument, NULL, 'V'},
762  {"stats", optional_argument, NULL, 'z'},
763  {NULL, 0, NULL, 0}
764  };
765 
766  int option;
767  int optindex = 0;
768 
769  pg_logging_init(argv[0]);
770  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_waldump"));
771  progname = get_progname(argv[0]);
772 
773  if (argc > 1)
774  {
775  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
776  {
777  usage();
778  exit(0);
779  }
780  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
781  {
782  puts("pg_waldump (PostgreSQL) " PG_VERSION);
783  exit(0);
784  }
785  }
786 
787  memset(&private, 0, sizeof(XLogDumpPrivate));
788  memset(&config, 0, sizeof(XLogDumpConfig));
789  memset(&stats, 0, sizeof(XLogDumpStats));
790 
791  private.timeline = 1;
792  private.startptr = InvalidXLogRecPtr;
793  private.endptr = InvalidXLogRecPtr;
794  private.endptr_reached = false;
795 
796  config.bkp_details = false;
797  config.stop_after_records = -1;
798  config.already_displayed_records = 0;
799  config.follow = false;
800  config.filter_by_rmgr = -1;
802  config.filter_by_xid_enabled = false;
803  config.stats = false;
804  config.stats_per_record = false;
805 
806  if (argc <= 1)
807  {
808  pg_log_error("no arguments specified");
809  goto bad_argument;
810  }
811 
812  while ((option = getopt_long(argc, argv, "be:fn:p:r:s:t:x:z",
813  long_options, &optindex)) != -1)
814  {
815  switch (option)
816  {
817  case 'b':
818  config.bkp_details = true;
819  break;
820  case 'e':
821  if (sscanf(optarg, "%X/%X", &xlogid, &xrecoff) != 2)
822  {
823  pg_log_error("could not parse end WAL location \"%s\"",
824  optarg);
825  goto bad_argument;
826  }
827  private.endptr = (uint64) xlogid << 32 | xrecoff;
828  break;
829  case 'f':
830  config.follow = true;
831  break;
832  case 'n':
833  if (sscanf(optarg, "%d", &config.stop_after_records) != 1)
834  {
835  pg_log_error("could not parse limit \"%s\"", optarg);
836  goto bad_argument;
837  }
838  break;
839  case 'p':
840  waldir = pg_strdup(optarg);
841  break;
842  case 'r':
843  {
844  int i;
845 
846  if (pg_strcasecmp(optarg, "list") == 0)
847  {
848  print_rmgr_list();
849  exit(EXIT_SUCCESS);
850  }
851 
852  for (i = 0; i <= RM_MAX_ID; i++)
853  {
854  if (pg_strcasecmp(optarg, RmgrDescTable[i].rm_name) == 0)
855  {
856  config.filter_by_rmgr = i;
857  break;
858  }
859  }
860 
861  if (config.filter_by_rmgr == -1)
862  {
863  pg_log_error("resource manager \"%s\" does not exist",
864  optarg);
865  goto bad_argument;
866  }
867  }
868  break;
869  case 's':
870  if (sscanf(optarg, "%X/%X", &xlogid, &xrecoff) != 2)
871  {
872  pg_log_error("could not parse start WAL location \"%s\"",
873  optarg);
874  goto bad_argument;
875  }
876  else
877  private.startptr = (uint64) xlogid << 32 | xrecoff;
878  break;
879  case 't':
880  if (sscanf(optarg, "%d", &private.timeline) != 1)
881  {
882  pg_log_error("could not parse timeline \"%s\"", optarg);
883  goto bad_argument;
884  }
885  break;
886  case 'x':
887  if (sscanf(optarg, "%u", &config.filter_by_xid) != 1)
888  {
889  pg_log_error("could not parse \"%s\" as a transaction ID",
890  optarg);
891  goto bad_argument;
892  }
893  config.filter_by_xid_enabled = true;
894  break;
895  case 'z':
896  config.stats = true;
897  config.stats_per_record = false;
898  if (optarg)
899  {
900  if (strcmp(optarg, "record") == 0)
901  config.stats_per_record = true;
902  else if (strcmp(optarg, "rmgr") != 0)
903  {
904  pg_log_error("unrecognized argument to --stats: %s",
905  optarg);
906  goto bad_argument;
907  }
908  }
909  break;
910  default:
911  goto bad_argument;
912  }
913  }
914 
915  if ((optind + 2) < argc)
916  {
917  pg_log_error("too many command-line arguments (first is \"%s\")",
918  argv[optind + 2]);
919  goto bad_argument;
920  }
921 
922  if (waldir != NULL)
923  {
924  /* validate path points to directory */
925  if (!verify_directory(waldir))
926  {
927  pg_log_error("could not open directory \"%s\": %m", waldir);
928  goto bad_argument;
929  }
930  }
931 
932  /* parse files as start/end boundaries, extract path if not specified */
933  if (optind < argc)
934  {
935  char *directory = NULL;
936  char *fname = NULL;
937  int fd;
938  XLogSegNo segno;
939 
940  split_path(argv[optind], &directory, &fname);
941 
942  if (waldir == NULL && directory != NULL)
943  {
944  waldir = directory;
945 
946  if (!verify_directory(waldir))
947  fatal_error("could not open directory \"%s\": %m", waldir);
948  }
949 
950  waldir = identify_target_directory(waldir, fname);
951  fd = open_file_in_directory(waldir, fname);
952  if (fd < 0)
953  fatal_error("could not open file \"%s\"", fname);
954  close(fd);
955 
956  /* parse position from file */
957  XLogFromFileName(fname, &private.timeline, &segno, WalSegSz);
958 
959  if (XLogRecPtrIsInvalid(private.startptr))
960  XLogSegNoOffsetToRecPtr(segno, 0, WalSegSz, private.startptr);
961  else if (!XLByteInSeg(private.startptr, segno, WalSegSz))
962  {
963  pg_log_error("start WAL location %X/%X is not inside file \"%s\"",
964  (uint32) (private.startptr >> 32),
965  (uint32) private.startptr,
966  fname);
967  goto bad_argument;
968  }
969 
970  /* no second file specified, set end position */
971  if (!(optind + 1 < argc) && XLogRecPtrIsInvalid(private.endptr))
972  XLogSegNoOffsetToRecPtr(segno + 1, 0, WalSegSz, private.endptr);
973 
974  /* parse ENDSEG if passed */
975  if (optind + 1 < argc)
976  {
977  XLogSegNo endsegno;
978 
979  /* ignore directory, already have that */
980  split_path(argv[optind + 1], &directory, &fname);
981 
982  fd = open_file_in_directory(waldir, fname);
983  if (fd < 0)
984  fatal_error("could not open file \"%s\"", fname);
985  close(fd);
986 
987  /* parse position from file */
988  XLogFromFileName(fname, &private.timeline, &endsegno, WalSegSz);
989 
990  if (endsegno < segno)
991  fatal_error("ENDSEG %s is before STARTSEG %s",
992  argv[optind + 1], argv[optind]);
993 
994  if (XLogRecPtrIsInvalid(private.endptr))
995  XLogSegNoOffsetToRecPtr(endsegno + 1, 0, WalSegSz,
996  private.endptr);
997 
998  /* set segno to endsegno for check of --end */
999  segno = endsegno;
1000  }
1001 
1002 
1003  if (!XLByteInSeg(private.endptr, segno, WalSegSz) &&
1004  private.endptr != (segno + 1) * WalSegSz)
1005  {
1006  pg_log_error("end WAL location %X/%X is not inside file \"%s\"",
1007  (uint32) (private.endptr >> 32),
1008  (uint32) private.endptr,
1009  argv[argc - 1]);
1010  goto bad_argument;
1011  }
1012  }
1013  else
1014  waldir = identify_target_directory(waldir, NULL);
1015 
1016  /* we don't know what to print */
1017  if (XLogRecPtrIsInvalid(private.startptr))
1018  {
1019  pg_log_error("no start WAL location given");
1020  goto bad_argument;
1021  }
1022 
1023  /* done with argument parsing, do the actual work */
1024 
1025  /* we have everything we need, start reading */
1026  xlogreader_state = XLogReaderAllocate(WalSegSz, waldir, WALDumpReadPage,
1027  &private);
1028  if (!xlogreader_state)
1029  fatal_error("out of memory");
1030 
1031  /* first find a valid recptr to start from */
1032  first_record = XLogFindNextRecord(xlogreader_state, private.startptr);
1033 
1034  if (first_record == InvalidXLogRecPtr)
1035  fatal_error("could not find a valid record after %X/%X",
1036  (uint32) (private.startptr >> 32),
1037  (uint32) private.startptr);
1038 
1039  /*
1040  * Display a message that we're skipping data if `from` wasn't a pointer
1041  * to the start of a record and also wasn't a pointer to the beginning of
1042  * a segment (e.g. we were used in file mode).
1043  */
1044  if (first_record != private.startptr &&
1045  XLogSegmentOffset(private.startptr, WalSegSz) != 0)
1046  printf(ngettext("first record is after %X/%X, at %X/%X, skipping over %u byte\n",
1047  "first record is after %X/%X, at %X/%X, skipping over %u bytes\n",
1048  (first_record - private.startptr)),
1049  (uint32) (private.startptr >> 32), (uint32) private.startptr,
1050  (uint32) (first_record >> 32), (uint32) first_record,
1051  (uint32) (first_record - private.startptr));
1052 
1053  for (;;)
1054  {
1055  /* try to read the next record */
1056  record = XLogReadRecord(xlogreader_state, &errormsg);
1057  if (!record)
1058  {
1059  if (!config.follow || private.endptr_reached)
1060  break;
1061  else
1062  {
1063  pg_usleep(1000000L); /* 1 second */
1064  continue;
1065  }
1066  }
1067 
1068  /* apply all specified filters */
1069  if (config.filter_by_rmgr != -1 &&
1070  config.filter_by_rmgr != record->xl_rmid)
1071  continue;
1072 
1073  if (config.filter_by_xid_enabled &&
1074  config.filter_by_xid != record->xl_xid)
1075  continue;
1076 
1077  /* process the record */
1078  if (config.stats == true)
1079  XLogDumpCountRecord(&config, &stats, xlogreader_state);
1080  else
1081  XLogDumpDisplayRecord(&config, xlogreader_state);
1082 
1083  /* check whether we printed enough */
1084  config.already_displayed_records++;
1085  if (config.stop_after_records > 0 &&
1087  break;
1088  }
1089 
1090  if (config.stats == true)
1091  XLogDumpDisplayStats(&config, &stats);
1092 
1093  if (errormsg)
1094  fatal_error("error in WAL record at %X/%X: %s",
1095  (uint32) (xlogreader_state->ReadRecPtr >> 32),
1096  (uint32) xlogreader_state->ReadRecPtr,
1097  errormsg);
1098 
1099  XLogReaderFree(xlogreader_state);
1100 
1101  return EXIT_SUCCESS;
1102 
1103 bad_argument:
1104  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
1105  return EXIT_FAILURE;
1106 }
#define fatal_error(...)
Definition: pg_waldump.c:72
WALOpenSegment wre_seg
Definition: xlogreader.h:269
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:97
static void usage(void)
Definition: pg_waldump.c:709
char ws_dir[MAXPGPATH]
Definition: xlogreader.h:47
static int WALDumpOpenSegment(XLogSegNo nextSegNo, WALSegmentContext *segcxt, TimeLineID *tli_p)
Definition: pg_waldump.c:283
static void XLogDumpDisplayStats(XLogDumpConfig *config, XLogDumpStats *stats)
Definition: pg_waldump.c:594
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int already_displayed_records
Definition: pg_waldump.c:45
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
XLogRecPtr endptr
Definition: pg_waldump.c:36
uint32 TimeLineID
Definition: xlogdefs.h:52
#define EXIT_SUCCESS
Definition: settings.h:149
static bool search_directory(const char *directory, const char *fname)
Definition: pg_waldump.c:156
uint32 TransactionId
Definition: c.h:513
uint64 count
Definition: pg_waldump.c:58
#define XLogRecHasBlockImage(decoder, block_id)
Definition: xlogreader.h:293
int filter_by_rmgr
Definition: pg_waldump.c:51
XLogRecPtr startptr
Definition: pg_waldump.c:35
const char * get_progname(const char *argv0)
Definition: path.c:453
#define pg_log_error(...)
Definition: logging.h:79
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
void pg_logging_init(const char *argv0)
Definition: logging.c:39
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
uint64 count
Definition: pg_waldump.c:67
uint16 hole_offset
Definition: xlogreader.h:77
struct Stats Stats
unsigned char uint8
Definition: c.h:365
int closedir(DIR *)
Definition: dirent.c:113
#define XLogRecHasBlockRef(decoder, block_id)
Definition: xlogreader.h:291
Stats record_stats[RM_NEXT_ID][MAX_XLINFO_TYPES]
Definition: pg_waldump.c:69
RmgrId xl_rmid
Definition: xlogrecord.h:47
#define printf(...)
Definition: port.h:198
uint32 BlockNumber
Definition: block.h:31
TransactionId filter_by_xid
Definition: pg_waldump.c:52
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
bool bkp_details
Definition: pg_waldump.c:43
bool endptr_reached
Definition: pg_waldump.c:37
void * private_data
Definition: xlogreader.h:127
uint16 bimg_len
Definition: xlogreader.h:79
#define fprintf
Definition: port.h:196
Definition: dirent.h:9
static char * identify_target_directory(char *directory, char *fname)
Definition: pg_waldump.c:237
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1230
#define XLogRecGetTotalLen(decoder)
Definition: xlogreader.h:282
#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes)
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:74
Stats rmgr_stats[RM_NEXT_ID]
Definition: pg_waldump.c:68
WALOpenSegment seg
Definition: xlogreader.h:172
bool filter_by_xid_enabled
Definition: pg_waldump.c:53
void pg_usleep(long microsec)
Definition: signal.c:53
#define required_argument
Definition: getopt_long.h:25
void pfree(void *pointer)
Definition: mcxt.c:1056
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:261
int optind
Definition: getopt.c:50
Definition: dirent.c:25
uint16 hole_length
Definition: xlogreader.h:78
#define IsXLogFileName(fname)
TimeLineID timeline
Definition: pg_waldump.c:34
#define MAXPGPATH
const RmgrDescData RmgrDescTable[RM_MAX_ID+1]
Definition: rmgrdesc.c:38
DIR * opendir(const char *)
Definition: dirent.c:33
#define XLogRecGetPrev(decoder)
Definition: xlogreader.h:283
#define XLogFromFileName(fname, tli, logSegNo, wal_segsz_bytes)
static void XLogDumpCountRecord(XLogDumpConfig *config, XLogDumpStats *stats, XLogReaderState *record)
Definition: pg_waldump.c:403
static char * buf
Definition: pg_test_fsync.c:67
uint64 XLogSegNo
Definition: xlogdefs.h:41
static void XLogDumpDisplayRecord(XLogDumpConfig *config, XLogReaderState *record)
Definition: pg_waldump.c:441
bool WALRead(char *buf, XLogRecPtr startptr, Size count, TimeLineID tli, WALOpenSegment *seg, WALSegmentContext *segcxt, WALSegmentOpen openSegment, WALReadError *errinfo)
Definition: xlogreader.c:1054
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:134
XLogSegNo ws_segno
Definition: xlogreader.h:40
char * datadir
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define InvalidTransactionId
Definition: transam.h:31
static void split_path(const char *path, char **dir, char **fname)
Definition: pg_waldump.c:107
unsigned int uint32
Definition: c.h:367
struct XLogDumpConfig XLogDumpConfig
static int WALDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetPtr, char *readBuff)
Definition: pg_waldump.c:326
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:284
ForkNumber
Definition: relpath.h:40
const char * rm_name
Definition: rmgrdesc.h:16
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:135
#define RM_MAX_ID
Definition: rmgr.h:33
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1481
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:286
#define no_argument
Definition: getopt_long.h:24
#define ngettext(s, p, n)
Definition: c.h:1142
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1174
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:147
static int WalSegSz
Definition: pg_waldump.c:30
#define XLOGDIR
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
const char *(* rm_identify)(uint8 info)
Definition: rmgrdesc.h:18
uint8 RmgrId
Definition: rmgr.h:11
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:738
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define MAX_XLINFO_TYPES
Definition: pg_waldump.c:63
Definition: regguts.h:298
size_t Size
Definition: c.h:466
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
static const char * directory
Definition: zic.c:622
static int open_file_in_directory(const char *directory, const char *fname)
Definition: pg_waldump.c:134
#define optional_argument
Definition: getopt_long.h:26
struct dirent * readdir(DIR *)
Definition: dirent.c:77
static bool verify_directory(const char *directory)
Definition: pg_waldump.c:90
struct XLogDumpPrivate XLogDumpPrivate
const char * name
Definition: encode.c:521
TimeLineID ws_tli
Definition: xlogreader.h:41
uint64 fpi_len
Definition: pg_waldump.c:60
uint64 rec_len
Definition: pg_waldump.c:59
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:73
TransactionId xl_xid
Definition: xlogrecord.h:44
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:435
static const char * progname
Definition: pg_waldump.c:28
static void XLogDumpStatsRow(const char *name, uint64 n, uint64 total_count, uint64 rec_len, uint64 total_rec_len, uint64 fpi_len, uint64 total_fpi_len, uint64 tot_len, uint64 total_len)
Definition: pg_waldump.c:553
char * optarg
Definition: getopt.c:52
int i
int stop_after_records
Definition: pg_waldump.c:44
#define EXIT_FAILURE
Definition: settings.h:153
struct XLogDumpStats XLogDumpStats
WALSegmentContext segcxt
Definition: xlogreader.h:171
void(* rm_desc)(StringInfo buf, XLogReaderState *record)
Definition: rmgrdesc.h:17
char d_name[MAX_PATH]
Definition: dirent.h:14
#define XLogRecBlockImageApply(decoder, block_id)
Definition: xlogreader.h:295
static void print_rmgr_list(void)
Definition: pg_waldump.c:75
bool stats_per_record
Definition: pg_waldump.c:48
#define close(a)
Definition: win32.h:12
const char *const forkNames[]
Definition: relpath.c:33
char data[XLOG_BLCKSZ]
Definition: c.h:1108
#define snprintf
Definition: port.h:192
#define _(x)
Definition: elog.c:87
int main(int argc, char **argv)
Definition: pg_waldump.c:737
#define read(a, b, c)
Definition: win32.h:13
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:154
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:285
static void XLogDumpRecordLen(XLogReaderState *record, uint32 *rec_len, uint32 *fpi_len)
Definition: pg_waldump.c:374