LCOV - code coverage report
Current view: top level - src/bin/pg_waldump - pg_waldump.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16beta1 Lines: 263 529 49.7 %
Date: 2023-05-30 18:12:27 Functions: 13 19 68.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * pg_waldump.c - decode and display WAL
       4             :  *
       5             :  * Copyright (c) 2013-2023, 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             :     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             :     }
     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          86 : verify_directory(const char *directory)
     114             : {
     115          86 :     DIR        *dir = opendir(directory);
     116             : 
     117          86 :     if (dir == NULL)
     118           2 :         return false;
     119          84 :     closedir(dir);
     120          84 :     return true;
     121             : }
     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           2 : create_fullpage_directory(char *path)
     129             : {
     130             :     int         ret;
     131             : 
     132           2 :     switch ((ret = pg_check_dir(path)))
     133             :     {
     134           2 :         case 0:
     135             :             /* Does not exist, so create it */
     136           2 :             if (pg_mkdir_p(path, pg_dir_create_mode) < 0)
     137           0 :                 pg_fatal("could not create directory \"%s\": %m", path);
     138           2 :             break;
     139           0 :         case 1:
     140             :             /* Present and empty, so do nothing */
     141           0 :             break;
     142           0 :         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             :             break;
     148           0 :         default:
     149             :             /* Trouble accessing directory */
     150           0 :             pg_fatal("could not access directory \"%s\": %m", path);
     151             :     }
     152           2 : }
     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           2 : split_path(const char *path, char **dir, char **fname)
     162             : {
     163             :     char       *sep;
     164             : 
     165             :     /* split filepath into directory & filename */
     166           2 :     sep = strrchr(path, '/');
     167             : 
     168             :     /* directory path */
     169           2 :     if (sep != NULL)
     170             :     {
     171           2 :         *dir = pnstrdup(path, sep - path);
     172           2 :         *fname = pg_strdup(sep + 1);
     173             :     }
     174             :     /* local directory */
     175             :     else
     176             :     {
     177           0 :         *dir = NULL;
     178           0 :         *fname = pg_strdup(path);
     179             :     }
     180           2 : }
     181             : 
     182             : /*
     183             :  * Open the file in the valid target directory.
     184             :  *
     185             :  * return a read only fd
     186             :  */
     187             : static int
     188         168 : open_file_in_directory(const char *directory, const char *fname)
     189             : {
     190         168 :     int         fd = -1;
     191             :     char        fpath[MAXPGPATH];
     192             : 
     193             :     Assert(directory != NULL);
     194             : 
     195         168 :     snprintf(fpath, MAXPGPATH, "%s/%s", directory, fname);
     196         168 :     fd = open(fpath, O_RDONLY | PG_BINARY, 0);
     197             : 
     198         168 :     if (fd < 0 && errno != ENOENT)
     199           0 :         pg_fatal("could not open file \"%s\": %m", fname);
     200         168 :     return fd;
     201             : }
     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          84 : search_directory(const char *directory, const char *fname)
     211             : {
     212          84 :     int         fd = -1;
     213             :     DIR        *xldir;
     214             : 
     215             :     /* open file if valid filename is provided */
     216          84 :     if (fname != NULL)
     217           2 :         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          82 :     else if ((xldir = opendir(directory)) != NULL)
     225             :     {
     226             :         struct dirent *xlde;
     227             : 
     228         240 :         while ((xlde = readdir(xldir)) != NULL)
     229             :         {
     230         240 :             if (IsXLogFileName(xlde->d_name))
     231             :             {
     232          82 :                 fd = open_file_in_directory(directory, xlde->d_name);
     233          82 :                 fname = pg_strdup(xlde->d_name);
     234          82 :                 break;
     235             :             }
     236             :         }
     237             : 
     238          82 :         closedir(xldir);
     239             :     }
     240             : 
     241             :     /* set WalSegSz if file is successfully opened */
     242          84 :     if (fd >= 0)
     243             :     {
     244             :         PGAlignedXLogBlock buf;
     245             :         int         r;
     246             : 
     247          84 :         r = read(fd, buf.data, XLOG_BLCKSZ);
     248          84 :         if (r == XLOG_BLCKSZ)
     249             :         {
     250          84 :             XLogLongPageHeader longhdr = (XLogLongPageHeader) buf.data;
     251             : 
     252          84 :             WalSegSz = longhdr->xlp_seg_size;
     253             : 
     254          84 :             if (!IsValidWalSegSize(WalSegSz))
     255           2 :                 pg_fatal(ngettext("WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" header specifies %d byte",
     256             :                                   "WAL segment size must be a power of two between 1 MB and 1 GB, but the WAL file \"%s\" header specifies %d bytes",
     257             :                                   WalSegSz),
     258             :                          fname, WalSegSz);
     259             :         }
     260           0 :         else if (r < 0)
     261           0 :             pg_fatal("could not read file \"%s\": %m",
     262             :                      fname);
     263             :         else
     264           0 :             pg_fatal("could not read file \"%s\": read %d of %d",
     265             :                      fname, r, XLOG_BLCKSZ);
     266          82 :         close(fd);
     267          82 :         return true;
     268             :     }
     269             : 
     270           0 :     return false;
     271             : }
     272             : 
     273             : /*
     274             :  * Identify the target directory.
     275             :  *
     276             :  * Try to find the file in several places:
     277             :  * if directory != NULL:
     278             :  *   directory /
     279             :  *   directory / XLOGDIR /
     280             :  * else
     281             :  *   .
     282             :  *   XLOGDIR /
     283             :  *   $PGDATA / XLOGDIR /
     284             :  *
     285             :  * The valid target directory is returned.
     286             :  */
     287             : static char *
     288          84 : identify_target_directory(char *directory, char *fname)
     289             : {
     290             :     char        fpath[MAXPGPATH];
     291             : 
     292          84 :     if (directory != NULL)
     293             :     {
     294          84 :         if (search_directory(directory, fname))
     295          82 :             return pg_strdup(directory);
     296             : 
     297             :         /* directory / XLOGDIR */
     298           0 :         snprintf(fpath, MAXPGPATH, "%s/%s", directory, XLOGDIR);
     299           0 :         if (search_directory(fpath, fname))
     300           0 :             return pg_strdup(fpath);
     301             :     }
     302             :     else
     303             :     {
     304             :         const char *datadir;
     305             : 
     306             :         /* current directory */
     307           0 :         if (search_directory(".", fname))
     308           0 :             return pg_strdup(".");
     309             :         /* XLOGDIR */
     310           0 :         if (search_directory(XLOGDIR, fname))
     311           0 :             return pg_strdup(XLOGDIR);
     312             : 
     313           0 :         datadir = getenv("PGDATA");
     314             :         /* $PGDATA / XLOGDIR */
     315           0 :         if (datadir != NULL)
     316             :         {
     317           0 :             snprintf(fpath, MAXPGPATH, "%s/%s", datadir, XLOGDIR);
     318           0 :             if (search_directory(fpath, fname))
     319           0 :                 return pg_strdup(fpath);
     320             :         }
     321             :     }
     322             : 
     323             :     /* could not locate WAL file */
     324           0 :     if (fname)
     325           0 :         pg_fatal("could not locate WAL file \"%s\"", fname);
     326             :     else
     327           0 :         pg_fatal("could not find any WAL file");
     328             : 
     329             :     return NULL;                /* not reached */
     330             : }
     331             : 
     332             : /* pg_waldump's XLogReaderRoutine->segment_open callback */
     333             : static void
     334          82 : WALDumpOpenSegment(XLogReaderState *state, XLogSegNo nextSegNo,
     335             :                    TimeLineID *tli_p)
     336             : {
     337          82 :     TimeLineID  tli = *tli_p;
     338             :     char        fname[MAXPGPATH];
     339             :     int         tries;
     340             : 
     341          82 :     XLogFileName(fname, tli, nextSegNo, state->segcxt.ws_segsize);
     342             : 
     343             :     /*
     344             :      * In follow mode there is a short period of time after the server has
     345             :      * written the end of the previous file before the new file is available.
     346             :      * So we loop for 5 seconds looking for the file to appear before giving
     347             :      * up.
     348             :      */
     349          82 :     for (tries = 0; tries < 10; tries++)
     350             :     {
     351          82 :         state->seg.ws_file = open_file_in_directory(state->segcxt.ws_dir, fname);
     352          82 :         if (state->seg.ws_file >= 0)
     353          82 :             return;
     354           0 :         if (errno == ENOENT)
     355             :         {
     356           0 :             int         save_errno = errno;
     357             : 
     358             :             /* File not there yet, try again */
     359           0 :             pg_usleep(500 * 1000);
     360             : 
     361           0 :             errno = save_errno;
     362           0 :             continue;
     363             :         }
     364             :         /* Any other error, fall through and fail */
     365           0 :         break;
     366             :     }
     367             : 
     368           0 :     pg_fatal("could not find file \"%s\": %m", fname);
     369             : }
     370             : 
     371             : /*
     372             :  * pg_waldump's XLogReaderRoutine->segment_close callback.  Same as
     373             :  * wal_segment_close
     374             :  */
     375             : static void
     376          82 : WALDumpCloseSegment(XLogReaderState *state)
     377             : {
     378          82 :     close(state->seg.ws_file);
     379             :     /* need to check errno? */
     380          82 :     state->seg.ws_file = -1;
     381          82 : }
     382             : 
     383             : /* pg_waldump's XLogReaderRoutine->page_read callback */
     384             : static int
     385        1446 : WALDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen,
     386             :                 XLogRecPtr targetPtr, char *readBuff)
     387             : {
     388        1446 :     XLogDumpPrivate *private = state->private_data;
     389        1446 :     int         count = XLOG_BLCKSZ;
     390             :     WALReadError errinfo;
     391             : 
     392        1446 :     if (private->endptr != InvalidXLogRecPtr)
     393             :     {
     394        1446 :         if (targetPagePtr + XLOG_BLCKSZ <= private->endptr)
     395        1284 :             count = XLOG_BLCKSZ;
     396         162 :         else if (targetPagePtr + reqLen <= private->endptr)
     397          80 :             count = private->endptr - targetPagePtr;
     398             :         else
     399             :         {
     400          82 :             private->endptr_reached = true;
     401          82 :             return -1;
     402             :         }
     403             :     }
     404             : 
     405        1364 :     if (!WALRead(state, readBuff, targetPagePtr, count, private->timeline,
     406             :                  &errinfo))
     407             :     {
     408           0 :         WALOpenSegment *seg = &errinfo.wre_seg;
     409             :         char        fname[MAXPGPATH];
     410             : 
     411           0 :         XLogFileName(fname, seg->ws_tli, seg->ws_segno,
     412             :                      state->segcxt.ws_segsize);
     413             : 
     414           0 :         if (errinfo.wre_errno != 0)
     415             :         {
     416           0 :             errno = errinfo.wre_errno;
     417           0 :             pg_fatal("could not read from file %s, offset %d: %m",
     418             :                      fname, errinfo.wre_off);
     419             :         }
     420             :         else
     421           0 :             pg_fatal("could not read from file %s, offset %d: read %d of %d",
     422             :                      fname, errinfo.wre_off, errinfo.wre_read,
     423             :                      errinfo.wre_req);
     424             :     }
     425             : 
     426        1364 :     return count;
     427             : }
     428             : 
     429             : /*
     430             :  * Boolean to return whether the given WAL record matches a specific relation
     431             :  * and optionally block.
     432             :  */
     433             : static bool
     434       47282 : XLogRecordMatchesRelationBlock(XLogReaderState *record,
     435             :                                RelFileLocator matchRlocator,
     436             :                                BlockNumber matchBlock,
     437             :                                ForkNumber matchFork)
     438             : {
     439             :     int         block_id;
     440             : 
     441       93478 :     for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
     442             :     {
     443             :         RelFileLocator rlocator;
     444             :         ForkNumber  forknum;
     445             :         BlockNumber blk;
     446             : 
     447       46596 :         if (!XLogRecGetBlockTagExtended(record, block_id,
     448             :                                         &rlocator, &forknum, &blk, NULL))
     449          40 :             continue;
     450             : 
     451       46556 :         if ((matchFork == InvalidForkNumber || matchFork == forknum) &&
     452       46556 :             (RelFileLocatorEquals(matchRlocator, emptyRelFileLocator) ||
     453       46556 :              RelFileLocatorEquals(matchRlocator, rlocator)) &&
     454           0 :             (matchBlock == InvalidBlockNumber || matchBlock == blk))
     455         400 :             return true;
     456             :     }
     457             : 
     458       46882 :     return false;
     459             : }
     460             : 
     461             : /*
     462             :  * Boolean to return whether the given WAL record contains a full page write.
     463             :  */
     464             : static bool
     465           0 : XLogRecordHasFPW(XLogReaderState *record)
     466             : {
     467             :     int         block_id;
     468             : 
     469           0 :     for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
     470             :     {
     471           0 :         if (!XLogRecHasBlockRef(record, block_id))
     472           0 :             continue;
     473             : 
     474           0 :         if (XLogRecHasBlockImage(record, block_id))
     475           0 :             return true;
     476             :     }
     477             : 
     478           0 :     return false;
     479             : }
     480             : 
     481             : /*
     482             :  * Function to externally save all FPWs stored in the given WAL record.
     483             :  * Decompression is applied to all the blocks saved, if necessary.
     484             :  */
     485             : static void
     486         400 : XLogRecordSaveFPWs(XLogReaderState *record, const char *savepath)
     487             : {
     488             :     int         block_id;
     489             : 
     490         800 :     for (block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
     491             :     {
     492             :         PGAlignedBlock buf;
     493             :         Page        page;
     494             :         char        filename[MAXPGPATH];
     495             :         char        forkname[FORKNAMECHARS + 2];    /* _ + terminating zero */
     496             :         FILE       *file;
     497             :         BlockNumber blk;
     498             :         RelFileLocator rnode;
     499             :         ForkNumber  fork;
     500             : 
     501         400 :         if (!XLogRecHasBlockRef(record, block_id))
     502         398 :             continue;
     503             : 
     504         400 :         if (!XLogRecHasBlockImage(record, block_id))
     505         398 :             continue;
     506             : 
     507           2 :         page = (Page) buf.data;
     508             : 
     509             :         /* Full page exists, so let's save it */
     510           2 :         if (!RestoreBlockImage(record, block_id, page))
     511           0 :             pg_fatal("%s", record->errormsg_buf);
     512             : 
     513           2 :         (void) XLogRecGetBlockTagExtended(record, block_id,
     514             :                                           &rnode, &fork, &blk, NULL);
     515             : 
     516           2 :         if (fork >= 0 && fork <= MAX_FORKNUM)
     517           2 :             sprintf(forkname, "_%s", forkNames[fork]);
     518             :         else
     519           0 :             pg_fatal("invalid fork number: %u", fork);
     520             : 
     521           2 :         snprintf(filename, MAXPGPATH, "%s/%08X-%08X.%u.%u.%u.%u%s", savepath,
     522           2 :                  LSN_FORMAT_ARGS(record->ReadRecPtr),
     523             :                  rnode.spcOid, rnode.dbOid, rnode.relNumber, blk, forkname);
     524             : 
     525           2 :         file = fopen(filename, PG_BINARY_W);
     526           2 :         if (!file)
     527           0 :             pg_fatal("could not open file \"%s\": %m", filename);
     528             : 
     529           2 :         if (fwrite(page, BLCKSZ, 1, file) != 1)
     530           0 :             pg_fatal("could not write file \"%s\": %m", filename);
     531             : 
     532           2 :         if (fclose(file) != 0)
     533           0 :             pg_fatal("could not close file \"%s\": %m", filename);
     534             :     }
     535         400 : }
     536             : 
     537             : /*
     538             :  * Print a record to stdout
     539             :  */
     540             : static void
     541           0 : XLogDumpDisplayRecord(XLogDumpConfig *config, XLogReaderState *record)
     542             : {
     543             :     const char *id;
     544           0 :     const RmgrDescData *desc = GetRmgrDesc(XLogRecGetRmid(record));
     545             :     uint32      rec_len;
     546             :     uint32      fpi_len;
     547           0 :     uint8       info = XLogRecGetInfo(record);
     548           0 :     XLogRecPtr  xl_prev = XLogRecGetPrev(record);
     549             :     StringInfoData s;
     550             : 
     551           0 :     XLogRecGetLen(record, &rec_len, &fpi_len);
     552             : 
     553           0 :     printf("rmgr: %-11s len (rec/tot): %6u/%6u, tx: %10u, lsn: %X/%08X, prev %X/%08X, ",
     554             :            desc->rm_name,
     555             :            rec_len, XLogRecGetTotalLen(record),
     556             :            XLogRecGetXid(record),
     557             :            LSN_FORMAT_ARGS(record->ReadRecPtr),
     558             :            LSN_FORMAT_ARGS(xl_prev));
     559             : 
     560           0 :     id = desc->rm_identify(info);
     561           0 :     if (id == NULL)
     562           0 :         printf("desc: UNKNOWN (%x) ", info & ~XLR_INFO_MASK);
     563             :     else
     564           0 :         printf("desc: %s ", id);
     565             : 
     566           0 :     initStringInfo(&s);
     567           0 :     desc->rm_desc(&s, record);
     568           0 :     printf("%s", s.data);
     569             : 
     570           0 :     resetStringInfo(&s);
     571           0 :     XLogRecGetBlockRefInfo(record, true, config->bkp_details, &s, NULL);
     572           0 :     printf("%s", s.data);
     573           0 :     pfree(s.data);
     574           0 : }
     575             : 
     576             : /*
     577             :  * Display a single row of record counts and sizes for an rmgr or record.
     578             :  */
     579             : static void
     580           0 : XLogDumpStatsRow(const char *name,
     581             :                  uint64 n, uint64 total_count,
     582             :                  uint64 rec_len, uint64 total_rec_len,
     583             :                  uint64 fpi_len, uint64 total_fpi_len,
     584             :                  uint64 tot_len, uint64 total_len)
     585             : {
     586             :     double      n_pct,
     587             :                 rec_len_pct,
     588             :                 fpi_len_pct,
     589             :                 tot_len_pct;
     590             : 
     591           0 :     n_pct = 0;
     592           0 :     if (total_count != 0)
     593           0 :         n_pct = 100 * (double) n / total_count;
     594             : 
     595           0 :     rec_len_pct = 0;
     596           0 :     if (total_rec_len != 0)
     597           0 :         rec_len_pct = 100 * (double) rec_len / total_rec_len;
     598             : 
     599           0 :     fpi_len_pct = 0;
     600           0 :     if (total_fpi_len != 0)
     601           0 :         fpi_len_pct = 100 * (double) fpi_len / total_fpi_len;
     602             : 
     603           0 :     tot_len_pct = 0;
     604           0 :     if (total_len != 0)
     605           0 :         tot_len_pct = 100 * (double) tot_len / total_len;
     606             : 
     607           0 :     printf("%-27s "
     608             :            "%20" INT64_MODIFIER "u (%6.02f) "
     609             :            "%20" INT64_MODIFIER "u (%6.02f) "
     610             :            "%20" INT64_MODIFIER "u (%6.02f) "
     611             :            "%20" INT64_MODIFIER "u (%6.02f)\n",
     612             :            name, n, n_pct, rec_len, rec_len_pct, fpi_len, fpi_len_pct,
     613             :            tot_len, tot_len_pct);
     614           0 : }
     615             : 
     616             : 
     617             : /*
     618             :  * Display summary statistics about the records seen so far.
     619             :  */
     620             : static void
     621           0 : XLogDumpDisplayStats(XLogDumpConfig *config, XLogStats *stats)
     622             : {
     623             :     int         ri,
     624             :                 rj;
     625           0 :     uint64      total_count = 0;
     626           0 :     uint64      total_rec_len = 0;
     627           0 :     uint64      total_fpi_len = 0;
     628           0 :     uint64      total_len = 0;
     629             :     double      rec_len_pct,
     630             :                 fpi_len_pct;
     631             : 
     632             :     /*
     633             :      * Leave if no stats have been computed yet, as tracked by the end LSN.
     634             :      */
     635           0 :     if (XLogRecPtrIsInvalid(stats->endptr))
     636           0 :         return;
     637             : 
     638             :     /*
     639             :      * Each row shows its percentages of the total, so make a first pass to
     640             :      * calculate column totals.
     641             :      */
     642             : 
     643           0 :     for (ri = 0; ri <= RM_MAX_ID; ri++)
     644             :     {
     645           0 :         if (!RmgrIdIsValid(ri))
     646           0 :             continue;
     647             : 
     648           0 :         total_count += stats->rmgr_stats[ri].count;
     649           0 :         total_rec_len += stats->rmgr_stats[ri].rec_len;
     650           0 :         total_fpi_len += stats->rmgr_stats[ri].fpi_len;
     651             :     }
     652           0 :     total_len = total_rec_len + total_fpi_len;
     653             : 
     654           0 :     printf("WAL statistics between %X/%X and %X/%X:\n",
     655             :            LSN_FORMAT_ARGS(stats->startptr), LSN_FORMAT_ARGS(stats->endptr));
     656             : 
     657             :     /*
     658             :      * 27 is strlen("Transaction/COMMIT_PREPARED"), 20 is strlen(2^64), 8 is
     659             :      * strlen("(100.00%)")
     660             :      */
     661             : 
     662           0 :     printf("%-27s %20s %8s %20s %8s %20s %8s %20s %8s\n"
     663             :            "%-27s %20s %8s %20s %8s %20s %8s %20s %8s\n",
     664             :            "Type", "N", "(%)", "Record size", "(%)", "FPI size", "(%)", "Combined size", "(%)",
     665             :            "----", "-", "---", "-----------", "---", "--------", "---", "-------------", "---");
     666             : 
     667           0 :     for (ri = 0; ri <= RM_MAX_ID; ri++)
     668             :     {
     669             :         uint64      count,
     670             :                     rec_len,
     671             :                     fpi_len,
     672             :                     tot_len;
     673             :         const RmgrDescData *desc;
     674             : 
     675           0 :         if (!RmgrIdIsValid(ri))
     676           0 :             continue;
     677             : 
     678           0 :         desc = GetRmgrDesc(ri);
     679             : 
     680           0 :         if (!config->stats_per_record)
     681             :         {
     682           0 :             count = stats->rmgr_stats[ri].count;
     683           0 :             rec_len = stats->rmgr_stats[ri].rec_len;
     684           0 :             fpi_len = stats->rmgr_stats[ri].fpi_len;
     685           0 :             tot_len = rec_len + fpi_len;
     686             : 
     687           0 :             if (RmgrIdIsCustom(ri) && count == 0)
     688           0 :                 continue;
     689             : 
     690           0 :             XLogDumpStatsRow(desc->rm_name,
     691             :                              count, total_count, rec_len, total_rec_len,
     692             :                              fpi_len, total_fpi_len, tot_len, total_len);
     693             :         }
     694             :         else
     695             :         {
     696           0 :             for (rj = 0; rj < MAX_XLINFO_TYPES; rj++)
     697             :             {
     698             :                 const char *id;
     699             : 
     700           0 :                 count = stats->record_stats[ri][rj].count;
     701           0 :                 rec_len = stats->record_stats[ri][rj].rec_len;
     702           0 :                 fpi_len = stats->record_stats[ri][rj].fpi_len;
     703           0 :                 tot_len = rec_len + fpi_len;
     704             : 
     705             :                 /* Skip undefined combinations and ones that didn't occur */
     706           0 :                 if (count == 0)
     707           0 :                     continue;
     708             : 
     709             :                 /* the upper four bits in xl_info are the rmgr's */
     710           0 :                 id = desc->rm_identify(rj << 4);
     711           0 :                 if (id == NULL)
     712           0 :                     id = psprintf("UNKNOWN (%x)", rj << 4);
     713             : 
     714           0 :                 XLogDumpStatsRow(psprintf("%s/%s", desc->rm_name, id),
     715             :                                  count, total_count, rec_len, total_rec_len,
     716             :                                  fpi_len, total_fpi_len, tot_len, total_len);
     717             :             }
     718             :         }
     719             :     }
     720             : 
     721           0 :     printf("%-27s %20s %8s %20s %8s %20s %8s %20s\n",
     722             :            "", "--------", "", "--------", "", "--------", "", "--------");
     723             : 
     724             :     /*
     725             :      * The percentages in earlier rows were calculated against the column
     726             :      * total, but the ones that follow are against the row total. Note that
     727             :      * these are displayed with a % symbol to differentiate them from the
     728             :      * earlier ones, and are thus up to 9 characters long.
     729             :      */
     730             : 
     731           0 :     rec_len_pct = 0;
     732           0 :     if (total_len != 0)
     733           0 :         rec_len_pct = 100 * (double) total_rec_len / total_len;
     734             : 
     735           0 :     fpi_len_pct = 0;
     736           0 :     if (total_len != 0)
     737           0 :         fpi_len_pct = 100 * (double) total_fpi_len / total_len;
     738             : 
     739           0 :     printf("%-27s "
     740             :            "%20" INT64_MODIFIER "u %-9s"
     741             :            "%20" INT64_MODIFIER "u %-9s"
     742             :            "%20" INT64_MODIFIER "u %-9s"
     743             :            "%20" INT64_MODIFIER "u %-6s\n",
     744             :            "Total", stats->count, "",
     745             :            total_rec_len, psprintf("[%.02f%%]", rec_len_pct),
     746             :            total_fpi_len, psprintf("[%.02f%%]", fpi_len_pct),
     747             :            total_len, "[100%]");
     748             : }
     749             : 
     750             : static void
     751           2 : usage(void)
     752             : {
     753           2 :     printf(_("%s decodes and displays PostgreSQL write-ahead logs for debugging.\n\n"),
     754             :            progname);
     755           2 :     printf(_("Usage:\n"));
     756           2 :     printf(_("  %s [OPTION]... [STARTSEG [ENDSEG]]\n"), progname);
     757           2 :     printf(_("\nOptions:\n"));
     758           2 :     printf(_("  -b, --bkp-details      output detailed information about backup blocks\n"));
     759           2 :     printf(_("  -B, --block=N          with --relation, only show records that modify block N\n"));
     760           2 :     printf(_("  -e, --end=RECPTR       stop reading at WAL location RECPTR\n"));
     761           2 :     printf(_("  -f, --follow           keep retrying after reaching end of WAL\n"));
     762           2 :     printf(_("  -F, --fork=FORK        only show records that modify blocks in fork FORK;\n"
     763             :              "                         valid names are main, fsm, vm, init\n"));
     764           2 :     printf(_("  -n, --limit=N          number of records to display\n"));
     765           2 :     printf(_("  -p, --path=PATH        directory in which to find WAL segment files or a\n"
     766             :              "                         directory with a ./pg_wal that contains such files\n"
     767             :              "                         (default: current directory, ./pg_wal, $PGDATA/pg_wal)\n"));
     768           2 :     printf(_("  -q, --quiet            do not print any output, except for errors\n"));
     769           2 :     printf(_("  -r, --rmgr=RMGR        only show records generated by resource manager RMGR;\n"
     770             :              "                         use --rmgr=list to list valid resource manager names\n"));
     771           2 :     printf(_("  -R, --relation=T/D/R   only show records that modify blocks in relation T/D/R\n"));
     772           2 :     printf(_("  -s, --start=RECPTR     start reading at WAL location RECPTR\n"));
     773           2 :     printf(_("  -t, --timeline=TLI     timeline from which to read WAL records\n"
     774             :              "                         (default: 1 or the value used in STARTSEG)\n"));
     775           2 :     printf(_("  -V, --version          output version information, then exit\n"));
     776           2 :     printf(_("  -w, --fullpage         only show records with a full page write\n"));
     777           2 :     printf(_("  -x, --xid=XID          only show records with transaction ID XID\n"));
     778           2 :     printf(_("  -z, --stats[=record]   show statistics instead of records\n"
     779             :              "                         (optionally, show per-record statistics)\n"));
     780           2 :     printf(_("  --save-fullpage=DIR    save full page images to DIR\n"));
     781           2 :     printf(_("  -?, --help             show this help, then exit\n"));
     782           2 :     printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     783           2 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
     784           2 : }
     785             : 
     786             : int
     787         244 : main(int argc, char **argv)
     788             : {
     789             :     uint32      xlogid;
     790             :     uint32      xrecoff;
     791             :     XLogReaderState *xlogreader_state;
     792             :     XLogDumpPrivate private;
     793             :     XLogDumpConfig config;
     794             :     XLogStats   stats;
     795             :     XLogRecord *record;
     796             :     XLogRecPtr  first_record;
     797         244 :     char       *waldir = NULL;
     798             :     char       *errormsg;
     799             : 
     800             :     static struct option long_options[] = {
     801             :         {"bkp-details", no_argument, NULL, 'b'},
     802             :         {"block", required_argument, NULL, 'B'},
     803             :         {"end", required_argument, NULL, 'e'},
     804             :         {"follow", no_argument, NULL, 'f'},
     805             :         {"fork", required_argument, NULL, 'F'},
     806             :         {"fullpage", no_argument, NULL, 'w'},
     807             :         {"help", no_argument, NULL, '?'},
     808             :         {"limit", required_argument, NULL, 'n'},
     809             :         {"path", required_argument, NULL, 'p'},
     810             :         {"quiet", no_argument, NULL, 'q'},
     811             :         {"relation", required_argument, NULL, 'R'},
     812             :         {"rmgr", required_argument, NULL, 'r'},
     813             :         {"start", required_argument, NULL, 's'},
     814             :         {"timeline", required_argument, NULL, 't'},
     815             :         {"xid", required_argument, NULL, 'x'},
     816             :         {"version", no_argument, NULL, 'V'},
     817             :         {"stats", optional_argument, NULL, 'z'},
     818             :         {"save-fullpage", required_argument, NULL, 1},
     819             :         {NULL, 0, NULL, 0}
     820             :     };
     821             : 
     822             :     int         option;
     823         244 :     int         optindex = 0;
     824             : 
     825             : #ifndef WIN32
     826         244 :     pqsignal(SIGINT, sigint_handler);
     827             : #endif
     828             : 
     829         244 :     pg_logging_init(argv[0]);
     830         244 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_waldump"));
     831         244 :     progname = get_progname(argv[0]);
     832             : 
     833         244 :     if (argc > 1)
     834             :     {
     835         244 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
     836             :         {
     837           2 :             usage();
     838           2 :             exit(0);
     839             :         }
     840         242 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
     841             :         {
     842         154 :             puts("pg_waldump (PostgreSQL) " PG_VERSION);
     843         154 :             exit(0);
     844             :         }
     845             :     }
     846             : 
     847          88 :     memset(&private, 0, sizeof(XLogDumpPrivate));
     848          88 :     memset(&config, 0, sizeof(XLogDumpConfig));
     849          88 :     memset(&stats, 0, sizeof(XLogStats));
     850             : 
     851          88 :     private.timeline = 1;
     852          88 :     private.startptr = InvalidXLogRecPtr;
     853          88 :     private.endptr = InvalidXLogRecPtr;
     854          88 :     private.endptr_reached = false;
     855             : 
     856          88 :     config.quiet = false;
     857          88 :     config.bkp_details = false;
     858          88 :     config.stop_after_records = -1;
     859          88 :     config.already_displayed_records = 0;
     860          88 :     config.follow = false;
     861             :     /* filter_by_rmgr array was zeroed by memset above */
     862          88 :     config.filter_by_rmgr_enabled = false;
     863          88 :     config.filter_by_xid = InvalidTransactionId;
     864          88 :     config.filter_by_xid_enabled = false;
     865          88 :     config.filter_by_extended = false;
     866          88 :     config.filter_by_relation_enabled = false;
     867          88 :     config.filter_by_relation_block_enabled = false;
     868          88 :     config.filter_by_relation_forknum = InvalidForkNumber;
     869          88 :     config.filter_by_fpw = false;
     870          88 :     config.save_fullpage_path = NULL;
     871          88 :     config.stats = false;
     872          88 :     config.stats_per_record = false;
     873             : 
     874          88 :     stats.startptr = InvalidXLogRecPtr;
     875          88 :     stats.endptr = InvalidXLogRecPtr;
     876             : 
     877          88 :     if (argc <= 1)
     878             :     {
     879           0 :         pg_log_error("no arguments specified");
     880           0 :         goto bad_argument;
     881             :     }
     882             : 
     883         514 :     while ((option = getopt_long(argc, argv, "bB:e:fF:n:p:qr:R:s:t:wx:z",
     884             :                                  long_options, &optindex)) != -1)
     885             :     {
     886         428 :         switch (option)
     887             :         {
     888           0 :             case 'b':
     889           0 :                 config.bkp_details = true;
     890           0 :                 break;
     891           0 :             case 'B':
     892           0 :                 if (sscanf(optarg, "%u", &config.filter_by_relation_block) != 1 ||
     893           0 :                     !BlockNumberIsValid(config.filter_by_relation_block))
     894             :                 {
     895           0 :                     pg_log_error("invalid block number: \"%s\"", optarg);
     896           0 :                     goto bad_argument;
     897             :                 }
     898           0 :                 config.filter_by_relation_block_enabled = true;
     899           0 :                 config.filter_by_extended = true;
     900           0 :                 break;
     901          84 :             case 'e':
     902          84 :                 if (sscanf(optarg, "%X/%X", &xlogid, &xrecoff) != 2)
     903             :                 {
     904           0 :                     pg_log_error("invalid WAL location: \"%s\"",
     905             :                                  optarg);
     906           0 :                     goto bad_argument;
     907             :                 }
     908          84 :                 private.endptr = (uint64) xlogid << 32 | xrecoff;
     909          84 :                 break;
     910           0 :             case 'f':
     911           0 :                 config.follow = true;
     912           0 :                 break;
     913           0 :             case 'F':
     914           0 :                 config.filter_by_relation_forknum = forkname_to_number(optarg);
     915           0 :                 if (config.filter_by_relation_forknum == InvalidForkNumber)
     916             :                 {
     917           0 :                     pg_log_error("invalid fork name: \"%s\"", optarg);
     918           0 :                     goto bad_argument;
     919             :                 }
     920           0 :                 config.filter_by_extended = true;
     921           0 :                 break;
     922           0 :             case 'n':
     923           0 :                 if (sscanf(optarg, "%d", &config.stop_after_records) != 1)
     924             :                 {
     925           0 :                     pg_log_error("invalid value \"%s\" for option %s", optarg, "-n/--limit");
     926           0 :                     goto bad_argument;
     927             :                 }
     928           0 :                 break;
     929          84 :             case 'p':
     930          84 :                 waldir = pg_strdup(optarg);
     931          84 :                 break;
     932          86 :             case 'q':
     933          86 :                 config.quiet = true;
     934          86 :                 break;
     935           0 :             case 'r':
     936             :                 {
     937             :                     int         rmid;
     938             : 
     939           0 :                     if (pg_strcasecmp(optarg, "list") == 0)
     940             :                     {
     941           0 :                         print_rmgr_list();
     942           0 :                         exit(EXIT_SUCCESS);
     943             :                     }
     944             : 
     945             :                     /*
     946             :                      * First look for the generated name of a custom rmgr, of
     947             :                      * the form "custom###". We accept this form, because the
     948             :                      * custom rmgr module is not loaded, so there's no way to
     949             :                      * know the real name. This convention should be
     950             :                      * consistent with that in rmgrdesc.c.
     951             :                      */
     952           0 :                     if (sscanf(optarg, "custom%03d", &rmid) == 1)
     953             :                     {
     954           0 :                         if (!RmgrIdIsCustom(rmid))
     955             :                         {
     956           0 :                             pg_log_error("custom resource manager \"%s\" does not exist",
     957             :                                          optarg);
     958           0 :                             goto bad_argument;
     959             :                         }
     960           0 :                         config.filter_by_rmgr[rmid] = true;
     961           0 :                         config.filter_by_rmgr_enabled = true;
     962             :                     }
     963             :                     else
     964             :                     {
     965             :                         /* then look for builtin rmgrs */
     966           0 :                         for (rmid = 0; rmid <= RM_MAX_BUILTIN_ID; rmid++)
     967             :                         {
     968           0 :                             if (pg_strcasecmp(optarg, GetRmgrDesc(rmid)->rm_name) == 0)
     969             :                             {
     970           0 :                                 config.filter_by_rmgr[rmid] = true;
     971           0 :                                 config.filter_by_rmgr_enabled = true;
     972           0 :                                 break;
     973             :                             }
     974             :                         }
     975           0 :                         if (rmid > RM_MAX_BUILTIN_ID)
     976             :                         {
     977           0 :                             pg_log_error("resource manager \"%s\" does not exist",
     978             :                                          optarg);
     979           0 :                             goto bad_argument;
     980             :                         }
     981             :                     }
     982             :                 }
     983           0 :                 break;
     984           2 :             case 'R':
     985           2 :                 if (sscanf(optarg, "%u/%u/%u",
     986             :                            &config.filter_by_relation.spcOid,
     987             :                            &config.filter_by_relation.dbOid,
     988           2 :                            &config.filter_by_relation.relNumber) != 3 ||
     989           2 :                     !OidIsValid(config.filter_by_relation.spcOid) ||
     990           2 :                     !RelFileNumberIsValid(config.filter_by_relation.relNumber))
     991             :                 {
     992           0 :                     pg_log_error("invalid relation specification: \"%s\"", optarg);
     993           0 :                     pg_log_error_detail("Expecting \"tablespace OID/database OID/relation filenode\".");
     994           0 :                     goto bad_argument;
     995             :                 }
     996           2 :                 config.filter_by_relation_enabled = true;
     997           2 :                 config.filter_by_extended = true;
     998           2 :                 break;
     999          84 :             case 's':
    1000          84 :                 if (sscanf(optarg, "%X/%X", &xlogid, &xrecoff) != 2)
    1001             :                 {
    1002           0 :                     pg_log_error("invalid WAL location: \"%s\"",
    1003             :                                  optarg);
    1004           0 :                     goto bad_argument;
    1005             :                 }
    1006             :                 else
    1007          84 :                     private.startptr = (uint64) xlogid << 32 | xrecoff;
    1008          84 :                 break;
    1009          84 :             case 't':
    1010             : 
    1011             :                 /*
    1012             :                  * This is like option_parse_int() but needs to handle
    1013             :                  * unsigned 32-bit int.  Also, we accept both decimal and
    1014             :                  * hexadecimal specifications here.
    1015             :                  */
    1016             :                 {
    1017             :                     char       *endptr;
    1018             :                     unsigned long val;
    1019             : 
    1020          84 :                     errno = 0;
    1021          84 :                     val = strtoul(optarg, &endptr, 0);
    1022             : 
    1023          84 :                     while (*endptr != '\0' && isspace((unsigned char) *endptr))
    1024           0 :                         endptr++;
    1025             : 
    1026          84 :                     if (*endptr != '\0')
    1027             :                     {
    1028           0 :                         pg_log_error("invalid value \"%s\" for option %s",
    1029             :                                      optarg, "-t/--timeline");
    1030           0 :                         goto bad_argument;
    1031             :                     }
    1032             : 
    1033          84 :                     if (errno == ERANGE || val < 1 || val > UINT_MAX)
    1034             :                     {
    1035           0 :                         pg_log_error("%s must be in range %u..%u",
    1036             :                                      "-t/--timeline", 1, UINT_MAX);
    1037           0 :                         goto bad_argument;
    1038             :                     }
    1039             : 
    1040          84 :                     private.timeline = val;
    1041             : 
    1042          84 :                     break;
    1043             :                 }
    1044           0 :             case 'w':
    1045           0 :                 config.filter_by_fpw = true;
    1046           0 :                 break;
    1047           0 :             case 'x':
    1048           0 :                 if (sscanf(optarg, "%u", &config.filter_by_xid) != 1)
    1049             :                 {
    1050           0 :                     pg_log_error("invalid transaction ID specification: \"%s\"",
    1051             :                                  optarg);
    1052           0 :                     goto bad_argument;
    1053             :                 }
    1054           0 :                 config.filter_by_xid_enabled = true;
    1055           0 :                 break;
    1056           0 :             case 'z':
    1057           0 :                 config.stats = true;
    1058           0 :                 config.stats_per_record = false;
    1059           0 :                 if (optarg)
    1060             :                 {
    1061           0 :                     if (strcmp(optarg, "record") == 0)
    1062           0 :                         config.stats_per_record = true;
    1063           0 :                     else if (strcmp(optarg, "rmgr") != 0)
    1064             :                     {
    1065           0 :                         pg_log_error("unrecognized value for option %s: %s",
    1066             :                                      "--stats", optarg);
    1067           0 :                         goto bad_argument;
    1068             :                     }
    1069             :                 }
    1070           0 :                 break;
    1071           2 :             case 1:
    1072           2 :                 config.save_fullpage_path = pg_strdup(optarg);
    1073           2 :                 break;
    1074           2 :             default:
    1075           2 :                 goto bad_argument;
    1076             :         }
    1077             :     }
    1078             : 
    1079          86 :     if (config.filter_by_relation_block_enabled &&
    1080           0 :         !config.filter_by_relation_enabled)
    1081             :     {
    1082           0 :         pg_log_error("option %s requires option %s to be specified",
    1083             :                      "-B/--block", "-R/--relation");
    1084           0 :         goto bad_argument;
    1085             :     }
    1086             : 
    1087          86 :     if ((optind + 2) < argc)
    1088             :     {
    1089           0 :         pg_log_error("too many command-line arguments (first is \"%s\")",
    1090             :                      argv[optind + 2]);
    1091           0 :         goto bad_argument;
    1092             :     }
    1093             : 
    1094          86 :     if (waldir != NULL)
    1095             :     {
    1096             :         /* validate path points to directory */
    1097          84 :         if (!verify_directory(waldir))
    1098             :         {
    1099           2 :             pg_log_error("could not open directory \"%s\": %m", waldir);
    1100           2 :             goto bad_argument;
    1101             :         }
    1102             :     }
    1103             : 
    1104          84 :     if (config.save_fullpage_path != NULL)
    1105           2 :         create_fullpage_directory(config.save_fullpage_path);
    1106             : 
    1107             :     /* parse files as start/end boundaries, extract path if not specified */
    1108          84 :     if (optind < argc)
    1109             :     {
    1110           2 :         char       *directory = NULL;
    1111           2 :         char       *fname = NULL;
    1112             :         int         fd;
    1113             :         XLogSegNo   segno;
    1114             : 
    1115           2 :         split_path(argv[optind], &directory, &fname);
    1116             : 
    1117           2 :         if (waldir == NULL && directory != NULL)
    1118             :         {
    1119           2 :             waldir = directory;
    1120             : 
    1121           2 :             if (!verify_directory(waldir))
    1122           0 :                 pg_fatal("could not open directory \"%s\": %m", waldir);
    1123             :         }
    1124             : 
    1125           2 :         waldir = identify_target_directory(waldir, fname);
    1126           2 :         fd = open_file_in_directory(waldir, fname);
    1127           2 :         if (fd < 0)
    1128           0 :             pg_fatal("could not open file \"%s\"", fname);
    1129           2 :         close(fd);
    1130             : 
    1131             :         /* parse position from file */
    1132           2 :         XLogFromFileName(fname, &private.timeline, &segno, WalSegSz);
    1133             : 
    1134           2 :         if (XLogRecPtrIsInvalid(private.startptr))
    1135           2 :             XLogSegNoOffsetToRecPtr(segno, 0, WalSegSz, private.startptr);
    1136           0 :         else if (!XLByteInSeg(private.startptr, segno, WalSegSz))
    1137             :         {
    1138           0 :             pg_log_error("start WAL location %X/%X is not inside file \"%s\"",
    1139             :                          LSN_FORMAT_ARGS(private.startptr),
    1140             :                          fname);
    1141           0 :             goto bad_argument;
    1142             :         }
    1143             : 
    1144             :         /* no second file specified, set end position */
    1145           2 :         if (!(optind + 1 < argc) && XLogRecPtrIsInvalid(private.endptr))
    1146           2 :             XLogSegNoOffsetToRecPtr(segno + 1, 0, WalSegSz, private.endptr);
    1147             : 
    1148             :         /* parse ENDSEG if passed */
    1149           2 :         if (optind + 1 < argc)
    1150             :         {
    1151             :             XLogSegNo   endsegno;
    1152             : 
    1153             :             /* ignore directory, already have that */
    1154           0 :             split_path(argv[optind + 1], &directory, &fname);
    1155             : 
    1156           0 :             fd = open_file_in_directory(waldir, fname);
    1157           0 :             if (fd < 0)
    1158           0 :                 pg_fatal("could not open file \"%s\"", fname);
    1159           0 :             close(fd);
    1160             : 
    1161             :             /* parse position from file */
    1162           0 :             XLogFromFileName(fname, &private.timeline, &endsegno, WalSegSz);
    1163             : 
    1164           0 :             if (endsegno < segno)
    1165           0 :                 pg_fatal("ENDSEG %s is before STARTSEG %s",
    1166             :                          argv[optind + 1], argv[optind]);
    1167             : 
    1168           0 :             if (XLogRecPtrIsInvalid(private.endptr))
    1169           0 :                 XLogSegNoOffsetToRecPtr(endsegno + 1, 0, WalSegSz,
    1170             :                                         private.endptr);
    1171             : 
    1172             :             /* set segno to endsegno for check of --end */
    1173           0 :             segno = endsegno;
    1174             :         }
    1175             : 
    1176             : 
    1177           2 :         if (!XLByteInSeg(private.endptr, segno, WalSegSz) &&
    1178           2 :             private.endptr != (segno + 1) * WalSegSz)
    1179             :         {
    1180           0 :             pg_log_error("end WAL location %X/%X is not inside file \"%s\"",
    1181             :                          LSN_FORMAT_ARGS(private.endptr),
    1182             :                          argv[argc - 1]);
    1183           0 :             goto bad_argument;
    1184             :         }
    1185             :     }
    1186             :     else
    1187          82 :         waldir = identify_target_directory(waldir, NULL);
    1188             : 
    1189             :     /* we don't know what to print */
    1190          82 :     if (XLogRecPtrIsInvalid(private.startptr))
    1191             :     {
    1192           0 :         pg_log_error("no start WAL location given");
    1193           0 :         goto bad_argument;
    1194             :     }
    1195             : 
    1196             :     /* done with argument parsing, do the actual work */
    1197             : 
    1198             :     /* we have everything we need, start reading */
    1199             :     xlogreader_state =
    1200          82 :         XLogReaderAllocate(WalSegSz, waldir,
    1201          82 :                            XL_ROUTINE(.page_read = WALDumpReadPage,
    1202             :                                       .segment_open = WALDumpOpenSegment,
    1203             :                                       .segment_close = WALDumpCloseSegment),
    1204             :                            &private);
    1205          82 :     if (!xlogreader_state)
    1206           0 :         pg_fatal("out of memory while allocating a WAL reading processor");
    1207             : 
    1208             :     /* first find a valid recptr to start from */
    1209          82 :     first_record = XLogFindNextRecord(xlogreader_state, private.startptr);
    1210             : 
    1211          82 :     if (first_record == InvalidXLogRecPtr)
    1212           0 :         pg_fatal("could not find a valid record after %X/%X",
    1213             :                  LSN_FORMAT_ARGS(private.startptr));
    1214             : 
    1215             :     /*
    1216             :      * Display a message that we're skipping data if `from` wasn't a pointer
    1217             :      * to the start of a record and also wasn't a pointer to the beginning of
    1218             :      * a segment (e.g. we were used in file mode).
    1219             :      */
    1220          82 :     if (first_record != private.startptr &&
    1221           2 :         XLogSegmentOffset(private.startptr, WalSegSz) != 0)
    1222           0 :         printf(ngettext("first record is after %X/%X, at %X/%X, skipping over %u byte\n",
    1223             :                         "first record is after %X/%X, at %X/%X, skipping over %u bytes\n",
    1224             :                         (first_record - private.startptr)),
    1225             :                LSN_FORMAT_ARGS(private.startptr),
    1226             :                LSN_FORMAT_ARGS(first_record),
    1227             :                (uint32) (first_record - private.startptr));
    1228             : 
    1229          82 :     if (config.stats == true && !config.quiet)
    1230           0 :         stats.startptr = first_record;
    1231             : 
    1232             :     for (;;)
    1233             :     {
    1234       47604 :         if (time_to_stop)
    1235             :         {
    1236             :             /* We've been Ctrl-C'ed, so leave */
    1237           0 :             break;
    1238             :         }
    1239             : 
    1240             :         /* try to read the next record */
    1241       47604 :         record = XLogReadRecord(xlogreader_state, &errormsg);
    1242       47604 :         if (!record)
    1243             :         {
    1244          82 :             if (!config.follow || private.endptr_reached)
    1245             :                 break;
    1246             :             else
    1247             :             {
    1248           0 :                 pg_usleep(1000000L);    /* 1 second */
    1249           0 :                 continue;
    1250             :             }
    1251             :         }
    1252             : 
    1253             :         /* apply all specified filters */
    1254       47522 :         if (config.filter_by_rmgr_enabled &&
    1255           0 :             !config.filter_by_rmgr[record->xl_rmid])
    1256           0 :             continue;
    1257             : 
    1258       47522 :         if (config.filter_by_xid_enabled &&
    1259           0 :             config.filter_by_xid != record->xl_xid)
    1260           0 :             continue;
    1261             : 
    1262             :         /* check for extended filtering */
    1263       47522 :         if (config.filter_by_extended &&
    1264       94564 :             !XLogRecordMatchesRelationBlock(xlogreader_state,
    1265       47282 :                                             config.filter_by_relation_enabled ?
    1266             :                                             config.filter_by_relation :
    1267             :                                             emptyRelFileLocator,
    1268       47282 :                                             config.filter_by_relation_block_enabled ?
    1269             :                                             config.filter_by_relation_block :
    1270             :                                             InvalidBlockNumber,
    1271             :                                             config.filter_by_relation_forknum))
    1272       46882 :             continue;
    1273             : 
    1274         640 :         if (config.filter_by_fpw && !XLogRecordHasFPW(xlogreader_state))
    1275           0 :             continue;
    1276             : 
    1277             :         /* perform any per-record work */
    1278         640 :         if (!config.quiet)
    1279             :         {
    1280           0 :             if (config.stats == true)
    1281             :             {
    1282           0 :                 XLogRecStoreStats(&stats, xlogreader_state);
    1283           0 :                 stats.endptr = xlogreader_state->EndRecPtr;
    1284             :             }
    1285             :             else
    1286           0 :                 XLogDumpDisplayRecord(&config, xlogreader_state);
    1287             :         }
    1288             : 
    1289             :         /* save full pages if requested */
    1290         640 :         if (config.save_fullpage_path != NULL)
    1291         400 :             XLogRecordSaveFPWs(xlogreader_state, config.save_fullpage_path);
    1292             : 
    1293             :         /* check whether we printed enough */
    1294         640 :         config.already_displayed_records++;
    1295         640 :         if (config.stop_after_records > 0 &&
    1296           0 :             config.already_displayed_records >= config.stop_after_records)
    1297           0 :             break;
    1298             :     }
    1299             : 
    1300          82 :     if (config.stats == true && !config.quiet)
    1301           0 :         XLogDumpDisplayStats(&config, &stats);
    1302             : 
    1303          82 :     if (time_to_stop)
    1304           0 :         exit(0);
    1305             : 
    1306          82 :     if (errormsg)
    1307           0 :         pg_fatal("error in WAL record at %X/%X: %s",
    1308             :                  LSN_FORMAT_ARGS(xlogreader_state->ReadRecPtr),
    1309             :                  errormsg);
    1310             : 
    1311          82 :     XLogReaderFree(xlogreader_state);
    1312             : 
    1313          82 :     return EXIT_SUCCESS;
    1314             : 
    1315           4 : bad_argument:
    1316           4 :     pg_log_error_hint("Try \"%s --help\" for more information.", progname);
    1317           4 :     return EXIT_FAILURE;
    1318             : }

Generated by: LCOV version 1.14