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 65 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 239 of file pg_waldump.c.

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

Referenced by main().

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

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 764 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_rmgr_enabled, XLogDumpConfig::filter_by_xid, XLogDumpConfig::filter_by_xid_enabled, XLogDumpConfig::follow, fprintf, get_progname(), getopt_long(), i, identify_target_directory(), InvalidTransactionId, InvalidXLogRecPtr, LSN_FORMAT_ARGS, 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.

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

◆ open_file_in_directory()

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

Definition at line 136 of file pg_waldump.c.

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

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

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

◆ print_rmgr_list()

static void print_rmgr_list ( void  )
static

Definition at line 77 of file pg_waldump.c.

References i, printf, RM_MAX_ID, and RmgrDescTable.

Referenced by main().

78 {
79  int i;
80 
81  for (i = 0; i <= RM_MAX_ID; i++)
82  {
83  printf("%s\n", RmgrDescTable[i].rm_name);
84  }
85 }
#define printf(...)
Definition: port.h:223
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 158 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().

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

References pg_strdup(), and pnstrdup().

Referenced by main().

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

◆ usage()

static void usage ( void  )
static

Definition at line 734 of file pg_waldump.c.

References _, printf, and progname.

Referenced by main().

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

◆ verify_directory()

static bool verify_directory ( const char *  directory)
static

Definition at line 92 of file pg_waldump.c.

References closedir(), and opendir().

Referenced by main().

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

◆ WALDumpCloseSegment()

static void WALDumpCloseSegment ( XLogReaderState state)
static

Definition at line 327 of file pg_waldump.c.

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

Referenced by main().

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

◆ WALDumpOpenSegment()

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

Definition at line 285 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().

287 {
288  TimeLineID tli = *tli_p;
289  char fname[MAXPGPATH];
290  int tries;
291 
292  XLogFileName(fname, tli, nextSegNo, state->segcxt.ws_segsize);
293 
294  /*
295  * In follow mode there is a short period of time after the server has
296  * written the end of the previous file before the new file is available.
297  * So we loop for 5 seconds looking for the file to appear before giving
298  * up.
299  */
300  for (tries = 0; tries < 10; tries++)
301  {
302  state->seg.ws_file = open_file_in_directory(state->segcxt.ws_dir, fname);
303  if (state->seg.ws_file >= 0)
304  return;
305  if (errno == ENOENT)
306  {
307  int save_errno = errno;
308 
309  /* File not there yet, try again */
310  pg_usleep(500 * 1000);
311 
312  errno = save_errno;
313  continue;
314  }
315  /* Any other error, fall through and fail */
316  break;
317  }
318 
319  fatal_error("could not find file \"%s\": %m", fname);
320 }
#define fatal_error(...)
Definition: pg_waldump.c:74
char ws_dir[MAXPGPATH]
Definition: xlogreader.h:54
uint32 TimeLineID
Definition: xlogdefs.h:59
WALOpenSegment seg
Definition: xlogreader.h:225
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:136
WALSegmentContext segcxt
Definition: xlogreader.h:224

◆ WALDumpReadPage()

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

Definition at line 336 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().

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

◆ XLogDumpCountRecord()

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

Definition at line 413 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().

415 {
416  RmgrId rmid;
417  uint8 recid;
418  uint32 rec_len;
419  uint32 fpi_len;
420 
421  stats->count++;
422 
423  rmid = XLogRecGetRmid(record);
424 
425  XLogDumpRecordLen(record, &rec_len, &fpi_len);
426 
427  /* Update per-rmgr statistics */
428 
429  stats->rmgr_stats[rmid].count++;
430  stats->rmgr_stats[rmid].rec_len += rec_len;
431  stats->rmgr_stats[rmid].fpi_len += fpi_len;
432 
433  /*
434  * Update per-record statistics, where the record is identified by a
435  * combination of the RmgrId and the four bits of the xl_info field that
436  * are the rmgr's domain (resulting in sixteen possible entries per
437  * RmgrId).
438  */
439 
440  recid = XLogRecGetInfo(record) >> 4;
441 
442  /*
443  * XACT records need to be handled differently. Those records use the
444  * first bit of those four bits for an optional flag variable and the
445  * following three bits for the opcode. We filter opcode out of xl_info
446  * and use it as the identifier of the record.
447  */
448  if (rmid == RM_XACT_ID)
449  recid &= 0x07;
450 
451  stats->record_stats[rmid][recid].count++;
452  stats->record_stats[rmid][recid].rec_len += rec_len;
453  stats->record_stats[rmid][recid].fpi_len += fpi_len;
454 }
uint64 count
Definition: pg_waldump.c:60
uint64 count
Definition: pg_waldump.c:69
unsigned char uint8
Definition: c.h:439
Stats record_stats[RM_NEXT_ID][MAX_XLINFO_TYPES]
Definition: pg_waldump.c:71
Stats rmgr_stats[RM_NEXT_ID]
Definition: pg_waldump.c:70
unsigned int uint32
Definition: c.h:441
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:315
uint8 RmgrId
Definition: rmgr.h:11
uint64 fpi_len
Definition: pg_waldump.c:62
uint64 rec_len
Definition: pg_waldump.c:61
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:316
static void XLogDumpRecordLen(XLogReaderState *record, uint32 *rec_len, uint32 *fpi_len)
Definition: pg_waldump.c:384

