LCOV - differential code coverage report
Current view: top level - src/bin/pg_waldump - pg_waldump.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 84.5 % 534 451 4 79 22 429 4 22
Current Date: 2026-03-14 14:10:32 -0400 Functions: 94.7 % 19 18 1 5 13 2
Baseline: lcov-20260315-024220-baseline Branches: 72.9 % 347 253 12 82 26 227 12 26
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 84.6 % 26 22 4 22
(360..) days: 84.4 % 508 429 79 429
Function coverage date bins:
(30,360] days: 100.0 % 2 2 2
(360..) days: 94.1 % 17 16 1 3 13
Branch coverage date bins:
(30,360] days: 68.4 % 38 26 12 26
(360..) days: 73.5 % 309 227 82 227

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

Generated by: LCOV version 2.4-beta