summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlvaro Herrera2013-02-22 19:46:24 +0000
committerAlvaro Herrera2013-02-22 19:56:55 +0000
commit639ed4e84b7493594860f56b78b25fd113e78fd7 (patch)
tree40488ddd0c7523bafcd0934fdf1e73a0432d1f82
parentc0c6acdfa055b0c76ea0d1defd4c2c0d5a5c256f (diff)
Add pg_xlogdump contrib program
This program relies on rm_desc backend routines and the xlogreader infrastructure to emit human-readable rendering of WAL records. Author: Andres Freund, with many reworks by Álvaro Reviewed (in a much earlier version) by Peter Eisentraut
-rw-r--r--contrib/pg_xlogdump/Makefile32
-rw-r--r--contrib/pg_xlogdump/compat.c94
-rw-r--r--contrib/pg_xlogdump/pg_xlogdump.c711
-rw-r--r--contrib/pg_xlogdump/rmgrdesc.c36
-rw-r--r--contrib/pg_xlogdump/rmgrdesc.h21
-rw-r--r--doc/src/sgml/contrib.sgml1
-rw-r--r--doc/src/sgml/filelist.sgml1
-rw-r--r--doc/src/sgml/pg_xlogdump.sgml205
-rw-r--r--doc/src/sgml/ref/pg_isready.sgml2
-rw-r--r--src/include/utils/palloc.h4
10 files changed, 1104 insertions, 3 deletions
diff --git a/contrib/pg_xlogdump/Makefile b/contrib/pg_xlogdump/Makefile
new file mode 100644
index 0000000000..f381967c97
--- /dev/null
+++ b/contrib/pg_xlogdump/Makefile
@@ -0,0 +1,32 @@
+# contrib/pg_xlogdump/Makefile
+
+PGFILEDESC = "pg_xlogdump"
+PGAPPICON=win32
+
+PROGRAM = pg_xlogdump
+OBJS = pg_xlogdump.o compat.o xlogreader.o rmgrdesc.o \
+ $(RMGRDESCOBJS) $(WIN32RES)
+
+RMGRDESCSOURCES = $(notdir $(wildcard $(top_srcdir)/src/backend/access/rmgrdesc/*desc.c))
+RMGRDESCOBJS = $(patsubst %.c,%.o,$(RMGRDESCSOURCES))
+
+EXTRA_CLEAN = $(RMGRDESCSOURCES) xlogreader.c rmgrdesc.c
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = contrib/pg_xlogdump
+top_builddir = ../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
+
+override CPPFLAGS := -DFRONTEND $(CPPFLAGS)
+
+rmgrdesc.c xlogreader.c: % : $(top_srcdir)/src/backend/access/transam/%
+ rm -f $@ && $(LN_S) $< .
+
+$(RMGRDESCSOURCES): % : $(top_srcdir)/src/backend/access/rmgrdesc/%
+ rm -f $@ && $(LN_S) $< .
diff --git a/contrib/pg_xlogdump/compat.c b/contrib/pg_xlogdump/compat.c
new file mode 100644
index 0000000000..1badababbb
--- /dev/null
+++ b/contrib/pg_xlogdump/compat.c
@@ -0,0 +1,94 @@
+/*-------------------------------------------------------------------------
+ *
+ * compat.c
+ * Reimplementations of various backend functions.
+ *
+ * Portions Copyright (c) 2012, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ * contrib/pg_xlogdump/compat.c
+ *
+ * This file contains client-side implementations for various backend
+ * functions that the rm_desc functions in *desc.c files rely on.
+ *
+ *-------------------------------------------------------------------------
+ */
+
+/* ugly hack, same as in e.g pg_controldata */
+#define FRONTEND 1
+#include "postgres.h"
+
+#include <time.h>
+
+#include "utils/datetime.h"
+#include "lib/stringinfo.h"
+
+/* copied from timestamp.c */
+pg_time_t
+timestamptz_to_time_t(TimestampTz t)
+{
+ pg_time_t result;
+
+#ifdef HAVE_INT64_TIMESTAMP
+ result = (pg_time_t) (t / USECS_PER_SEC +
+ ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY));
+#else
+ result = (pg_time_t) (t +
+ ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY));
+#endif
+ return result;
+}
+
+/*
+ * Stopgap implementation of timestamptz_to_str that doesn't depend on backend
+ * infrastructure.
+ *
+ * XXX: The backend timestamp infrastructure should instead be split out and
+ * moved into src/common.
+ */
+const char *
+timestamptz_to_str(TimestampTz dt)
+{
+ static char buf[MAXDATELEN + 1];
+ static char ts[MAXDATELEN + 1];
+ static char zone[MAXDATELEN + 1];
+ pg_time_t result = timestamptz_to_time_t(dt);
+ struct tm *ltime = localtime(&result);
+
+ strftime(ts, sizeof(zone), "%Y-%m-%d %H:%M:%S", ltime);
+ strftime(zone, sizeof(zone), "%Z", ltime);
+
+#ifdef HAVE_INT64_TIMESTAMP
+ sprintf(buf, "%s.%06d %s", ts, (int)(dt % USECS_PER_SEC), zone);
+#else
+ sprintf(buf, "%s.%.6f %s", ts, fabs(dt - floor(dt)), zone);
+#endif
+
+ return buf;
+}
+
+/*
+ * Provide a hacked up compat layer for StringInfos so xlog desc functions can
+ * be linked/called.
+ */
+void
+appendStringInfo(StringInfo str, const char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ vprintf(fmt, args);
+ va_end(args);
+}
+
+void
+appendStringInfoString(StringInfo str, const char *string)
+{
+ appendStringInfo(str, "%s", string);
+}
+
+void
+appendStringInfoChar(StringInfo str, char ch)
+{
+ appendStringInfo(str, "%c", ch);
+}
diff --git a/contrib/pg_xlogdump/pg_xlogdump.c b/contrib/pg_xlogdump/pg_xlogdump.c
new file mode 100644
index 0000000000..aef21898fb
--- /dev/null
+++ b/contrib/pg_xlogdump/pg_xlogdump.c
@@ -0,0 +1,711 @@
+/*-------------------------------------------------------------------------
+ *
+ * pg_xlogdump.c - decode and display WAL
+ *
+ * Copyright (c) 2012, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ * contrib/pg_xlogdump/pg_xlogdump.c
+ *-------------------------------------------------------------------------
+ */
+
+#define FRONTEND 1
+#include "postgres.h"
+
+#include <dirent.h>
+#include <unistd.h>
+
+#include "access/xlog.h"
+#include "access/xlogreader.h"
+#include "access/transam.h"
+#include "common/fe_memutils.h"
+#include "common/relpath.h"
+#include "getopt_long.h"
+#include "rmgrdesc.h"
+
+
+static const char *progname;
+
+typedef struct XLogDumpPrivate
+{
+ TimeLineID timeline;
+ char *inpath;
+ XLogRecPtr startptr;
+ XLogRecPtr endptr;
+} XLogDumpPrivate;
+
+typedef struct XLogDumpConfig
+{
+ /* display options */
+ bool bkp_details;
+ int stop_after_records;
+ int already_displayed_records;
+
+ /* filter options */
+ int filter_by_rmgr;
+ TransactionId filter_by_xid;
+ TransactionId filter_by_xid_enabled;
+} XLogDumpConfig;
+
+static void
+fatal_error(const char *fmt,...)
+__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
+
+/*
+ * Big red button to push when things go horribly wrong.
+ */
+static void
+fatal_error(const char *fmt,...)
+{
+ va_list args;
+
+ fflush(stdout);
+
+ fprintf(stderr, "%s: FATAL: ", progname);
+ va_start(args, fmt);
+ vfprintf(stderr, fmt, args);
+ va_end(args);
+ fputc('\n', stderr);
+
+ exit(EXIT_FAILURE);
+}
+
+static void
+print_rmgr_list(void)
+{
+ int i;
+
+ for (i = 0; i < RM_MAX_ID + 1; i++)
+ {
+ printf("%s\n", RmgrDescTable[i].rm_name);
+ }
+}
+
+/*
+ * Check whether directory exists and whether we can open it. Keep errno set so
+ * that the caller can report errors somewhat more accurately.
+ */
+static bool
+verify_directory(const char *directory)
+{
+ DIR *dir = opendir(directory);
+ if (dir == NULL)
+ return false;
+ closedir(dir);
+ return true;
+}
+
+/*
+ * Split a pathname as dirname(1) and basename(1) would.
+ *
+ * XXX this probably doesn't do very well on Windows. We probably need to
+ * apply canonicalize_path(), at the very least.
+ */
+static void
+split_path(const char *path, char **dir, char **fname)
+{
+ char *sep;
+
+ /* split filepath into directory & filename */
+ sep = strrchr(path, '/');
+
+ /* directory path */
+ if (sep != NULL)
+ {
+ *dir = pg_strdup(path);
+ (*dir)[(sep - path) + 1] = '\0'; /* no strndup */
+ *fname = pg_strdup(sep + 1);
+ }
+ /* local directory */
+ else
+ {
+ *dir = NULL;
+ *fname = pg_strdup(path);
+ }
+}
+
+/*
+ * Try to find the file in several places:
+ * if directory == NULL:
+ * fname
+ * XLOGDIR / fname
+ * $PGDATA / XLOGDIR / fname
+ * else
+ * directory / fname
+ * directory / XLOGDIR / fname
+ *
+ * return a read only fd
+ */
+static int
+fuzzy_open_file(const char *directory, const char *fname)
+{
+ int fd = -1;
+ char fpath[MAXPGPATH];
+
+ if (directory == NULL)
+ {
+ const char *datadir;
+
+ /* fname */
+ fd = open(fname, O_RDONLY | PG_BINARY, 0);
+ if (fd < 0 && errno != ENOENT)
+ return -1;
+ else if (fd > 0)
+ return fd;
+
+ /* XLOGDIR / fname */
+ snprintf(fpath, MAXPGPATH, "%s/%s",
+ XLOGDIR, fname);
+ fd = open(fpath, O_RDONLY | PG_BINARY, 0);
+ if (fd < 0 && errno != ENOENT)
+ return -1;
+ else if (fd > 0)
+ return fd;
+
+ datadir = getenv("PGDATA");
+ /* $PGDATA / XLOGDIR / fname */
+ if (datadir != NULL)
+ {
+ snprintf(fpath, MAXPGPATH, "%s/%s/%s",
+ datadir, XLOGDIR, fname);
+ fd = open(fpath, O_RDONLY | PG_BINARY, 0);
+ if (fd < 0 && errno != ENOENT)
+ return -1;
+ else if (fd > 0)
+ return fd;
+ }
+ }
+ else
+ {
+ /* directory / fname */
+ snprintf(fpath, MAXPGPATH, "%s/%s",
+ directory, fname);
+ fd = open(fpath, O_RDONLY | PG_BINARY, 0);
+ if (fd < 0 && errno != ENOENT)
+ return -1;
+ else if (fd > 0)
+ return fd;
+
+ /* directory / XLOGDIR / fname */
+ snprintf(fpath, MAXPGPATH, "%s/%s/%s",
+ directory, XLOGDIR, fname);
+ fd = open(fpath, O_RDONLY | PG_BINARY, 0);
+ if (fd < 0 && errno != ENOENT)
+ return -1;
+ else if (fd > 0)
+ return fd;
+ }
+ return -1;
+}
+
+/*
+ * Read count bytes from a segment file in the specified directory, for the
+ * given timeline, containing the specified record pointer; store the data in
+ * the passed buffer.
+ */
+static void
+XLogDumpXLogRead(const char *directory, TimeLineID timeline_id,
+ XLogRecPtr startptr, char *buf, Size count)
+{
+ char *p;
+ XLogRecPtr recptr;
+ Size nbytes;
+
+ static int sendFile = -1;
+ static XLogSegNo sendSegNo = 0;
+ static uint32 sendOff = 0;
+
+ p = buf;
+ recptr = startptr;
+ nbytes = count;
+
+ while (nbytes > 0)
+ {
+ uint32 startoff;
+ int segbytes;
+ int readbytes;
+
+ startoff = recptr % XLogSegSize;
+
+ if (sendFile < 0 || !XLByteInSeg(recptr, sendSegNo))
+ {
+ char fname[MAXFNAMELEN];
+
+ /* Switch to another logfile segment */
+ if (sendFile >= 0)
+ close(sendFile);
+
+ XLByteToSeg(recptr, sendSegNo);
+
+ XLogFileName(fname, timeline_id, sendSegNo);
+
+ sendFile = fuzzy_open_file(directory, fname);
+
+ if (sendFile < 0)
+ fatal_error("could not find file \"%s\": %s",
+ fname, strerror(errno));
+ sendOff = 0;
+ }
+
+ /* Need to seek in the file? */
+ if (sendOff != startoff)
+ {
+ if (lseek(sendFile, (off_t) startoff, SEEK_SET) < 0)
+ {
+ int err = errno;
+ char fname[MAXPGPATH];
+
+ XLogFileName(fname, timeline_id, sendSegNo);
+
+ fatal_error("could not seek in log segment %s to offset %u: %s",
+ fname, startoff, strerror(err));
+ }
+ sendOff = startoff;
+ }
+
+ /* How many bytes are within this segment? */
+ if (nbytes > (XLogSegSize - startoff))
+ segbytes = XLogSegSize - startoff;
+ else
+ segbytes = nbytes;
+
+ readbytes = read(sendFile, p, segbytes);
+ if (readbytes <= 0)
+ {
+ int err = errno;
+ char fname[MAXPGPATH];
+
+ XLogFileName(fname, timeline_id, sendSegNo);
+
+ fatal_error("could not read from log segment %s, offset %d, length %d: %s",
+ fname, sendOff, segbytes, strerror(err));
+ }
+
+ /* Update state for read */
+ recptr += readbytes;
+
+ sendOff += readbytes;
+ nbytes -= readbytes;
+ p += readbytes;
+ }
+}
+
+/*
+ * XLogReader read_page callback
+ */
+static int
+XLogDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen,
+ XLogRecPtr targetPtr, char *readBuff, TimeLineID *curFileTLI)
+{
+ XLogDumpPrivate *private = state->private_data;
+ int count = XLOG_BLCKSZ;
+
+ if (private->endptr != InvalidXLogRecPtr)
+ {
+ if (targetPagePtr + XLOG_BLCKSZ <= private->endptr)
+ count = XLOG_BLCKSZ;
+ else if (targetPagePtr + reqLen <= private->endptr)
+ count = private->endptr - targetPagePtr;
+ else
+ return -1;
+ }
+
+ XLogDumpXLogRead(private->inpath, private->timeline, targetPagePtr,
+ readBuff, count);
+
+ return count;
+}
+
+/*
+ * Print a record to stdout
+ */
+static void
+XLogDumpDisplayRecord(XLogDumpConfig *config, XLogRecPtr ReadRecPtr, XLogRecord *record)
+{
+ const RmgrDescData *desc = &RmgrDescTable[record->xl_rmid];
+
+ if (config->filter_by_rmgr != -1 &&
+ config->filter_by_rmgr != record->xl_rmid)
+ return;
+
+ if (config->filter_by_xid_enabled &&
+ config->filter_by_xid != record->xl_xid)
+ return;
+
+ config->already_displayed_records++;
+
+ printf("rmgr: %-11s len (rec/tot): %6u/%6u, tx: %10u, lsn: %X/%08X, prev %X/%08X, bkp: %u%u%u%u, desc: ",
+ desc->rm_name,
+ record->xl_len, record->xl_tot_len,
+ record->xl_xid,
+ (uint32) (ReadRecPtr >> 32), (uint32) ReadRecPtr,
+ (uint32) (record->xl_prev >> 32), (uint32) record->xl_prev,
+ !!(XLR_BKP_BLOCK(0) & record->xl_info),
+ !!(XLR_BKP_BLOCK(1) & record->xl_info),
+ !!(XLR_BKP_BLOCK(2) & record->xl_info),
+ !!(XLR_BKP_BLOCK(3) & record->xl_info));
+
+ /* the desc routine will printf the description directly to stdout */
+ desc->rm_desc(NULL, record->xl_info, XLogRecGetData(record));
+
+ putchar('\n');
+
+ if (config->bkp_details)
+ {
+ int bkpnum;
+ char *blk = (char *) XLogRecGetData(record) + record->xl_len;
+
+ for (bkpnum = 0; bkpnum < XLR_MAX_BKP_BLOCKS; bkpnum++)
+ {
+ BkpBlock bkpb;
+
+ if (!(XLR_BKP_BLOCK(bkpnum) & record->xl_info))
+ continue;
+
+ memcpy(&bkpb, blk, sizeof(BkpBlock));
+ blk += sizeof(BkpBlock);
+ blk += BLCKSZ - bkpb.hole_length;
+
+ printf("\tbackup bkp #%u; rel %u/%u/%u; fork: %s; block: %u; hole: offset: %u, length: %u\n",
+ bkpnum,
+ bkpb.node.spcNode, bkpb.node.dbNode, bkpb.node.relNode,
+ forkNames[bkpb.fork],
+ bkpb.block, bkpb.hole_offset, bkpb.hole_length);
+ }
+ }
+}
+
+static void
+usage(void)
+{
+ printf("%s decodes and displays PostgreSQL transaction logs for debugging.\n\n",
+ progname);
+ printf("Usage:\n");
+ printf(" %s [OPTION] [STARTSEG [ENDSEG]] \n", progname);
+ printf("\nGeneral options:\n");
+ printf(" -V, --version output version information, then exit\n");
+ printf(" -?, --help show this help, then exit\n");
+ printf("\nContent options:\n");
+ printf(" -b, --bkp-details output detailed information about backup blocks\n");
+ printf(" -e, --end=RECPTR stop reading at log position RECPTR\n");
+ printf(" -n, --limit=N number of records to display\n");
+ printf(" -p, --path=PATH directory in which to find log segment files\n");
+ printf(" (default: ./pg_xlog)\n");
+ printf(" -r, --rmgr=RMGR only show records generated by resource manager RMGR\n");
+ printf(" use --rmgr=list to list valid resource manager names\n");
+ printf(" -s, --start=RECPTR stop reading at log position RECPTR\n");
+ printf(" -t, --timeline=TLI timeline from which to read log records\n");
+ printf(" (default: 1 or the value used in STARTSEG)\n");
+ printf(" -x, --xid=XID only show records with TransactionId XID\n");
+}
+
+int
+main(int argc, char **argv)
+{
+ uint32 xlogid;
+ uint32 xrecoff;
+ XLogReaderState *xlogreader_state;
+ XLogDumpPrivate private;
+ XLogDumpConfig config;
+ XLogRecord *record;
+ XLogRecPtr first_record;
+ char *errormsg;
+
+ static struct option long_options[] = {
+ {"bkp-details", no_argument, NULL, 'b'},
+ {"end", required_argument, NULL, 'e'},
+ {"help", no_argument, NULL, '?'},
+ {"limit", required_argument, NULL, 'n'},
+ {"path", required_argument, NULL, 'p'},
+ {"rmgr", required_argument, NULL, 'r'},
+ {"start", required_argument, NULL, 's'},
+ {"timeline", required_argument, NULL, 't'},
+ {"xid", required_argument, NULL, 'x'},
+ {"version", no_argument, NULL, 'V'},
+ {NULL, 0, NULL, 0}
+ };
+
+ int option;
+ int optindex = 0;
+
+ progname = get_progname(argv[0]);
+
+ memset(&private, 0, sizeof(XLogDumpPrivate));
+ memset(&config, 0, sizeof(XLogDumpConfig));
+
+ private.timeline = 1;
+ private.startptr = InvalidXLogRecPtr;
+ private.endptr = InvalidXLogRecPtr;
+
+ config.bkp_details = false;
+ config.stop_after_records = -1;
+ config.already_displayed_records = 0;
+ config.filter_by_rmgr = -1;
+ config.filter_by_xid = InvalidTransactionId;
+ config.filter_by_xid_enabled = false;
+
+ if (argc <= 1)
+ {
+ fprintf(stderr, "%s: no arguments specified\n", progname);
+ goto bad_argument;
+ }
+
+ while ((option = getopt_long(argc, argv, "be:?n:p:r:s:t:Vx:",
+ long_options, &optindex)) != -1)
+ {
+ switch (option)
+ {
+ case 'b':
+ config.bkp_details = true;
+ break;
+ case 'e':
+ if (sscanf(optarg, "%X/%X", &xlogid, &xrecoff) != 2)
+ {
+ fprintf(stderr, "%s: could not parse end log position \"%s\"\n",
+ progname, optarg);
+ goto bad_argument;
+ }
+ private.endptr = (uint64) xlogid << 32 | xrecoff;
+ break;
+ case '?':
+ usage();
+ exit(EXIT_SUCCESS);
+ break;
+ case 'n':
+ if (sscanf(optarg, "%d", &config.stop_after_records) != 1)
+ {
+ fprintf(stderr, "%s: could not parse limit \"%s\"\n",
+ progname, optarg);
+ goto bad_argument;
+ }
+ break;
+ case 'p':
+ private.inpath = pg_strdup(optarg);
+ break;
+ case 'r':
+ {
+ int i;
+
+ if (pg_strcasecmp(optarg, "list") == 0)
+ {
+ print_rmgr_list();
+ exit(EXIT_SUCCESS);
+ }
+
+ for (i = 0; i < RM_MAX_ID; i++)
+ {
+ if (pg_strcasecmp(optarg, RmgrDescTable[i].rm_name) == 0)
+ {
+ config.filter_by_rmgr = i;
+ break;
+ }
+ }
+
+ if (config.filter_by_rmgr == -1)
+ {
+ fprintf(stderr, "%s: resource manager \"%s\" does not exist\n",
+ progname, optarg);
+ goto bad_argument;
+ }
+ }
+ break;
+ case 's':
+ if (sscanf(optarg, "%X/%X", &xlogid, &xrecoff) != 2)
+ {
+ fprintf(stderr, "%s: could not parse end log position \"%s\"\n",
+ progname, optarg);
+ goto bad_argument;
+ }
+ else
+ private.startptr = (uint64) xlogid << 32 | xrecoff;
+ break;
+ case 't':
+ if (sscanf(optarg, "%d", &private.timeline) != 1)
+ {
+ fprintf(stderr, "%s: could not parse timeline \"%s\"\n",
+ progname, optarg);
+ goto bad_argument;
+ }
+ break;
+ case 'V':
+ puts("pg_xlogdump (PostgreSQL) " PG_VERSION);
+ exit(EXIT_SUCCESS);
+ break;
+ case 'x':
+ if (sscanf(optarg, "%u", &config.filter_by_xid) != 1)
+ {
+ fprintf(stderr, "%s: could not parse \"%s\" as a valid xid\n",
+ progname, optarg);
+ goto bad_argument;
+ }
+ config.filter_by_xid_enabled = true;
+ break;
+ default:
+ goto bad_argument;
+ }
+ }
+
+ if ((optind + 2) < argc)
+ {
+ fprintf(stderr,
+ "%s: too many command-line arguments (first is \"%s\")\n",
+ progname, argv[optind + 2]);
+ goto bad_argument;
+ }
+
+ if (private.inpath != NULL)
+ {
+ /* validate path points to directory */
+ if (!verify_directory(private.inpath))
+ {
+ fprintf(stderr,
+ "%s: path \"%s\" cannot be opened: %s",
+ progname, private.inpath, strerror(errno));
+ goto bad_argument;
+ }
+ }
+
+ /* parse files as start/end boundaries, extract path if not specified */
+ if (optind < argc)
+ {
+ char *directory = NULL;
+ char *fname = NULL;
+ int fd;
+ XLogSegNo segno;
+
+ split_path(argv[optind], &directory, &fname);
+
+ if (private.inpath == NULL && directory != NULL)
+ {
+ private.inpath = directory;
+
+ if (!verify_directory(private.inpath))
+ fatal_error("cannot open directory \"%s\": %s",
+ private.inpath, strerror(errno));
+ }
+
+ fd = fuzzy_open_file(private.inpath, fname);
+ if (fd < 0)
+ fatal_error("could not open file \"%s\"", fname);
+ close(fd);
+
+ /* parse position from file */
+ XLogFromFileName(fname, &private.timeline, &segno);
+
+ if (XLogRecPtrIsInvalid(private.startptr))
+ XLogSegNoOffsetToRecPtr(segno, 0, private.startptr);
+ else if (!XLByteInSeg(private.startptr, segno))
+ {
+ fprintf(stderr,
+ "%s: start log position %X/%X is not inside file \"%s\"\n",
+ progname,
+ (uint32) (private.startptr >> 32),
+ (uint32) private.startptr,
+ fname);
+ goto bad_argument;
+ }
+
+ /* no second file specified, set end position */
+ if (!(optind + 1 < argc) && XLogRecPtrIsInvalid(private.endptr))
+ XLogSegNoOffsetToRecPtr(segno + 1, 0, private.endptr);
+
+ /* parse ENDSEG if passed */
+ if (optind + 1 < argc)
+ {
+ XLogSegNo endsegno;
+
+ /* ignore directory, already have that */
+ split_path(argv[optind + 1], &directory, &fname);
+
+ fd = fuzzy_open_file(private.inpath, fname);
+ if (fd < 0)
+ fatal_error("could not open file \"%s\"", fname);
+ close(fd);
+
+ /* parse position from file */
+ XLogFromFileName(fname, &private.timeline, &endsegno);
+
+ if (endsegno < segno)
+ fatal_error("ENDSEG %s is before STARTSEG %s",
+ argv[optind + 1], argv[optind]);
+
+ if (XLogRecPtrIsInvalid(private.endptr))
+ XLogSegNoOffsetToRecPtr(endsegno + 1, 0, private.endptr);
+
+ /* set segno to endsegno for check of --end */
+ segno = endsegno;
+ }
+
+
+ if (!XLByteInSeg(private.endptr, segno) &&
+ private.endptr != (segno + 1) * XLogSegSize)
+ {
+ fprintf(stderr,
+ "%s: end log position %X/%X is not inside file \"%s\"\n",
+ progname,
+ (uint32) (private.endptr >> 32),
+ (uint32) private.endptr,
+ argv[argc - 1]);
+ goto bad_argument;
+ }
+ }
+
+ /* we don't know what to print */
+ if (XLogRecPtrIsInvalid(private.startptr))
+ {
+ fprintf(stderr, "%s: no start log position given in range mode.\n", progname);
+ goto bad_argument;
+ }
+
+ /* done with argument parsing, do the actual work */
+
+ /* we have everything we need, start reading */
+ xlogreader_state = XLogReaderAllocate(XLogDumpReadPage, &private);
+ if (!xlogreader_state)
+ fatal_error("out of memory");
+
+ /* first find a valid recptr to start from */
+ first_record = XLogFindNextRecord(xlogreader_state, private.startptr);
+
+ if (first_record == InvalidXLogRecPtr)
+ fatal_error("could not find a valid record after %X/%X",
+ (uint32) (private.startptr >> 32),
+ (uint32) private.startptr);
+
+ /*
+ * Display a message that we're skipping data if `from` wasn't a pointer to
+ * the start of a record and also wasn't a pointer to the beginning of a
+ * segment (e.g. we were used in file mode).
+ */
+ if (first_record != private.startptr && (private.startptr % XLogSegSize) != 0)
+ printf("first record is after %X/%X, at %X/%X, skipping over %u bytes\n",
+ (uint32) (private.startptr >> 32), (uint32) private.startptr,
+ (uint32) (first_record >> 32), (uint32) first_record,
+ (uint32) (first_record - private.startptr));
+
+ while ((record = XLogReadRecord(xlogreader_state, first_record, &errormsg)))
+ {
+ /* continue after the last record */
+ first_record = InvalidXLogRecPtr;
+ XLogDumpDisplayRecord(&config, xlogreader_state->ReadRecPtr, record);
+
+ /* check whether we printed enough */
+ if (config.stop_after_records > 0 &&
+ config.already_displayed_records >= config.stop_after_records)
+ break;
+ }
+
+ if (errormsg)
+ fatal_error("error in WAL record at %X/%X: %s\n",
+ (uint32) (xlogreader_state->ReadRecPtr >> 32),
+ (uint32) xlogreader_state->ReadRecPtr,
+ errormsg);
+
+ XLogReaderFree(xlogreader_state);
+
+ return EXIT_SUCCESS;
+
+bad_argument:
+ fprintf(stderr, "Try \"%s --help\" for more information.\n", progname);
+ return EXIT_FAILURE;
+}
diff --git a/contrib/pg_xlogdump/rmgrdesc.c b/contrib/pg_xlogdump/rmgrdesc.c
new file mode 100644
index 0000000000..0508c8dae9
--- /dev/null
+++ b/contrib/pg_xlogdump/rmgrdesc.c
@@ -0,0 +1,36 @@
+/*
+ * rmgrdesc.c
+ *
+ * pg_xlogdump resource managers definition
+ *
+ * contrib/pg_xlogdump/rmgrdesc.c
+ */
+#define FRONTEND 1
+#include "postgres.h"
+
+#include "access/clog.h"
+#include "access/gin.h"
+#include "access/gist_private.h"
+#include "access/hash.h"
+#include "access/heapam_xlog.h"
+#include "access/multixact.h"
+#include "access/nbtree.h"
+#include "access/rmgr.h"
+#include "access/spgist.h"
+#include "access/xact.h"
+#include "access/xlog_internal.h"
+#include "catalog/storage_xlog.h"
+#include "commands/dbcommands.h"
+#include "commands/sequence.h"
+#include "commands/tablespace.h"
+#include "rmgrdesc.h"
+#include "storage/standby.h"
+#include "utils/relmapper.h"
+
+#define PG_RMGR(symname,name,redo,desc,startup,cleanup,restartpoint) \
+ { name, desc, },
+
+const RmgrDescData RmgrDescTable[RM_MAX_ID + 1] = {
+#include "access/rmgrlist.h"
+};
+
diff --git a/contrib/pg_xlogdump/rmgrdesc.h b/contrib/pg_xlogdump/rmgrdesc.h
new file mode 100644
index 0000000000..2341739a14
--- /dev/null
+++ b/contrib/pg_xlogdump/rmgrdesc.h
@@ -0,0 +1,21 @@
+/*
+ * rmgrdesc.h
+ *
+ * pg_xlogdump resource managers declaration
+ *
+ * contrib/pg_xlogdump/rmgrdesc.h
+ */
+#ifndef RMGRDESC_H
+#define RMGRDESC_H
+
+#include "lib/stringinfo.h"
+
+typedef struct RmgrDescData
+{
+ const char *rm_name;
+ void (*rm_desc) (StringInfo buf, uint8 xl_info, char *rec);
+} RmgrDescData;
+
+extern const RmgrDescData RmgrDescTable[];
+
+#endif /* RMGRDESC_H */
diff --git a/doc/src/sgml/contrib.sgml b/doc/src/sgml/contrib.sgml
index 39e9827fca..dd8e09ed29 100644
--- a/doc/src/sgml/contrib.sgml
+++ b/doc/src/sgml/contrib.sgml
@@ -207,5 +207,6 @@ pages.
&pgtestfsync;
&pgtesttiming;
&pgupgrade;
+ &pgxlogdump;
</sect1>
</appendix>
diff --git a/doc/src/sgml/filelist.sgml b/doc/src/sgml/filelist.sgml
index 5d55ef357b..b623f58b1a 100644
--- a/doc/src/sgml/filelist.sgml
+++ b/doc/src/sgml/filelist.sgml
@@ -134,6 +134,7 @@
<!ENTITY pgtesttiming SYSTEM "pgtesttiming.sgml">
<!ENTITY pgtrgm SYSTEM "pgtrgm.sgml">
<!ENTITY pgupgrade SYSTEM "pgupgrade.sgml">
+<!ENTITY pgxlogdump SYSTEM "pg_xlogdump.sgml">
<!ENTITY postgres-fdw SYSTEM "postgres-fdw.sgml">
<!ENTITY seg SYSTEM "seg.sgml">
<!ENTITY contrib-spi SYSTEM "contrib-spi.sgml">
diff --git a/doc/src/sgml/pg_xlogdump.sgml b/doc/src/sgml/pg_xlogdump.sgml
new file mode 100644
index 0000000000..15bdfbb46c
--- /dev/null
+++ b/doc/src/sgml/pg_xlogdump.sgml
@@ -0,0 +1,205 @@
+<!--
+doc/src/sgml/ref/pg_xlogdump.sgml
+PostgreSQL documentation
+-->
+
+<refentry id="pg_xlogdump">
+ <refmeta>
+ <refentrytitle><application>pg_xlogdump</application></refentrytitle>
+ <manvolnum>1</manvolnum>
+ <refmiscinfo>Application</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+ <refname>pg_xlogdump</refname>
+ <refpurpose>Display a human-readable rendering of the write-ahead log of a <productname>PostgreSQL</productname> database cluster</refpurpose>
+ </refnamediv>
+
+ <indexterm zone="pg_xlogdump">
+ <primary>pg_xlogdump</primary>
+ </indexterm>
+
+ <refsynopsisdiv>
+ <cmdsynopsis>
+ <command>pg_xlogdump</command>
+ <arg rep="repeat" choice="opt"><option>option</option></arg>
+ <arg choice="opt"><option>startseg</option>
+ <arg choice="opt"><option>endseg</option></arg>
+ </arg>
+ </cmdsynopsis>
+ </refsynopsisdiv>
+
+ <refsect1 id="R1-APP-PGXLOGDUMP-1">
+ <title>Description</title>
+ <para>
+ <command>pg_xlogdump</command> display the write-ahead log (WAL) and is mainly
+ useful for debugging or educational purposes.
+ </para>
+
+ <para>
+ This utility can only be run by the user who installed the server, because
+ it requires read-only access to the data directory.
+ </para>
+ </refsect1>
+
+ <refsect1>
+ <title>Options</title>
+
+ <para>
+ The following command-line options control the location and format of the
+ output:
+
+ <variablelist>
+
+ <varlistentry>
+ <term><replaceable class="parameter">startseg</replaceable></term>
+ <listitem>
+ <para>
+ Start reading at the specified log segment file. This implicitly determines
+ the path in which files will be searched for, and the timeline to use.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">endseg</replaceable></term>
+ <listitem>
+ <para>
+ Stop after reading the specified log segment file.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><option>-b</option></term>
+ <term><option>--bkp-details</option></term>
+ <listitem>
+ <para>
+ Output detailed information about backup blocks.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><option>-e <replaceable>end</replaceable></option></term>
+ <term><option>--end=<replaceable>end</replaceable></option></term>
+ <listitem>
+ <para>
+ Stop reading at the specified log position, instead of reading to the
+ end of the log stream.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><option>-n <replaceable>limit</replaceable></option></term>
+ <term><option>--limit=<replaceable>limit</replaceable></option></term>
+ <listitem>
+ <para>
+ Display the specified number of records, then stop.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><option>-p <replaceable>path</replaceable></option></term>
+ <term><option>--path=<replaceable>path</replaceable></option></term>
+ <listitem>
+ <para>
+ Directory in which to find log segment files. The default is to search
+ for them in the <literal>pg_xlog</literal> subdirectory of the current
+ directory.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><option>-r <replaceable>rmgr</replaceable></option></term>
+ <term><option>--rmgr=<replaceable>rmgr</replaceable></option></term>
+ <listitem>
+ <para>
+ Only display records generated by the specified resource manager.
+ If <literal>list</> is passed as name, print a list of valid resource manager
+ names, and exit.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><option>-s <replaceable>start</replaceable></option></term>
+ <term><option>--start=<replaceable>start</replaceable></option></term>
+ <listitem>
+ <para>
+ Log position at which to start reading. The default is to start reading
+ the first valid log record found in the earliest file found.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><option>-t <replaceable>timeline</replaceable></option></term>
+ <term><option>--timelime=<replaceable>timeline</replaceable></option></term>
+ <listitem>
+ <para>
+ Timeline from which to read log records. The default is to use the
+ value in <literal>startseg</>, if that is specified; otherwise, the
+ default is 1.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><option>-V</></term>
+ <term><option>--version</></term>
+ <listitem>
+ <para>
+ Print the <application>pg_xlogdump</application> version and exit.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><option>-x <replaceable>xid</replaceable></option></term>
+ <term><option>--xid=<replaceable>xid</replaceable></option></term>
+ <listitem>
+ <para>
+ Only display records marked with the given TransactionId.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><option>-?</></term>
+ <term><option>--help</></term>
+ <listitem>
+ <para>
+ Show help about <application>pg_xlogdump</application> command line
+ arguments, and exit.
+ </para>
+ </listitem>
+ </varlistentry>
+ </variablelist>
+ </para>
+ </refsect1>
+
+ <refsect1>
+ <title>Notes</title>
+ <para>
+ Can give wrong results when the server is running.
+ </para>
+
+ <para>
+ Only the specified timeline is displayed (or the default, if none is
+ specified). Records in other timelines are ignored.
+ </para>
+ </refsect1>
+
+ <refsect1>
+ <title>See Also</title>
+
+ <simplelist type="inline">
+ <member><xref linkend="wal"></member>
+ </simplelist>
+ </rfsect1>
+
+</refentry>
diff --git a/doc/src/sgml/ref/pg_isready.sgml b/doc/src/sgml/ref/pg_isready.sgml
index 407d73ba5b..19ff1d4935 100644
--- a/doc/src/sgml/ref/pg_isready.sgml
+++ b/doc/src/sgml/ref/pg_isready.sgml
@@ -12,7 +12,7 @@ PostgreSQL documentation
<refnamediv>
<refname>pg_isready</refname>
- <refpurpose>checks the connection status of a <productname>PostgreSQL</productname> server</refpurpose>
+ <refpurpose>check the connection status of a <productname>PostgreSQL</productname> server</refpurpose>
</refnamediv>
<indexterm zone="app-pg-isready">
diff --git a/src/include/utils/palloc.h b/src/include/utils/palloc.h
index 7fc7ccc3b5..fc5d6053d2 100644
--- a/src/include/utils/palloc.h
+++ b/src/include/utils/palloc.h
@@ -28,8 +28,6 @@
#ifndef PALLOC_H
#define PALLOC_H
-#ifndef FRONTEND
-
/*
* Type MemoryContextData is declared in nodes/memnodes.h. Most users
* of memory allocation should just treat it as an abstract type, so we
@@ -37,6 +35,8 @@
*/
typedef struct MemoryContextData *MemoryContext;
+#ifndef FRONTEND
+
/*
* CurrentMemoryContext is the default allocation context for palloc().
* We declare it here so that palloc() can be a macro. Avoid accessing it