◆ XLogDumpDisplayRecord()

static void XLogDumpDisplayRecord ( XLogDumpConfig config,
XLogReaderState record 
)
static

Definition at line 460 of file pg_waldump.c.

References DecodedBkpBlock::bimg_info, DecodedBkpBlock::bimg_len, XLogDumpConfig::bkp_details, BKPIMAGE_COMPRESS_LZ4, BKPIMAGE_COMPRESS_PGLZ, BKPIMAGE_COMPRESSED, XLogReaderState::blocks, StringInfoData::data, RelFileNode::dbNode, forkNames, DecodedBkpBlock::hole_length, DecodedBkpBlock::hole_offset, initStringInfo(), LSN_FORMAT_ARGS, 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().

461 {
462  const char *id;
463  const RmgrDescData *desc = &RmgrDescTable[XLogRecGetRmid(record)];
464  uint32 rec_len;
465  uint32 fpi_len;
466  RelFileNode rnode;
467  ForkNumber forknum;
468  BlockNumber blk;
469  int block_id;
470  uint8 info = XLogRecGetInfo(record);
471  XLogRecPtr xl_prev = XLogRecGetPrev(record);
472  StringInfoData s;
473 
474  XLogDumpRecordLen(record, &rec_len, &fpi_len);
475 
476  printf("rmgr: %-11s len (rec/tot): %6u/%6u, tx: %10u, lsn: %X/%08X, prev %X/%08X, ",
477  desc->rm_name,
478  rec_len, XLogRecGetTotalLen(record),
479  XLogRecGetXid(record),
480  LSN_FORMAT_ARGS(record->ReadRecPtr),
481  LSN_FORMAT_ARGS(xl_prev));
482 
483  id = desc->rm_identify(info);
484  if (id == NULL)
485  printf("desc: UNKNOWN (%x) ", info & ~XLR_INFO_MASK);
486  else
487  printf("desc: %s ", id);
488 
489  initStringInfo(&s);
490  desc->rm_desc(&s, record);
491  printf("%s", s.data);
492  pfree(s.data);
493 
494  if (!config->bkp_details)
495  {
496  /* print block references (short format) */
497  for (block_id = 0; block_id <= record->max_block_id; block_id++)
498  {
499  if (!XLogRecHasBlockRef(record, block_id))
500  continue;
501 
502  XLogRecGetBlockTag(record, block_id, &rnode, &forknum, &blk);
503  if (forknum != MAIN_FORKNUM)
504  printf(", blkref #%u: rel %u/%u/%u fork %s blk %u",
505  block_id,
506  rnode.spcNode, rnode.dbNode, rnode.relNode,
507  forkNames[forknum],
508  blk);
509  else
510  printf(", blkref #%u: rel %u/%u/%u blk %u",
511  block_id,
512  rnode.spcNode, rnode.dbNode, rnode.relNode,
513  blk);
514  if (XLogRecHasBlockImage(record, block_id))
515  {
516  if (XLogRecBlockImageApply(record, block_id))
517  printf(" FPW");
518  else
519  printf(" FPW for WAL verification");
520  }
521  }
522  putchar('\n');
523  }
524  else
525  {
526  /* print block references (detailed format) */
527  putchar('\n');
528  for (block_id = 0; block_id <= record->max_block_id; block_id++)
529  {
530  if (!XLogRecHasBlockRef(record, block_id))
531  continue;
532 
533  XLogRecGetBlockTag(record, block_id, &rnode, &forknum, &blk);
534  printf("\tblkref #%u: rel %u/%u/%u fork %s blk %u",
535  block_id,
536  rnode.spcNode, rnode.dbNode, rnode.relNode,
537  forkNames[forknum],
538  blk);
539  if (XLogRecHasBlockImage(record, block_id))
540  {
541  uint8 bimg_info = record->blocks[block_id].bimg_info;
542 
543  if (BKPIMAGE_COMPRESSED(bimg_info))
544  {
545  const char *method;
546 
547  if ((bimg_info & BKPIMAGE_COMPRESS_PGLZ) != 0)
548  method = "pglz";
549  else if ((bimg_info & BKPIMAGE_COMPRESS_LZ4) != 0)
550  method = "lz4";
551  else
552  method = "unknown";
553 
554  printf(" (FPW%s); hole: offset: %u, length: %u, "
555  "compression saved: %u, method: %s",
556  XLogRecBlockImageApply(record, block_id) ?
557  "" : " for WAL verification",
558  record->blocks[block_id].hole_offset,
559  record->blocks[block_id].hole_length,
560  BLCKSZ -
561  record->blocks[block_id].hole_length -
562  record->blocks[block_id].bimg_len,
563  method);
564  }
565  else
566  {
567  printf(" (FPW%s); hole: offset: %u, length: %u",
568  XLogRecBlockImageApply(record, block_id) ?
569  "" : " for WAL verification",
570  record->blocks[block_id].hole_offset,
571  record->blocks[block_id].hole_length);
572  }
573  }
574  putchar('\n');
575  }
576  }
577 }
#define XLogRecHasBlockImage(decoder, block_id)
Definition: xlogreader.h:325
uint16 hole_offset
Definition: xlogreader.h:135
unsigned char uint8
Definition: c.h:439
#define XLogRecHasBlockRef(decoder, block_id)
Definition: xlogreader.h:323
#define printf(...)
Definition: port.h:223
uint32 BlockNumber
Definition: block.h:31
bool bkp_details
Definition: pg_waldump.c:44
#define XLogRecGetTotalLen(decoder)
Definition: xlogreader.h:313
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
void pfree(void *pointer)
Definition: mcxt.c:1169
uint16 hole_length
Definition: xlogreader.h:136
const RmgrDescData RmgrDescTable[RM_MAX_ID+1]
Definition: rmgrdesc.c:38
#define XLogRecGetPrev(decoder)
Definition: xlogreader.h:314
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:175
unsigned int uint32
Definition: c.h:441
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:315
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:1531
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:317
const char *(* rm_identify)(uint8 info)
Definition: rmgrdesc.h:18
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define BKPIMAGE_COMPRESS_LZ4
Definition: xlogrecord.h:151
#define BKPIMAGE_COMPRESS_PGLZ
Definition: xlogrecord.h:150
#define BKPIMAGE_COMPRESSED(info)
Definition: xlogrecord.h:152
void(* rm_desc)(StringInfo buf, XLogReaderState *record)
Definition: rmgrdesc.h:17
#define XLogRecBlockImageApply(decoder, block_id)
Definition: xlogreader.h:327
const char *const forkNames[]
Definition: relpath.c:33
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:207
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:316
static void XLogDumpRecordLen(XLogReaderState *record, uint32 *rec_len, uint32 *fpi_len)
Definition: pg_waldump.c:384

