PostgreSQL Source Code  git master
pg_waldump.c File Reference
#include "postgres.h"
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include "access/transam.h"
#include "access/xlog_internal.h"
#include "access/xlogreader.h"
#include "access/xlogrecord.h"
#include "common/fe_memutils.h"
#include "common/logging.h"
#include "getopt_long.h"
#include "rmgrdesc.h"
Include dependency graph for pg_waldump.c:

Go to the source code of this file.

Data Structures

struct  XLogDumpPrivate
 
struct  XLogDumpConfig
 
struct  Stats
 
struct  XLogDumpStats
 

Macros

#define FRONTEND   1
 
#define MAX_XLINFO_TYPES   16
 
#define fatal_error(...)   do { pg_log_fatal(__VA_ARGS__); exit(EXIT_FAILURE); } while(0)
 

Typedefs

typedef struct XLogDumpPrivate XLogDumpPrivate
 
typedef struct XLogDumpConfig XLogDumpConfig
 
typedef struct Stats Stats
 
typedef struct XLogDumpStats XLogDumpStats
 

Functions

static void print_rmgr_list (void)
 
static bool verify_directory (const char *directory)
 
static void split_path (const char *path, char **dir, char **fname)
 
static int open_file_in_directory (const char *directory, const char *fname)
 
static bool search_directory (const char *directory, const char *fname)
 
static char * identify_target_directory (char *directory, char *fname)
 
static void WALDumpOpenSegment (XLogReaderState *state, XLogSegNo nextSegNo, TimeLineID *tli_p)
 
static void WALDumpCloseSegment (XLogReaderState *state)
 
static int WALDumpReadPage (XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetPtr, char *readBuff)
 
static void XLogDumpRecordLen (XLogReaderState *record, uint32 *rec_len, uint32 *fpi_len)
 
static void XLogDumpCountRecord (XLogDumpConfig *config, XLogDumpStats *stats, XLogReaderState *record)
 
static void XLogDumpDisplayRecord (XLogDumpConfig *config, XLogReaderState *record)
 
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)
 
static void XLogDumpDisplayStats (XLogDumpConfig *config, XLogDumpStats *stats)
 
static void usage (void)
 
int main (int argc, char **argv)
 

Variables

static const char * progname
 
static int WalSegSz
 

Macro Definition Documentation

◆ fatal_error

#define fatal_error (   ...)    do { pg_log_fatal(__VA_ARGS__); exit(EXIT_FAILURE); } while(0)

◆ FRONTEND

#define FRONTEND   1

Definition at line 12 of file pg_waldump.c.

◆ MAX_XLINFO_TYPES

#define MAX_XLINFO_TYPES   16

Definition at line 64 of file pg_waldump.c.

Referenced by XLogDumpDisplayStats().

Typedef Documentation

◆ Stats

typedef struct Stats Stats

◆ XLogDumpConfig

◆ XLogDumpPrivate

◆ XLogDumpStats

typedef struct XLogDumpStats XLogDumpStats

Function Documentation

◆ identify_target_directory()

static char* identify_target_directory ( char *  directory,
char *  fname 
)
static

Definition at line 238 of file pg_waldump.c.

References datadir, fatal_error, MAXPGPATH, pg_strdup(), search_directory(), snprintf, and XLOGDIR.

Referenced by main().

239 {
240  char fpath[MAXPGPATH];
241 
242  if (directory != NULL)
243  {
244  if (search_directory(directory, fname))
245  return pg_strdup(directory);
246 
247  /* directory / XLOGDIR */
248  snprintf(fpath, MAXPGPATH, "%s/%s", directory, XLOGDIR);
249  if (search_directory(fpath, fname))
250  return pg_strdup(fpath);
251  }
252  else
253  {
254  const char *datadir;
255 
256  /* current directory */
257  if (search_directory(".", fname))
258  return pg_strdup(".");
259  /* XLOGDIR */
260  if (search_directory(XLOGDIR, fname))
261  return pg_strdup(XLOGDIR);
262 
263  datadir = getenv("PGDATA");
264  /* $PGDATA / XLOGDIR */
265  if (datadir != NULL)
266  {
267  snprintf(fpath, MAXPGPATH, "%s/%s", datadir, XLOGDIR);
268  if (search_directory(fpath, fname))
269  return pg_strdup(fpath);
270  }
271  }
272 
273  /* could not locate WAL file */
274  if (fname)
275  fatal_error("could not locate WAL file \"%s\"", fname);
276  else
277  fatal_error("could not find any WAL file");
278 
279  return NULL; /* not reached */
280 }
#define fatal_error(...)
Definition: pg_waldump.c:73
static bool search_directory(const char *directory, const char *fname)
Definition: pg_waldump.c:157
#define MAXPGPATH
char * datadir
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define XLOGDIR
static const char * directory
Definition: zic.c:622
#define snprintf
Definition: port.h:193

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 748 of file pg_waldump.c.

References _, XLogDumpConfig::already_displayed_records, XLogDumpConfig::bkp_details, close, directory, XLogDumpPrivate::endptr, EXIT_FAILURE, EXIT_SUCCESS, fatal_error, fd(), XLogDumpConfig::filter_by_rmgr, XLogDumpConfig::filter_by_xid, XLogDumpConfig::filter_by_xid_enabled, XLogDumpConfig::follow, fprintf, get_progname(), getopt_long(), i, identify_target_directory(), InvalidTransactionId, InvalidXLogRecPtr, ngettext, no_argument, open_file_in_directory(), optarg, optind, optional_argument, pg_log_error, pg_logging_init(), pg_strcasecmp(), pg_strdup(), PG_TEXTDOMAIN, pg_usleep(), print_rmgr_list(), printf, progname, XLogDumpConfig::quiet, XLogReaderState::ReadRecPtr, required_argument, RM_MAX_ID, RmgrDescTable, set_pglocale_pgservice(), split_path(), XLogDumpPrivate::startptr, XLogDumpConfig::stats, XLogDumpConfig::stats_per_record, XLogDumpConfig::stop_after_records, XLogDumpPrivate::timeline, usage(), verify_directory(), WALDumpCloseSegment(), WALDumpOpenSegment(), WALDumpReadPage(), WalSegSz, XLogRecord::xl_rmid, XL_ROUTINE, XLogRecord::xl_xid, XLByteInSeg, XLogDumpCountRecord(), XLogDumpDisplayRecord(), XLogDumpDisplayStats(), XLogFromFileName, XLogReaderAllocate(), XLogReaderFree(), XLogReadRecord(), XLogRecPtrIsInvalid, XLogSegmentOffset, and XLogSegNoOffsetToRecPtr.

