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