PostgreSQL Source Code  git master
pg_waldump.c File Reference
#include "postgres.h"
#include <dirent.h>
#include <limits.h>
#include <signal.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 "access/xlogstats.h"
#include "common/fe_memutils.h"
#include "common/file_perm.h"
#include "common/file_utils.h"
#include "common/logging.h"
#include "common/relpath.h"
#include "getopt_long.h"
#include "rmgrdesc.h"
#include "storage/bufpage.h"
Include dependency graph for pg_waldump.c:

Go to the source code of this file.

Data Structures

struct  XLogDumpPrivate
 
struct  XLogDumpConfig
 

Macros

#define FRONTEND   1
 

Typedefs

typedef struct XLogDumpPrivate XLogDumpPrivate
 
typedef struct XLogDumpConfig XLogDumpConfig
 

Functions

static void sigint_handler (SIGNAL_ARGS)
 
static void print_rmgr_list (void)
 
static bool verify_directory (const char *directory)
 
static void create_fullpage_directory (char *path)
 
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 bool XLogRecordMatchesRelationBlock (XLogReaderState *record, RelFileLocator matchRlocator, BlockNumber matchBlock, ForkNumber matchFork)
 
static bool XLogRecordHasFPW (XLogReaderState *record)
 
static void XLogRecordSaveFPWs (XLogReaderState *record, const char *savepath)
 
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, XLogStats *stats)
 
static void usage (void)
 
int main (int argc, char **argv)
 

Variables

static const char * progname
 
static int WalSegSz
 
static volatile sig_atomic_t time_to_stop = false
 
static const RelFileLocator emptyRelFileLocator = {0, 0, 0}
 

Macro Definition Documentation

◆ FRONTEND

#define FRONTEND   1

Definition at line 12 of file pg_waldump.c.

Typedef Documentation

◆ XLogDumpConfig

◆ XLogDumpPrivate

Function Documentation

◆ create_fullpage_directory()

static void create_fullpage_directory ( char *  path)
static

Definition at line 128 of file pg_waldump.c.

129 {
130  int ret;
131 
132  switch ((ret = pg_check_dir(path)))
133  {
134  case 0:
135  /* Does not exist, so create it */
136  if (pg_mkdir_p(path, pg_dir_create_mode) < 0)
137  pg_fatal("could not create directory \"%s\": %m", path);
138  break;
139  case 1:
140  /* Present and empty, so do nothing */
141  break;
142  case 2:
143  case 3:
144  case 4:
145  /* Exists and not empty */
146  pg_fatal("directory \"%s\" exists but is not empty", path);
147  break;
148  default:
149  /* Trouble accessing directory */
150  pg_fatal("could not access directory \"%s\": %m", path);
151  }
152 }
int pg_dir_create_mode
Definition: file_perm.c:18
#define pg_fatal(...)
int pg_mkdir_p(char *path, int omode)
Definition: pgmkdirp.c:57
int pg_check_dir(const char *dir)
Definition: pgcheckdir.c:33

References pg_check_dir(), pg_dir_create_mode, pg_fatal, and pg_mkdir_p().

Referenced by main().

◆ identify_target_directory()

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

Definition at line 292 of file pg_waldump.c.