749 {
750  uint32 xlogid;
751  uint32 xrecoff;
752  XLogReaderState *xlogreader_state;
753  XLogDumpPrivate private;
754  XLogDumpConfig config;
755  XLogDumpStats stats;
756  XLogRecord *record;
757  XLogRecPtr first_record;
758  char *waldir = NULL;
759  char *errormsg;
760 
761  static struct option long_options[] = {
762  {"bkp-details", no_argument, NULL, 'b'},
763  {"end", required_argument, NULL, 'e'},
764  {"follow", no_argument, NULL, 'f'},
765  {"help", no_argument, NULL, '?'},
766  {"limit", required_argument, NULL, 'n'},
767  {"path", required_argument, NULL, 'p'},
768  {"quiet", no_argument, NULL, 'q'},
769  {"rmgr", required_argument, NULL, 'r'},
770  {"start", required_argument, NULL, 's'},
771  {"timeline", required_argument, NULL, 't'},
772  {"xid", required_argument, NULL, 'x'},
773  {"version", no_argument, NULL, 'V'},
774  {"stats", optional_argument, NULL, 'z'},
775  {NULL, 0, NULL, 0}
776  };
777 
778  int option;
779  int optindex = 0;
780 
781  pg_logging_init(argv[0]);
782  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_waldump"));
783  progname = get_progname(argv[0]);
784 
785  if (argc > 1)
786  {
787  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
788  {
789  usage();
790  exit(0);
791  }
792  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
793  {
794  puts("pg_waldump (PostgreSQL) " PG_VERSION);
795  exit(0);
796  }
797  }
798 
799  memset(&private, 0, sizeof(XLogDumpPrivate));
800  memset(&config, 0, sizeof(XLogDumpConfig));
801  memset(&stats, 0, sizeof(XLogDumpStats));
802 
803  private.timeline = 1;
804  private.startptr = InvalidXLogRecPtr;
805  private.endptr = InvalidXLogRecPtr;
806  private.endptr_reached = false;
807 
808  config.quiet = false;
809  config.bkp_details = false;
810  config.stop_after_records = -1;
811  config.already_displayed_records = 0;
812  config.follow = false;
813  config.filter_by_rmgr = -1;
815  config.filter_by_xid_enabled = false;
816  config.stats = false;
817  config.stats_per_record = false;
818 
819  if (argc <= 1)
820  {
821  pg_log_error("no arguments specified");
822  goto bad_argument;
823  }
824 
825  while ((option = getopt_long(argc, argv, "be:fn:p:qr:s:t:x:z",
826  long_options, &optindex)) != -1)
827  {
828  switch (option)
829  {
830  case 'b':
831  config.bkp_details = true;
832  break;
833  case 'e':
834  if (sscanf(optarg, "%X/%X", &xlogid, &xrecoff) != 2)
835  {
836  pg_log_error("could not parse end WAL location \"%s\"",
837  optarg);
838  goto bad_argument;
839  }
840  private.endptr = (uint64) xlogid << 32 | xrecoff;
841  break;
842  case 'f':
843  config.follow = true;
844  break;
845  case 'n':
846  if (sscanf(optarg, "%d", &config.stop_after_records) != 1)
847  {
848  pg_log_error("could not parse limit \"%s\"", optarg);
849  goto bad_argument;
850  }
851  break;
852  case 'p':
853  waldir = pg_strdup(optarg);
854  break;
855  case 'q':
856  config.quiet = true;
857  break;
858  case 'r':
859  {
860  int i;
861 
862  if (pg_strcasecmp(optarg, "list") == 0)
863  {
864  print_rmgr_list();
865  exit(EXIT_SUCCESS);
866  }
867 
868  for (i = 0; i <= RM_MAX_ID; i++)
869  {
870  if (pg_strcasecmp(optarg, RmgrDescTable[i].rm_name) == 0)
871  {
872  config.filter_by_rmgr = i;
873  break;
874  }
875  }
876 
877  if (config.filter_by_rmgr == -1)
878  {
879  pg_log_error("resource manager \"%s\" does not exist",
880  optarg);
881  goto bad_argument;
882  }
883  }
884  break;
885  case 's':
886  if (sscanf(optarg, "%X/%X", &xlogid, &xrecoff) != 2)
887  {
888  pg_log_error("could not parse start WAL location \"%s\"",
889  optarg);
890  goto bad_argument;
891  }
892  else
893  private.startptr = (uint64) xlogid << 32 | xrecoff;
894  break;
895  case 't':
896  if (sscanf(optarg, "%d", &private.timeline) != 1)
897  {
898  pg_log_error("could not parse timeline \"%s\"", optarg);
899  goto bad_argument;
900  }
901  break;
902  case 'x':
903  if (sscanf(optarg, "%u", &config.filter_by_xid) != 1)
904  {
905  pg_log_error("could not parse \"%s\" as a transaction ID",
906  optarg);
907  goto bad_argument;
908  }
909  config.filter_by_xid_enabled = true;
910  break;
911  case 'z':
912  config.stats = true;
913  config.stats_per_record = false;
914  if (optarg)
915  {
916  if (strcmp(optarg, "record") == 0)
917  config.stats_per_record = true;
918  else if (strcmp(optarg, "rmgr") != 0)
919  {
920  pg_log_error("unrecognized argument to --stats: %s",
921  optarg);
922  goto bad_argument;
923  }
924  }
925  break;
926  default:
927  goto bad_argument;
928  }
929  }
930 
931  if ((optind + 2) < argc)
932  {
933  pg_log_error("too many command-line arguments (first is \"%s\")",
934  argv[optind + 2]);
935  goto bad_argument;
936  }
937 
938  if (waldir != NULL)
939  {
940  /* validate path points to directory */
941  if (!verify_directory(waldir))
942  {
943  pg_log_error("could not open directory \"%s\": %m", waldir);
944  goto bad_argument;
945  }
946  }
947 
948  /* parse files as start/end boundaries, extract path if not specified */
949  if (optind < argc)
950  {
951  char *directory = NULL;
952  char *fname = NULL;
953  int fd;
954  XLogSegNo segno;
955 
956  split_path(argv[optind], &directory, &fname);
957 
958  if (waldir == NULL && directory != NULL)
959  {
960  waldir = directory;
961 
962  if (!verify_directory(waldir))
963  fatal_error("could not open directory \"%s\": %m", waldir);
964  }
965 
966  waldir = identify_target_directory(waldir, fname);
967  fd = open_file_in_directory(waldir, fname);
968  if (fd < 0)
969  fatal_error("could not open file \"%s\"", fname);
970  close(fd);
971 
972  /* parse position from file */
973  XLogFromFileName(fname, &private.timeline, &segno, WalSegSz);
974 
975  if (XLogRecPtrIsInvalid(private.startptr))
976  XLogSegNoOffsetToRecPtr(segno, 0, WalSegSz, private.startptr);
977  else if (!XLByteInSeg(private.startptr, segno, WalSegSz))
978  {
979  pg_log_error("start WAL location %X/%X is not inside file \"%s\"",
980  (uint32) (private.startptr >> 32),
981  (uint32) private.startptr,
982  fname);
983  goto bad_argument;
984  }
985 
986  /* no second file specified, set end position */
987  if (!(optind + 1 < argc) && XLogRecPtrIsInvalid(private.endptr))
988  XLogSegNoOffsetToRecPtr(segno + 1, 0, WalSegSz, private.endptr);
989 
990  /* parse ENDSEG if passed */
991  if (optind + 1 < argc)
992  {
993  XLogSegNo endsegno;
994 
995  /* ignore directory, already have that */
996  split_path(argv[optind + 1], &directory, &fname);
997 
998  fd = open_file_in_directory(waldir, fname);
999  if (fd < 0)
1000  fatal_error("could not open file \"%s\"", fname);
1001  close(fd);
1002 
1003  /* parse position from file */
1004  XLogFromFileName(fname, &private.timeline, &endsegno, WalSegSz);
1005 
1006  if (endsegno < segno)
1007  fatal_error("ENDSEG %s is before STARTSEG %s",
1008  argv[optind + 1], argv[optind]);
1009 
1010  if (XLogRecPtrIsInvalid(private.endptr))
1011  XLogSegNoOffsetToRecPtr(endsegno + 1, 0, WalSegSz,
1012  private.endptr);
1013 
1014  /* set segno to endsegno for check of --end */
1015  segno = endsegno;
1016  }
1017 
1018 
1019  if (!XLByteInSeg(private.endptr, segno, WalSegSz) &&
1020  private.endptr != (segno + 1) * WalSegSz)
1021  {
1022  pg_log_error("end WAL location %X/%X is not inside file \"%s\"",
1023  (uint32) (private.endptr >> 32),
1024  (uint32) private.endptr,
1025  argv[argc - 1]);
1026  goto bad_argument;
1027  }
1028  }
1029  else
1030  waldir = identify_target_directory(waldir, NULL);
1031 
1032  /* we don't know what to print */
1033  if (XLogRecPtrIsInvalid(private.startptr))
1034  {
1035  pg_log_error("no start WAL location given");
1036  goto bad_argument;
1037  }
1038 
1039  /* done with argument parsing, do the actual work */
1040 
1041  /* we have everything we need, start reading */
1042  xlogreader_state =
1043  XLogReaderAllocate(WalSegSz, waldir,
1044  XL_ROUTINE(.page_read = WALDumpReadPage,
1045  .segment_open = WALDumpOpenSegment,
1046  .segment_close = WALDumpCloseSegment),
1047  &private);
1048  if (!xlogreader_state)
1049  fatal_error("out of memory");
1050 
1051  /* first find a valid recptr to start from */
1052  first_record = XLogFindNextRecord(xlogreader_state, private.startptr);
1053 
1054  if (first_record == InvalidXLogRecPtr)
1055  fatal_error("could not find a valid record after %X/%X",
1056  (uint32) (private.startptr >> 32),
1057  (uint32) private.startptr);
1058 
1059  /*
1060  * Display a message that we're skipping data if `from` wasn't a pointer
1061  * to the start of a record and also wasn't a pointer to the beginning of
1062  * a segment (e.g. we were used in file mode).
1063  */
1064  if (first_record != private.startptr &&
1065  XLogSegmentOffset(private.startptr, WalSegSz) != 0)
1066  printf(ngettext("first record is after %X/%X, at %X/%X, skipping over %u byte\n",
1067  "first record is after %X/%X, at %X/%X, skipping over %u bytes\n",
1068  (first_record - private.startptr)),
1069  (uint32) (private.startptr >> 32), (uint32) private.startptr,
1070  (uint32) (first_record >> 32), (uint32) first_record,
1071  (uint32) (first_record - private.startptr));
1072 
1073  for (;;)
1074  {
1075  /* try to read the next record */
1076  record = XLogReadRecord(xlogreader_state, &errormsg);
1077  if (!record)
1078  {
1079  if (!config.follow || private.endptr_reached)
1080  break;
1081  else
1082  {
1083  pg_usleep(1000000L); /* 1 second */
1084  continue;
1085  }
1086  }
1087 
1088  /* apply all specified filters */
1089  if (config.filter_by_rmgr != -1 &&
1090  config.filter_by_rmgr != record->xl_rmid)
1091  continue;
1092 
1093  if (config.filter_by_xid_enabled &&
1094  config.filter_by_xid != record->xl_xid)
1095  continue;
1096 
1097  /* perform any per-record work */
1098  if (!config.quiet)
1099  {
1100  if (config.stats == true)
1101  XLogDumpCountRecord(&config, &stats, xlogreader_state);
1102  else
1103  XLogDumpDisplayRecord(&config, xlogreader_state);
1104  }
1105 
1106  /* check whether we printed enough */
1107  config.already_displayed_records++;
1108  if (config.stop_after_records > 0 &&
1110  break;
1111  }
1112 
1113  if (config.stats == true && !config.quiet)
1114  XLogDumpDisplayStats(&config, &stats);
1115 
1116  if (errormsg)
1117  fatal_error("error in WAL record at %X/%X: %s",
1118  (uint32) (xlogreader_state->ReadRecPtr >> 32),
1119  (uint32) xlogreader_state->ReadRecPtr,
1120  errormsg);
1121 
1122  XLogReaderFree(xlogreader_state);
1123 
1124  return EXIT_SUCCESS;
1125 
1126 bad_argument:
1127  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
1128  return EXIT_FAILURE;
1129 }
#define fatal_error(...)
Definition: pg_waldump.c:73
static void usage(void)
Definition: pg_waldump.c:718
static void WALDumpOpenSegment(XLogReaderState *state, XLogSegNo nextSegNo, TimeLineID *tli_p)
Definition: pg_waldump.c:284
static void XLogDumpDisplayStats(XLogDumpConfig *config, XLogDumpStats *stats)
Definition: pg_waldump.c:603
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int already_displayed_records
Definition: pg_waldump.c:46
#define EXIT_SUCCESS
Definition: settings.h:150
int filter_by_rmgr
Definition: pg_waldump.c:52
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:76
RmgrId xl_rmid
Definition: xlogrecord.h:47
#define printf(...)
Definition: port.h:199
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
#define fprintf
Definition: port.h:197
static char * identify_target_directory(char *directory, char *fname)
Definition: pg_waldump.c:238
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes)
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
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:266
int optind
Definition: getopt.c:50
const RmgrDescData RmgrDescTable[RM_MAX_ID+1]
Definition: rmgrdesc.c:38
#define XLogFromFileName(fname, tli, logSegNo, wal_segsz_bytes)
static void WALDumpCloseSegment(XLogReaderState *state)
Definition: pg_waldump.c:326
static void XLogDumpCountRecord(XLogDumpConfig *config, XLogDumpStats *stats, XLogReaderState *record)
Definition: pg_waldump.c:412
uint64 XLogSegNo
Definition: xlogdefs.h:41
static void XLogDumpDisplayRecord(XLogDumpConfig *config, XLogReaderState *record)
Definition: pg_waldump.c:450
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:175
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:108
unsigned int uint32
Definition: c.h:367
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition: xlogreader.c:73
static int WALDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetPtr, char *readBuff)
Definition: pg_waldump.c:335
#define XL_ROUTINE(...)
Definition: xlogreader.h:116
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:137
#define RM_MAX_ID
Definition: rmgr.h:33
#define no_argument
Definition: getopt_long.h:24
#define ngettext(s, p, n)
Definition: c.h:1146
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1178
static int WalSegSz
Definition: pg_waldump.c:30
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static const char * directory
Definition: zic.c:622
static int open_file_in_directory(const char *directory, const char *fname)
Definition: pg_waldump.c:135
#define optional_argument
Definition: getopt_long.h:26
static bool verify_directory(const char *directory)
Definition: pg_waldump.c:91
TransactionId xl_xid
Definition: xlogrecord.h:44
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:434
static const char * progname
Definition: pg_waldump.c:28
char * optarg
Definition: getopt.c:52
int i
int stop_after_records
Definition: pg_waldump.c:45
#define EXIT_FAILURE
Definition: settings.h:154
static void print_rmgr_list(void)
Definition: pg_waldump.c:76
bool stats_per_record
Definition: pg_waldump.c:49
#define close(a)
Definition: win32.h:12
#define _(x)
Definition: elog.c:88
static XLogRecPtr startptr
Definition: basebackup.c:124

