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