293 {
294  char fpath[MAXPGPATH];
295 
296  if (directory != NULL)
297  {
298  if (search_directory(directory, fname))
299  return pg_strdup(directory);
300 
301  /* directory / XLOGDIR */
302  snprintf(fpath, MAXPGPATH, "%s/%s", directory, XLOGDIR);
303  if (search_directory(fpath, fname))
304  return pg_strdup(fpath);
305  }
306  else
307  {
308  const char *datadir;
309 
310  /* current directory */
311  if (search_directory(".", fname))
312  return pg_strdup(".");
313  /* XLOGDIR */
314  if (search_directory(XLOGDIR, fname))
315  return pg_strdup(XLOGDIR);
316 
317  datadir = getenv("PGDATA");
318  /* $PGDATA / XLOGDIR */
319  if (datadir != NULL)
320  {
321  snprintf(fpath, MAXPGPATH, "%s/%s", datadir, XLOGDIR);
322  if (search_directory(fpath, fname))
323  return pg_strdup(fpath);
324  }
325  }
326 
327  /* could not locate WAL file */
328  if (fname)
329  pg_fatal("could not locate WAL file \"%s\"", fname);
330  else
331  pg_fatal("could not find any WAL file");
332 
333  return NULL; /* not reached */
334 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define MAXPGPATH
char * datadir
static bool search_directory(const char *directory, const char *fname)
Definition: pg_waldump.c:210
#define snprintf
Definition: port.h:238
#define XLOGDIR
static const char * directory
Definition: zic.c:634

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

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 792 of file pg_waldump.c.

793 {
794  uint32 xlogid;
795  uint32 xrecoff;
796  XLogReaderState *xlogreader_state;
797  XLogDumpPrivate private;
798  XLogDumpConfig config;
799  XLogStats stats;
800  XLogRecord *record;
801  XLogRecPtr first_record;
802  char *waldir = NULL;
803  char *errormsg;
804 
805  static struct option long_options[] = {
806  {"bkp-details", no_argument, NULL, 'b'},
807  {"block", required_argument, NULL, 'B'},
808  {"end", required_argument, NULL, 'e'},
809  {"follow", no_argument, NULL, 'f'},
810  {"fork", required_argument, NULL, 'F'},
811  {"fullpage", no_argument, NULL, 'w'},
812  {"help", no_argument, NULL, '?'},
813  {"limit", required_argument, NULL, 'n'},
814  {"path", required_argument, NULL, 'p'},
815  {"quiet", no_argument, NULL, 'q'},
816  {"relation", required_argument, NULL, 'R'},
817  {"rmgr", required_argument, NULL, 'r'},
818  {"start", required_argument, NULL, 's'},
819  {"timeline", required_argument, NULL, 't'},
820  {"xid", required_argument, NULL, 'x'},
821  {"version", no_argument, NULL, 'V'},
822  {"stats", optional_argument, NULL, 'z'},
823  {"save-fullpage", required_argument, NULL, 1},
824  {NULL, 0, NULL, 0}
825  };
826 
827  int option;
828  int optindex = 0;
829 
830 #ifndef WIN32
831  pqsignal(SIGINT, sigint_handler);
832 #endif
833 
834  pg_logging_init(argv[0]);
835  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_waldump"));
836  progname = get_progname(argv[0]);
837 
838  if (argc > 1)
839  {
840  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
841  {
842  usage();
843  exit(0);
844  }
845  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
846  {
847  puts("pg_waldump (PostgreSQL) " PG_VERSION);
848  exit(0);
849  }
850  }
851 
852  memset(&private, 0, sizeof(XLogDumpPrivate));
853  memset(&config, 0, sizeof(XLogDumpConfig));
854  memset(&stats, 0, sizeof(XLogStats));
855 
856  private.timeline = 1;
857  private.startptr = InvalidXLogRecPtr;
858  private.endptr = InvalidXLogRecPtr;
859  private.endptr_reached = false;
860 
861  config.quiet = false;
862  config.bkp_details = false;
863  config.stop_after_records = -1;
864  config.already_displayed_records = 0;
865  config.follow = false;
866  /* filter_by_rmgr array was zeroed by memset above */
867  config.filter_by_rmgr_enabled = false;
869  config.filter_by_xid_enabled = false;
870  config.filter_by_extended = false;
871  config.filter_by_relation_enabled = false;
872  config.filter_by_relation_block_enabled = false;
874  config.filter_by_fpw = false;
875  config.save_fullpage_path = NULL;
876  config.stats = false;
877  config.stats_per_record = false;
878 
879  stats.startptr = InvalidXLogRecPtr;
880  stats.endptr = InvalidXLogRecPtr;
881 
882  if (argc <= 1)
883  {
884  pg_log_error("no arguments specified");
885  goto bad_argument;
886  }
887 
888  while ((option = getopt_long(argc, argv, "bB:e:fF:n:p:qr:R:s:t:wx:z",
889  long_options, &optindex)) != -1)
890  {
891  switch (option)
892  {
893  case 'b':
894  config.bkp_details = true;
895  break;
896  case 'B':
897  if (sscanf(optarg, "%u", &config.filter_by_relation_block) != 1 ||
899  {
900  pg_log_error("invalid block number: \"%s\"", optarg);
901  goto bad_argument;
902  }
903  config.filter_by_relation_block_enabled = true;
904  config.filter_by_extended = true;
905  break;
906  case 'e':
907  if (sscanf(optarg, "%X/%X", &xlogid, &xrecoff) != 2)
908  {
909  pg_log_error("invalid WAL location: \"%s\"",
910  optarg);
911  goto bad_argument;
912  }
913  private.endptr = (uint64) xlogid << 32 | xrecoff;
914  break;
915  case 'f':
916  config.follow = true;
917  break;
918  case 'F':
921  {
922  pg_log_error("invalid fork name: \"%s\"", optarg);
923  goto bad_argument;
924  }
925  config.filter_by_extended = true;
926  break;
927  case 'n':
928  if (sscanf(optarg, "%d", &config.stop_after_records) != 1)
929  {
930  pg_log_error("invalid value \"%s\" for option %s", optarg, "-n/--limit");
931  goto bad_argument;
932  }
933  break;
934  case 'p':
935  waldir = pg_strdup(optarg);
936  break;
937  case 'q':
938  config.quiet = true;
939  break;
940  case 'r':
941  {
942  int rmid;
943 
944  if (pg_strcasecmp(optarg, "list") == 0)
945  {
946  print_rmgr_list();
948  }
949 
950  /*
951  * First look for the generated name of a custom rmgr, of
952  * the form "custom###". We accept this form, because the
953  * custom rmgr module is not loaded, so there's no way to
954  * know the real name. This convention should be
955  * consistent with that in rmgrdesc.c.
956  */
957  if (sscanf(optarg, "custom%03d", &rmid) == 1)
958  {
959  if (!RmgrIdIsCustom(rmid))
960  {
961  pg_log_error("custom resource manager \"%s\" does not exist",
962  optarg);
963  goto bad_argument;
964  }
965  config.filter_by_rmgr[rmid] = true;
966  config.filter_by_rmgr_enabled = true;
967  }
968  else
969  {
970  /* then look for builtin rmgrs */
971  for (rmid = 0; rmid <= RM_MAX_BUILTIN_ID; rmid++)
972  {
973  if (pg_strcasecmp(optarg, GetRmgrDesc(rmid)->rm_name) == 0)
974  {
975  config.filter_by_rmgr[rmid] = true;
976  config.filter_by_rmgr_enabled = true;
977  break;
978  }
979  }
980  if (rmid > RM_MAX_BUILTIN_ID)
981  {
982  pg_log_error("resource manager \"%s\" does not exist",
983  optarg);
984  goto bad_argument;
985  }
986  }
987  }
988  break;
989  case 'R':
990  if (sscanf(optarg, "%u/%u/%u",
991  &config.filter_by_relation.spcOid,
992  &config.filter_by_relation.dbOid,
993  &config.filter_by_relation.relNumber) != 3 ||
996  {
997  pg_log_error("invalid relation specification: \"%s\"", optarg);
998  pg_log_error_detail("Expecting \"tablespace OID/database OID/relation filenode\".");
999  goto bad_argument;
1000  }
1001  config.filter_by_relation_enabled = true;
1002  config.filter_by_extended = true;
1003  break;
1004  case 's':
1005  if (sscanf(optarg, "%X/%X", &xlogid, &xrecoff) != 2)
1006  {
1007  pg_log_error("invalid WAL location: \"%s\"",
1008  optarg);
1009  goto bad_argument;
1010  }
1011  else
1012  private.startptr = (uint64) xlogid << 32 | xrecoff;
1013  break;
1014  case 't':
1015 
1016  /*
1017  * This is like option_parse_int() but needs to handle
1018  * unsigned 32-bit int. Also, we accept both decimal and
1019  * hexadecimal specifications here.
1020  */
1021  {
1022  char *endptr;
1023  unsigned long val;
1024 
1025  errno = 0;
1026  val = strtoul(optarg, &endptr, 0);
1027 
1028  while (*endptr != '\0' && isspace((unsigned char) *endptr))
1029  endptr++;
1030 
1031  if (*endptr != '\0')
1032  {
1033  pg_log_error("invalid value \"%s\" for option %s",
1034  optarg, "-t/--timeline");
1035  goto bad_argument;
1036  }
1037 
1038  if (errno == ERANGE || val < 1 || val > UINT_MAX)
1039  {
1040  pg_log_error("%s must be in range %u..%u",
1041  "-t/--timeline", 1, UINT_MAX);
1042  goto bad_argument;
1043  }
1044 
1045  private.timeline = val;
1046 
1047  break;
1048  }
1049  case 'w':
1050  config.filter_by_fpw = true;
1051  break;
1052  case 'x':
1053  if (sscanf(optarg, "%u", &config.filter_by_xid) != 1)
1054  {
1055  pg_log_error("invalid transaction ID specification: \"%s\"",
1056  optarg);
1057  goto bad_argument;
1058  }
1059  config.filter_by_xid_enabled = true;
1060  break;
1061  case 'z':
1062  config.stats = true;
1063  config.stats_per_record = false;
1064  if (optarg)
1065  {
1066  if (strcmp(optarg, "record") == 0)
1067  config.stats_per_record = true;
1068  else if (strcmp(optarg, "rmgr") != 0)
1069  {
1070  pg_log_error("unrecognized value for option %s: %s",
1071  "--stats", optarg);
1072  goto bad_argument;
1073  }
1074  }
1075  break;
1076  case 1:
1078  break;
1079  default:
1080  goto bad_argument;
1081  }
1082  }
1083 
1084  if (config.filter_by_relation_block_enabled &&
1086  {
1087  pg_log_error("option %s requires option %s to be specified",
1088  "-B/--block", "-R/--relation");
1089  goto bad_argument;
1090  }
1091 
1092  if ((optind + 2) < argc)
1093  {
1094  pg_log_error("too many command-line arguments (first is \"%s\")",
1095  argv[optind + 2]);
1096  goto bad_argument;
1097  }
1098 
1099  if (waldir != NULL)
1100  {
1101  /* validate path points to directory */
1102  if (!verify_directory(waldir))
1103  {
1104  pg_log_error("could not open directory \"%s\": %m", waldir);
1105  goto bad_argument;
1106  }
1107  }
1108 
1109  if (config.save_fullpage_path != NULL)
1111 
1112  /* parse files as start/end boundaries, extract path if not specified */
1113  if (optind < argc)
1114  {
1115  char *directory = NULL;
1116  char *fname = NULL;
1117  int fd;
1118  XLogSegNo segno;
1119 
1120  split_path(argv[optind], &directory, &fname);
1121 
1122  if (waldir == NULL && directory != NULL)
1123  {
1124  waldir = directory;
1125 
1126  if (!verify_directory(waldir))
1127  pg_fatal("could not open directory \"%s\": %m", waldir);
1128  }
1129 
1130  waldir = identify_target_directory(waldir, fname);
1131  fd = open_file_in_directory(waldir, fname);
1132  if (fd < 0)
1133  pg_fatal("could not open file \"%s\"", fname);
1134  close(fd);
1135 
1136  /* parse position from file */
1137  XLogFromFileName(fname, &private.timeline, &segno, WalSegSz);
1138 
1139  if (XLogRecPtrIsInvalid(private.startptr))
1140  XLogSegNoOffsetToRecPtr(segno, 0, WalSegSz, private.startptr);
1141  else if (!XLByteInSeg(private.startptr, segno, WalSegSz))
1142  {
1143  pg_log_error("start WAL location %X/%X is not inside file \"%s\"",
1144  LSN_FORMAT_ARGS(private.startptr),
1145  fname);
1146  goto bad_argument;
1147  }
1148 
1149  /* no second file specified, set end position */
1150  if (!(optind + 1 < argc) && XLogRecPtrIsInvalid(private.endptr))
1151  XLogSegNoOffsetToRecPtr(segno + 1, 0, WalSegSz, private.endptr);
1152 
1153  /* parse ENDSEG if passed */
1154  if (optind + 1 < argc)
1155  {
1156  XLogSegNo endsegno;
1157 
1158  /* ignore directory, already have that */
1159  split_path(argv[optind + 1], &directory, &fname);
1160 
1161  fd = open_file_in_directory(waldir, fname);
1162  if (fd < 0)
1163  pg_fatal("could not open file \"%s\"", fname);
1164  close(fd);
1165 
1166  /* parse position from file */
1167  XLogFromFileName(fname, &private.timeline, &endsegno, WalSegSz);
1168 
1169  if (endsegno < segno)
1170  pg_fatal("ENDSEG %s is before STARTSEG %s",
1171  argv[optind + 1], argv[optind]);
1172 
1173  if (XLogRecPtrIsInvalid(private.endptr))
1174  XLogSegNoOffsetToRecPtr(endsegno + 1, 0, WalSegSz,
1175  private.endptr);
1176 
1177  /* set segno to endsegno for check of --end */
1178  segno = endsegno;
1179  }
1180 
1181 
1182  if (!XLByteInSeg(private.endptr, segno, WalSegSz) &&
1183  private.endptr != (segno + 1) * WalSegSz)
1184  {
1185  pg_log_error("end WAL location %X/%X is not inside file \"%s\"",
1186  LSN_FORMAT_ARGS(private.endptr),
1187  argv[argc - 1]);
1188  goto bad_argument;
1189  }
1190  }
1191  else
1192  waldir = identify_target_directory(waldir, NULL);
1193 
1194  /* we don't know what to print */
1195  if (XLogRecPtrIsInvalid(private.startptr))
1196  {
1197  pg_log_error("no start WAL location given");
1198  goto bad_argument;
1199  }
1200 
1201  /* done with argument parsing, do the actual work */
1202 
1203  /* we have everything we need, start reading */
1204  xlogreader_state =
1205  XLogReaderAllocate(WalSegSz, waldir,
1206  XL_ROUTINE(.page_read = WALDumpReadPage,
1207  .segment_open = WALDumpOpenSegment,
1208  .segment_close = WALDumpCloseSegment),
1209  &private);
1210  if (!xlogreader_state)
1211  pg_fatal("out of memory while allocating a WAL reading processor");
1212 
1213  /* first find a valid recptr to start from */
1214  first_record = XLogFindNextRecord(xlogreader_state, private.startptr);
1215 
1216  if (first_record == InvalidXLogRecPtr)
1217  pg_fatal("could not find a valid record after %X/%X",
1218  LSN_FORMAT_ARGS(private.startptr));
1219 
1220  /*
1221  * Display a message that we're skipping data if `from` wasn't a pointer
1222  * to the start of a record and also wasn't a pointer to the beginning of
1223  * a segment (e.g. we were used in file mode).
1224  */
1225  if (first_record != private.startptr &&
1226  XLogSegmentOffset(private.startptr, WalSegSz) != 0)
1227  pg_log_info(ngettext("first record is after %X/%X, at %X/%X, skipping over %u byte",
1228  "first record is after %X/%X, at %X/%X, skipping over %u bytes",
1229  (first_record - private.startptr)),
1230  LSN_FORMAT_ARGS(private.startptr),
1231  LSN_FORMAT_ARGS(first_record),
1232  (uint32) (first_record - private.startptr));
1233 
1234  if (config.stats == true && !config.quiet)
1235  stats.startptr = first_record;
1236 
1237  for (;;)
1238  {
1239  if (time_to_stop)
1240  {
1241  /* We've been Ctrl-C'ed, so leave */
1242  break;
1243  }
1244 
1245  /* try to read the next record */
1246  record = XLogReadRecord(xlogreader_state, &errormsg);
1247  if (!record)
1248  {
1249  if (!config.follow || private.endptr_reached)
1250  break;
1251  else
1252  {
1253  pg_usleep(1000000L); /* 1 second */
1254  continue;
1255  }
1256  }
1257 
1258  /* apply all specified filters */
1259  if (config.filter_by_rmgr_enabled &&
1260  !config.filter_by_rmgr[record->xl_rmid])
1261  continue;
1262 
1263  if (config.filter_by_xid_enabled &&
1264  config.filter_by_xid != record->xl_xid)
1265  continue;
1266 
1267  /* check for extended filtering */
1268  if (config.filter_by_extended &&
1269  !XLogRecordMatchesRelationBlock(xlogreader_state,
1271  config.filter_by_relation :
1274  config.filter_by_relation_block :
1277  continue;
1278 
1279  if (config.filter_by_fpw && !XLogRecordHasFPW(xlogreader_state))
1280  continue;
1281 
1282  /* perform any per-record work */
1283  if (!config.quiet)
1284  {
1285  if (config.stats == true)
1286  {
1287  XLogRecStoreStats(&stats, xlogreader_state);
1288  stats.endptr = xlogreader_state->EndRecPtr;
1289  }
1290  else
1291  XLogDumpDisplayRecord(&config, xlogreader_state);
1292  }
1293 
1294  /* save full pages if requested */
1295  if (config.save_fullpage_path != NULL)
1296  XLogRecordSaveFPWs(xlogreader_state, config.save_fullpage_path);
1297 
1298  /* check whether we printed enough */
1299  config.already_displayed_records++;
1300  if (config.stop_after_records > 0 &&
1302  break;
1303  }
1304 
1305  if (config.stats == true && !config.quiet)
1306  XLogDumpDisplayStats(&config, &stats);
1307 
1308  if (time_to_stop)
1309  exit(0);
1310 
1311  if (errormsg)
1312  pg_fatal("error in WAL record at %X/%X: %s",
1313  LSN_FORMAT_ARGS(xlogreader_state->ReadRecPtr),
1314  errormsg);
1315 
1316  XLogReaderFree(xlogreader_state);
1317 
1318  return EXIT_SUCCESS;
1319 
1320 bad_argument:
1321  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
1322  return EXIT_FAILURE;
1323 }
#define InvalidBlockNumber
Definition: block.h:33
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition: block.h:71
unsigned int uint32
Definition: c.h:506
#define ngettext(s, p, n)
Definition: c.h:1181
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1214
#define OidIsValid(objectId)
Definition: c.h:775
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:448
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define no_argument
Definition: getopt_long.h:24
#define required_argument
Definition: getopt_long.h:25
#define optional_argument
Definition: getopt_long.h:26
long val
Definition: informix.c:670
#define close(a)
Definition: win32.h:12
exit(1)
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_hint(...)
Definition: logging.h:112
#define pg_log_info(...)
Definition: logging.h:124
#define pg_log_error_detail(...)
Definition: logging.h:109
PGDLLIMPORT int optind
Definition: getopt.c:50
PGDLLIMPORT char * optarg
Definition: getopt.c:52
static void XLogDumpDisplayStats(XLogDumpConfig *config, XLogStats *stats)
Definition: pg_waldump.c:626
static int WalSegSz
Definition: pg_waldump.c:42
static void WALDumpCloseSegment(XLogReaderState *state)
Definition: pg_waldump.c:380
static volatile sig_atomic_t time_to_stop
Definition: pg_waldump.c:43
static void split_path(const char *path, char **dir, char **fname)
Definition: pg_waldump.c:161
static int open_file_in_directory(const char *directory, const char *fname)
Definition: pg_waldump.c:188
static void XLogDumpDisplayRecord(XLogDumpConfig *config, XLogReaderState *record)
Definition: pg_waldump.c:546
static void create_fullpage_directory(char *path)
Definition: pg_waldump.c:128
static bool verify_directory(const char *directory)
Definition: pg_waldump.c:113
static const RelFileLocator emptyRelFileLocator
Definition: pg_waldump.c:45
static void print_rmgr_list(void)
Definition: pg_waldump.c:98
static void WALDumpOpenSegment(XLogReaderState *state, XLogSegNo nextSegNo, TimeLineID *tli_p)
Definition: pg_waldump.c:338
static const char * progname
Definition: pg_waldump.c:40
static void usage(void)
Definition: pg_waldump.c:756
static int WALDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetPtr, char *readBuff)
Definition: pg_waldump.c:389
static void XLogRecordSaveFPWs(XLogReaderState *record, const char *savepath)
Definition: pg_waldump.c:490
static char * identify_target_directory(char *directory, char *fname)
Definition: pg_waldump.c:292
static bool XLogRecordHasFPW(XLogReaderState *record)
Definition: pg_waldump.c:469
static void sigint_handler(SIGNAL_ARGS)
Definition: pg_waldump.c:91
static bool XLogRecordMatchesRelationBlock(XLogReaderState *record, RelFileLocator matchRlocator, BlockNumber matchBlock, ForkNumber matchFork)
Definition: pg_waldump.c:438
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * get_progname(const char *argv0)
Definition: path.c:574
pqsigfunc pqsignal(int signo, pqsigfunc func)
static int fd(const char *x, int i)
Definition: preproc-init.c:105
ForkNumber forkname_to_number(const char *forkName)
Definition: relpath.c:50
@ InvalidForkNumber
Definition: relpath.h:49
#define RelFileNumberIsValid(relnumber)
Definition: relpath.h:27
#define RM_MAX_BUILTIN_ID
Definition: rmgr.h:34
static bool RmgrIdIsCustom(int rmid)
Definition: rmgr.h:48
const RmgrDescData * GetRmgrDesc(RmgrId rmid)
Definition: rmgrdesc.c:87
#define EXIT_SUCCESS
Definition: settings.h:163
#define EXIT_FAILURE
Definition: settings.h:167
void pg_usleep(long microsec)
Definition: signal.c:53
RelFileNumber relNumber
bool filter_by_xid_enabled
Definition: pg_waldump.c:70
RelFileLocator filter_by_relation
Definition: pg_waldump.c:71
char * save_fullpage_path
Definition: pg_waldump.c:80
bool bkp_details
Definition: pg_waldump.c:59
bool filter_by_rmgr[RM_MAX_ID+1]
Definition: pg_waldump.c:67
bool filter_by_extended
Definition: pg_waldump.c:72
bool stats_per_record
Definition: pg_waldump.c:64
int already_displayed_records
Definition: pg_waldump.c:61
int stop_after_records
Definition: pg_waldump.c:60
bool filter_by_fpw
Definition: pg_waldump.c:77
bool filter_by_relation_enabled
Definition: pg_waldump.c:73
BlockNumber filter_by_relation_block
Definition: pg_waldump.c:74
bool filter_by_rmgr_enabled
Definition: pg_waldump.c:68
TransactionId filter_by_xid
Definition: pg_waldump.c:69
ForkNumber filter_by_relation_forknum
Definition: pg_waldump.c:76
bool filter_by_relation_block_enabled
Definition: pg_waldump.c:75
XLogRecPtr EndRecPtr
Definition: xlogreader.h:207
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:206
TransactionId xl_xid
Definition: xlogrecord.h:44
RmgrId xl_rmid
Definition: xlogrecord.h:47
#define InvalidTransactionId
Definition: transam.h:31
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
static void XLogFromFileName(const char *fname, TimeLineID *tli, XLogSegNo *logSegNo, int wal_segsz_bytes)
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes)
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
uint64 XLogSegNo
Definition: xlogdefs.h:48
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:389
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:161
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition: xlogreader.c:106
XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:1383
#define XL_ROUTINE(...)
Definition: xlogreader.h:117
void XLogRecStoreStats(XLogStats *stats, XLogReaderState *record)
Definition: xlogstats.c:54

References XLogDumpConfig::already_displayed_records, XLogDumpConfig::bkp_details, BlockNumberIsValid(), close, create_fullpage_directory(), RelFileLocator::dbOid, directory, emptyRelFileLocator, XLogReaderState::EndRecPtr, exit(), EXIT_FAILURE, EXIT_SUCCESS, fd(), XLogDumpConfig::filter_by_extended, XLogDumpConfig::filter_by_fpw, XLogDumpConfig::filter_by_relation, XLogDumpConfig::filter_by_relation_block, XLogDumpConfig::filter_by_relation_block_enabled, XLogDumpConfig::filter_by_relation_enabled, XLogDumpConfig::filter_by_relation_forknum, XLogDumpConfig::filter_by_rmgr, XLogDumpConfig::filter_by_rmgr_enabled, XLogDumpConfig::filter_by_xid, XLogDumpConfig::filter_by_xid_enabled, XLogDumpConfig::follow, forkname_to_number(), get_progname(), getopt_long(), GetRmgrDesc(), identify_target_directory(), InvalidBlockNumber, InvalidForkNumber, InvalidTransactionId, InvalidXLogRecPtr, LSN_FORMAT_ARGS, ngettext, no_argument, OidIsValid, open_file_in_directory(), optarg, optind, optional_argument, pg_fatal, pg_log_error, pg_log_error_detail, pg_log_error_hint, pg_log_info, pg_logging_init(), pg_strcasecmp(), pg_strdup(), PG_TEXTDOMAIN, pg_usleep(), pqsignal(), print_rmgr_list(), progname, XLogDumpConfig::quiet, XLogReaderState::ReadRecPtr, RelFileNumberIsValid, RelFileLocator::relNumber, required_argument, RM_MAX_BUILTIN_ID, RmgrIdIsCustom(), XLogDumpConfig::save_fullpage_path, set_pglocale_pgservice(), sigint_handler(), RelFileLocator::spcOid, split_path(), XLogDumpConfig::stats, XLogDumpConfig::stats_per_record, XLogDumpConfig::stop_after_records, time_to_stop, usage(), val, verify_directory(), WALDumpCloseSegment(), WALDumpOpenSegment(), WALDumpReadPage(), WalSegSz, XLogRecord::xl_rmid, XL_ROUTINE, XLogRecord::xl_xid, XLByteInSeg, XLogDumpDisplayRecord(), XLogDumpDisplayStats(), XLogFindNextRecord(), XLogFromFileName(), XLogReaderAllocate(), XLogReaderFree(), XLogReadRecord(), XLogRecordHasFPW(), XLogRecordMatchesRelationBlock(), XLogRecordSaveFPWs(), XLogRecPtrIsInvalid, XLogRecStoreStats(), XLogSegmentOffset, and XLogSegNoOffsetToRecPtr.

◆ open_file_in_directory()

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

Definition at line 188 of file pg_waldump.c.

189 {
190  int fd = -1;
191  char fpath[MAXPGPATH];
192 
193  Assert(directory != NULL);
194 
195  snprintf(fpath, MAXPGPATH, "%s/%s", directory, fname);
196  fd = open(fpath, O_RDONLY | PG_BINARY, 0);
197 
198  if (fd < 0 && errno != ENOENT)
199  pg_fatal("could not open file \"%s\": %m", fname);
200  return fd;
201 }
#define Assert(condition)
Definition: c.h:858
#define PG_BINARY
Definition: c.h:1273

References Assert, directory, fd(), MAXPGPATH, PG_BINARY, pg_fatal, and snprintf.

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

◆ print_rmgr_list()

static void print_rmgr_list ( void  )
static

Definition at line 98 of file pg_waldump.c.

99 {
100  int i;
101 
102  for (i = 0; i <= RM_MAX_BUILTIN_ID; i++)
103  {
104  printf("%s\n", GetRmgrDesc(i)->rm_name);
105  }
106 }
int i
Definition: isn.c:73
#define printf(...)
Definition: port.h:244

References GetRmgrDesc(), i, printf, and RM_MAX_BUILTIN_ID.

Referenced by main().

◆ search_directory()

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

Definition at line 210 of file pg_waldump.c.

211 {
212  int fd = -1;
213  DIR *xldir;
214 
215  /* open file if valid filename is provided */
216  if (fname != NULL)
218 
219  /*
220  * A valid file name is not passed, so search the complete directory. If
221  * we find any file whose name is a valid WAL file name then try to open
222  * it. If we cannot open it, bail out.
223  */
224  else if ((xldir = opendir(directory)) != NULL)
225  {
226  struct dirent *xlde;
227 
228  while ((xlde = readdir(xldir)) != NULL)
229  {
230  if (IsXLogFileName(xlde->d_name))
231  {
233  fname = pg_strdup(xlde->d_name);
234  break;
235  }
236  }
237 
238  closedir(xldir);
239  }
240 
241  /* set WalSegSz if file is successfully opened */
242  if (fd >= 0)
243  {
245  int r;
246 
247  r = read(fd, buf.data, XLOG_BLCKSZ);
248  if (r == XLOG_BLCKSZ)
249  {
250  XLogLongPageHeader longhdr = (XLogLongPageHeader) buf.data;
251 
252  WalSegSz = longhdr->xlp_seg_size;
253 
255  {
256  pg_log_error(ngettext("invalid WAL segment size in WAL file \"%s\" (%d byte)",
257  "invalid WAL segment size in WAL file \"%s\" (%d bytes)",
258  WalSegSz),
259  fname, WalSegSz);
260  pg_log_error_detail("The WAL segment size must be a power of two between 1 MB and 1 GB.");
261  exit(1);
262  }
263  }
264  else if (r < 0)
265  pg_fatal("could not read file \"%s\": %m",
266  fname);
267  else
268  pg_fatal("could not read file \"%s\": read %d of %d",
269  fname, r, XLOG_BLCKSZ);
270  close(fd);
271  return true;
272  }
273 
274  return false;
275 }
int closedir(DIR *)
Definition: dirent.c:127
struct dirent * readdir(DIR *)
Definition: dirent.c:78
DIR * opendir(const char *)
Definition: dirent.c:33
#define read(a, b, c)
Definition: win32.h:13
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
static char * buf
Definition: pg_test_fsync.c:73
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:96
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:71
static bool IsXLogFileName(const char *fname)

References buf, close, closedir(), dirent::d_name, directory, exit(), fd(), if(), IsValidWalSegSize, IsXLogFileName(), ngettext, open_file_in_directory(), opendir(), pg_fatal, pg_log_error, pg_log_error_detail, pg_strdup(), read, readdir(), WalSegSz, and XLogLongPageHeaderData::xlp_seg_size.

Referenced by identify_target_directory().

◆ sigint_handler()

static void sigint_handler ( SIGNAL_ARGS  )
static

Definition at line 91 of file pg_waldump.c.

92 {
93  time_to_stop = true;
94 }

References time_to_stop.

Referenced by main().

◆ split_path()

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

Definition at line 161 of file pg_waldump.c.

162 {
163  char *sep;
164 
165  /* split filepath into directory & filename */
166  sep = strrchr(path, '/');
167 
168  /* directory path */
169  if (sep != NULL)
170  {
171  *dir = pnstrdup(path, sep - path);
172  *fname = pg_strdup(sep + 1);
173  }
174  /* local directory */
175  else
176  {
177  *dir = NULL;
178  *fname = pg_strdup(path);
179  }
180 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1706

References pg_strdup(), and pnstrdup().

Referenced by main().

◆ usage()

static void usage ( void  )
static

Definition at line 756 of file pg_waldump.c.

757 {
758  printf(_("%s decodes and displays PostgreSQL write-ahead logs for debugging.\n\n"),
759  progname);
760  printf(_("Usage:\n"));
761  printf(_(" %s [OPTION]... [STARTSEG [ENDSEG]]\n"), progname);
762  printf(_("\nOptions:\n"));
763  printf(_(" -b, --bkp-details output detailed information about backup blocks\n"));
764  printf(_(" -B, --block=N with --relation, only show records that modify block N\n"));
765  printf(_(" -e, --end=RECPTR stop reading at WAL location RECPTR\n"));
766  printf(_(" -f, --follow keep retrying after reaching end of WAL\n"));
767  printf(_(" -F, --fork=FORK only show records that modify blocks in fork FORK;\n"
768  " valid names are main, fsm, vm, init\n"));
769  printf(_(" -n, --limit=N number of records to display\n"));
770  printf(_(" -p, --path=PATH directory in which to find WAL segment files or a\n"
771  " directory with a ./pg_wal that contains such files\n"
772  " (default: current directory, ./pg_wal, $PGDATA/pg_wal)\n"));
773  printf(_(" -q, --quiet do not print any output, except for errors\n"));
774  printf(_(" -r, --rmgr=RMGR only show records generated by resource manager RMGR;\n"
775  " use --rmgr=list to list valid resource manager names\n"));
776  printf(_(" -R, --relation=T/D/R only show records that modify blocks in relation T/D/R\n"));
777  printf(_(" -s, --start=RECPTR start reading at WAL location RECPTR\n"));
778  printf(_(" -t, --timeline=TLI timeline from which to read WAL records\n"
779  " (default: 1 or the value used in STARTSEG)\n"));
780  printf(_(" -V, --version output version information, then exit\n"));
781  printf(_(" -w, --fullpage only show records with a full page write\n"));
782  printf(_(" -x, --xid=XID only show records with transaction ID XID\n"));
783  printf(_(" -z, --stats[=record] show statistics instead of records\n"
784  " (optionally, show per-record statistics)\n"));
785  printf(_(" --save-fullpage=DIR save full page images to DIR\n"));
786  printf(_(" -?, --help show this help, then exit\n"));
787  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
788  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
789 }
#define _(x)
Definition: elog.c:90

References _, printf, and progname.

Referenced by main().

◆ verify_directory()

static bool verify_directory ( const char *  directory)
static

Definition at line 113 of file pg_waldump.c.

114 {
115  DIR *dir = opendir(directory);
116 
117  if (dir == NULL)
118  return false;
119  closedir(dir);
120  return true;
121 }

References closedir(), directory, and opendir().

Referenced by main().

◆ WALDumpCloseSegment()

static void WALDumpCloseSegment ( XLogReaderState state)
static

Definition at line 380 of file pg_waldump.c.

381 {
382  close(state->seg.ws_file);
383  /* need to check errno? */
384  state->seg.ws_file = -1;
385 }
Definition: regguts.h:323

References close.

Referenced by main().

◆ WALDumpOpenSegment()

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

Definition at line 338 of file pg_waldump.c.

340 {
341  TimeLineID tli = *tli_p;
342  char fname[MAXPGPATH];
343  int tries;
344 
345  XLogFileName(fname, tli, nextSegNo, state->segcxt.ws_segsize);
346 
347  /*
348  * In follow mode there is a short period of time after the server has
349  * written the end of the previous file before the new file is available.
350  * So we loop for 5 seconds looking for the file to appear before giving
351  * up.
352  */
353  for (tries = 0; tries < 10; tries++)
354  {
355  state->seg.ws_file = open_file_in_directory(state->segcxt.ws_dir, fname);
356  if (state->seg.ws_file >= 0)
357  return;
358  if (errno == ENOENT)
359  {
360  int save_errno = errno;
361 
362  /* File not there yet, try again */
363  pg_usleep(500 * 1000);
364 
365  errno = save_errno;
366  continue;
367  }
368  /* Any other error, fall through and fail */
369  break;
370  }
371 
372  pg_fatal("could not find file \"%s\": %m", fname);
373 }
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
uint32 TimeLineID
Definition: xlogdefs.h:59

References MAXPGPATH, open_file_in_directory(), pg_fatal, pg_usleep(), and XLogFileName().

Referenced by main().

◆ WALDumpReadPage()

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

Definition at line 389 of file pg_waldump.c.

391 {
392  XLogDumpPrivate *private = state->private_data;
393  int count = XLOG_BLCKSZ;
394  WALReadError errinfo;
395 
396  if (private->endptr != InvalidXLogRecPtr)
397  {
398  if (targetPagePtr + XLOG_BLCKSZ <= private->endptr)
399  count = XLOG_BLCKSZ;
400  else if (targetPagePtr + reqLen <= private->endptr)
401  count = private->endptr - targetPagePtr;
402  else
403  {
404  private->endptr_reached = true;
405  return -1;
406  }
407  }
408 
409  if (!WALRead(state, readBuff, targetPagePtr, count, private->timeline,
410  &errinfo))
411  {
412  WALOpenSegment *seg = &errinfo.wre_seg;
413  char fname[MAXPGPATH];
414 
415  XLogFileName(fname, seg->ws_tli, seg->ws_segno,
416  state->segcxt.ws_segsize);
417 
418  if (errinfo.wre_errno != 0)
419  {
420  errno = errinfo.wre_errno;
421  pg_fatal("could not read from file \"%s\", offset %d: %m",
422  fname, errinfo.wre_off);
423  }
424  else
425  pg_fatal("could not read from file \"%s\", offset %d: read %d of %d",
426  fname, errinfo.wre_off, errinfo.wre_read,
427  errinfo.wre_req);
428  }
429 
430  return count;
431 }
XLogSegNo ws_segno
Definition: xlogreader.h:48
TimeLineID ws_tli
Definition: xlogreader.h:49
WALOpenSegment wre_seg
Definition: xlogreader.h:388
bool WALRead(XLogReaderState *state, char *buf, XLogRecPtr startptr, Size count, TimeLineID tli, WALReadError *errinfo)
Definition: xlogreader.c:1503

References InvalidXLogRecPtr, MAXPGPATH, pg_fatal, WALRead(), WALReadError::wre_errno, WALReadError::wre_off, WALReadError::wre_read, WALReadError::wre_req, WALReadError::wre_seg, WALOpenSegment::ws_segno, WALOpenSegment::ws_tli, and XLogFileName().

Referenced by main().

◆ XLogDumpDisplayRecord()

static void XLogDumpDisplayRecord ( XLogDumpConfig config,
XLogReaderState record 
)
static

Definition at line 546 of file pg_waldump.c.

547 {
548  const char *id;
549  const RmgrDescData *desc = GetRmgrDesc(XLogRecGetRmid(record));
550  uint32 rec_len;
551  uint32 fpi_len;
552  uint8 info = XLogRecGetInfo(record);
553  XLogRecPtr xl_prev = XLogRecGetPrev(record);
554  StringInfoData s;
555 
556  XLogRecGetLen(record, &rec_len, &fpi_len);
557 
558  printf("rmgr: %-11s len (rec/tot): %6u/%6u, tx: %10u, lsn: %X/%08X, prev %X/%08X, ",
559  desc->rm_name,
560  rec_len, XLogRecGetTotalLen(record),
561  XLogRecGetXid(record),
562  LSN_FORMAT_ARGS(record->ReadRecPtr),
563  LSN_FORMAT_ARGS(xl_prev));
564 
565  id = desc->rm_identify(info);
566  if (id == NULL)
567  printf("desc: UNKNOWN (%x) ", info & ~XLR_INFO_MASK);
568  else
569  printf("desc: %s ", id);
570 
571  initStringInfo(&s);
572  desc->rm_desc(&s, record);
573  printf("%s", s.data);
574 
575  resetStringInfo(&s);
576  XLogRecGetBlockRefInfo(record, true, config->bkp_details, &s, NULL);
577  printf("%s", s.data);
578  pfree(s.data);
579 }
unsigned char uint8
Definition: c.h:504
void pfree(void *pointer)
Definition: mcxt.c:1520
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:78
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
const char *(* rm_identify)(uint8 info)
Definition: rmgrdesc.h:18
const char * rm_name
Definition: rmgrdesc.h:16
void(* rm_desc)(StringInfo buf, XLogReaderState *record)
Definition: rmgrdesc.h:17
void XLogRecGetBlockRefInfo(XLogReaderState *record, bool pretty, bool detailed_format, StringInfo buf, uint32 *fpi_len)
Definition: xlogdesc.c:216
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:410
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:411
#define XLogRecGetTotalLen(decoder)
Definition: xlogreader.h:408
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:412
#define XLogRecGetPrev(decoder)
Definition: xlogreader.h:409
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
void XLogRecGetLen(XLogReaderState *record, uint32 *rec_len, uint32 *fpi_len)
Definition: xlogstats.c:22

References XLogDumpConfig::bkp_details, StringInfoData::data, GetRmgrDesc(), initStringInfo(), LSN_FORMAT_ARGS, pfree(), printf, XLogReaderState::ReadRecPtr, resetStringInfo(), RmgrDescData::rm_desc, RmgrDescData::rm_identify, RmgrDescData::rm_name, XLogRecGetBlockRefInfo(), XLogRecGetInfo, XLogRecGetLen(), XLogRecGetPrev, XLogRecGetRmid, XLogRecGetTotalLen, XLogRecGetXid, and XLR_INFO_MASK.

Referenced by main().

◆ XLogDumpDisplayStats()

static void XLogDumpDisplayStats ( XLogDumpConfig config,
XLogStats stats 
)
static

Definition at line 626 of file pg_waldump.c.

627 {
628  int ri,
629  rj;
630  uint64 total_count = 0;
631  uint64 total_rec_len = 0;
632  uint64 total_fpi_len = 0;
633  uint64 total_len = 0;
634  double rec_len_pct,
635  fpi_len_pct;
636 
637  /*
638  * Leave if no stats have been computed yet, as tracked by the end LSN.
639  */
640  if (XLogRecPtrIsInvalid(stats->endptr))
641  return;
642 
643  /*
644  * Each row shows its percentages of the total, so make a first pass to
645  * calculate column totals.
646  */
647 
648  for (ri = 0; ri <= RM_MAX_ID; ri++)
649  {
650  if (!RmgrIdIsValid(ri))
651  continue;
652 
653  total_count += stats->rmgr_stats[ri].count;
654  total_rec_len += stats->rmgr_stats[ri].rec_len;
655  total_fpi_len += stats->rmgr_stats[ri].fpi_len;
656  }
657  total_len = total_rec_len + total_fpi_len;
658 
659  printf("WAL statistics between %X/%X and %X/%X:\n",
660  LSN_FORMAT_ARGS(stats->startptr), LSN_FORMAT_ARGS(stats->endptr));
661 
662  /*
663  * 27 is strlen("Transaction/COMMIT_PREPARED"), 20 is strlen(2^64), 8 is
664  * strlen("(100.00%)")
665  */
666 
667  printf("%-27s %20s %8s %20s %8s %20s %8s %20s %8s\n"
668  "%-27s %20s %8s %20s %8s %20s %8s %20s %8s\n",
669  "Type", "N", "(%)", "Record size", "(%)", "FPI size", "(%)", "Combined size", "(%)",
670  "----", "-", "---", "-----------", "---", "--------", "---", "-------------", "---");
671 
672  for (ri = 0; ri <= RM_MAX_ID; ri++)
673  {
674  uint64 count,
675  rec_len,
676  fpi_len,
677  tot_len;
678  const RmgrDescData *desc;
679 
680  if (!RmgrIdIsValid(ri))
681  continue;
682 
683  desc = GetRmgrDesc(ri);
684 
685  if (!config->stats_per_record)
686  {
687  count = stats->rmgr_stats[ri].count;
688  rec_len = stats->rmgr_stats[ri].rec_len;
689  fpi_len = stats->rmgr_stats[ri].fpi_len;
690  tot_len = rec_len + fpi_len;
691 
692  if (RmgrIdIsCustom(ri) && count == 0)
693  continue;
694 
696  count, total_count, rec_len, total_rec_len,
697  fpi_len, total_fpi_len, tot_len, total_len);
698  }
699  else
700  {
701  for (rj = 0; rj < MAX_XLINFO_TYPES; rj++)
702  {
703  const char *id;
704 
705  count = stats->record_stats[ri][rj].count;
706  rec_len = stats->record_stats[ri][rj].rec_len;
707  fpi_len = stats->record_stats[ri][rj].fpi_len;
708  tot_len = rec_len + fpi_len;
709 
710  /* Skip undefined combinations and ones that didn't occur */
711  if (count == 0)
712  continue;
713 
714  /* the upper four bits in xl_info are the rmgr's */
715  id = desc->rm_identify(rj << 4);
716  if (id == NULL)
717  id = psprintf("UNKNOWN (%x)", rj << 4);
718 
719  XLogDumpStatsRow(psprintf("%s/%s", desc->rm_name, id),
720  count, total_count, rec_len, total_rec_len,
721  fpi_len, total_fpi_len, tot_len, total_len);
722  }
723  }
724  }
725 
726  printf("%-27s %20s %8s %20s %8s %20s %8s %20s\n",
727  "", "--------", "", "--------", "", "--------", "", "--------");
728 
729  /*
730  * The percentages in earlier rows were calculated against the column
731  * total, but the ones that follow are against the row total. Note that
732  * these are displayed with a % symbol to differentiate them from the
733  * earlier ones, and are thus up to 9 characters long.
734  */
735 
736  rec_len_pct = 0;
737  if (total_len != 0)
738  rec_len_pct = 100 * (double) total_rec_len / total_len;
739 
740  fpi_len_pct = 0;
741  if (total_len != 0)
742  fpi_len_pct = 100 * (double) total_fpi_len / total_len;
743 
744  printf("%-27s "
745  "%20" INT64_MODIFIER "u %-9s"
746  "%20" INT64_MODIFIER "u %-9s"
747  "%20" INT64_MODIFIER "u %-9s"
748  "%20" INT64_MODIFIER "u %-6s\n",
749  "Total", stats->count, "",
750  total_rec_len, psprintf("[%.02f%%]", rec_len_pct),
751  total_fpi_len, psprintf("[%.02f%%]", fpi_len_pct),
752  total_len, "[100%]");
753 }
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:585
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define RmgrIdIsValid(rmid)
Definition: rmgr.h:53
#define RM_MAX_ID
Definition: rmgr.h:33
uint64 count
Definition: xlogstats.h:23
uint64 fpi_len
Definition: xlogstats.h:25
uint64 rec_len
Definition: xlogstats.h:24
XLogRecStats record_stats[RM_MAX_ID+1][MAX_XLINFO_TYPES]
Definition: xlogstats.h:36
uint64 count
Definition: xlogstats.h:30
XLogRecStats rmgr_stats[RM_MAX_ID+1]
Definition: xlogstats.h:35
#define MAX_XLINFO_TYPES
Definition: xlogstats.h:19

References XLogRecStats::count, XLogStats::count, XLogRecStats::fpi_len, GetRmgrDesc(), LSN_FORMAT_ARGS, MAX_XLINFO_TYPES, printf, psprintf(), XLogRecStats::rec_len, XLogStats::record_stats, RmgrDescData::rm_identify, RM_MAX_ID, RmgrDescData::rm_name, XLogStats::rmgr_stats, RmgrIdIsCustom(), RmgrIdIsValid, XLogDumpConfig::stats_per_record, XLogDumpStatsRow(), and XLogRecPtrIsInvalid.

Referenced by main().

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

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

References name, and printf.

Referenced by XLogDumpDisplayStats().

◆ XLogRecordHasFPW()

static bool XLogRecordHasFPW ( XLogReaderState record)
static

Definition at line 469 of file pg_waldump.c.

470 {
471  int block_id;
472 
473  for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
474  {
475  if (!XLogRecHasBlockRef(record, block_id))
476  continue;
477 
478  if (XLogRecHasBlockImage(record, block_id))
479  return true;
480  }
481 
482  return false;
483 }
#define XLogRecMaxBlockId(decoder)
Definition: xlogreader.h:418
#define XLogRecHasBlockImage(decoder, block_id)
Definition: xlogreader.h:423
#define XLogRecHasBlockRef(decoder, block_id)
Definition: xlogreader.h:420

References XLogRecHasBlockImage, XLogRecHasBlockRef, and XLogRecMaxBlockId.

Referenced by main().

◆ XLogRecordMatchesRelationBlock()

static bool XLogRecordMatchesRelationBlock ( XLogReaderState record,
RelFileLocator  matchRlocator,
BlockNumber  matchBlock,
ForkNumber  matchFork 
)
static

Definition at line 438 of file pg_waldump.c.

442 {
443  int block_id;
444 
445  for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
446  {
447  RelFileLocator rlocator;
448  ForkNumber forknum;
449  BlockNumber blk;
450 
451  if (!XLogRecGetBlockTagExtended(record, block_id,
452  &rlocator, &forknum, &blk, NULL))
453  continue;
454 
455  if ((matchFork == InvalidForkNumber || matchFork == forknum) &&
456  (RelFileLocatorEquals(matchRlocator, emptyRelFileLocator) ||
457  RelFileLocatorEquals(matchRlocator, rlocator)) &&
458  (matchBlock == InvalidBlockNumber || matchBlock == blk))
459  return true;
460  }
461 
462  return false;
463 }
uint32 BlockNumber
Definition: block.h:31
#define RelFileLocatorEquals(locator1, locator2)
ForkNumber
Definition: relpath.h:48
bool XLogRecGetBlockTagExtended(XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum, Buffer *prefetch_buffer)
Definition: xlogreader.c:1997

References emptyRelFileLocator, InvalidBlockNumber, InvalidForkNumber, RelFileLocatorEquals, XLogRecGetBlockTagExtended(), and XLogRecMaxBlockId.

Referenced by main().

◆ XLogRecordSaveFPWs()

static void XLogRecordSaveFPWs ( XLogReaderState record,
const char *  savepath 
)
static

Definition at line 490 of file pg_waldump.c.

491 {
492  int block_id;
493 
494  for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
495  {
497  Page page;
498  char filename[MAXPGPATH];
499  char forkname[FORKNAMECHARS + 2]; /* _ + terminating zero */
500  FILE *file;
501  BlockNumber blk;
502  RelFileLocator rnode;
503  ForkNumber fork;
504 
505  if (!XLogRecHasBlockRef(record, block_id))
506  continue;
507 
508  if (!XLogRecHasBlockImage(record, block_id))
509  continue;
510 
511  page = (Page) buf.data;
512 
513  /* Full page exists, so let's save it */
514  if (!RestoreBlockImage(record, block_id, page))
515  pg_fatal("%s", record->errormsg_buf);
516 
517  (void) XLogRecGetBlockTagExtended(record, block_id,
518  &rnode, &fork, &blk, NULL);
519 
520  if (fork >= 0 && fork <= MAX_FORKNUM)
521  sprintf(forkname, "_%s", forkNames[fork]);
522  else
523  pg_fatal("invalid fork number: %u", fork);
524 
525  snprintf(filename, MAXPGPATH, "%s/%08X-%08X-%08X.%u.%u.%u.%u%s", savepath,
526  record->seg.ws_tli,
527  LSN_FORMAT_ARGS(record->ReadRecPtr),
528  rnode.spcOid, rnode.dbOid, rnode.relNumber, blk, forkname);
529 
530  file = fopen(filename, PG_BINARY_W);
531  if (!file)
532  pg_fatal("could not open file \"%s\": %m", filename);
533 
534  if (fwrite(page, BLCKSZ, 1, file) != 1)
535  pg_fatal("could not write file \"%s\": %m", filename);
536 
537  if (fclose(file) != 0)
538  pg_fatal("could not close file \"%s\": %m", filename);
539  }
540 }
Pointer Page
Definition: bufpage.h:78
#define PG_BINARY_W
Definition: c.h:1276
static char * filename
Definition: pg_dumpall.c:119
#define sprintf
Definition: port.h:240
const char *const forkNames[]
Definition: relpath.c:33
#define MAX_FORKNUM
Definition: relpath.h:62
#define FORKNAMECHARS
Definition: relpath.h:64
char * errormsg_buf
Definition: xlogreader.h:311
WALOpenSegment seg
Definition: xlogreader.h:272
bool RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
Definition: xlogreader.c:2056

References buf, RelFileLocator::dbOid, XLogReaderState::errormsg_buf, filename, FORKNAMECHARS, forkNames, if(), LSN_FORMAT_ARGS, MAX_FORKNUM, MAXPGPATH, PG_BINARY_W, pg_fatal, XLogReaderState::ReadRecPtr, RelFileLocator::relNumber, RestoreBlockImage(), XLogReaderState::seg, snprintf, RelFileLocator::spcOid, sprintf, WALOpenSegment::ws_tli, XLogRecGetBlockTagExtended(), XLogRecHasBlockImage, XLogRecHasBlockRef, and XLogRecMaxBlockId.

Referenced by main().

Variable Documentation

◆ emptyRelFileLocator

const RelFileLocator emptyRelFileLocator = {0, 0, 0}
static

Definition at line 45 of file pg_waldump.c.

Referenced by main(), and XLogRecordMatchesRelationBlock().

◆ progname

const char* progname
static

Definition at line 40 of file pg_waldump.c.

Referenced by main(), and usage().

◆ time_to_stop

volatile sig_atomic_t time_to_stop = false
static

Definition at line 43 of file pg_waldump.c.

Referenced by main(), and sigint_handler().

◆ WalSegSz

int WalSegSz
static

Definition at line 42 of file pg_waldump.c.

Referenced by main(), and search_directory().