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/xlogreader.h"
#include "access/xlogrecord.h"
#include "access/xlog_internal.h"
#include "access/transam.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 XLogDumpXLogRead (const char *directory, TimeLineID timeline_id, XLogRecPtr startptr, char *buf, Size count)
 
static int XLogDumpReadPage (XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetPtr, char *readBuff)
 
static void XLogDumpRecordLen (XLogReaderState *record, uint32 *rec_len, uint32 *fpi_len)
 
static void XLogDumpCountRecord (XLogDumpConfig *config, XLogDumpStats *stats, XLogReaderState *record)
 
static void XLogDumpDisplayRecord (XLogDumpConfig *config, XLogReaderState *record)
 
static void XLogDumpStatsRow (const char *name, uint64 n, uint64 total_count, uint64 rec_len, uint64 total_rec_len, uint64 fpi_len, uint64 total_fpi_len, uint64 tot_len, uint64 total_len)
 
static void XLogDumpDisplayStats (XLogDumpConfig *config, XLogDumpStats *stats)
 
static void usage (void)
 
int main (int argc, char **argv)
 

Variables

static const char * progname
 
static int WalSegSz
 

Macro Definition Documentation

◆ fatal_error

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

◆ FRONTEND

#define FRONTEND   1

Definition at line 12 of file pg_waldump.c.

◆ MAX_XLINFO_TYPES

#define MAX_XLINFO_TYPES   16

Definition at line 64 of file pg_waldump.c.

Referenced by XLogDumpDisplayStats().

Typedef Documentation

◆ Stats

typedef struct Stats Stats

◆ XLogDumpConfig

◆ XLogDumpPrivate

◆ XLogDumpStats

typedef struct XLogDumpStats XLogDumpStats

Function Documentation

◆ identify_target_directory()

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

Definition at line 240 of file pg_waldump.c.

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

Referenced by main().

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

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 798 of file pg_waldump.c.

References _, XLogDumpConfig::already_displayed_records, XLogDumpConfig::bkp_details, close, directory, XLogDumpPrivate::endptr, EXIT_FAILURE, EXIT_SUCCESS, fatal_error, fd(), XLogDumpConfig::filter_by_rmgr, XLogDumpConfig::filter_by_xid, XLogDumpConfig::filter_by_xid_enabled, XLogDumpConfig::follow, fprintf, get_progname(), getopt_long(), i, identify_target_directory(), InvalidTransactionId, InvalidXLogRecPtr, ngettext, no_argument, open_file_in_directory(), optarg, optind, optional_argument, pg_log_error, pg_logging_init(), pg_strcasecmp(), pg_strdup(), PG_TEXTDOMAIN, pg_usleep(), print_rmgr_list(), printf, progname, 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, strerror, XLogDumpPrivate::timeline, usage(), verify_directory(), WalSegSz, XLogRecord::xl_rmid, XLogRecord::xl_xid, XLByteInSeg, XLogDumpCountRecord(), XLogDumpDisplayRecord(), XLogDumpDisplayStats(), XLogDumpReadPage(), XLogFromFileName, XLogReaderAllocate(), XLogReaderFree(), XLogReadRecord(), XLogRecPtrIsInvalid, XLogSegmentOffset, and XLogSegNoOffsetToRecPtr.