◆ open_file_in_directory()

static int open_file_in_directory ( const char *  directory,
const char *  fname 
)
static

Definition at line 135 of file pg_waldump.c.

References Assert, fatal_error, fd(), MAXPGPATH, PG_BINARY, and snprintf.

Referenced by main(), search_directory(), and WALDumpOpenSegment().

136 {
137  int fd = -1;
138  char fpath[MAXPGPATH];
139 
140  Assert(directory != NULL);
141 
142  snprintf(fpath, MAXPGPATH, "%s/%s", directory, fname);
143  fd = open(fpath, O_RDONLY | PG_BINARY, 0);
144 
145  if (fd < 0 && errno != ENOENT)
146  fatal_error("could not open file \"%s\": %m", fname);
147  return fd;
148 }
#define fatal_error(...)
Definition: pg_waldump.c:73
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1234
#define MAXPGPATH
#define Assert(condition)
Definition: c.h:738
static const char * directory
Definition: zic.c:622
#define snprintf
Definition: port.h:193

◆ print_rmgr_list()

static void print_rmgr_list ( void  )
static

Definition at line 76 of file pg_waldump.c.

References i, printf, RM_MAX_ID, and RmgrDescTable.

Referenced by main().

77 {
78  int i;
79 
80  for (i = 0; i <= RM_MAX_ID; i++)
81  {
82  printf("%s\n", RmgrDescTable[i].rm_name);
83  }
84 }
#define printf(...)
Definition: port.h:199
const RmgrDescData RmgrDescTable[RM_MAX_ID+1]
Definition: rmgrdesc.c:38
#define RM_MAX_ID
Definition: rmgr.h:33
int i