◆ XLogDumpDisplayStats()

static void XLogDumpDisplayStats ( XLogDumpConfig config,
XLogDumpStats stats 
)
static

Definition at line 624 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().

625 {
626  int ri,
627  rj;
628  uint64 total_count = 0;
629  uint64 total_rec_len = 0;
630  uint64 total_fpi_len = 0;
631  uint64 total_len = 0;
632  double rec_len_pct,
633  fpi_len_pct;
634 
635  /*
636  * Each row shows its percentages of the total, so make a first pass to
637  * calculate column totals.
638  */
639 
640  for (ri = 0; ri < RM_NEXT_ID; ri++)
641  {
642  total_count += stats->rmgr_stats[ri].count;
643  total_rec_len += stats->rmgr_stats[ri].rec_len;
644  total_fpi_len += stats->rmgr_stats[ri].fpi_len;
645  }
646  total_len = total_rec_len + total_fpi_len;
647 
648  /*
649  * 27 is strlen("Transaction/COMMIT_PREPARED"), 20 is strlen(2^64), 8 is
650  * strlen("(100.00%)")
651  */
652 
653  printf("%-27s %20s %8s %20s %8s %20s %8s %20s %8s\n"
654  "%-27s %20s %8s %20s %8s %20s %8s %20s %8s\n",
655  "Type", "N", "(%)", "Record size", "(%)", "FPI size", "(%)", "Combined size", "(%)",
656  "----", "-", "---", "-----------", "---", "--------", "---", "-------------", "---");
657 
658  for (ri = 0; ri < RM_NEXT_ID; ri++)
659  {
660  uint64 count,
661  rec_len,
662  fpi_len,
663  tot_len;
664  const RmgrDescData *desc = &RmgrDescTable[ri];
665 
666  if (!config->stats_per_record)
667  {
668  count = stats->rmgr_stats[ri].count;
669  rec_len = stats->rmgr_stats[ri].rec_len;
670  fpi_len = stats->rmgr_stats[ri].fpi_len;
671  tot_len = rec_len + fpi_len;
672 
674  count, total_count, rec_len, total_rec_len,
675  fpi_len, total_fpi_len, tot_len, total_len);
676  }
677  else
678  {
679  for (rj = 0; rj < MAX_XLINFO_TYPES; rj++)
680  {
681  const char *id;
682 
683  count = stats->record_stats[ri][rj].count;
684  rec_len = stats->record_stats[ri][rj].rec_len;
685  fpi_len = stats->record_stats[ri][rj].fpi_len;
686  tot_len = rec_len + fpi_len;
687 
688  /* Skip undefined combinations and ones that didn't occur */
689  if (count == 0)
690  continue;
691 
692  /* the upper four bits in xl_info are the rmgr's */
693  id = desc->rm_identify(rj << 4);
694  if (id == NULL)
695  id = psprintf("UNKNOWN (%x)", rj << 4);
696 
697  XLogDumpStatsRow(psprintf("%s/%s", desc->rm_name, id),
698  count, total_count, rec_len, total_rec_len,
699  fpi_len, total_fpi_len, tot_len, total_len);
700  }
701  }
702  }
703 
704  printf("%-27s %20s %8s %20s %8s %20s %8s %20s\n",
705  "", "--------", "", "--------", "", "--------", "", "--------");
706 
707  /*
708  * The percentages in earlier rows were calculated against the column
709  * total, but the ones that follow are against the row total. Note that
710  * these are displayed with a % symbol to differentiate them from the
711  * earlier ones, and are thus up to 9 characters long.
712  */
713 
714  rec_len_pct = 0;
715  if (total_len != 0)
716  rec_len_pct = 100 * (double) total_rec_len / total_len;
717 
718  fpi_len_pct = 0;
719  if (total_len != 0)
720  fpi_len_pct = 100 * (double) total_fpi_len / total_len;
721 
722  printf("%-27s "
723  "%20" INT64_MODIFIER "u %-9s"
724  "%20" INT64_MODIFIER "u %-9s"
725  "%20" INT64_MODIFIER "u %-9s"
726  "%20" INT64_MODIFIER "u %-6s\n",
727  "Total", stats->count, "",
728  total_rec_len, psprintf("[%.02f%%]", rec_len_pct),
729  total_fpi_len, psprintf("[%.02f%%]", fpi_len_pct),
730  total_len, "[100%]");
731 }
uint64 count
Definition: pg_waldump.c:60
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
uint64 count
Definition: pg_waldump.c:69
Stats record_stats[RM_NEXT_ID][MAX_XLINFO_TYPES]
Definition: pg_waldump.c:71
#define printf(...)
Definition: port.h:223
Stats rmgr_stats[RM_NEXT_ID]
Definition: pg_waldump.c:70
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:65
uint64 fpi_len
Definition: pg_waldump.c:62
uint64 rec_len
Definition: pg_waldump.c:61
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:583
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 384 of file pg_waldump.c.

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