799 {
800  uint32 xlogid;
801  uint32 xrecoff;
802  XLogReaderState *xlogreader_state;
803  XLogDumpPrivate private;
804  XLogDumpConfig config;
805  XLogDumpStats stats;
806  XLogRecord *record;
807  XLogRecPtr first_record;
808  char *waldir = NULL;
809  char *errormsg;
810 
811  static struct option long_options[] = {
812  {"bkp-details", no_argument, NULL, 'b'},
813  {"end", required_argument, NULL, 'e'},
814  {"follow", no_argument, NULL, 'f'},
815  {"help", no_argument, NULL, '?'},
816  {"limit", required_argument, NULL, 'n'},
817  {"path", required_argument, NULL, 'p'},
818  {"rmgr", required_argument, NULL, 'r'},
819  {"start", required_argument, NULL, 's'},
820  {"timeline", required_argument, NULL, 't'},
821  {"xid", required_argument, NULL, 'x'},
822  {"version", no_argument, NULL, 'V'},
823  {"stats", optional_argument, NULL, 'z'},
824  {NULL, 0, NULL, 0}
825  };
826 
827  int option;
828  int optindex = 0;
829 
830  pg_logging_init(argv[0]);
831  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_waldump"));
832  progname = get_progname(argv[0]);
833 
834  if (argc > 1)
835  {
836  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
837  {
838  usage();
839  exit(0);
840  }
841  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
842  {
843  puts("pg_waldump (PostgreSQL) " PG_VERSION);
844  exit(0);
845  }
846  }
847 
848  memset(&private, 0, sizeof(XLogDumpPrivate));
849  memset(&config, 0, sizeof(XLogDumpConfig));
850  memset(&stats, 0, sizeof(XLogDumpStats));
851 
852  private.timeline = 1;
853  private.startptr = InvalidXLogRecPtr;
854  private.endptr = InvalidXLogRecPtr;
855  private.endptr_reached = false;
856 
857  config.bkp_details = false;
858  config.stop_after_records = -1;
859  config.already_displayed_records = 0;
860  config.follow = false;
861  config.filter_by_rmgr = -1;
863  config.filter_by_xid_enabled = false;
864  config.stats = false;
865  config.stats_per_record = false;
866 
867  if (argc <= 1)
868  {
869  pg_log_error("no arguments specified");
870  goto bad_argument;
871  }
872 
873  while ((option = getopt_long(argc, argv, "be:fn:p:r:s:t:x:z",
874  long_options, &optindex)) != -1)
875  {
876  switch (option)
877  {
878  case 'b':
879  config.bkp_details = true;
880  break;
881  case 'e':
882  if (sscanf(optarg, "%X/%X", &xlogid, &xrecoff) != 2)
883  {
884  pg_log_error("could not parse end WAL location \"%s\"",
885  optarg);
886  goto bad_argument;
887  }
888  private.endptr = (uint64) xlogid << 32 | xrecoff;
889  break;
890  case 'f':
891  config.follow = true;
892  break;
893  case 'n':
894  if (sscanf(optarg, "%d", &config.stop_after_records) != 1)
895  {
896  pg_log_error("could not parse limit \"%s\"", optarg);
897  goto bad_argument;
898  }
899  break;
900  case 'p':
901  waldir = pg_strdup(optarg);
902  break;
903  case 'r':
904  {
905  int i;
906 
907  if (pg_strcasecmp(optarg, "list") == 0)
908  {
909  print_rmgr_list();
910  exit(EXIT_SUCCESS);
911  }
912 
913  for (i = 0; i <= RM_MAX_ID; i++)
914  {
915  if (pg_strcasecmp(optarg, RmgrDescTable[i].rm_name) == 0)
916  {
917  config.filter_by_rmgr = i;
918  break;
919  }
920  }
921 
922  if (config.filter_by_rmgr == -1)
923  {
924  pg_log_error("resource manager \"%s\" does not exist",
925  optarg);
926  goto bad_argument;
927  }
928  }
929  break;
930  case 's':
931  if (sscanf(optarg, "%X/%X", &xlogid, &xrecoff) != 2)
932  {
933  pg_log_error("could not parse start WAL location \"%s\"",
934  optarg);
935  goto bad_argument;
936  }
937  else
938  private.startptr = (uint64) xlogid << 32 | xrecoff;
939  break;
940  case 't':
941  if (sscanf(optarg, "%d", &private.timeline) != 1)
942  {
943  pg_log_error("could not parse timeline \"%s\"", optarg);
944  goto bad_argument;
945  }
946  break;
947  case 'x':
948  if (sscanf(optarg, "%u", &config.filter_by_xid) != 1)
949  {
950  pg_log_error("could not parse \"%s\" as a transaction ID",
951  optarg);
952  goto bad_argument;
953  }
954  config.filter_by_xid_enabled = true;
955  break;
956  case 'z':
957  config.stats = true;
958  config.stats_per_record = false;
959  if (optarg)
960  {
961  if (strcmp(optarg, "record") == 0)
962  config.stats_per_record = true;
963  else if (strcmp(optarg, "rmgr") != 0)
964  {
965  pg_log_error("unrecognized argument to --stats: %s",
966  optarg);
967  goto bad_argument;
968  }
969  }
970  break;
971  default:
972  goto bad_argument;
973  }
974  }
975 
976  if ((optind + 2) < argc)
977  {
978  pg_log_error("too many command-line arguments (first is \"%s\")",
979  argv[optind + 2]);
980  goto bad_argument;
981  }
982 
983  if (waldir != NULL)
984  {
985  /* validate path points to directory */
986  if (!verify_directory(waldir))
987  {
988  pg_log_error("path \"%s\" could not be opened: %s",
989  waldir, strerror(errno));
990  goto bad_argument;
991  }
992  }
993 
994  /* parse files as start/end boundaries, extract path if not specified */
995  if (optind < argc)
996  {
997  char *directory = NULL;
998  char *fname = NULL;
999  int fd;
1000  XLogSegNo segno;
1001 
1002  split_path(argv[optind], &directory, &fname);
1003 
1004  if (waldir == NULL && directory != NULL)
1005  {
1006  waldir = directory;
1007 
1008  if (!verify_directory(waldir))
1009  fatal_error("could not open directory \"%s\": %s",
1010  waldir, strerror(errno));
1011  }
1012 
1013  waldir = identify_target_directory(waldir, fname);
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, &segno, WalSegSz);
1021 
1022  if (XLogRecPtrIsInvalid(private.startptr))
1023  XLogSegNoOffsetToRecPtr(segno, 0, WalSegSz, private.startptr);
1024  else if (!XLByteInSeg(private.startptr, segno, WalSegSz))
1025  {
1026  pg_log_error("start WAL location %X/%X is not inside file \"%s\"",
1027  (uint32) (private.startptr >> 32),
1028  (uint32) private.startptr,
1029  fname);
1030  goto bad_argument;
1031  }
1032 
1033  /* no second file specified, set end position */
1034  if (!(optind + 1 < argc) && XLogRecPtrIsInvalid(private.endptr))
1035  XLogSegNoOffsetToRecPtr(segno + 1, 0, WalSegSz, private.endptr);
1036 
1037  /* parse ENDSEG if passed */
1038  if (optind + 1 < argc)
1039  {
1040  XLogSegNo endsegno;
1041 
1042  /* ignore directory, already have that */
1043  split_path(argv[optind + 1], &directory, &fname);
1044 
1045  fd = open_file_in_directory(waldir, fname);
1046  if (fd < 0)
1047  fatal_error("could not open file \"%s\"", fname);
1048  close(fd);
1049 
1050  /* parse position from file */
1051  XLogFromFileName(fname, &private.timeline, &endsegno, WalSegSz);
1052 
1053  if (endsegno < segno)
1054  fatal_error("ENDSEG %s is before STARTSEG %s",
1055  argv[optind + 1], argv[optind]);
1056 
1057  if (XLogRecPtrIsInvalid(private.endptr))
1058  XLogSegNoOffsetToRecPtr(endsegno + 1, 0, WalSegSz,
1059  private.endptr);
1060 
1061  /* set segno to endsegno for check of --end */
1062  segno = endsegno;
1063  }
1064 
1065 
1066  if (!XLByteInSeg(private.endptr, segno, WalSegSz) &&
1067  private.endptr != (segno + 1) * WalSegSz)
1068  {
1069  pg_log_error("end WAL location %X/%X is not inside file \"%s\"",
1070  (uint32) (private.endptr >> 32),
1071  (uint32) private.endptr,
1072  argv[argc - 1]);
1073  goto bad_argument;
1074  }
1075  }
1076  else
1077  waldir = identify_target_directory(waldir, NULL);
1078 
1079  /* we don't know what to print */
1080  if (XLogRecPtrIsInvalid(private.startptr))
1081  {
1082  pg_log_error("no start WAL location given");
1083  goto bad_argument;
1084  }
1085 
1086  /* done with argument parsing, do the actual work */
1087 
1088  /* we have everything we need, start reading */
1089  xlogreader_state = XLogReaderAllocate(WalSegSz, waldir, XLogDumpReadPage,
1090  &private);
1091  if (!xlogreader_state)
1092  fatal_error("out of memory");
1093 
1094  /* first find a valid recptr to start from */
1095  first_record = XLogFindNextRecord(xlogreader_state, private.startptr);
1096 
1097  if (first_record == InvalidXLogRecPtr)
1098  fatal_error("could not find a valid record after %X/%X",
1099  (uint32) (private.startptr >> 32),
1100  (uint32) private.startptr);
1101 
1102  /*
1103  * Display a message that we're skipping data if `from` wasn't a pointer
1104  * to the start of a record and also wasn't a pointer to the beginning of
1105  * a segment (e.g. we were used in file mode).
1106  */
1107  if (first_record != private.startptr &&
1108  XLogSegmentOffset(private.startptr, WalSegSz) != 0)
1109  printf(ngettext("first record is after %X/%X, at %X/%X, skipping over %u byte\n",
1110  "first record is after %X/%X, at %X/%X, skipping over %u bytes\n",
1111  (first_record - private.startptr)),
1112  (uint32) (private.startptr >> 32), (uint32) private.startptr,
1113  (uint32) (first_record >> 32), (uint32) first_record,
1114  (uint32) (first_record - private.startptr));
1115 
1116  for (;;)
1117  {
1118  /* try to read the next record */
1119  record = XLogReadRecord(xlogreader_state, first_record, &errormsg);
1120  if (!record)
1121  {
1122  if (!config.follow || private.endptr_reached)
1123  break;
1124  else
1125  {
1126  pg_usleep(1000000L); /* 1 second */
1127  continue;
1128  }
1129  }
1130 
1131  /* after reading the first record, continue at next one */
1132  first_record = InvalidXLogRecPtr;
1133 
1134  /* apply all specified filters */
1135  if (config.filter_by_rmgr != -1 &&
1136  config.filter_by_rmgr != record->xl_rmid)
1137  continue;
1138 
1139  if (config.filter_by_xid_enabled &&
1140  config.filter_by_xid != record->xl_xid)
1141  continue;
1142 
1143  /* process the record */
1144  if (config.stats == true)
1145  XLogDumpCountRecord(&config, &stats, xlogreader_state);
1146  else
1147  XLogDumpDisplayRecord(&config, xlogreader_state);
1148 
1149  /* check whether we printed enough */
1150  config.already_displayed_records++;
1151  if (config.stop_after_records > 0 &&
1153  break;
1154  }
1155 
1156  if (config.stats == true)
1157  XLogDumpDisplayStats(&config, &stats);
1158 
1159  if (errormsg)
1160  fatal_error("error in WAL record at %X/%X: %s",
1161  (uint32) (xlogreader_state->ReadRecPtr >> 32),
1162  (uint32) xlogreader_state->ReadRecPtr,
1163  errormsg);
1164 
1165  XLogReaderFree(xlogreader_state);
1166 
1167  return EXIT_SUCCESS;
1168 
1169 bad_argument:
1170  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
1171  return EXIT_FAILURE;
1172 }
#define fatal_error(...)
Definition: pg_waldump.c:73
static void usage(void)
Definition: pg_waldump.c:770
static void XLogDumpDisplayStats(XLogDumpConfig *config, XLogDumpStats *stats)
Definition: pg_waldump.c:655
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int already_displayed_records
Definition: pg_waldump.c:46
#define EXIT_SUCCESS
Definition: settings.h:150
int filter_by_rmgr
Definition: pg_waldump.c:52
const char * get_progname(const char *argv0)
Definition: path.c:453
#define pg_log_error(...)
Definition: logging.h:79
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
void pg_logging_init(const char *argv0)
Definition: logging.c:39
RmgrId xl_rmid
Definition: xlogrecord.h:47
#define printf(...)
Definition: port.h:198
TransactionId filter_by_xid
Definition: pg_waldump.c:53
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
bool bkp_details
Definition: pg_waldump.c:44
#define fprintf
Definition: port.h:196
static char * identify_target_directory(char *directory, char *fname)
Definition: pg_waldump.c:240
static int fd(const char *x, int i)
Definition: preproc-init.c:105
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:237
#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes)
bool filter_by_xid_enabled
Definition: pg_waldump.c:54
void pg_usleep(long microsec)
Definition: signal.c:53
#define required_argument
Definition: getopt_long.h:25
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 XLogDumpCountRecord(XLogDumpConfig *config, XLogDumpStats *stats, XLogReaderState *record)
Definition: pg_waldump.c:468
uint64 XLogSegNo
Definition: xlogdefs.h:41
static void XLogDumpDisplayRecord(XLogDumpConfig *config, XLogReaderState *record)
Definition: pg_waldump.c:506
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:132
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define InvalidTransactionId
Definition: transam.h:31
static void split_path(const char *path, char **dir, char **fname)
Definition: pg_waldump.c:108
unsigned int uint32
Definition: c.h:358
static int XLogDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetPtr, char *readBuff)
Definition: pg_waldump.c:410
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:133
#define RM_MAX_ID
Definition: rmgr.h:33
#define no_argument
Definition: getopt_long.h:24
#define ngettext(s, p, n)
Definition: c.h:1103
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1135
static int WalSegSz
Definition: pg_waldump.c:31
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define strerror
Definition: port.h:205
static const char * directory
Definition: zic.c:622
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:91
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:71
TransactionId xl_xid
Definition: xlogrecord.h:44
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:565
static const char * progname
Definition: pg_waldump.c:29
char * optarg
Definition: getopt.c:52
int i
int stop_after_records
Definition: pg_waldump.c:45
#define EXIT_FAILURE
Definition: settings.h:154
static void print_rmgr_list(void)
Definition: pg_waldump.c:76
bool stats_per_record
Definition: pg_waldump.c:49
#define close(a)
Definition: win32.h:12
#define _(x)
Definition: elog.c:84
static XLogRecPtr startptr
Definition: basebackup.c:118