◆ search_directory()

static bool search_directory ( const char *  directory,
const char *  fname 
)
static

Definition at line 157 of file pg_waldump.c.

References buf, close, closedir(), dirent::d_name, PGAlignedXLogBlock::data, fatal_error, fd(), IsValidWalSegSize, IsXLogFileName, ngettext, open_file_in_directory(), opendir(), read, readdir(), WalSegSz, and XLogLongPageHeaderData::xlp_seg_size.

Referenced by identify_target_directory().

158 {
159  int fd = -1;
160  DIR *xldir;
161 
162  /* open file if valid filename is provided */
163  if (fname != NULL)
164  fd = open_file_in_directory(directory, fname);
165 
166  /*
167  * A valid file name is not passed, so search the complete directory. If
168  * we find any file whose name is a valid WAL file name then try to open
169  * it. If we cannot open it, bail out.
170  */
171  else if ((xldir = opendir(directory)) != NULL)
172  {
173  struct dirent *xlde;
174 
175  while ((xlde = readdir(xldir)) != NULL)
176  {
177  if (IsXLogFileName(xlde->d_name))
178  {
180  fname = xlde->d_name;
181  break;
182  }
183  }
184 
185  closedir(xldir);
186  }
187 
188  /* set WalSegSz if file is successfully opened */
189  if (fd >= 0)
190  {
192  int r;
193 
194  r = read(fd, buf.data, XLOG_BLCKSZ);
195  if (r == XLOG_BLCKSZ)
196  {
198 
199  WalSegSz = longhdr->xlp_seg_size;
200 
202  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",
203  "WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" header specifies %d bytes",
204  WalSegSz),
205  fname, WalSegSz);
206  }
207  else
208  {
209  if (errno != 0)
210  fatal_error("could not read file \"%s\": %m",
211  fname);
212  else
213  fatal_error("could not read file \"%s\": read %d of %zu",
214  fname, r, (Size) XLOG_BLCKSZ);
215  }
216  close(fd);
217  return true;
218  }
219 
220  return false;
221 }
#define fatal_error(...)
Definition: pg_waldump.c:73
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:97
int closedir(DIR *)
Definition: dirent.c:113
Definition: dirent.h:9
static int fd(const char *x, int i)
Definition: preproc-init.c:105
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:74
Definition: dirent.c:25
#define IsXLogFileName(fname)
DIR * opendir(const char *)
Definition: dirent.c:33
static char * buf
Definition: pg_test_fsync.c:67
#define ngettext(s, p, n)
Definition: c.h:1146
static int WalSegSz
Definition: pg_waldump.c:30
size_t Size
Definition: c.h:466
static const char * directory
Definition: zic.c:622
static int open_file_in_directory(const char *directory, const char *fname)
Definition: pg_waldump.c:135
struct dirent * readdir(DIR *)
Definition: dirent.c:77
char d_name[MAX_PATH]
Definition: dirent.h:14
#define close(a)
Definition: win32.h:12
char data[XLOG_BLCKSZ]
Definition: c.h:1112
#define read(a, b, c)
Definition: win32.h:13

