LCOV - code coverage report
Current view: top level - src/bin/pg_waldump - pg_waldump.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 0.0 % 623 0
Test Date: 2026-01-26 10:56:24 Functions: 0.0 % 19 0
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * pg_waldump.c - decode and display WAL
       4              :  *
       5              :  * Copyright (c) 2013-2026, PostgreSQL Global Development Group
       6              :  *
       7              :  * IDENTIFICATION
       8              :  *                src/bin/pg_waldump/pg_waldump.c
       9              :  *-------------------------------------------------------------------------
      10              :  */
      11              : 
      12              : #define FRONTEND 1
      13              : #include "postgres.h"
      14              : 
      15              : #include <dirent.h>
      16              : #include <limits.h>
      17              : #include <signal.h>
      18              : #include <sys/stat.h>
      19              : #include <unistd.h>
      20              : 
      21              : #include "access/transam.h"
      22              : #include "access/xlog_internal.h"
      23              : #include "access/xlogreader.h"
      24              : #include "access/xlogrecord.h"
      25              : #include "access/xlogstats.h"
      26              : #include "common/fe_memutils.h"
      27              : #include "common/file_perm.h"
      28              : #include "common/file_utils.h"
      29              : #include "common/logging.h"
      30              : #include "common/relpath.h"
      31              : #include "getopt_long.h"
      32              : #include "rmgrdesc.h"
      33              : #include "storage/bufpage.h"
      34              : 
      35              : /*
      36              :  * NOTE: For any code change or issue fix here, it is highly recommended to
      37              :  * give a thought about doing the same in pg_walinspect contrib module as well.
      38              :  */
      39              : 
      40              : static const char *progname;
      41              : 
      42              : static int      WalSegSz;
      43              : static volatile sig_atomic_t time_to_stop = false;
      44              : 
      45              : static const RelFileLocator emptyRelFileLocator = {0, 0, 0};
      46              : 
      47              : typedef struct XLogDumpPrivate
      48              : {
      49              :         TimeLineID      timeline;
      50              :         XLogRecPtr      startptr;
      51              :         XLogRecPtr      endptr;
      52              :         bool            endptr_reached;
      53              : } XLogDumpPrivate;
      54              : 
      55              : typedef struct XLogDumpConfig
      56              : {
      57              :         /* display options */
      58              :         bool            quiet;
      59              :         bool            bkp_details;
      60              :         int                     stop_after_records;
      61              :         int                     already_displayed_records;
      62              :         bool            follow;
      63              :         bool            stats;
      64              :         bool            stats_per_record;
      65              : 
      66              :         /* filter options */
      67              :         bool            filter_by_rmgr[RM_MAX_ID + 1];
      68              :         bool            filter_by_rmgr_enabled;
      69              :         TransactionId filter_by_xid;
      70              :         bool            filter_by_xid_enabled;
      71              :         RelFileLocator filter_by_relation;
      72              :         bool            filter_by_extended;
      73              :         bool            filter_by_relation_enabled;
      74              :         BlockNumber filter_by_relation_block;
      75              :         bool            filter_by_relation_block_enabled;
      76              :         ForkNumber      filter_by_relation_forknum;
      77              :         bool            filter_by_fpw;
      78              : 
      79              :         /* save options */
      80              :         char       *save_fullpage_path;
      81              : } XLogDumpConfig;
      82              : 
      83              : 
      84              : /*
      85              :  * When sigint is called, just tell the system to exit at the next possible
      86              :  * moment.
      87              :  */
      88              : #ifndef WIN32
      89              : 
      90              : static void
      91            0 : sigint_handler(SIGNAL_ARGS)
      92              : {
      93            0 :         time_to_stop = true;
      94            0 : }
      95              : #endif
      96              : 
      97              : static void
      98            0 : print_rmgr_list(void)
      99              : {
     100            0 :         int                     i;
     101              : 
     102            0 :         for (i = 0; i <= RM_MAX_BUILTIN_ID; i++)
     103              :         {
     104            0 :                 printf("%s\n", GetRmgrDesc(i)->rm_name);
     105            0 :         }
     106            0 : }
     107              : 
     108              : /*
     109              :  * Check whether directory exists and whether we can open it. Keep errno set so
     110              :  * that the caller can report errors somewhat more accurately.
     111              :  */
     112              : static bool
     113            0 : verify_directory(const char *directory)
     114              : {
     115            0 :         DIR                *dir = opendir(directory);
     116              : 
     117            0 :         if (dir == NULL)
     118            0 :                 return false;
     119            0 :         closedir(dir);
     120            0 :         return true;
     121            0 : }
     122              : 
     123              : /*
     124              :  * Create if necessary the directory storing the full-page images extracted
     125              :  * from the WAL records read.
     126              :  */
     127              : static void
     128            0 : create_fullpage_directory(char *path)
     129              : {
     130            0 :         int                     ret;
     131              : 
     132            0 :         switch ((ret = pg_check_dir(path)))
     133              :         {
     134              :                 case 0:
     135              :                         /* Does not exist, so create it */
     136            0 :                         if (pg_mkdir_p(path, pg_dir_create_mode) < 0)
     137            0 :                                 pg_fatal("could not create directory \"%s\": %m", path);
     138            0 :                         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            0 :                         pg_fatal("directory \"%s\" exists but is not empty", path);
     147            0 :                         break;
     148              :                 default:
     149              :                         /* Trouble accessing directory */
     150            0 :                         pg_fatal("could not access directory \"%s\": %m", path);
     151            0 :         }
     152            0 : }
     153              : 
     154              : /*
     155              :  * Split a pathname as dirname(1) and basename(1) would.
     156              :  *
     157              :  * XXX this probably doesn't do very well on Windows.  We probably need to
     158              :  * apply canonicalize_path(), at the very least.
     159              :  */
     160              : static void
     161            0 : split_path(const char *path, char **dir, char **fname)
     162              : {
     163            0 :         const char *sep;
     164              : 
     165              :         /* split filepath into directory & filename */
     166            0 :         sep = strrchr(path, '/');
     167              : 
     168              :         /* directory path */
     169            0 :         if (sep != NULL)
     170              :         {
     171            0 :                 *dir = pnstrdup(path, sep - path);
     172            0 :                 *fname = pg_strdup(sep + 1);
     173            0 :         }
     174              :         /* local directory */
     175              :         else
     176              :         {
     177            0 :                 *dir = NULL;
     178            0 :                 *fname = pg_strdup(path);
     179              :         }
     180            0 : }
     181              : 
     182              : /*
     183              :  * Open the file in the valid target directory.
     184              :  *
     185              :  * return a read only fd
     186              :  */
     187              : static int
     188            0 : open_file_in_directory(const char *directory, const char *fname)
     189              : {
     190            0 :         int                     fd = -1;
     191            0 :         char            fpath[MAXPGPATH];
     192              : 
     193            0 :         Assert(directory != NULL);
     194              : 
     195            0 :         snprintf(fpath, MAXPGPATH, "%s/%s", directory, fname);
     196            0 :         fd = open(fpath, O_RDONLY | PG_BINARY, 0);
     197              : 
     198            0 :         if (fd < 0 && errno != ENOENT)
     199            0 :                 pg_fatal("could not open file \"%s\": %m", fname);
     200            0 :         return fd;
     201            0 : }
     202              : 
     203              : /*
     204              :  * Try to find fname in the given directory. Returns true if it is found,
     205              :  * false otherwise. If fname is NULL, search the complete directory for any
     206              :  * file with a valid WAL file name. If file is successfully opened, set the
     207              :  * wal segment size.
     208              :  */
     209              : static bool
     210            0 : search_directory(const char *directory, const char *fname)
     211              : {
     212            0 :         int                     fd = -1;
     213            0 :         DIR                *xldir;
     214              : 
     215              :         /* open file if valid filename is provided */
     216            0 :         if (fname != NULL)
     217            0 :                 fd = open_file_in_directory(directory, fname);
     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            0 :         else if ((xldir = opendir(directory)) != NULL)
     225              :         {
     226            0 :                 struct dirent *xlde;
     227              : 
     228            0 :                 while ((xlde = readdir(xldir)) != NULL)
     229              :                 {
     230            0 :                         if (IsXLogFileName(xlde->d_name))
     231              :                         {
     232            0 :                                 fd = open_file_in_directory(directory, xlde->d_name);
     233            0 :                                 fname = pg_strdup(xlde->d_name);
     234            0 :                                 break;
     235              :                         }
     236              :                 }
     237              : 
     238            0 :                 closedir(xldir);
     239            0 :         }
     240              : 
     241              :         /* set WalSegSz if file is successfully opened */
     242            0 :         if (fd >= 0)
     243              :         {
     244            0 :                 PGAlignedXLogBlock buf;
     245            0 :                 int                     r;
     246              : 
     247            0 :                 r = read(fd, buf.data, XLOG_BLCKSZ);
     248            0 :                 if (r == XLOG_BLCKSZ)
     249              :                 {
     250            0 :                         XLogLongPageHeader longhdr = (XLogLongPageHeader) buf.data;
     251              : 
     252            0 :                         WalSegSz = longhdr->xlp_seg_size;
     253              : 
     254            0 :                         if (!IsValidWalSegSize(WalSegSz))
     255              :                         {
     256            0 :                                 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            0 :                                 pg_log_error_detail("The WAL segment size must be a power of two between 1 MB and 1 GB.");
     261            0 :                                 exit(1);
     262              :                         }
     263            0 :                 }
     264            0 :                 else if (r < 0)
     265            0 :                         pg_fatal("could not read file \"%s\": %m",
     266              :                                          fname);
     267              :                 else
     268            0 :                         pg_fatal("could not read file \"%s\": read %d of %d",
     269              :                                          fname, r, XLOG_BLCKSZ);
     270            0 :                 close(fd);
     271            0 :                 return true;
     272            0 :         }
     273              : 
     274            0 :         return false;
     275            0 : }
     276              : 
     277              : /*
     278              :  * Identify the target directory.
     279              :  *
     280              :  * Try to find the file in several places:
     281              :  * if directory != NULL:
     282              :  *       directory /
     283              :  *       directory / XLOGDIR /
     284              :  * else
     285              :  *       .
     286              :  *       XLOGDIR /
     287              :  *       $PGDATA / XLOGDIR /
     288              :  *
     289              :  * The valid target directory is returned.
     290              :  */
     291              : static char *
     292            0 : identify_target_directory(char *directory, char *fname)
     293              : {
     294            0 :         char            fpath[MAXPGPATH];
     295              : 
     296            0 :         if (directory != NULL)
     297              :         {
     298            0 :                 if (search_directory(directory, fname))
     299            0 :                         return pg_strdup(directory);
     300              : 
     301              :                 /* directory / XLOGDIR */
     302            0 :                 snprintf(fpath, MAXPGPATH, "%s/%s", directory, XLOGDIR);
     303            0 :                 if (search_directory(fpath, fname))
     304            0 :                         return pg_strdup(fpath);
     305            0 :         }
     306              :         else
     307              :         {
     308            0 :                 const char *datadir;
     309              : 
     310              :                 /* current directory */
     311            0 :                 if (search_directory(".", fname))
     312            0 :                         return pg_strdup(".");
     313              :                 /* XLOGDIR */
     314            0 :                 if (search_directory(XLOGDIR, fname))
     315            0 :                         return pg_strdup(XLOGDIR);
     316              : 
     317            0 :                 datadir = getenv("PGDATA");
     318              :                 /* $PGDATA / XLOGDIR */
     319            0 :                 if (datadir != NULL)
     320              :                 {
     321            0 :                         snprintf(fpath, MAXPGPATH, "%s/%s", datadir, XLOGDIR);
     322            0 :                         if (search_directory(fpath, fname))
     323            0 :                                 return pg_strdup(fpath);
     324            0 :                 }
     325            0 :         }
     326              : 
     327              :         /* could not locate WAL file */
     328            0 :         if (fname)
     329            0 :                 pg_fatal("could not locate WAL file \"%s\"", fname);
     330              :         else
     331            0 :                 pg_fatal("could not find any WAL file");
     332              : 
     333            0 :         return NULL;                            /* not reached */
     334            0 : }
     335              : 
     336              : /* pg_waldump's XLogReaderRoutine->segment_open callback */
     337              : static void
     338            0 : WALDumpOpenSegment(XLogReaderState *state, XLogSegNo nextSegNo,
     339              :                                    TimeLineID *tli_p)
     340              : {
     341            0 :         TimeLineID      tli = *tli_p;
     342            0 :         char            fname[MAXPGPATH];
     343            0 :         int                     tries;
     344              : 
     345            0 :         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            0 :         for (tries = 0; tries < 10; tries++)
     354              :         {
     355            0 :                 state->seg.ws_file = open_file_in_directory(state->segcxt.ws_dir, fname);
     356            0 :                 if (state->seg.ws_file >= 0)
     357            0 :                         return;
     358            0 :                 if (errno == ENOENT)
     359              :                 {
     360            0 :                         int                     save_errno = errno;
     361              : 
     362              :                         /* File not there yet, try again */
     363            0 :                         pg_usleep(500 * 1000);
     364              : 
     365            0 :                         errno = save_errno;
     366              :                         continue;
     367            0 :                 }
     368              :                 /* Any other error, fall through and fail */
     369            0 :                 break;
     370              :         }
     371              : 
     372            0 :         pg_fatal("could not find file \"%s\": %m", fname);
     373            0 : }
     374              : 
     375              : /*
     376              :  * pg_waldump's XLogReaderRoutine->segment_close callback.  Same as
     377              :  * wal_segment_close
     378              :  */
     379              : static void
     380            0 : WALDumpCloseSegment(XLogReaderState *state)
     381              : {
     382            0 :         close(state->seg.ws_file);
     383              :         /* need to check errno? */
     384            0 :         state->seg.ws_file = -1;
     385            0 : }
     386              : 
     387              : /* pg_waldump's XLogReaderRoutine->page_read callback */
     388              : static int
     389            0 : WALDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen,
     390              :                                 XLogRecPtr targetPtr, char *readBuff)
     391              : {
     392            0 :         XLogDumpPrivate *private = state->private_data;
     393            0 :         int                     count = XLOG_BLCKSZ;
     394            0 :         WALReadError errinfo;
     395              : 
     396            0 :         if (XLogRecPtrIsValid(private->endptr))
     397              :         {
     398            0 :                 if (targetPagePtr + XLOG_BLCKSZ <= private->endptr)
     399            0 :                         count = XLOG_BLCKSZ;
     400            0 :                 else if (targetPagePtr + reqLen <= private->endptr)
     401            0 :                         count = private->endptr - targetPagePtr;
     402              :                 else
     403              :                 {
     404            0 :                         private->endptr_reached = true;
     405            0 :                         return -1;
     406              :                 }
     407            0 :         }
     408              : 
     409            0 :         if (!WALRead(state, readBuff, targetPagePtr, count, private->timeline,
     410              :                                  &errinfo))
     411              :         {
     412            0 :                 WALOpenSegment *seg = &errinfo.wre_seg;
     413            0 :                 char            fname[MAXPGPATH];
     414              : 
     415            0 :                 XLogFileName(fname, seg->ws_tli, seg->ws_segno,
     416            0 :                                          state->segcxt.ws_segsize);
     417              : 
     418            0 :                 if (errinfo.wre_errno != 0)
     419              :                 {
     420            0 :                         errno = errinfo.wre_errno;
     421            0 :                         pg_fatal("could not read from file \"%s\", offset %d: %m",
     422              :                                          fname, errinfo.wre_off);
     423            0 :                 }
     424              :                 else
     425            0 :                         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            0 :         }
     429              : 
     430            0 :         return count;
     431            0 : }
     432              : 
     433              : /*
     434              :  * Boolean to return whether the given WAL record matches a specific relation
     435              :  * and optionally block.
     436              :  */
     437              : static bool
     438            0 : XLogRecordMatchesRelationBlock(XLogReaderState *record,
     439              :                                                            RelFileLocator matchRlocator,
     440              :                                                            BlockNumber matchBlock,
     441              :                                                            ForkNumber matchFork)
     442              : {
     443            0 :         int                     block_id;
     444              : 
     445            0 :         for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
     446              :         {
     447            0 :                 RelFileLocator rlocator;
     448            0 :                 ForkNumber      forknum;
     449            0 :                 BlockNumber blk;
     450              : 
     451            0 :                 if (!XLogRecGetBlockTagExtended(record, block_id,
     452              :                                                                                 &rlocator, &forknum, &blk, NULL))
     453            0 :                         continue;
     454              : 
     455            0 :                 if ((matchFork == InvalidForkNumber || matchFork == forknum) &&
     456            0 :                         (RelFileLocatorEquals(matchRlocator, emptyRelFileLocator) ||
     457            0 :                          RelFileLocatorEquals(matchRlocator, rlocator)) &&
     458            0 :                         (matchBlock == InvalidBlockNumber || matchBlock == blk))
     459            0 :                         return true;
     460            0 :         }
     461              : 
     462            0 :         return false;
     463            0 : }
     464              : 
     465              : /*
     466              :  * Boolean to return whether the given WAL record contains a full page write.
     467              :  */
     468              : static bool
     469            0 : XLogRecordHasFPW(XLogReaderState *record)
     470              : {
     471            0 :         int                     block_id;
     472              : 
     473            0 :         for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
     474              :         {
     475            0 :                 if (!XLogRecHasBlockRef(record, block_id))
     476            0 :                         continue;
     477              : 
     478            0 :                 if (XLogRecHasBlockImage(record, block_id))
     479            0 :                         return true;
     480            0 :         }
     481              : 
     482            0 :         return false;
     483            0 : }
     484              : 
     485              : /*
     486              :  * Function to externally save all FPWs stored in the given WAL record.
     487              :  * Decompression is applied to all the blocks saved, if necessary.
     488              :  */
     489              : static void
     490            0 : XLogRecordSaveFPWs(XLogReaderState *record, const char *savepath)
     491              : {
     492            0 :         int                     block_id;
     493              : 
     494            0 :         for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
     495              :         {
     496            0 :                 PGAlignedBlock buf;
     497            0 :                 Page            page;
     498            0 :                 char            filename[MAXPGPATH];
     499            0 :                 char            forkname[FORKNAMECHARS + 2];    /* _ + terminating zero */
     500            0 :                 FILE       *file;
     501            0 :                 BlockNumber blk;
     502            0 :                 RelFileLocator rnode;
     503            0 :                 ForkNumber      fork;
     504              : 
     505            0 :                 if (!XLogRecHasBlockRef(record, block_id))
     506            0 :                         continue;
     507              : 
     508            0 :                 if (!XLogRecHasBlockImage(record, block_id))
     509            0 :                         continue;
     510              : 
     511            0 :                 page = (Page) buf.data;
     512              : 
     513              :                 /* Full page exists, so let's save it */
     514            0 :                 if (!RestoreBlockImage(record, block_id, page))
     515            0 :                         pg_fatal("%s", record->errormsg_buf);
     516              : 
     517            0 :                 (void) XLogRecGetBlockTagExtended(record, block_id,
     518              :                                                                                   &rnode, &fork, &blk, NULL);
     519              : 
     520            0 :                 if (fork >= 0 && fork <= MAX_FORKNUM)
     521            0 :                         sprintf(forkname, "_%s", forkNames[fork]);
     522              :                 else
     523            0 :                         pg_fatal("invalid fork number: %u", fork);
     524              : 
     525            0 :                 snprintf(filename, MAXPGPATH, "%s/%08X-%08X-%08X.%u.%u.%u.%u%s", savepath,
     526            0 :                                  record->seg.ws_tli,
     527            0 :                                  LSN_FORMAT_ARGS(record->ReadRecPtr),
     528            0 :                                  rnode.spcOid, rnode.dbOid, rnode.relNumber, blk, forkname);
     529              : 
     530            0 :                 file = fopen(filename, PG_BINARY_W);
     531            0 :                 if (!file)
     532            0 :                         pg_fatal("could not open file \"%s\": %m", filename);
     533              : 
     534            0 :                 if (fwrite(page, BLCKSZ, 1, file) != 1)
     535            0 :                         pg_fatal("could not write file \"%s\": %m", filename);
     536              : 
     537            0 :                 if (fclose(file) != 0)
     538            0 :                         pg_fatal("could not close file \"%s\": %m", filename);
     539            0 :         }
     540            0 : }
     541              : 
     542              : /*
     543              :  * Print a record to stdout
     544              :  */
     545              : static void
     546            0 : XLogDumpDisplayRecord(XLogDumpConfig *config, XLogReaderState *record)
     547              : {
     548            0 :         const char *id;
     549            0 :         const RmgrDescData *desc = GetRmgrDesc(XLogRecGetRmid(record));
     550            0 :         uint32          rec_len;
     551            0 :         uint32          fpi_len;
     552            0 :         uint8           info = XLogRecGetInfo(record);
     553            0 :         XLogRecPtr      xl_prev = XLogRecGetPrev(record);
     554            0 :         StringInfoData s;
     555              : 
     556            0 :         XLogRecGetLen(record, &rec_len, &fpi_len);
     557              : 
     558            0 :         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            0 :         id = desc->rm_identify(info);
     566            0 :         if (id == NULL)
     567            0 :                 printf("desc: UNKNOWN (%x) ", info & ~XLR_INFO_MASK);
     568              :         else
     569            0 :                 printf("desc: %s ", id);
     570              : 
     571            0 :         initStringInfo(&s);
     572            0 :         desc->rm_desc(&s, record);
     573            0 :         printf("%s", s.data);
     574              : 
     575            0 :         resetStringInfo(&s);
     576            0 :         XLogRecGetBlockRefInfo(record, true, config->bkp_details, &s, NULL);
     577            0 :         printf("%s", s.data);
     578            0 :         pfree(s.data);
     579            0 : }
     580              : 
     581              : /*
     582              :  * Display a single row of record counts and sizes for an rmgr or record.
     583              :  */
     584              : static void
     585            0 : XLogDumpStatsRow(const char *name,
     586              :                                  uint64 n, uint64 total_count,
     587              :                                  uint64 rec_len, uint64 total_rec_len,
     588              :                                  uint64 fpi_len, uint64 total_fpi_len,
     589              :                                  uint64 tot_len, uint64 total_len)
     590              : {
     591            0 :         double          n_pct,
     592              :                                 rec_len_pct,
     593              :                                 fpi_len_pct,
     594              :                                 tot_len_pct;
     595              : 
     596            0 :         n_pct = 0;
     597            0 :         if (total_count != 0)
     598            0 :                 n_pct = 100 * (double) n / total_count;
     599              : 
     600            0 :         rec_len_pct = 0;
     601            0 :         if (total_rec_len != 0)
     602            0 :                 rec_len_pct = 100 * (double) rec_len / total_rec_len;
     603              : 
     604            0 :         fpi_len_pct = 0;
     605            0 :         if (total_fpi_len != 0)
     606            0 :                 fpi_len_pct = 100 * (double) fpi_len / total_fpi_len;
     607              : 
     608            0 :         tot_len_pct = 0;
     609            0 :         if (total_len != 0)
     610            0 :                 tot_len_pct = 100 * (double) tot_len / total_len;
     611              : 
     612            0 :         printf("%-27s "
     613              :                    "%20" PRIu64 " (%6.02f) "
     614              :                    "%20" PRIu64 " (%6.02f) "
     615              :                    "%20" PRIu64 " (%6.02f) "
     616              :                    "%20" PRIu64 " (%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            0 : }
     620              : 
     621              : 
     622              : /*
     623              :  * Display summary statistics about the records seen so far.
     624              :  */
     625              : static void
     626            0 : XLogDumpDisplayStats(XLogDumpConfig *config, XLogStats *stats)
     627              : {
     628            0 :         int                     ri,
     629              :                                 rj;
     630            0 :         uint64          total_count = 0;
     631            0 :         uint64          total_rec_len = 0;
     632            0 :         uint64          total_fpi_len = 0;
     633            0 :         uint64          total_len = 0;
     634            0 :         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            0 :         if (!XLogRecPtrIsValid(stats->endptr))
     641            0 :                 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            0 :         for (ri = 0; ri <= RM_MAX_ID; ri++)
     649              :         {
     650            0 :                 if (!RmgrIdIsValid(ri))
     651            0 :                         continue;
     652              : 
     653            0 :                 total_count += stats->rmgr_stats[ri].count;
     654            0 :                 total_rec_len += stats->rmgr_stats[ri].rec_len;
     655            0 :                 total_fpi_len += stats->rmgr_stats[ri].fpi_len;
     656            0 :         }
     657            0 :         total_len = total_rec_len + total_fpi_len;
     658              : 
     659            0 :         printf("WAL statistics between %X/%08X and %X/%08X:\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            0 :         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            0 :         for (ri = 0; ri <= RM_MAX_ID; ri++)
     673              :         {
     674            0 :                 uint64          count,
     675              :                                         rec_len,
     676              :                                         fpi_len,
     677              :                                         tot_len;
     678            0 :                 const RmgrDescData *desc;
     679              : 
     680            0 :                 if (!RmgrIdIsValid(ri))
     681            0 :                         continue;
     682              : 
     683            0 :                 desc = GetRmgrDesc(ri);
     684              : 
     685            0 :                 if (!config->stats_per_record)
     686              :                 {
     687            0 :                         count = stats->rmgr_stats[ri].count;
     688            0 :                         rec_len = stats->rmgr_stats[ri].rec_len;
     689            0 :                         fpi_len = stats->rmgr_stats[ri].fpi_len;
     690            0 :                         tot_len = rec_len + fpi_len;
     691              : 
     692            0 :                         if (RmgrIdIsCustom(ri) && count == 0)
     693            0 :                                 continue;
     694              : 
     695            0 :                         XLogDumpStatsRow(desc->rm_name,
     696            0 :                                                          count, total_count, rec_len, total_rec_len,
     697            0 :                                                          fpi_len, total_fpi_len, tot_len, total_len);
     698            0 :                 }
     699              :                 else
     700              :                 {
     701            0 :                         for (rj = 0; rj < MAX_XLINFO_TYPES; rj++)
     702              :                         {
     703            0 :                                 const char *id;
     704              : 
     705            0 :                                 count = stats->record_stats[ri][rj].count;
     706            0 :                                 rec_len = stats->record_stats[ri][rj].rec_len;
     707            0 :                                 fpi_len = stats->record_stats[ri][rj].fpi_len;
     708            0 :                                 tot_len = rec_len + fpi_len;
     709              : 
     710              :                                 /* Skip undefined combinations and ones that didn't occur */
     711            0 :                                 if (count == 0)
     712            0 :                                         continue;
     713              : 
     714              :                                 /* the upper four bits in xl_info are the rmgr's */
     715            0 :                                 id = desc->rm_identify(rj << 4);
     716            0 :                                 if (id == NULL)
     717            0 :                                         id = psprintf("UNKNOWN (%x)", rj << 4);
     718              : 
     719            0 :                                 XLogDumpStatsRow(psprintf("%s/%s", desc->rm_name, id),
     720            0 :                                                                  count, total_count, rec_len, total_rec_len,
     721            0 :                                                                  fpi_len, total_fpi_len, tot_len, total_len);
     722            0 :                         }
     723              :                 }
     724            0 :         }
     725              : 
     726            0 :         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            0 :         rec_len_pct = 0;
     737            0 :         if (total_len != 0)
     738            0 :                 rec_len_pct = 100 * (double) total_rec_len / total_len;
     739              : 
     740            0 :         fpi_len_pct = 0;
     741            0 :         if (total_len != 0)
     742            0 :                 fpi_len_pct = 100 * (double) total_fpi_len / total_len;
     743              : 
     744            0 :         printf("%-27s "
     745              :                    "%20" PRIu64 " %-9s"
     746              :                    "%20" PRIu64 " %-9s"
     747              :                    "%20" PRIu64 " %-9s"
     748              :                    "%20" PRIu64 " %-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            0 : }
     754              : 
     755              : static void
     756            0 : usage(void)
     757              : {
     758            0 :         printf(_("%s decodes and displays PostgreSQL write-ahead logs for debugging.\n\n"),
     759              :                    progname);
     760            0 :         printf(_("Usage:\n"));
     761            0 :         printf(_("  %s [OPTION]... [STARTSEG [ENDSEG]]\n"), progname);
     762            0 :         printf(_("\nOptions:\n"));
     763            0 :         printf(_("  -b, --bkp-details      output detailed information about backup blocks\n"));
     764            0 :         printf(_("  -B, --block=N          with --relation, only show records that modify block N\n"));
     765            0 :         printf(_("  -e, --end=RECPTR       stop reading at WAL location RECPTR\n"));
     766            0 :         printf(_("  -f, --follow           keep retrying after reaching end of WAL\n"));
     767            0 :         printf(_("  -F, --fork=FORK        only show records that modify blocks in fork FORK;\n"
     768              :                          "                         valid names are main, fsm, vm, init\n"));
     769            0 :         printf(_("  -n, --limit=N          number of records to display\n"));
     770            0 :         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            0 :         printf(_("  -q, --quiet            do not print any output, except for errors\n"));
     774            0 :         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            0 :         printf(_("  -R, --relation=T/D/R   only show records that modify blocks in relation T/D/R\n"));
     777            0 :         printf(_("  -s, --start=RECPTR     start reading at WAL location RECPTR\n"));
     778            0 :         printf(_("  -t, --timeline=TLI     timeline from which to read WAL records\n"
     779              :                          "                         (default: 1 or the value used in STARTSEG)\n"));
     780            0 :         printf(_("  -V, --version          output version information, then exit\n"));
     781            0 :         printf(_("  -w, --fullpage         only show records with a full page write\n"));
     782            0 :         printf(_("  -x, --xid=XID          only show records with transaction ID XID\n"));
     783            0 :         printf(_("  -z, --stats[=record]   show statistics instead of records\n"
     784              :                          "                         (optionally, show per-record statistics)\n"));
     785            0 :         printf(_("  --save-fullpage=DIR    save full page images to DIR\n"));
     786            0 :         printf(_("  -?, --help             show this help, then exit\n"));
     787            0 :         printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     788            0 :         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
     789            0 : }
     790              : 
     791              : int
     792            0 : main(int argc, char **argv)
     793              : {
     794            0 :         uint32          xlogid;
     795            0 :         uint32          xrecoff;
     796            0 :         XLogReaderState *xlogreader_state;
     797            0 :         XLogDumpPrivate private;
     798            0 :         XLogDumpConfig config;
     799            0 :         XLogStats       stats;
     800            0 :         XLogRecord *record;
     801            0 :         XLogRecPtr      first_record;
     802            0 :         char       *waldir = NULL;
     803            0 :         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            0 :         int                     option;
     828            0 :         int                     optindex = 0;
     829              : 
     830              : #ifndef WIN32
     831            0 :         pqsignal(SIGINT, sigint_handler);
     832              : #endif
     833              : 
     834            0 :         pg_logging_init(argv[0]);
     835            0 :         set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_waldump"));
     836            0 :         progname = get_progname(argv[0]);
     837              : 
     838            0 :         if (argc > 1)
     839              :         {
     840            0 :                 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
     841              :                 {
     842            0 :                         usage();
     843            0 :                         exit(0);
     844              :                 }
     845            0 :                 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
     846              :                 {
     847            0 :                         puts("pg_waldump (PostgreSQL) " PG_VERSION);
     848            0 :                         exit(0);
     849              :                 }
     850            0 :         }
     851              : 
     852            0 :         memset(&private, 0, sizeof(XLogDumpPrivate));
     853            0 :         memset(&config, 0, sizeof(XLogDumpConfig));
     854            0 :         memset(&stats, 0, sizeof(XLogStats));
     855              : 
     856            0 :         private.timeline = 1;
     857            0 :         private.startptr = InvalidXLogRecPtr;
     858            0 :         private.endptr = InvalidXLogRecPtr;
     859            0 :         private.endptr_reached = false;
     860              : 
     861            0 :         config.quiet = false;
     862            0 :         config.bkp_details = false;
     863            0 :         config.stop_after_records = -1;
     864            0 :         config.already_displayed_records = 0;
     865            0 :         config.follow = false;
     866              :         /* filter_by_rmgr array was zeroed by memset above */
     867            0 :         config.filter_by_rmgr_enabled = false;
     868            0 :         config.filter_by_xid = InvalidTransactionId;
     869            0 :         config.filter_by_xid_enabled = false;
     870            0 :         config.filter_by_extended = false;
     871            0 :         config.filter_by_relation_enabled = false;
     872            0 :         config.filter_by_relation_block_enabled = false;
     873            0 :         config.filter_by_relation_forknum = InvalidForkNumber;
     874            0 :         config.filter_by_fpw = false;
     875            0 :         config.save_fullpage_path = NULL;
     876            0 :         config.stats = false;
     877            0 :         config.stats_per_record = false;
     878              : 
     879            0 :         stats.startptr = InvalidXLogRecPtr;
     880            0 :         stats.endptr = InvalidXLogRecPtr;
     881              : 
     882            0 :         if (argc <= 1)
     883              :         {
     884            0 :                 pg_log_error("no arguments specified");
     885            0 :                 goto bad_argument;
     886              :         }
     887              : 
     888            0 :         while ((option = getopt_long(argc, argv, "bB:e:fF:n:p:qr:R:s:t:wx:z",
     889            0 :                                                                  long_options, &optindex)) != -1)
     890              :         {
     891            0 :                 switch (option)
     892              :                 {
     893              :                         case 'b':
     894            0 :                                 config.bkp_details = true;
     895            0 :                                 break;
     896              :                         case 'B':
     897            0 :                                 if (sscanf(optarg, "%u", &config.filter_by_relation_block) != 1 ||
     898            0 :                                         !BlockNumberIsValid(config.filter_by_relation_block))
     899              :                                 {
     900            0 :                                         pg_log_error("invalid block number: \"%s\"", optarg);
     901            0 :                                         goto bad_argument;
     902              :                                 }
     903            0 :                                 config.filter_by_relation_block_enabled = true;
     904            0 :                                 config.filter_by_extended = true;
     905            0 :                                 break;
     906              :                         case 'e':
     907            0 :                                 if (sscanf(optarg, "%X/%08X", &xlogid, &xrecoff) != 2)
     908              :                                 {
     909            0 :                                         pg_log_error("invalid WAL location: \"%s\"",
     910              :                                                                  optarg);
     911            0 :                                         goto bad_argument;
     912              :                                 }
     913            0 :                                 private.endptr = (uint64) xlogid << 32 | xrecoff;
     914            0 :                                 break;
     915              :                         case 'f':
     916            0 :                                 config.follow = true;
     917            0 :                                 break;
     918              :                         case 'F':
     919            0 :                                 config.filter_by_relation_forknum = forkname_to_number(optarg);
     920            0 :                                 if (config.filter_by_relation_forknum == InvalidForkNumber)
     921              :                                 {
     922            0 :                                         pg_log_error("invalid fork name: \"%s\"", optarg);
     923            0 :                                         goto bad_argument;
     924              :                                 }
     925            0 :                                 config.filter_by_extended = true;
     926            0 :                                 break;
     927              :                         case 'n':
     928            0 :                                 if (sscanf(optarg, "%d", &config.stop_after_records) != 1)
     929              :                                 {
     930            0 :                                         pg_log_error("invalid value \"%s\" for option %s", optarg, "-n/--limit");
     931            0 :                                         goto bad_argument;
     932              :                                 }
     933            0 :                                 break;
     934              :                         case 'p':
     935            0 :                                 waldir = pg_strdup(optarg);
     936            0 :                                 break;
     937              :                         case 'q':
     938            0 :                                 config.quiet = true;
     939            0 :                                 break;
     940              :                         case 'r':
     941              :                                 {
     942            0 :                                         int                     rmid;
     943              : 
     944            0 :                                         if (pg_strcasecmp(optarg, "list") == 0)
     945              :                                         {
     946            0 :                                                 print_rmgr_list();
     947            0 :                                                 exit(EXIT_SUCCESS);
     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            0 :                                         if (sscanf(optarg, "custom%03d", &rmid) == 1)
     958              :                                         {
     959            0 :                                                 if (!RmgrIdIsCustom(rmid))
     960              :                                                 {
     961            0 :                                                         pg_log_error("custom resource manager \"%s\" does not exist",
     962              :                                                                                  optarg);
     963            0 :                                                         goto bad_argument;
     964              :                                                 }
     965            0 :                                                 config.filter_by_rmgr[rmid] = true;
     966            0 :                                                 config.filter_by_rmgr_enabled = true;
     967            0 :                                         }
     968              :                                         else
     969              :                                         {
     970              :                                                 /* then look for builtin rmgrs */
     971            0 :                                                 for (rmid = 0; rmid <= RM_MAX_BUILTIN_ID; rmid++)
     972              :                                                 {
     973            0 :                                                         if (pg_strcasecmp(optarg, GetRmgrDesc(rmid)->rm_name) == 0)
     974              :                                                         {
     975            0 :                                                                 config.filter_by_rmgr[rmid] = true;
     976            0 :                                                                 config.filter_by_rmgr_enabled = true;
     977            0 :                                                                 break;
     978              :                                                         }
     979            0 :                                                 }
     980            0 :                                                 if (rmid > RM_MAX_BUILTIN_ID)
     981              :                                                 {
     982            0 :                                                         pg_log_error("resource manager \"%s\" does not exist",
     983              :                                                                                  optarg);
     984            0 :                                                         goto bad_argument;
     985              :                                                 }
     986              :                                         }
     987            0 :                                 }
     988            0 :                                 break;
     989              :                         case 'R':
     990            0 :                                 if (sscanf(optarg, "%u/%u/%u",
     991            0 :                                                    &config.filter_by_relation.spcOid,
     992            0 :                                                    &config.filter_by_relation.dbOid,
     993            0 :                                                    &config.filter_by_relation.relNumber) != 3 ||
     994            0 :                                         !OidIsValid(config.filter_by_relation.spcOid) ||
     995            0 :                                         !RelFileNumberIsValid(config.filter_by_relation.relNumber))
     996              :                                 {
     997            0 :                                         pg_log_error("invalid relation specification: \"%s\"", optarg);
     998            0 :                                         pg_log_error_detail("Expecting \"tablespace OID/database OID/relation filenode\".");
     999            0 :                                         goto bad_argument;
    1000              :                                 }
    1001            0 :                                 config.filter_by_relation_enabled = true;
    1002            0 :                                 config.filter_by_extended = true;
    1003            0 :                                 break;
    1004              :                         case 's':
    1005            0 :                                 if (sscanf(optarg, "%X/%08X", &xlogid, &xrecoff) != 2)
    1006              :                                 {
    1007            0 :                                         pg_log_error("invalid WAL location: \"%s\"",
    1008              :                                                                  optarg);
    1009            0 :                                         goto bad_argument;
    1010              :                                 }
    1011              :                                 else
    1012            0 :                                         private.startptr = (uint64) xlogid << 32 | xrecoff;
    1013            0 :                                 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            0 :                                         char       *endptr;
    1023            0 :                                         unsigned long val;
    1024              : 
    1025            0 :                                         errno = 0;
    1026            0 :                                         val = strtoul(optarg, &endptr, 0);
    1027              : 
    1028            0 :                                         while (*endptr != '\0' && isspace((unsigned char) *endptr))
    1029            0 :                                                 endptr++;
    1030              : 
    1031            0 :                                         if (*endptr != '\0')
    1032              :                                         {
    1033            0 :                                                 pg_log_error("invalid value \"%s\" for option %s",
    1034              :                                                                          optarg, "-t/--timeline");
    1035            0 :                                                 goto bad_argument;
    1036              :                                         }
    1037              : 
    1038            0 :                                         if (errno == ERANGE || val < 1 || val > UINT_MAX)
    1039              :                                         {
    1040            0 :                                                 pg_log_error("%s must be in range %u..%u",
    1041              :                                                                          "-t/--timeline", 1, UINT_MAX);
    1042            0 :                                                 goto bad_argument;
    1043              :                                         }
    1044              : 
    1045            0 :                                         private.timeline = val;
    1046              : 
    1047            0 :                                         break;
    1048            0 :                                 }
    1049              :                         case 'w':
    1050            0 :                                 config.filter_by_fpw = true;
    1051            0 :                                 break;
    1052              :                         case 'x':
    1053            0 :                                 if (sscanf(optarg, "%u", &config.filter_by_xid) != 1)
    1054              :                                 {
    1055            0 :                                         pg_log_error("invalid transaction ID specification: \"%s\"",
    1056              :                                                                  optarg);
    1057            0 :                                         goto bad_argument;
    1058              :                                 }
    1059            0 :                                 config.filter_by_xid_enabled = true;
    1060            0 :                                 break;
    1061              :                         case 'z':
    1062            0 :                                 config.stats = true;
    1063            0 :                                 config.stats_per_record = false;
    1064            0 :                                 if (optarg)
    1065              :                                 {
    1066            0 :                                         if (strcmp(optarg, "record") == 0)
    1067            0 :                                                 config.stats_per_record = true;
    1068            0 :                                         else if (strcmp(optarg, "rmgr") != 0)
    1069              :                                         {
    1070            0 :                                                 pg_log_error("unrecognized value for option %s: %s",
    1071              :                                                                          "--stats", optarg);
    1072            0 :                                                 goto bad_argument;
    1073              :                                         }
    1074            0 :                                 }
    1075            0 :                                 break;
    1076              :                         case 1:
    1077            0 :                                 config.save_fullpage_path = pg_strdup(optarg);
    1078            0 :                                 break;
    1079              :                         default:
    1080            0 :                                 goto bad_argument;
    1081              :                 }
    1082              :         }
    1083              : 
    1084            0 :         if (config.filter_by_relation_block_enabled &&
    1085            0 :                 !config.filter_by_relation_enabled)
    1086              :         {
    1087            0 :                 pg_log_error("option %s requires option %s to be specified",
    1088              :                                          "-B/--block", "-R/--relation");
    1089            0 :                 goto bad_argument;
    1090              :         }
    1091              : 
    1092            0 :         if ((optind + 2) < argc)
    1093              :         {
    1094            0 :                 pg_log_error("too many command-line arguments (first is \"%s\")",
    1095              :                                          argv[optind + 2]);
    1096            0 :                 goto bad_argument;
    1097              :         }
    1098              : 
    1099            0 :         if (waldir != NULL)
    1100              :         {
    1101              :                 /* validate path points to directory */
    1102            0 :                 if (!verify_directory(waldir))
    1103              :                 {
    1104            0 :                         pg_log_error("could not open directory \"%s\": %m", waldir);
    1105            0 :                         goto bad_argument;
    1106              :                 }
    1107            0 :         }
    1108              : 
    1109            0 :         if (config.save_fullpage_path != NULL)
    1110            0 :                 create_fullpage_directory(config.save_fullpage_path);
    1111              : 
    1112              :         /* parse files as start/end boundaries, extract path if not specified */
    1113            0 :         if (optind < argc)
    1114              :         {
    1115            0 :                 char       *directory = NULL;
    1116            0 :                 char       *fname = NULL;
    1117            0 :                 int                     fd;
    1118            0 :                 XLogSegNo       segno;
    1119              : 
    1120            0 :                 split_path(argv[optind], &directory, &fname);
    1121              : 
    1122            0 :                 if (waldir == NULL && directory != NULL)
    1123              :                 {
    1124            0 :                         waldir = directory;
    1125              : 
    1126            0 :                         if (!verify_directory(waldir))
    1127            0 :                                 pg_fatal("could not open directory \"%s\": %m", waldir);
    1128            0 :                 }
    1129              : 
    1130            0 :                 waldir = identify_target_directory(waldir, fname);
    1131            0 :                 fd = open_file_in_directory(waldir, fname);
    1132            0 :                 if (fd < 0)
    1133            0 :                         pg_fatal("could not open file \"%s\"", fname);
    1134            0 :                 close(fd);
    1135              : 
    1136              :                 /* parse position from file */
    1137            0 :                 XLogFromFileName(fname, &private.timeline, &segno, WalSegSz);
    1138              : 
    1139            0 :                 if (!XLogRecPtrIsValid(private.startptr))
    1140            0 :                         XLogSegNoOffsetToRecPtr(segno, 0, WalSegSz, private.startptr);
    1141            0 :                 else if (!XLByteInSeg(private.startptr, segno, WalSegSz))
    1142              :                 {
    1143            0 :                         pg_log_error("start WAL location %X/%08X is not inside file \"%s\"",
    1144              :                                                  LSN_FORMAT_ARGS(private.startptr),
    1145              :                                                  fname);
    1146            0 :                         goto bad_argument;
    1147              :                 }
    1148              : 
    1149              :                 /* no second file specified, set end position */
    1150            0 :                 if (!(optind + 1 < argc) && !XLogRecPtrIsValid(private.endptr))
    1151            0 :                         XLogSegNoOffsetToRecPtr(segno + 1, 0, WalSegSz, private.endptr);
    1152              : 
    1153              :                 /* parse ENDSEG if passed */
    1154            0 :                 if (optind + 1 < argc)
    1155              :                 {
    1156            0 :                         XLogSegNo       endsegno;
    1157              : 
    1158              :                         /* ignore directory, already have that */
    1159            0 :                         split_path(argv[optind + 1], &directory, &fname);
    1160              : 
    1161            0 :                         fd = open_file_in_directory(waldir, fname);
    1162            0 :                         if (fd < 0)
    1163            0 :                                 pg_fatal("could not open file \"%s\"", fname);
    1164            0 :                         close(fd);
    1165              : 
    1166              :                         /* parse position from file */
    1167            0 :                         XLogFromFileName(fname, &private.timeline, &endsegno, WalSegSz);
    1168              : 
    1169            0 :                         if (endsegno < segno)
    1170            0 :                                 pg_fatal("ENDSEG %s is before STARTSEG %s",
    1171              :                                                  argv[optind + 1], argv[optind]);
    1172              : 
    1173            0 :                         if (!XLogRecPtrIsValid(private.endptr))
    1174            0 :                                 XLogSegNoOffsetToRecPtr(endsegno + 1, 0, WalSegSz,
    1175              :                                                                                 private.endptr);
    1176              : 
    1177              :                         /* set segno to endsegno for check of --end */
    1178            0 :                         segno = endsegno;
    1179            0 :                 }
    1180              : 
    1181              : 
    1182            0 :                 if (!XLByteInSeg(private.endptr, segno, WalSegSz) &&
    1183            0 :                         private.endptr != (segno + 1) * WalSegSz)
    1184              :                 {
    1185            0 :                         pg_log_error("end WAL location %X/%08X is not inside file \"%s\"",
    1186              :                                                  LSN_FORMAT_ARGS(private.endptr),
    1187              :                                                  argv[argc - 1]);
    1188            0 :                         goto bad_argument;
    1189              :                 }
    1190            0 :         }
    1191              :         else
    1192            0 :                 waldir = identify_target_directory(waldir, NULL);
    1193              : 
    1194              :         /* we don't know what to print */
    1195            0 :         if (!XLogRecPtrIsValid(private.startptr))
    1196              :         {
    1197            0 :                 pg_log_error("no start WAL location given");
    1198            0 :                 goto bad_argument;
    1199              :         }
    1200              : 
    1201              :         /* done with argument parsing, do the actual work */
    1202              : 
    1203              :         /* we have everything we need, start reading */
    1204            0 :         xlogreader_state =
    1205            0 :                 XLogReaderAllocate(WalSegSz, waldir,
    1206            0 :                                                    XL_ROUTINE(.page_read = WALDumpReadPage,
    1207              :                                                                           .segment_open = WALDumpOpenSegment,
    1208              :                                                                           .segment_close = WALDumpCloseSegment),
    1209              :                                                    &private);
    1210            0 :         if (!xlogreader_state)
    1211            0 :                 pg_fatal("out of memory while allocating a WAL reading processor");
    1212              : 
    1213              :         /* first find a valid recptr to start from */
    1214            0 :         first_record = XLogFindNextRecord(xlogreader_state, private.startptr);
    1215              : 
    1216            0 :         if (!XLogRecPtrIsValid(first_record))
    1217            0 :                 pg_fatal("could not find a valid record after %X/%08X",
    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            0 :         if (first_record != private.startptr &&
    1226            0 :                 XLogSegmentOffset(private.startptr, WalSegSz) != 0)
    1227            0 :                 pg_log_info(ngettext("first record is after %X/%08X, at %X/%08X, skipping over %u byte",
    1228              :                                                          "first record is after %X/%08X, at %X/%08X, 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            0 :         if (config.stats == true && !config.quiet)
    1235            0 :                 stats.startptr = first_record;
    1236              : 
    1237            0 :         for (;;)
    1238              :         {
    1239            0 :                 if (time_to_stop)
    1240              :                 {
    1241              :                         /* We've been Ctrl-C'ed, so leave */
    1242            0 :                         break;
    1243              :                 }
    1244              : 
    1245              :                 /* try to read the next record */
    1246            0 :                 record = XLogReadRecord(xlogreader_state, &errormsg);
    1247            0 :                 if (!record)
    1248              :                 {
    1249            0 :                         if (!config.follow || private.endptr_reached)
    1250            0 :                                 break;
    1251              :                         else
    1252              :                         {
    1253            0 :                                 pg_usleep(1000000L);    /* 1 second */
    1254            0 :                                 continue;
    1255              :                         }
    1256              :                 }
    1257              : 
    1258              :                 /* apply all specified filters */
    1259            0 :                 if (config.filter_by_rmgr_enabled &&
    1260            0 :                         !config.filter_by_rmgr[record->xl_rmid])
    1261            0 :                         continue;
    1262              : 
    1263            0 :                 if (config.filter_by_xid_enabled &&
    1264            0 :                         config.filter_by_xid != record->xl_xid)
    1265            0 :                         continue;
    1266              : 
    1267              :                 /* check for extended filtering */
    1268            0 :                 if (config.filter_by_extended &&
    1269            0 :                         !XLogRecordMatchesRelationBlock(xlogreader_state,
    1270            0 :                                                                                         config.filter_by_relation_enabled ?
    1271            0 :                                                                                         config.filter_by_relation :
    1272            0 :                                                                                         emptyRelFileLocator,
    1273            0 :                                                                                         config.filter_by_relation_block_enabled ?
    1274            0 :                                                                                         config.filter_by_relation_block :
    1275              :                                                                                         InvalidBlockNumber,
    1276            0 :                                                                                         config.filter_by_relation_forknum))
    1277            0 :                         continue;
    1278              : 
    1279            0 :                 if (config.filter_by_fpw && !XLogRecordHasFPW(xlogreader_state))
    1280            0 :                         continue;
    1281              : 
    1282              :                 /* perform any per-record work */
    1283            0 :                 if (!config.quiet)
    1284              :                 {
    1285            0 :                         if (config.stats == true)
    1286              :                         {
    1287            0 :                                 XLogRecStoreStats(&stats, xlogreader_state);
    1288            0 :                                 stats.endptr = xlogreader_state->EndRecPtr;
    1289            0 :                         }
    1290              :                         else
    1291            0 :                                 XLogDumpDisplayRecord(&config, xlogreader_state);
    1292            0 :                 }
    1293              : 
    1294              :                 /* save full pages if requested */
    1295            0 :                 if (config.save_fullpage_path != NULL)
    1296            0 :                         XLogRecordSaveFPWs(xlogreader_state, config.save_fullpage_path);
    1297              : 
    1298              :                 /* check whether we printed enough */
    1299            0 :                 config.already_displayed_records++;
    1300            0 :                 if (config.stop_after_records > 0 &&
    1301            0 :                         config.already_displayed_records >= config.stop_after_records)
    1302            0 :                         break;
    1303              :         }
    1304              : 
    1305            0 :         if (config.stats == true && !config.quiet)
    1306            0 :                 XLogDumpDisplayStats(&config, &stats);
    1307              : 
    1308            0 :         if (time_to_stop)
    1309            0 :                 exit(0);
    1310              : 
    1311            0 :         if (errormsg)
    1312            0 :                 pg_fatal("error in WAL record at %X/%08X: %s",
    1313              :                                  LSN_FORMAT_ARGS(xlogreader_state->ReadRecPtr),
    1314              :                                  errormsg);
    1315              : 
    1316            0 :         XLogReaderFree(xlogreader_state);
    1317              : 
    1318            0 :         return EXIT_SUCCESS;
    1319              : 
    1320              : bad_argument:
    1321            0 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    1322            0 :         return EXIT_FAILURE;
    1323            0 : }
        

Generated by: LCOV version 2.3.2-1