◆ 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, snprintf, and strerror.

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

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\": %s",
148  fname, strerror(errno));
149  return fd;
150 }
#define fatal_error(...)
Definition: pg_waldump.c:73
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1191
#define MAXPGPATH
#define Assert(condition)
Definition: c.h:732
#define strerror
Definition: port.h:205
static const char * directory
Definition: zic.c:622
#define snprintf
Definition: port.h:192

◆ print_rmgr_list()

static void print_rmgr_list ( void  )
static

Definition at line 76 of file pg_waldump.c.

References i, printf, RM_MAX_ID, and RmgrDescTable.

Referenced by main().

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

Referenced by identify_target_directory().

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

◆ split_path()

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

Definition at line 108 of file pg_waldump.c.

References pg_strdup().

Referenced by main().

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

◆ usage()

static void usage ( void  )
static

Definition at line 770 of file pg_waldump.c.

References _, printf, and progname.

Referenced by main().

771 {
772  printf(_("%s decodes and displays PostgreSQL write-ahead logs for debugging.\n\n"),
773  progname);
774  printf(_("Usage:\n"));
775  printf(_(" %s [OPTION]... [STARTSEG [ENDSEG]]\n"), progname);
776  printf(_("\nOptions:\n"));
777  printf(_(" -b, --bkp-details output detailed information about backup blocks\n"));
778  printf(_(" -e, --end=RECPTR stop reading at WAL location RECPTR\n"));
779  printf(_(" -f, --follow keep retrying after reaching end of WAL\n"));
780  printf(_(" -n, --limit=N number of records to display\n"));
781  printf(_(" -p, --path=PATH directory in which to find log segment files or a\n"
782  " directory with a ./pg_wal that contains such files\n"
783  " (default: current directory, ./pg_wal, $PGDATA/pg_wal)\n"));
784  printf(_(" -r, --rmgr=RMGR only show records generated by resource manager RMGR;\n"
785  " use --rmgr=list to list valid resource manager names\n"));
786  printf(_(" -s, --start=RECPTR start reading at WAL location RECPTR\n"));
787  printf(_(" -t, --timeline=TLI timeline from which to read log records\n"
788  " (default: 1 or the value used in STARTSEG)\n"));
789  printf(_(" -V, --version output version information, then exit\n"));
790  printf(_(" -x, --xid=XID only show records with transaction ID XID\n"));
791  printf(_(" -z, --stats[=record] show statistics instead of records\n"
792  " (optionally, show per-record statistics)\n"));
793  printf(_(" -?, --help show this help, then exit\n"));
794  printf(_("\nReport bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
795 }
#define printf(...)
Definition: port.h:198
static const char * progname
Definition: pg_waldump.c:29
#define _(x)
Definition: elog.c:84

◆ verify_directory()

static bool verify_directory ( const char *  directory)
static

Definition at line 91 of file pg_waldump.c.

References closedir(), and opendir().

Referenced by main().

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

◆ XLogDumpCountRecord()

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

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

470 {
471  RmgrId rmid;
472  uint8 recid;
473  uint32 rec_len;
474  uint32 fpi_len;
475 
476  stats->count++;
477 
478  rmid = XLogRecGetRmid(record);
479 
480  XLogDumpRecordLen(record, &rec_len, &fpi_len);
481 
482  /* Update per-rmgr statistics */
483 
484  stats->rmgr_stats[rmid].count++;
485  stats->rmgr_stats[rmid].rec_len += rec_len;
486  stats->rmgr_stats[rmid].fpi_len += fpi_len;
487 
488  /*
489  * Update per-record statistics, where the record is identified by a
490  * combination of the RmgrId and the four bits of the xl_info field that
491  * are the rmgr's domain (resulting in sixteen possible entries per
492  * RmgrId).
493  */
494 
495  recid = XLogRecGetInfo(record) >> 4;
496 
497  stats->record_stats[rmid][recid].count++;
498  stats->record_stats[rmid][recid].rec_len += rec_len;
499  stats->record_stats[rmid][recid].fpi_len += fpi_len;
500 }
uint64 count
Definition: pg_waldump.c:59
uint64 count
Definition: pg_waldump.c:68
unsigned char uint8
Definition: c.h:356
Stats record_stats[RM_NEXT_ID][MAX_XLINFO_TYPES]
Definition: pg_waldump.c:70
Stats rmgr_stats[RM_NEXT_ID]
Definition: pg_waldump.c:69
unsigned int uint32
Definition: c.h:358
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:242
uint8 RmgrId
Definition: rmgr.h:11
uint64 fpi_len
Definition: pg_waldump.c:61
uint64 rec_len
Definition: pg_waldump.c:60
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:243
static void XLogDumpRecordLen(XLogReaderState *record, uint32 *rec_len, uint32 *fpi_len)
Definition: pg_waldump.c:439

◆ XLogDumpDisplayRecord()

static void XLogDumpDisplayRecord ( XLogDumpConfig config,
XLogReaderState record 
)
static

Definition at line 506 of file pg_waldump.c.

References DecodedBkpBlock::bimg_info, DecodedBkpBlock::bimg_len, XLogDumpConfig::bkp_details, BKPIMAGE_IS_COMPRESSED, XLogReaderState::blocks, RelFileNode::dbNode, forkNames, DecodedBkpBlock::hole_length, DecodedBkpBlock::hole_offset, MAIN_FORKNUM, XLogReaderState::max_block_id, printf, psprintf(), 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().

507 {
508  const char *id;
509  const RmgrDescData *desc = &RmgrDescTable[XLogRecGetRmid(record)];
510  uint32 rec_len;
511  uint32 fpi_len;
512  RelFileNode rnode;
513  ForkNumber forknum;
514  BlockNumber blk;
515  int block_id;
516  uint8 info = XLogRecGetInfo(record);
517  XLogRecPtr xl_prev = XLogRecGetPrev(record);
518 
519  XLogDumpRecordLen(record, &rec_len, &fpi_len);
520 
521  id = desc->rm_identify(info);
522  if (id == NULL)
523  id = psprintf("UNKNOWN (%x)", info & ~XLR_INFO_MASK);
524 
525  printf("rmgr: %-11s len (rec/tot): %6u/%6u, tx: %10u, lsn: %X/%08X, prev %X/%08X, ",
526  desc->rm_name,
527  rec_len, XLogRecGetTotalLen(record),
528  XLogRecGetXid(record),
529  (uint32) (record->ReadRecPtr >> 32), (uint32) record->ReadRecPtr,
530  (uint32) (xl_prev >> 32), (uint32) xl_prev);
531  printf("desc: %s ", id);
532 
533  /* the desc routine will printf the description directly to stdout */
534  desc->rm_desc(NULL, record);
535 
536  if (!config->bkp_details)
537  {
538  /* print block references (short format) */
539  for (block_id = 0; block_id <= record->max_block_id; block_id++)
540  {
541  if (!XLogRecHasBlockRef(record, block_id))
542  continue;
543 
544  XLogRecGetBlockTag(record, block_id, &rnode, &forknum, &blk);
545  if (forknum != MAIN_FORKNUM)
546  printf(", blkref #%u: rel %u/%u/%u fork %s blk %u",
547  block_id,
548  rnode.spcNode, rnode.dbNode, rnode.relNode,
549  forkNames[forknum],
550  blk);
551  else
552  printf(", blkref #%u: rel %u/%u/%u blk %u",
553  block_id,
554  rnode.spcNode, rnode.dbNode, rnode.relNode,
555  blk);
556  if (XLogRecHasBlockImage(record, block_id))
557  {
558  if (XLogRecBlockImageApply(record, block_id))
559  printf(" FPW");
560  else
561  printf(" FPW for WAL verification");
562  }
563  }
564  putchar('\n');
565  }
566  else
567  {
568  /* print block references (detailed format) */
569  putchar('\n');
570  for (block_id = 0; block_id <= record->max_block_id; block_id++)
571  {
572  if (!XLogRecHasBlockRef(record, block_id))
573  continue;
574 
575  XLogRecGetBlockTag(record, block_id, &rnode, &forknum, &blk);
576  printf("\tblkref #%u: rel %u/%u/%u fork %s blk %u",
577  block_id,
578  rnode.spcNode, rnode.dbNode, rnode.relNode,
579  forkNames[forknum],
580  blk);
581  if (XLogRecHasBlockImage(record, block_id))
582  {
583  if (record->blocks[block_id].bimg_info &
585  {
586  printf(" (FPW%s); hole: offset: %u, length: %u, "
587  "compression saved: %u\n",
588  XLogRecBlockImageApply(record, block_id) ?
589  "" : " for WAL verification",
590  record->blocks[block_id].hole_offset,
591  record->blocks[block_id].hole_length,
592  BLCKSZ -
593  record->blocks[block_id].hole_length -
594  record->blocks[block_id].bimg_len);
595  }
596  else
597  {
598  printf(" (FPW%s); hole: offset: %u, length: %u\n",
599  XLogRecBlockImageApply(record, block_id) ?
600  "" : " for WAL verification",
601  record->blocks[block_id].hole_offset,
602  record->blocks[block_id].hole_length);
603  }
604  }
605  putchar('\n');
606  }
607  }
608 }
#define XLogRecHasBlockImage(decoder, block_id)
Definition: xlogreader.h:251
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
uint16 hole_offset
Definition: xlogreader.h:76
unsigned char uint8
Definition: c.h:356
#define XLogRecHasBlockRef(decoder, block_id)
Definition: xlogreader.h:249
#define printf(...)
Definition: port.h:198
uint32 BlockNumber
Definition: block.h:31
bool bkp_details
Definition: pg_waldump.c:44
uint16 bimg_len
Definition: xlogreader.h:78
#define XLogRecGetTotalLen(decoder)
Definition: xlogreader.h:240
uint16 hole_length
Definition: xlogreader.h:77
const RmgrDescData RmgrDescTable[RM_MAX_ID+1]
Definition: rmgrdesc.c:38
#define XLogRecGetPrev(decoder)
Definition: xlogreader.h:241
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:132
unsigned int uint32
Definition: c.h:358
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:242
ForkNumber
Definition: relpath.h:40
const char * rm_name
Definition: rmgrdesc.h:16
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1367
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:244
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:147
const char *(* rm_identify)(uint8 info)
Definition: rmgrdesc.h:18
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
void(* rm_desc)(StringInfo buf, XLogReaderState *record)
Definition: rmgrdesc.h:17
#define XLogRecBlockImageApply(decoder, block_id)
Definition: xlogreader.h:253
const char *const forkNames[]
Definition: relpath.c:33
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:152
#define XLogRecGetRmid(decoder)
Definition: xlogreader.h:243
static void XLogDumpRecordLen(XLogReaderState *record, uint32 *rec_len, uint32 *fpi_len)
Definition: pg_waldump.c:439

◆ XLogDumpDisplayStats()

static void XLogDumpDisplayStats ( XLogDumpConfig config,
XLogDumpStats stats 
)
static

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

656 {
657  int ri,
658  rj;
659  uint64 total_count = 0;
660  uint64 total_rec_len = 0;
661  uint64 total_fpi_len = 0;
662  uint64 total_len = 0;
663  double rec_len_pct,
664  fpi_len_pct;
665 
666  /* ---
667  * Make a first pass to calculate column totals:
668  * count(*),
669  * sum(xl_len+SizeOfXLogRecord),
670  * sum(xl_tot_len-xl_len-SizeOfXLogRecord), and
671  * sum(xl_tot_len).
672  * These are used to calculate percentages for each record type.
673  * ---
674  */
675 
676  for (ri = 0; ri < RM_NEXT_ID; ri++)
677  {
678  total_count += stats->rmgr_stats[ri].count;
679  total_rec_len += stats->rmgr_stats[ri].rec_len;
680  total_fpi_len += stats->rmgr_stats[ri].fpi_len;
681  }
682  total_len = total_rec_len + total_fpi_len;
683 
684  /*
685  * 27 is strlen("Transaction/COMMIT_PREPARED"), 20 is strlen(2^64), 8 is
686  * strlen("(100.00%)")
687  */
688 
689  printf("%-27s %20s %8s %20s %8s %20s %8s %20s %8s\n"
690  "%-27s %20s %8s %20s %8s %20s %8s %20s %8s\n",
691  "Type", "N", "(%)", "Record size", "(%)", "FPI size", "(%)", "Combined size", "(%)",
692  "----", "-", "---", "-----------", "---", "--------", "---", "-------------", "---");
693 
694  for (ri = 0; ri < RM_NEXT_ID; ri++)
695  {
696  uint64 count,
697  rec_len,
698  fpi_len,
699  tot_len;
700  const RmgrDescData *desc = &RmgrDescTable[ri];
701 
702  if (!config->stats_per_record)
703  {
704  count = stats->rmgr_stats[ri].count;
705  rec_len = stats->rmgr_stats[ri].rec_len;
706  fpi_len = stats->rmgr_stats[ri].fpi_len;
707  tot_len = rec_len + fpi_len;
708 
710  count, total_count, rec_len, total_rec_len,
711  fpi_len, total_fpi_len, tot_len, total_len);
712  }
713  else
714  {
715  for (rj = 0; rj < MAX_XLINFO_TYPES; rj++)
716  {
717  const char *id;
718 
719  count = stats->record_stats[ri][rj].count;
720  rec_len = stats->record_stats[ri][rj].rec_len;
721  fpi_len = stats->record_stats[ri][rj].fpi_len;
722  tot_len = rec_len + fpi_len;
723 
724  /* Skip undefined combinations and ones that didn't occur */
725  if (count == 0)
726  continue;
727 
728  /* the upper four bits in xl_info are the rmgr's */
729  id = desc->rm_identify(rj << 4);
730  if (id == NULL)
731  id = psprintf("UNKNOWN (%x)", rj << 4);
732 
733  XLogDumpStatsRow(psprintf("%s/%s", desc->rm_name, id),
734  count, total_count, rec_len, total_rec_len,
735  fpi_len, total_fpi_len, tot_len, total_len);
736  }
737  }
738  }
739 
740  printf("%-27s %20s %8s %20s %8s %20s %8s %20s\n",
741  "", "--------", "", "--------", "", "--------", "", "--------");
742 
743  /*
744  * The percentages in earlier rows were calculated against the column
745  * total, but the ones that follow are against the row total. Note that
746  * these are displayed with a % symbol to differentiate them from the
747  * earlier ones, and are thus up to 9 characters long.
748  */
749 
750  rec_len_pct = 0;
751  if (total_len != 0)
752  rec_len_pct = 100 * (double) total_rec_len / total_len;
753 
754  fpi_len_pct = 0;
755  if (total_len != 0)
756  fpi_len_pct = 100 * (double) total_fpi_len / total_len;
757 
758  printf("%-27s "
759  "%20" INT64_MODIFIER "u %-9s"
760  "%20" INT64_MODIFIER "u %-9s"
761  "%20" INT64_MODIFIER "u %-9s"
762  "%20" INT64_MODIFIER "u %-6s\n",
763  "Total", stats->count, "",
764  total_rec_len, psprintf("[%.02f%%]", rec_len_pct),
765  total_fpi_len, psprintf("[%.02f%%]", fpi_len_pct),
766  total_len, "[100%]");
767 }
uint64 count
Definition: pg_waldump.c:59
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
uint64 count
Definition: pg_waldump.c:68
Stats record_stats[RM_NEXT_ID][MAX_XLINFO_TYPES]
Definition: pg_waldump.c:70
#define printf(...)
Definition: port.h:198
Stats rmgr_stats[RM_NEXT_ID]
Definition: pg_waldump.c:69
const RmgrDescData RmgrDescTable[RM_MAX_ID+1]
Definition: rmgrdesc.c:38
const char * rm_name
Definition: rmgrdesc.h:16
const char *(* rm_identify)(uint8 info)
Definition: rmgrdesc.h:18
#define MAX_XLINFO_TYPES
Definition: pg_waldump.c:64
uint64 fpi_len
Definition: pg_waldump.c:61
uint64 rec_len
Definition: pg_waldump.c:60
static void XLogDumpStatsRow(const char *name, uint64 n, uint64 total_count, uint64 rec_len, uint64 total_rec_len, uint64 fpi_len, uint64 total_fpi_len, uint64 tot_len, uint64 total_len)
Definition: pg_waldump.c:614
bool stats_per_record
Definition: pg_waldump.c:49

◆ XLogDumpReadPage()

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

Definition at line 410 of file pg_waldump.c.

References XLogDumpPrivate::endptr, InvalidXLogRecPtr, XLogReaderState::private_data, XLogReaderState::segcxt, WALSegmentContext::ws_dir, and XLogDumpXLogRead().

Referenced by main().

412 {
413  XLogDumpPrivate *private = state->private_data;
414  int count = XLOG_BLCKSZ;
415 
416  if (private->endptr != InvalidXLogRecPtr)
417  {
418  if (targetPagePtr + XLOG_BLCKSZ <= private->endptr)
419  count = XLOG_BLCKSZ;
420  else if (targetPagePtr + reqLen <= private->endptr)
421  count = private->endptr - targetPagePtr;
422  else
423  {
424  private->endptr_reached = true;
425  return -1;
426  }
427  }
428 
429  XLogDumpXLogRead(state->segcxt.ws_dir, private->timeline, targetPagePtr,
430  readBuff, count);
431 
432  return count;
433 }
char ws_dir[MAXPGPATH]
Definition: xlogreader.h:46
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void * private_data
Definition: xlogreader.h:126
static void XLogDumpXLogRead(const char *directory, TimeLineID timeline_id, XLogRecPtr startptr, char *buf, Size count)
Definition: pg_waldump.c:290
WALSegmentContext segcxt
Definition: xlogreader.h:169

◆ XLogDumpRecordLen()

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

Definition at line 439 of file pg_waldump.c.

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

Referenced by XLogDumpCountRecord(), and XLogDumpDisplayRecord().

440 {
441  int block_id;
442 
443  /*
444  * Calculate the amount of FPI data in the record.
445  *
446  * XXX: We peek into xlogreader's private decoded backup blocks for the
447  * bimg_len indicating the length of FPI data. It doesn't seem worth it to
448  * add an accessor macro for this.
449  */
450  *fpi_len = 0;
451  for (block_id = 0; block_id <= record->max_block_id; block_id++)
452  {
453  if (XLogRecHasBlockImage(record, block_id))
454  *fpi_len += record->blocks[block_id].bimg_len;
455  }
456 
457  /*
458  * Calculate the length of the record as the total length - the length of
459  * all the block images.
460  */
461  *rec_len = XLogRecGetTotalLen(record) - *fpi_len;
462 }
#define XLogRecHasBlockImage(decoder, block_id)
Definition: xlogreader.h:251
uint16 bimg_len
Definition: xlogreader.h:78
#define XLogRecGetTotalLen(decoder)
Definition: xlogreader.h:240
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:152

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

References printf.

Referenced by XLogDumpDisplayStats().

619 {
620  double n_pct,
621  rec_len_pct,
622  fpi_len_pct,
623  tot_len_pct;
624 
625  n_pct = 0;
626  if (total_count != 0)
627  n_pct = 100 * (double) n / total_count;
628 
629  rec_len_pct = 0;
630  if (total_rec_len != 0)
631  rec_len_pct = 100 * (double) rec_len / total_rec_len;
632 
633  fpi_len_pct = 0;
634  if (total_fpi_len != 0)
635  fpi_len_pct = 100 * (double) fpi_len / total_fpi_len;
636 
637  tot_len_pct = 0;
638  if (total_len != 0)
639  tot_len_pct = 100 * (double) tot_len / total_len;
640 
641  printf("%-27s "
642  "%20" INT64_MODIFIER "u (%6.02f) "
643  "%20" INT64_MODIFIER "u (%6.02f) "
644  "%20" INT64_MODIFIER "u (%6.02f) "
645  "%20" INT64_MODIFIER "u (%6.02f)\n",
646  name, n, n_pct, rec_len, rec_len_pct, fpi_len, fpi_len_pct,
647  tot_len, tot_len_pct);
648 }
#define printf(...)
Definition: port.h:198
const char * name
Definition: encode.c:521

◆ XLogDumpXLogRead()

static void XLogDumpXLogRead ( const char *  directory,
TimeLineID  timeline_id,
XLogRecPtr  startptr,
char *  buf,
Size  count 
)
static

Definition at line 290 of file pg_waldump.c.

References buf, close, fatal_error, MAXFNAMELEN, MAXPGPATH, open_file_in_directory(), pg_usleep(), read, sendFile(), XLogDumpPrivate::startptr, strerror, WalSegSz, XLByteInSeg, XLByteToSeg, XLogFileName, and XLogSegmentOffset.

Referenced by XLogDumpReadPage().

292 {
293  char *p;
294  XLogRecPtr recptr;
295  Size nbytes;
296 
297  static int sendFile = -1;
298  static XLogSegNo sendSegNo = 0;
299  static uint32 sendOff = 0;
300 
301  p = buf;
302  recptr = startptr;
303  nbytes = count;
304 
305  while (nbytes > 0)
306  {
307  uint32 startoff;
308  int segbytes;
309  int readbytes;
310 
311  startoff = XLogSegmentOffset(recptr, WalSegSz);
312 
313  if (sendFile < 0 || !XLByteInSeg(recptr, sendSegNo, WalSegSz))
314  {
315  char fname[MAXFNAMELEN];
316  int tries;
317 
318  /* Switch to another logfile segment */
319  if (sendFile >= 0)
320  close(sendFile);
321 
322  XLByteToSeg(recptr, sendSegNo, WalSegSz);
323 
324  XLogFileName(fname, timeline_id, sendSegNo, WalSegSz);
325 
326  /*
327  * In follow mode there is a short period of time after the server
328  * has written the end of the previous file before the new file is
329  * available. So we loop for 5 seconds looking for the file to
330  * appear before giving up.
331  */
332  for (tries = 0; tries < 10; tries++)
333  {
334  sendFile = open_file_in_directory(directory, fname);
335  if (sendFile >= 0)
336  break;
337  if (errno == ENOENT)
338  {
339  int save_errno = errno;
340 
341  /* File not there yet, try again */
342  pg_usleep(500 * 1000);
343 
344  errno = save_errno;
345  continue;
346  }
347  /* Any other error, fall through and fail */
348  break;
349  }
350 
351  if (sendFile < 0)
352  fatal_error("could not find file \"%s\": %s",
353  fname, strerror(errno));
354  sendOff = 0;
355  }
356 
357  /* Need to seek in the file? */
358  if (sendOff != startoff)
359  {
360  if (lseek(sendFile, (off_t) startoff, SEEK_SET) < 0)
361  {
362  int err = errno;
363  char fname[MAXPGPATH];
364 
365  XLogFileName(fname, timeline_id, sendSegNo, WalSegSz);
366 
367  fatal_error("could not seek in log file %s to offset %u: %s",
368  fname, startoff, strerror(err));
369  }
370  sendOff = startoff;
371  }
372 
373  /* How many bytes are within this segment? */
374  if (nbytes > (WalSegSz - startoff))
375  segbytes = WalSegSz - startoff;
376  else
377  segbytes = nbytes;
378 
379  readbytes = read(sendFile, p, segbytes);
380  if (readbytes <= 0)
381  {
382  int err = errno;
383  char fname[MAXPGPATH];
384  int save_errno = errno;
385 
386  XLogFileName(fname, timeline_id, sendSegNo, WalSegSz);
387  errno = save_errno;
388 
389  if (readbytes < 0)
390  fatal_error("could not read from log file %s, offset %u, length %d: %s",
391  fname, sendOff, segbytes, strerror(err));
392  else if (readbytes == 0)
393  fatal_error("could not read from log file %s, offset %u: read %d of %zu",
394  fname, sendOff, readbytes, (Size) segbytes);
395  }
396 
397  /* Update state for read */
398  recptr += readbytes;
399 
400  sendOff += readbytes;
401  nbytes -= readbytes;
402  p += readbytes;
403  }
404 }
#define fatal_error(...)
Definition: pg_waldump.c:73
#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes)
void pg_usleep(long microsec)
Definition: signal.c:53
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:68
uint64 XLogSegNo
Definition: xlogdefs.h:41
unsigned int uint32
Definition: c.h:358
#define MAXFNAMELEN
static int WalSegSz
Definition: pg_waldump.c:31
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define strerror
Definition: port.h:205
size_t Size
Definition: c.h:466
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
static const char * directory
Definition: zic.c:622
static int open_file_in_directory(const char *directory, const char *fname)
Definition: pg_waldump.c:136
#define close(a)
Definition: win32.h:12
static bool sendFile(const char *readfilename, const char *tarfilename, struct stat *statbuf, bool missing_ok, Oid dboid)
Definition: basebackup.c:1370
#define read(a, b, c)
Definition: win32.h:13
static XLogRecPtr startptr
Definition: basebackup.c:118
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

Variable Documentation

◆ progname

const char* progname
static

Definition at line 29 of file pg_waldump.c.

Referenced by main(), and usage().

◆ WalSegSz

int WalSegSz
static

Definition at line 31 of file pg_waldump.c.

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