◆ split_path()

static void split_path ( const char *  path,
char **  dir,
char **  fname 
)
static

Definition at line 108 of file pg_waldump.c.

References pg_strdup(), and pnstrdup().

Referenced by main().

109 {
110  char *sep;
111 
112  /* split filepath into directory & filename */
113  sep = strrchr(path, '/');
114 
115  /* directory path */
116  if (sep != NULL)
117  {
118  *dir = pnstrdup(path, sep - path);
119  *fname = pg_strdup(sep + 1);
120  }
121  /* local directory */
122  else
123  {
124  *dir = NULL;
125  *fname = pg_strdup(path);
126  }
127 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85

◆ usage()

static void usage ( void  )
static

Definition at line 718 of file pg_waldump.c.

References _, printf, and progname.

Referenced by main().

719 {
720  printf(_("%s decodes and displays PostgreSQL write-ahead logs for debugging.\n\n"),
721  progname);
722  printf(_("Usage:\n"));
723  printf(_(" %s [OPTION]... [STARTSEG [ENDSEG]]\n"), progname);
724  printf(_("\nOptions:\n"));
725  printf(_(" -b, --bkp-details output detailed information about backup blocks\n"));
726  printf(_(" -e, --end=RECPTR stop reading at WAL location RECPTR\n"));
727  printf(_(" -f, --follow keep retrying after reaching end of WAL\n"));
728  printf(_(" -n, --limit=N number of records to display\n"));
729  printf(_(" -p, --path=PATH directory in which to find log segment files or a\n"
730  " directory with a ./pg_wal that contains such files\n"
731  " (default: current directory, ./pg_wal, $PGDATA/pg_wal)\n"));
732  printf(_(" -q, --quiet do not print any output, except for errors\n"));
733  printf(_(" -r, --rmgr=RMGR only show records generated by resource manager RMGR;\n"
734  " use --rmgr=list to list valid resource manager names\n"));
735  printf(_(" -s, --start=RECPTR start reading at WAL location RECPTR\n"));
736  printf(_(" -t, --timeline=TLI timeline from which to read log records\n"
737  " (default: 1 or the value used in STARTSEG)\n"));
738  printf(_(" -V, --version output version information, then exit\n"));
739  printf(_(" -x, --xid=XID only show records with transaction ID XID\n"));
740  printf(_(" -z, --stats[=record] show statistics instead of records\n"
741  " (optionally, show per-record statistics)\n"));
742  printf(_(" -?, --help show this help, then exit\n"));
743  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
744  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
745 }
#define printf(...)
Definition: port.h:199
static const char * progname
Definition: pg_waldump.c:28
#define _(x)
Definition: elog.c:88

◆ verify_directory()

static bool verify_directory ( const char *  directory)
static

Definition at line 91 of file pg_waldump.c.

References closedir(), and opendir().

Referenced by main().

92 {
93  DIR *dir = opendir(directory);
94 
95  if (dir == NULL)
96  return false;
97  closedir(dir);
98  return true;
99 }
int closedir(DIR *)
Definition: dirent.c:113
Definition: dirent.c:25
DIR * opendir(const char *)
Definition: dirent.c:33
static const char * directory
Definition: zic.c:622

◆ WALDumpCloseSegment()

static void WALDumpCloseSegment ( XLogReaderState state)
static

Definition at line 326 of file pg_waldump.c.

References close, XLogReaderState::seg, and WALOpenSegment::ws_file.

Referenced by main().

327 {
328  close(state->seg.ws_file);
329  /* need to check errno? */
330  state->seg.ws_file = -1;
331 }
WALOpenSegment seg
Definition: xlogreader.h:213
#define close(a)
Definition: win32.h:12

◆ WALDumpOpenSegment()

static void WALDumpOpenSegment ( XLogReaderState state,
XLogSegNo  nextSegNo,
TimeLineID tli_p 
)
static

Definition at line 284 of file pg_waldump.c.

References fatal_error, MAXPGPATH, open_file_in_directory(), pg_usleep(), XLogReaderState::seg, XLogReaderState::segcxt, WALSegmentContext::ws_dir, WALOpenSegment::ws_file, WALSegmentContext::ws_segsize, and XLogFileName.

Referenced by main().

286 {
287  TimeLineID tli = *tli_p;
288  char fname[MAXPGPATH];
289  int tries;
290 
291  XLogFileName(fname, tli, nextSegNo, state->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  state->seg.ws_file = open_file_in_directory(state->segcxt.ws_dir, fname);
302  if (state->seg.ws_file >= 0)
303  return;
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 }
#define fatal_error(...)
Definition: pg_waldump.c:73
char ws_dir[MAXPGPATH]
Definition: xlogreader.h:54
uint32 TimeLineID
Definition: xlogdefs.h:52
WALOpenSegment seg
Definition: xlogreader.h:213
void pg_usleep(long microsec)
Definition: signal.c:53
#define MAXPGPATH
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
static int open_file_in_directory(const char *directory, const char *fname)
Definition: pg_waldump.c:135
WALSegmentContext segcxt
Definition: xlogreader.h:212

◆ WALDumpReadPage()

static int WALDumpReadPage ( XLogReaderState state,
XLogRecPtr  targetPagePtr,
int  reqLen,
XLogRecPtr  targetPtr,
char *  readBuff 
)
static

Definition at line 335 of file pg_waldump.c.

References XLogDumpPrivate::endptr, fatal_error, InvalidXLogRecPtr, MAXPGPATH, XLogReaderState::private_data, XLogReaderState::segcxt, WALRead(), WALReadError::wre_errno, WALReadError::wre_off, WALReadError::wre_read, WALReadError::wre_req, WALReadError::wre_seg, WALOpenSegment::ws_segno, WALSegmentContext::ws_segsize, WALOpenSegment::ws_tli, and XLogFileName.

Referenced by main().

337 {
338  XLogDumpPrivate *private = state->private_data;
339  int count = XLOG_BLCKSZ;
340  WALReadError errinfo;
341 
342  if (private->endptr != InvalidXLogRecPtr)
343  {
344  if (targetPagePtr + XLOG_BLCKSZ <= private->endptr)
345  count = XLOG_BLCKSZ;
346  else if (targetPagePtr + reqLen <= private->endptr)
347  count = private->endptr - targetPagePtr;
348  else
349  {
350  private->endptr_reached = true;
351  return -1;
352  }
353  }
354 
355  if (!WALRead(state, readBuff, targetPagePtr, count, private->timeline,
356  &errinfo))
357  {
358  WALOpenSegment *seg = &errinfo.wre_seg;
359  char fname[MAXPGPATH];
360 
361  XLogFileName(fname, seg->ws_tli, seg->ws_segno,
362  state->segcxt.ws_segsize);
363 
364  if (errinfo.wre_errno != 0)
365  {
366  errno = errinfo.wre_errno;
367  fatal_error("could not read from file %s, offset %u: %m",
368  fname, errinfo.wre_off);
369  }
370  else
371  fatal_error("could not read from file %s, offset %u: read %d of %zu",
372  fname, errinfo.wre_off, errinfo.wre_read,
373  (Size) errinfo.wre_req);
374  }
375 
376  return count;
377 }
#define fatal_error(...)
Definition: pg_waldump.c:73
WALOpenSegment wre_seg
Definition: xlogreader.h:293
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void * private_data
Definition: xlogreader.h:168
#define MAXPGPATH
XLogSegNo ws_segno
Definition: xlogreader.h:47
size_t Size
Definition: c.h:466
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
TimeLineID ws_tli
Definition: xlogreader.h:48
bool WALRead(XLogReaderState *state, char *buf, XLogRecPtr startptr, Size count, TimeLineID tli, WALReadError *errinfo)
Definition: xlogreader.c:1060
WALSegmentContext segcxt
Definition: xlogreader.h:212

◆ XLogDumpCountRecord()

static void XLogDumpCountRecord ( XLogDumpConfig config,
XLogDumpStats stats,
XLogReaderState record 
)
static

Definition at line 412 of file pg_waldump.c.

References Stats::count, XLogDumpStats::count, Stats::fpi_len, Stats::rec_len, XLogDumpStats::record_stats, XLogDumpStats::rmgr_stats, XLogDumpRecordLen(), XLogRecGetInfo, and XLogRecGetRmid.

Referenced by main().

414 {
415  RmgrId rmid;
416  uint8 recid;
417  uint32 rec_len;
418  uint32 fpi_len;
419 
420  stats->count++;
421 
422  rmid = XLogRecGetRmid(record);
423 
424  XLogDumpRecordLen(record, &rec_len, &fpi_len);
425 
426  /* Update per-rmgr statistics */
427 
428  stats->rmgr_stats[rmid].count++;
429  stats->rmgr_stats[rmid].rec_len += rec_len;
430  stats->rmgr_stats[rmid].fpi_len += fpi_len;
431 
432  /*
433  * Update per-record statistics, where the record is identified by a
434  * combination of the RmgrId and the four bits of the xl_info field that
435  * are the rmgr's domain (resulting in sixteen possible entries per
436  * RmgrId).
437  */
438 
439  recid = XLogRecGetInfo(record) >> 4;
440 
441  stats->record_stats[rmid][recid].count++;
442  stats->record_stats[rmid][recid].rec_len += rec_len;
443  stats->record_stats[rmid][recid].fpi_len += fpi_len;
444 }
uint64 count
Definition: pg_waldump.c:59
uint64 count
Definition: pg_waldump.c:68
unsigned char uint8
Definition: c.h:365
Stats record_stats[RM_NEXT_ID][MAX_XLINFO_TYPES]
Definition: pg_waldump.c:70
Stats rmgr_stats[RM_NEXT_ID]
Definition: pg_waldump.c:69
unsigned int uint32
Definition: c.h:367
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:307
uint8 RmgrId
Definition: rmgr.h:11
uint64 fpi_len
Definition: pg_waldump.c:61
uint64 rec_len
Definition: pg_waldump.c:60
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:308
static void XLogDumpRecordLen(XLogReaderState *record, uint32 *rec_len, uint32 *fpi_len)
Definition: pg_waldump.c:383

◆ XLogDumpDisplayRecord()

static void XLogDumpDisplayRecord ( XLogDumpConfig config,
XLogReaderState record 
)
static

Definition at line 450 of file pg_waldump.c.

References DecodedBkpBlock::bimg_info, DecodedBkpBlock::bimg_len, XLogDumpConfig::bkp_details, BKPIMAGE_IS_COMPRESSED, XLogReaderState::blocks, StringInfoData::data, RelFileNode::dbNode, forkNames, DecodedBkpBlock::hole_length, DecodedBkpBlock::hole_offset, initStringInfo(), MAIN_FORKNUM, XLogReaderState::max_block_id, pfree(), printf, XLogReaderState::ReadRecPtr, RelFileNode::relNode, RmgrDescData::rm_desc, RmgrDescData::rm_identify, RmgrDescData::rm_name, RmgrDescTable, RelFileNode::spcNode, XLogDumpRecordLen(), XLogRecBlockImageApply, XLogRecGetBlockTag(), XLogRecGetInfo, XLogRecGetPrev, XLogRecGetRmid, XLogRecGetTotalLen, XLogRecGetXid, XLogRecHasBlockImage, XLogRecHasBlockRef, and XLR_INFO_MASK.

Referenced by main().

451 {
452  const char *id;
453  const RmgrDescData *desc = &RmgrDescTable[XLogRecGetRmid(record)];
454  uint32 rec_len;
455  uint32 fpi_len;
456  RelFileNode rnode;
457  ForkNumber forknum;
458  BlockNumber blk;
459  int block_id;
460  uint8 info = XLogRecGetInfo(record);
461  XLogRecPtr xl_prev = XLogRecGetPrev(record);
462  StringInfoData s;
463 
464  XLogDumpRecordLen(record, &rec_len, &fpi_len);
465 
466  printf("rmgr: %-11s len (rec/tot): %6u/%6u, tx: %10u, lsn: %X/%08X, prev %X/%08X, ",
467  desc->rm_name,
468  rec_len, XLogRecGetTotalLen(record),
469  XLogRecGetXid(record),
470  (uint32) (record->ReadRecPtr >> 32), (uint32) record->ReadRecPtr,
471  (uint32) (xl_prev >> 32), (uint32) xl_prev);
472 
473  id = desc->rm_identify(info);
474  if (id == NULL)
475  printf("desc: UNKNOWN (%x) ", info & ~XLR_INFO_MASK);
476  else
477  printf("desc: %s ", id);
478 
479  initStringInfo(&s);
480  desc->rm_desc(&s, record);
481  printf("%s", s.data);
482  pfree(s.data);
483 
484  if (!config->bkp_details)
485  {
486  /* print block references (short format) */
487  for (block_id = 0; block_id <= record->max_block_id; block_id++)
488  {
489  if (!XLogRecHasBlockRef(record, block_id))
490  continue;
491 
492  XLogRecGetBlockTag(record, block_id, &rnode, &forknum, &blk);
493  if (forknum != MAIN_FORKNUM)
494  printf(", blkref #%u: rel %u/%u/%u fork %s blk %u",
495  block_id,
496  rnode.spcNode, rnode.dbNode, rnode.relNode,
497  forkNames[forknum],
498  blk);
499  else
500  printf(", blkref #%u: rel %u/%u/%u blk %u",
501  block_id,
502  rnode.spcNode, rnode.dbNode, rnode.relNode,
503  blk);
504  if (XLogRecHasBlockImage(record, block_id))
505  {
506  if (XLogRecBlockImageApply(record, block_id))
507  printf(" FPW");
508  else
509  printf(" FPW for WAL verification");
510  }
511  }
512  putchar('\n');
513  }
514  else
515  {
516  /* print block references (detailed format) */
517  putchar('\n');
518  for (block_id = 0; block_id <= record->max_block_id; block_id++)
519  {
520  if (!XLogRecHasBlockRef(record, block_id))
521  continue;
522 
523  XLogRecGetBlockTag(record, block_id, &rnode, &forknum, &blk);
524  printf("\tblkref #%u: rel %u/%u/%u fork %s blk %u",
525  block_id,
526  rnode.spcNode, rnode.dbNode, rnode.relNode,
527  forkNames[forknum],
528  blk);
529  if (XLogRecHasBlockImage(record, block_id))
530  {
531  if (record->blocks[block_id].bimg_info &
533  {
534  printf(" (FPW%s); hole: offset: %u, length: %u, "
535  "compression saved: %u",
536  XLogRecBlockImageApply(record, block_id) ?
537  "" : " for WAL verification",
538  record->blocks[block_id].hole_offset,
539  record->blocks[block_id].hole_length,
540  BLCKSZ -
541  record->blocks[block_id].hole_length -
542  record->blocks[block_id].bimg_len);
543  }
544  else
545  {
546  printf(" (FPW%s); hole: offset: %u, length: %u",
547  XLogRecBlockImageApply(record, block_id) ?
548  "" : " for WAL verification",
549  record->blocks[block_id].hole_offset,
550  record->blocks[block_id].hole_length);
551  }
552  }
553  putchar('\n');
554  }
555  }
556 }
#define XLogRecHasBlockImage(decoder, block_id)
Definition: xlogreader.h:316
uint16 hole_offset
Definition: xlogreader.h:135
unsigned char uint8
Definition: c.h:365
#define XLogRecHasBlockRef(decoder, block_id)
Definition: xlogreader.h:314
#define printf(...)
Definition: port.h:199
uint32 BlockNumber
Definition: block.h:31
bool bkp_details
Definition: pg_waldump.c:44
#define XLogRecGetTotalLen(decoder)
Definition: xlogreader.h:305
void pfree(void *pointer)
Definition: mcxt.c:1056
uint16 hole_length
Definition: xlogreader.h:136
const RmgrDescData RmgrDescTable[RM_MAX_ID+1]
Definition: rmgrdesc.c:38
#define XLogRecGetPrev(decoder)
Definition: xlogreader.h:306
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:175
unsigned int uint32
Definition: c.h:367
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:307
ForkNumber
Definition: relpath.h:40
const char * rm_name
Definition: rmgrdesc.h:16
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1490
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:309
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:147
const char *(* rm_identify)(uint8 info)
Definition: rmgrdesc.h:18
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
void(* rm_desc)(StringInfo buf, XLogReaderState *record)
Definition: rmgrdesc.h:17
#define XLogRecBlockImageApply(decoder, block_id)
Definition: xlogreader.h:318
const char *const forkNames[]
Definition: relpath.c:33
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:195
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:308
static void XLogDumpRecordLen(XLogReaderState *record, uint32 *rec_len, uint32 *fpi_len)
Definition: pg_waldump.c:383

◆ XLogDumpDisplayStats()

static void XLogDumpDisplayStats ( XLogDumpConfig config,
XLogDumpStats stats 
)
static

Definition at line 603 of file pg_waldump.c.

References Stats::count, XLogDumpStats::count, Stats::fpi_len, MAX_XLINFO_TYPES, printf, psprintf(), Stats::rec_len, XLogDumpStats::record_stats, RmgrDescData::rm_identify, RmgrDescData::rm_name, RM_NEXT_ID, XLogDumpStats::rmgr_stats, RmgrDescTable, XLogDumpConfig::stats_per_record, and XLogDumpStatsRow().

Referenced by main().

604 {
605  int ri,
606  rj;
607  uint64 total_count = 0;
608  uint64 total_rec_len = 0;
609  uint64 total_fpi_len = 0;
610  uint64 total_len = 0;
611  double rec_len_pct,
612  fpi_len_pct;
613 
614  /* ---
615  * Make a first pass to calculate column totals:
616  * count(*),
617  * sum(xl_len+SizeOfXLogRecord),
618  * sum(xl_tot_len-xl_len-SizeOfXLogRecord), and
619  * sum(xl_tot_len).
620  * These are used to calculate percentages for each record type.
621  * ---
622  */
623 
624  for (ri = 0; ri < RM_NEXT_ID; ri++)
625  {
626  total_count += stats->rmgr_stats[ri].count;
627  total_rec_len += stats->rmgr_stats[ri].rec_len;
628  total_fpi_len += stats->rmgr_stats[ri].fpi_len;
629  }
630  total_len = total_rec_len + total_fpi_len;
631 
632  /*
633  * 27 is strlen("Transaction/COMMIT_PREPARED"), 20 is strlen(2^64), 8 is
634  * strlen("(100.00%)")
635  */
636 
637  printf("%-27s %20s %8s %20s %8s %20s %8s %20s %8s\n"
638  "%-27s %20s %8s %20s %8s %20s %8s %20s %8s\n",
639  "Type", "N", "(%)", "Record size", "(%)", "FPI size", "(%)", "Combined size", "(%)",
640  "----", "-", "---", "-----------", "---", "--------", "---", "-------------", "---");
641 
642  for (ri = 0; ri < RM_NEXT_ID; ri++)
643  {
644  uint64 count,
645  rec_len,
646  fpi_len,
647  tot_len;
648  const RmgrDescData *desc = &RmgrDescTable[ri];
649 
650  if (!config->stats_per_record)
651  {
652  count = stats->rmgr_stats[ri].count;
653  rec_len = stats->rmgr_stats[ri].rec_len;
654  fpi_len = stats->rmgr_stats[ri].fpi_len;
655  tot_len = rec_len + fpi_len;
656 
658  count, total_count, rec_len, total_rec_len,
659  fpi_len, total_fpi_len, tot_len, total_len);
660  }
661  else
662  {
663  for (rj = 0; rj < MAX_XLINFO_TYPES; rj++)
664  {
665  const char *id;
666 
667  count = stats->record_stats[ri][rj].count;
668  rec_len = stats->record_stats[ri][rj].rec_len;
669  fpi_len = stats->record_stats[ri][rj].fpi_len;
670  tot_len = rec_len + fpi_len;
671 
672  /* Skip undefined combinations and ones that didn't occur */
673  if (count == 0)
674  continue;
675 
676  /* the upper four bits in xl_info are the rmgr's */
677  id = desc->rm_identify(rj << 4);
678  if (id == NULL)
679  id = psprintf("UNKNOWN (%x)", rj << 4);
680 
681  XLogDumpStatsRow(psprintf("%s/%s", desc->rm_name, id),
682  count, total_count, rec_len, total_rec_len,
683  fpi_len, total_fpi_len, tot_len, total_len);
684  }
685  }
686  }
687 
688  printf("%-27s %20s %8s %20s %8s %20s %8s %20s\n",
689  "", "--------", "", "--------", "", "--------", "", "--------");
690 
691  /*
692  * The percentages in earlier rows were calculated against the column
693  * total, but the ones that follow are against the row total. Note that
694  * these are displayed with a % symbol to differentiate them from the
695  * earlier ones, and are thus up to 9 characters long.
696  */
697 
698  rec_len_pct = 0;
699  if (total_len != 0)
700  rec_len_pct = 100 * (double) total_rec_len / total_len;
701 
702  fpi_len_pct = 0;
703  if (total_len != 0)
704  fpi_len_pct = 100 * (double) total_fpi_len / total_len;
705 
706  printf("%-27s "
707  "%20" INT64_MODIFIER "u %-9s"
708  "%20" INT64_MODIFIER "u %-9s"
709  "%20" INT64_MODIFIER "u %-9s"
710  "%20" INT64_MODIFIER "u %-6s\n",
711  "Total", stats->count, "",
712  total_rec_len, psprintf("[%.02f%%]", rec_len_pct),
713  total_fpi_len, psprintf("[%.02f%%]", fpi_len_pct),
714  total_len, "[100%]");
715 }
uint64 count
Definition: pg_waldump.c:59
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
uint64 count
Definition: pg_waldump.c:68
Stats record_stats[RM_NEXT_ID][MAX_XLINFO_TYPES]
Definition: pg_waldump.c:70
#define printf(...)
Definition: port.h:199
Stats rmgr_stats[RM_NEXT_ID]
Definition: pg_waldump.c:69
const RmgrDescData RmgrDescTable[RM_MAX_ID+1]
Definition: rmgrdesc.c:38
const char * rm_name
Definition: rmgrdesc.h:16
const char *(* rm_identify)(uint8 info)
Definition: rmgrdesc.h:18
#define MAX_XLINFO_TYPES
Definition: pg_waldump.c:64
uint64 fpi_len
Definition: pg_waldump.c:61
uint64 rec_len
Definition: pg_waldump.c:60
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:562
bool stats_per_record
Definition: pg_waldump.c:49

◆ XLogDumpRecordLen()

static void XLogDumpRecordLen ( XLogReaderState record,
uint32 rec_len,
uint32 fpi_len 
)
static

Definition at line 383 of file pg_waldump.c.

References DecodedBkpBlock::bimg_len, XLogReaderState::blocks, XLogReaderState::max_block_id, XLogRecGetTotalLen, and XLogRecHasBlockImage.

Referenced by XLogDumpCountRecord(), and XLogDumpDisplayRecord().

384 {
385  int block_id;
386 
387  /*
388  * Calculate the amount of FPI data in the record.
389  *
390  * XXX: We peek into xlogreader's private decoded backup blocks for the
391  * bimg_len indicating the length of FPI data. It doesn't seem worth it to
392  * add an accessor macro for this.
393  */
394  *fpi_len = 0;
395  for (block_id = 0; block_id <= record->max_block_id; block_id++)
396  {
397  if (XLogRecHasBlockImage(record, block_id))
398  *fpi_len += record->blocks[block_id].bimg_len;
399  }
400 
401  /*
402  * Calculate the length of the record as the total length - the length of
403  * all the block images.
404  */
405  *rec_len = XLogRecGetTotalLen(record) - *fpi_len;
406 }
#define XLogRecHasBlockImage(decoder, block_id)
Definition: xlogreader.h:316
#define XLogRecGetTotalLen(decoder)
Definition: xlogreader.h:305
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:195

◆ XLogDumpStatsRow()

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 
)
static

Definition at line 562 of file pg_waldump.c.

References printf.

Referenced by XLogDumpDisplayStats().

567 {
568  double n_pct,
569  rec_len_pct,
570  fpi_len_pct,
571  tot_len_pct;
572 
573  n_pct = 0;
574  if (total_count != 0)
575  n_pct = 100 * (double) n / total_count;
576 
577  rec_len_pct = 0;
578  if (total_rec_len != 0)
579  rec_len_pct = 100 * (double) rec_len / total_rec_len;
580 
581  fpi_len_pct = 0;
582  if (total_fpi_len != 0)
583  fpi_len_pct = 100 * (double) fpi_len / total_fpi_len;
584 
585  tot_len_pct = 0;
586  if (total_len != 0)
587  tot_len_pct = 100 * (double) tot_len / total_len;
588 
589  printf("%-27s "
590  "%20" INT64_MODIFIER "u (%6.02f) "
591  "%20" INT64_MODIFIER "u (%6.02f) "
592  "%20" INT64_MODIFIER "u (%6.02f) "
593  "%20" INT64_MODIFIER "u (%6.02f)\n",
594  name, n, n_pct, rec_len, rec_len_pct, fpi_len, fpi_len_pct,
595  tot_len, tot_len_pct);
596 }
#define printf(...)
Definition: port.h:199
const char * name
Definition: encode.c:555

Variable Documentation

◆ progname

const char* progname
static

Definition at line 28 of file pg_waldump.c.

Referenced by main(), and usage().

◆ WalSegSz

int WalSegSz
static

Definition at line 30 of file pg_waldump.c.

Referenced by main(), and search_directory().