Referenced by XLogDumpCountRecord(), and XLogDumpDisplayRecord().

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

◆ 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 583 of file pg_waldump.c.

References printf.

Referenced by XLogDumpDisplayStats().

588 {
589  double n_pct,
590  rec_len_pct,
591  fpi_len_pct,
592  tot_len_pct;
593 
594  n_pct = 0;
595  if (total_count != 0)
596  n_pct = 100 * (double) n / total_count;
597 
598  rec_len_pct = 0;
599  if (total_rec_len != 0)
600  rec_len_pct = 100 * (double) rec_len / total_rec_len;
601 
602  fpi_len_pct = 0;
603  if (total_fpi_len != 0)
604  fpi_len_pct = 100 * (double) fpi_len / total_fpi_len;
605 
606  tot_len_pct = 0;
607  if (total_len != 0)
608  tot_len_pct = 100 * (double) tot_len / total_len;
609 
610  printf("%-27s "
611  "%20" INT64_MODIFIER "u (%6.02f) "
612  "%20" INT64_MODIFIER "u (%6.02f) "
613  "%20" INT64_MODIFIER "u (%6.02f) "
614  "%20" INT64_MODIFIER "u (%6.02f)\n",
615  name, n, n_pct, rec_len, rec_len_pct, fpi_len, fpi_len_pct,
616  tot_len, tot_len_pct);
617 }
#define printf(...)
Definition: port.h:223
const char * name
Definition: encode.c:561

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().