LCOV - differential code coverage report
Current view: top level - src/bin/pg_resetwal - pg_resetwal.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 89.9 % 536 482 54 1 481 1
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 13 13 1 12
Baseline: lcov-20250906-005545-baseline Branches: 73.4 % 312 229 83 229
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 1 1 1
(30,360] days: 93.3 % 15 14 1 14
(360..) days: 89.8 % 520 467 53 467
Function coverage date bins:
(360..) days: 100.0 % 13 13 1 12
Branch coverage date bins:
(30,360] days: 87.5 % 8 7 1 7
(360..) days: 73.0 % 304 222 82 222

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pg_resetwal.c
                                  4                 :                :  *    A utility to "zero out" the xlog when it's corrupt beyond recovery.
                                  5                 :                :  *    Can also rebuild pg_control if needed.
                                  6                 :                :  *
                                  7                 :                :  * The theory of operation is fairly simple:
                                  8                 :                :  *    1. Read the existing pg_control (which will include the last
                                  9                 :                :  *       checkpoint record).
                                 10                 :                :  *    2. If pg_control is corrupt, attempt to intuit reasonable values,
                                 11                 :                :  *       by scanning the old xlog if necessary.
                                 12                 :                :  *    3. Modify pg_control to reflect a "shutdown" state with a checkpoint
                                 13                 :                :  *       record at the start of xlog.
                                 14                 :                :  *    4. Flush the existing xlog files and write a new segment with
                                 15                 :                :  *       just a checkpoint record in it.  The new segment is positioned
                                 16                 :                :  *       just past the end of the old xlog, so that existing LSNs in
                                 17                 :                :  *       data pages will appear to be "in the past".
                                 18                 :                :  * This is all pretty straightforward except for the intuition part of
                                 19                 :                :  * step 2 ...
                                 20                 :                :  *
                                 21                 :                :  *
                                 22                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                 23                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 24                 :                :  *
                                 25                 :                :  * src/bin/pg_resetwal/pg_resetwal.c
                                 26                 :                :  *
                                 27                 :                :  *-------------------------------------------------------------------------
                                 28                 :                :  */
                                 29                 :                : 
                                 30                 :                : /*
                                 31                 :                :  * We have to use postgres.h not postgres_fe.h here, because there's so much
                                 32                 :                :  * backend-only stuff in the XLOG include files we need.  But we need a
                                 33                 :                :  * frontend-ish environment otherwise.  Hence this ugly hack.
                                 34                 :                :  */
                                 35                 :                : #define FRONTEND 1
                                 36                 :                : 
                                 37                 :                : #include "postgres.h"
                                 38                 :                : 
                                 39                 :                : #include <dirent.h>
                                 40                 :                : #include <fcntl.h>
                                 41                 :                : #include <sys/stat.h>
                                 42                 :                : #include <sys/time.h>
                                 43                 :                : #include <time.h>
                                 44                 :                : #include <unistd.h>
                                 45                 :                : 
                                 46                 :                : #include "access/heaptoast.h"
                                 47                 :                : #include "access/multixact.h"
                                 48                 :                : #include "access/transam.h"
                                 49                 :                : #include "access/xlog.h"
                                 50                 :                : #include "access/xlog_internal.h"
                                 51                 :                : #include "common/controldata_utils.h"
                                 52                 :                : #include "common/fe_memutils.h"
                                 53                 :                : #include "common/file_perm.h"
                                 54                 :                : #include "common/logging.h"
                                 55                 :                : #include "common/restricted_token.h"
                                 56                 :                : #include "common/string.h"
                                 57                 :                : #include "fe_utils/option_utils.h"
                                 58                 :                : #include "getopt_long.h"
                                 59                 :                : #include "pg_getopt.h"
                                 60                 :                : #include "storage/large_object.h"
                                 61                 :                : 
                                 62                 :                : static ControlFileData ControlFile; /* pg_control values */
                                 63                 :                : static XLogSegNo newXlogSegNo;  /* new XLOG segment # */
                                 64                 :                : static bool guessed = false;    /* T if we had to guess at any values */
                                 65                 :                : static const char *progname;
                                 66                 :                : static uint32 set_xid_epoch = (uint32) -1;
                                 67                 :                : static TransactionId set_oldest_xid = 0;
                                 68                 :                : static TransactionId set_xid = 0;
                                 69                 :                : static TransactionId set_oldest_commit_ts_xid = 0;
                                 70                 :                : static TransactionId set_newest_commit_ts_xid = 0;
                                 71                 :                : static Oid  set_oid = 0;
                                 72                 :                : static MultiXactId set_mxid = 0;
                                 73                 :                : static MultiXactOffset set_mxoff = (MultiXactOffset) -1;
                                 74                 :                : static TimeLineID minXlogTli = 0;
                                 75                 :                : static XLogSegNo minXlogSegNo = 0;
                                 76                 :                : static int  WalSegSz;
                                 77                 :                : static int  set_wal_segsize;
                                 78                 :                : static int  set_char_signedness = -1;
                                 79                 :                : 
                                 80                 :                : static void CheckDataVersion(void);
                                 81                 :                : static bool read_controlfile(void);
                                 82                 :                : static void GuessControlValues(void);
                                 83                 :                : static void PrintControlValues(bool guessed);
                                 84                 :                : static void PrintNewControlValues(void);
                                 85                 :                : static void RewriteControlFile(void);
                                 86                 :                : static void FindEndOfXLOG(void);
                                 87                 :                : static void KillExistingXLOG(void);
                                 88                 :                : static void KillExistingArchiveStatus(void);
                                 89                 :                : static void KillExistingWALSummaries(void);
                                 90                 :                : static void WriteEmptyXLOG(void);
                                 91                 :                : static void usage(void);
                                 92                 :                : 
                                 93                 :                : 
                                 94                 :                : int
 8409 peter_e@gmx.net            95                 :CBC         177 : main(int argc, char *argv[])
                                 96                 :                : {
                                 97                 :                :     static struct option long_options[] = {
                                 98                 :                :         {"commit-timestamp-ids", required_argument, NULL, 'c'},
                                 99                 :                :         {"pgdata", required_argument, NULL, 'D'},
                                100                 :                :         {"epoch", required_argument, NULL, 'e'},
                                101                 :                :         {"force", no_argument, NULL, 'f'},
                                102                 :                :         {"next-wal-file", required_argument, NULL, 'l'},
                                103                 :                :         {"multixact-ids", required_argument, NULL, 'm'},
                                104                 :                :         {"dry-run", no_argument, NULL, 'n'},
                                105                 :                :         {"next-oid", required_argument, NULL, 'o'},
                                106                 :                :         {"multixact-offset", required_argument, NULL, 'O'},
                                107                 :                :         {"oldest-transaction-id", required_argument, NULL, 'u'},
                                108                 :                :         {"next-transaction-id", required_argument, NULL, 'x'},
                                109                 :                :         {"wal-segsize", required_argument, NULL, 1},
                                110                 :                :         {"char-signedness", required_argument, NULL, 2},
                                111                 :                :         {NULL, 0, NULL, 0}
                                112                 :                :     };
                                113                 :                : 
                                114                 :                :     int         c;
                                115                 :            177 :     bool        force = false;
                                116                 :            177 :     bool        noupdate = false;
 4609 alvherre@alvh.no-ip.      117                 :            177 :     MultiXactId set_oldestmxid = 0;
                                118                 :                :     char       *endptr;
                                119                 :                :     char       *endptr2;
 3999 heikki.linnakangas@i      120                 :            177 :     char       *DataDir = NULL;
 2909 andres@anarazel.de        121                 :            177 :     char       *log_fname = NULL;
                                122                 :                :     int         fd;
                                123                 :                : 
 2350 peter@eisentraut.org      124                 :            177 :     pg_logging_init(argv[0]);
 3131 rhaas@postgresql.org      125                 :            177 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_resetwal"));
 8191 bruce@momjian.us          126                 :            177 :     progname = get_progname(argv[0]);
                                127                 :                : 
 8409 peter_e@gmx.net           128         [ +  + ]:            177 :     if (argc > 1)
                                129                 :                :     {
                                130   [ +  +  -  + ]:            176 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
                                131                 :                :         {
                                132                 :              1 :             usage();
                                133                 :              1 :             exit(0);
                                134                 :                :         }
                                135   [ +  +  +  + ]:            175 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
                                136                 :                :         {
 3131 rhaas@postgresql.org      137                 :             45 :             puts("pg_resetwal (PostgreSQL) " PG_VERSION);
 8409 peter_e@gmx.net           138                 :             45 :             exit(0);
                                139                 :                :         }
                                140                 :                :     }
                                141                 :                : 
                                142                 :                : 
 1503 bruce@momjian.us          143         [ +  + ]:            293 :     while ((c = getopt_long(argc, argv, "c:D:e:fl:m:no:O:u:x:", long_options, NULL)) != -1)
                                144                 :                :     {
 8409 peter_e@gmx.net           145   [ +  +  +  +  :            185 :         switch (c)
                                     +  +  +  +  +  
                                        +  +  +  +  
                                                 + ]
                                146                 :                :         {
 3999 heikki.linnakangas@i      147                 :              4 :             case 'D':
                                148                 :              4 :                 DataDir = optarg;
                                149                 :              4 :                 break;
                                150                 :                : 
 8409 peter_e@gmx.net           151                 :             35 :             case 'f':
                                152                 :             35 :                 force = true;
                                153                 :             35 :                 break;
                                154                 :                : 
                                155                 :             38 :             case 'n':
                                156                 :             38 :                 noupdate = true;
                                157                 :             38 :                 break;
                                158                 :                : 
 6956 tgl@sss.pgh.pa.us         159                 :             13 :             case 'e':
 1478 peter@eisentraut.org      160                 :             13 :                 errno = 0;
 6956 tgl@sss.pgh.pa.us         161                 :             13 :                 set_xid_epoch = strtoul(optarg, &endptr, 0);
 1478 peter@eisentraut.org      162   [ +  +  +  -  :             13 :                 if (endptr == optarg || *endptr != '\0' || errno != 0)
                                              -  + ]
                                163                 :                :                 {
                                164                 :                :                     /*------
                                165                 :                :                       translator: the second %s is a command line argument (-e, etc) */
 2350                           166                 :              1 :                     pg_log_error("invalid argument for option %s", "-e");
 1247 tgl@sss.pgh.pa.us         167                 :              1 :                     pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 6956                           168                 :              1 :                     exit(1);
                                169                 :                :                 }
                                170         [ +  + ]:             12 :                 if (set_xid_epoch == -1)
 1247                           171                 :              1 :                     pg_fatal("transaction ID epoch (-e) must not be -1");
 6956                           172                 :             11 :                 break;
                                173                 :                : 
 1503 bruce@momjian.us          174                 :             12 :             case 'u':
 1478 peter@eisentraut.org      175                 :             12 :                 errno = 0;
 1503 bruce@momjian.us          176                 :             12 :                 set_oldest_xid = strtoul(optarg, &endptr, 0);
 1478 peter@eisentraut.org      177   [ +  +  +  -  :             12 :                 if (endptr == optarg || *endptr != '\0' || errno != 0)
                                              -  + ]
                                178                 :                :                 {
 1503 bruce@momjian.us          179                 :              1 :                     pg_log_error("invalid argument for option %s", "-u");
 1247 tgl@sss.pgh.pa.us         180                 :              1 :                     pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 1503 bruce@momjian.us          181                 :              1 :                     exit(1);
                                182                 :                :                 }
                                183         [ +  + ]:             11 :                 if (!TransactionIdIsNormal(set_oldest_xid))
 1247 tgl@sss.pgh.pa.us         184                 :              1 :                     pg_fatal("oldest transaction ID (-u) must be greater than or equal to %u", FirstNormalTransactionId);
 1503 bruce@momjian.us          185                 :             10 :                 break;
                                186                 :                : 
 8409 peter_e@gmx.net           187                 :             12 :             case 'x':
 1478 peter@eisentraut.org      188                 :             12 :                 errno = 0;
 8375 tgl@sss.pgh.pa.us         189                 :             12 :                 set_xid = strtoul(optarg, &endptr, 0);
 1478 peter@eisentraut.org      190   [ +  +  +  -  :             12 :                 if (endptr == optarg || *endptr != '\0' || errno != 0)
                                              -  + ]
                                191                 :                :                 {
 2350                           192                 :              1 :                     pg_log_error("invalid argument for option %s", "-x");
 1247 tgl@sss.pgh.pa.us         193                 :              1 :                     pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 8375                           194                 :              1 :                     exit(1);
                                195                 :                :                 }
 1503 bruce@momjian.us          196         [ +  + ]:             11 :                 if (!TransactionIdIsNormal(set_xid))
 1247 tgl@sss.pgh.pa.us         197                 :              1 :                     pg_fatal("transaction ID (-x) must be greater than or equal to %u", FirstNormalTransactionId);
 8409 peter_e@gmx.net           198                 :             10 :                 break;
                                199                 :                : 
 3930 alvherre@alvh.no-ip.      200                 :             14 :             case 'c':
 1478 peter@eisentraut.org      201                 :             14 :                 errno = 0;
 3540 mail@joeconway.com        202                 :             14 :                 set_oldest_commit_ts_xid = strtoul(optarg, &endptr, 0);
 1478 peter@eisentraut.org      203   [ +  +  +  -  :             14 :                 if (endptr == optarg || *endptr != ',' || errno != 0)
                                              -  + ]
                                204                 :                :                 {
 2350                           205                 :              1 :                     pg_log_error("invalid argument for option %s", "-c");
 1247 tgl@sss.pgh.pa.us         206                 :              1 :                     pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 3930 alvherre@alvh.no-ip.      207                 :              1 :                     exit(1);
                                208                 :                :                 }
 3540 mail@joeconway.com        209                 :             13 :                 set_newest_commit_ts_xid = strtoul(endptr + 1, &endptr2, 0);
 1478 peter@eisentraut.org      210   [ +  +  +  -  :             13 :                 if (endptr2 == endptr + 1 || *endptr2 != '\0' || errno != 0)
                                              -  + ]
                                211                 :                :                 {
 2350                           212                 :              1 :                     pg_log_error("invalid argument for option %s", "-c");
 1247 tgl@sss.pgh.pa.us         213                 :              1 :                     pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 3930 alvherre@alvh.no-ip.      214                 :              1 :                     exit(1);
                                215                 :                :                 }
                                216                 :                : 
  697 peter@eisentraut.org      217         [ +  + ]:             12 :                 if (set_oldest_commit_ts_xid < FirstNormalTransactionId &&
                                218         [ +  - ]:              1 :                     set_oldest_commit_ts_xid != InvalidTransactionId)
                                219                 :              1 :                     pg_fatal("transaction ID (-c) must be either %u or greater than or equal to %u", InvalidTransactionId, FirstNormalTransactionId);
                                220                 :                : 
                                221         [ +  + ]:             11 :                 if (set_newest_commit_ts_xid < FirstNormalTransactionId &&
                                222         [ +  + ]:              3 :                     set_newest_commit_ts_xid != InvalidTransactionId)
                                223                 :              1 :                     pg_fatal("transaction ID (-c) must be either %u or greater than or equal to %u", InvalidTransactionId, FirstNormalTransactionId);
 3930 alvherre@alvh.no-ip.      224                 :             10 :                 break;
                                225                 :                : 
 8375 tgl@sss.pgh.pa.us         226                 :             12 :             case 'o':
 1478 peter@eisentraut.org      227                 :             12 :                 errno = 0;
 8375 tgl@sss.pgh.pa.us         228                 :             12 :                 set_oid = strtoul(optarg, &endptr, 0);
 1478 peter@eisentraut.org      229   [ +  +  +  -  :             12 :                 if (endptr == optarg || *endptr != '\0' || errno != 0)
                                              -  + ]
                                230                 :                :                 {
 2350                           231                 :              1 :                     pg_log_error("invalid argument for option %s", "-o");
 1247 tgl@sss.pgh.pa.us         232                 :              1 :                     pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 8375                           233                 :              1 :                     exit(1);
                                234                 :                :                 }
                                235         [ +  + ]:             11 :                 if (set_oid == 0)
 1247                           236                 :              1 :                     pg_fatal("OID (-o) must not be 0");
 8375                           237                 :             10 :                 break;
                                238                 :                : 
 7436                           239                 :             14 :             case 'm':
 1478 peter@eisentraut.org      240                 :             14 :                 errno = 0;
 7436 tgl@sss.pgh.pa.us         241                 :             14 :                 set_mxid = strtoul(optarg, &endptr, 0);
 1478 peter@eisentraut.org      242   [ +  +  +  -  :             14 :                 if (endptr == optarg || *endptr != ',' || errno != 0)
                                              -  + ]
                                243                 :                :                 {
 2350                           244                 :              1 :                     pg_log_error("invalid argument for option %s", "-m");
 1247 tgl@sss.pgh.pa.us         245                 :              1 :                     pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 4609 alvherre@alvh.no-ip.      246                 :              1 :                     exit(1);
                                247                 :                :                 }
                                248                 :                : 
                                249                 :             13 :                 set_oldestmxid = strtoul(endptr + 1, &endptr2, 0);
 1478 peter@eisentraut.org      250   [ +  +  +  -  :             13 :                 if (endptr2 == endptr + 1 || *endptr2 != '\0' || errno != 0)
                                              -  + ]
                                251                 :                :                 {
 2350                           252                 :              1 :                     pg_log_error("invalid argument for option %s", "-m");
 1247 tgl@sss.pgh.pa.us         253                 :              1 :                     pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 7436                           254                 :              1 :                     exit(1);
                                255                 :                :                 }
                                256         [ +  + ]:             12 :                 if (set_mxid == 0)
 1247                           257                 :              1 :                     pg_fatal("multitransaction ID (-m) must not be 0");
                                258                 :                : 
                                259                 :                :                 /*
                                260                 :                :                  * XXX It'd be nice to have more sanity checks here, e.g. so
                                261                 :                :                  * that oldest is not wrapped around w.r.t. nextMulti.
                                262                 :                :                  */
 4609 alvherre@alvh.no-ip.      263         [ +  + ]:             11 :                 if (set_oldestmxid == 0)
 1247 tgl@sss.pgh.pa.us         264                 :              1 :                     pg_fatal("oldest multitransaction ID (-m) must not be 0");
 7436                           265                 :             10 :                 break;
                                266                 :                : 
 7395                           267                 :             12 :             case 'O':
 1478 peter@eisentraut.org      268                 :             12 :                 errno = 0;
 7395 tgl@sss.pgh.pa.us         269                 :             12 :                 set_mxoff = strtoul(optarg, &endptr, 0);
 1478 peter@eisentraut.org      270   [ +  +  +  -  :             12 :                 if (endptr == optarg || *endptr != '\0' || errno != 0)
                                              -  + ]
                                271                 :                :                 {
 2350                           272                 :              1 :                     pg_log_error("invalid argument for option %s", "-O");
 1247 tgl@sss.pgh.pa.us         273                 :              1 :                     pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 7395                           274                 :              1 :                     exit(1);
                                275                 :                :                 }
                                276         [ +  + ]:             11 :                 if (set_mxoff == -1)
 1247                           277                 :              1 :                     pg_fatal("multitransaction offset (-O) must not be -1");
 7395                           278                 :             10 :                 break;
                                279                 :                : 
 8409 peter_e@gmx.net           280                 :             11 :             case 'l':
 3719 fujii@postgresql.org      281         [ +  + ]:             11 :                 if (strspn(optarg, "01234567890ABCDEFabcdef") != XLOG_FNAME_LEN)
                                282                 :                :                 {
 2350 peter@eisentraut.org      283                 :              1 :                     pg_log_error("invalid argument for option %s", "-l");
 1247 tgl@sss.pgh.pa.us         284                 :              1 :                     pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 8409 peter_e@gmx.net           285                 :              1 :                     exit(1);
                                286                 :                :                 }
                                287                 :                : 
                                288                 :                :                 /*
                                289                 :                :                  * XLogFromFileName requires wal segment size which is not yet
                                290                 :                :                  * set. Hence wal details are set later on.
                                291                 :                :                  */
 2909 andres@anarazel.de        292                 :             10 :                 log_fname = pg_strdup(optarg);
 8409 peter_e@gmx.net           293                 :             10 :                 break;
                                294                 :                : 
 2722                           295                 :              4 :             case 1:
                                296                 :                :                 {
                                297                 :                :                     int         wal_segsize_mb;
                                298                 :                : 
  740 peter@eisentraut.org      299         [ +  + ]:              4 :                     if (!option_parse_int(optarg, "--wal-segsize", 1, 1024, &wal_segsize_mb))
                                300                 :              1 :                         exit(1);
                                301                 :              3 :                     set_wal_segsize = wal_segsize_mb * 1024 * 1024;
                                302   [ +  -  +  +  :              3 :                     if (!IsValidWalSegSize(set_wal_segsize))
                                        +  -  -  + ]
  739 dgustafsson@postgres      303                 :              1 :                         pg_fatal("argument of %s must be a power of two between 1 and 1024", "--wal-segsize");
  740 peter@eisentraut.org      304                 :              2 :                     break;
                                305                 :                :                 }
                                306                 :                : 
  197 msawada@postgresql.o      307                 :              3 :             case 2:
                                308                 :                :                 {
                                309                 :              3 :                     errno = 0;
                                310                 :                : 
                                311         [ -  + ]:              3 :                     if (pg_strcasecmp(optarg, "signed") == 0)
  197 msawada@postgresql.o      312                 :UBC           0 :                         set_char_signedness = 1;
  197 msawada@postgresql.o      313         [ +  + ]:CBC           3 :                     else if (pg_strcasecmp(optarg, "unsigned") == 0)
                                314                 :              2 :                         set_char_signedness = 0;
                                315                 :                :                     else
                                316                 :                :                     {
                                317                 :              1 :                         pg_log_error("invalid argument for option %s", "--char-signedness");
                                318                 :              1 :                         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
                                319                 :              1 :                         exit(1);
                                320                 :                :                     }
                                321                 :              2 :                     break;
                                322                 :                :                 }
                                323                 :                : 
 8409 peter_e@gmx.net           324                 :              1 :             default:
                                325                 :                :                 /* getopt_long already emitted a complaint */
 1247 tgl@sss.pgh.pa.us         326                 :              1 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 8409 peter_e@gmx.net           327                 :              1 :                 exit(1);
                                328                 :                :         }
                                329                 :                :     }
                                330                 :                : 
 3970 heikki.linnakangas@i      331   [ +  +  +  + ]:            108 :     if (DataDir == NULL && optind < argc)
                                332                 :            103 :         DataDir = argv[optind++];
                                333                 :                : 
                                334                 :                :     /* Complain if any arguments remain */
                                335         [ +  + ]:            108 :     if (optind < argc)
                                336                 :                :     {
 2350 peter@eisentraut.org      337                 :              1 :         pg_log_error("too many command-line arguments (first is \"%s\")",
                                338                 :                :                      argv[optind]);
 1247 tgl@sss.pgh.pa.us         339                 :              1 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 3970 heikki.linnakangas@i      340                 :              1 :         exit(1);
                                341                 :                :     }
                                342                 :                : 
                                343         [ +  + ]:            107 :     if (DataDir == NULL)
                                344                 :                :     {
 2350 peter@eisentraut.org      345                 :              1 :         pg_log_error("no data directory specified");
 1247 tgl@sss.pgh.pa.us         346                 :              1 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 8409 peter_e@gmx.net           347                 :              1 :         exit(1);
                                348                 :                :     }
                                349                 :                : 
                                350                 :                :     /*
                                351                 :                :      * Don't allow pg_resetwal to be run as root, to avoid overwriting the
                                352                 :                :      * ownership of files in the data directory. We need only check for root
                                353                 :                :      * -- any other user won't have sufficient permissions to modify files in
                                354                 :                :      * the data directory.
                                355                 :                :      */
                                356                 :                : #ifndef WIN32
 7571 neilc@samurai.com         357         [ -  + ]:            106 :     if (geteuid() == 0)
                                358                 :                :     {
 2350 peter@eisentraut.org      359                 :UBC           0 :         pg_log_error("cannot be executed by \"root\"");
 1247 tgl@sss.pgh.pa.us         360                 :              0 :         pg_log_error_hint("You must run %s as the PostgreSQL superuser.",
                                361                 :                :                           progname);
 7571 neilc@samurai.com         362                 :              0 :         exit(1);
                                363                 :                :     }
                                364                 :                : #endif
                                365                 :                : 
 2350 peter@eisentraut.org      366                 :CBC         106 :     get_restricted_token();
                                367                 :                : 
                                368                 :                :     /* Set mask based on PGDATA permissions */
 2709 sfrost@snowman.net        369         [ +  + ]:            106 :     if (!GetDataDirectoryCreatePerm(DataDir))
 1247 tgl@sss.pgh.pa.us         370                 :              1 :         pg_fatal("could not read permissions of directory \"%s\": %m",
                                371                 :                :                  DataDir);
                                372                 :                : 
 2709 sfrost@snowman.net        373                 :            105 :     umask(pg_mode_mask);
                                374                 :                : 
  708 peter@eisentraut.org      375         [ -  + ]:            105 :     if (chdir(DataDir) < 0)
  708 peter@eisentraut.org      376                 :UBC           0 :         pg_fatal("could not change directory to \"%s\": %m",
                                377                 :                :                  DataDir);
                                378                 :                : 
                                379                 :                :     /* Check that data directory matches our server version */
 3022 tgl@sss.pgh.pa.us         380                 :CBC         105 :     CheckDataVersion();
                                381                 :                : 
                                382                 :                :     /*
                                383                 :                :      * Check for a postmaster lock file --- if there is one, refuse to
                                384                 :                :      * proceed, on grounds we might be interfering with a live installation.
                                385                 :                :      */
 4671                           386         [ +  + ]:            105 :     if ((fd = open("postmaster.pid", O_RDONLY, 0)) < 0)
                                387                 :                :     {
 8409 peter_e@gmx.net           388         [ -  + ]:            104 :         if (errno != ENOENT)
 1247 tgl@sss.pgh.pa.us         389                 :UBC           0 :             pg_fatal("could not open file \"%s\" for reading: %m",
                                390                 :                :                      "postmaster.pid");
                                391                 :                :     }
                                392                 :                :     else
                                393                 :                :     {
 2350 peter@eisentraut.org      394                 :CBC           1 :         pg_log_error("lock file \"%s\" exists", "postmaster.pid");
 1247 tgl@sss.pgh.pa.us         395                 :              1 :         pg_log_error_hint("Is a server running?  If not, delete the lock file and try again.");
 7035 bruce@momjian.us          396                 :              1 :         exit(1);
                                397                 :                :     }
                                398                 :                : 
                                399                 :                :     /*
                                400                 :                :      * Attempt to read the existing pg_control file
                                401                 :                :      */
 2028 peter@eisentraut.org      402         [ +  + ]:            104 :     if (!read_controlfile())
 7035 bruce@momjian.us          403                 :              4 :         GuessControlValues();
                                404                 :                : 
                                405                 :                :     /*
                                406                 :                :      * If no new WAL segment size was specified, use the control file value.
                                407                 :                :      */
 2722 peter_e@gmx.net           408         [ +  + ]:            104 :     if (set_wal_segsize != 0)
                                409                 :              2 :         WalSegSz = set_wal_segsize;
                                410                 :                :     else
                                411                 :            102 :         WalSegSz = ControlFile.xlog_seg_size;
                                412                 :                : 
 2909 andres@anarazel.de        413         [ +  + ]:            104 :     if (log_fname != NULL)
                                414                 :             10 :         XLogFromFileName(log_fname, &minXlogTli, &minXlogSegNo, WalSegSz);
                                415                 :                : 
                                416                 :                :     /*
                                417                 :                :      * Also look at existing segment files to set up newXlogSegNo
                                418                 :                :      */
 6847 tgl@sss.pgh.pa.us         419                 :            104 :     FindEndOfXLOG();
                                420                 :                : 
                                421                 :                :     /*
                                422                 :                :      * If we're not going to proceed with the reset, print the current control
                                423                 :                :      * file parameters.
                                424                 :                :      */
 4286 heikki.linnakangas@i      425   [ +  +  +  +  :            104 :     if ((guessed && !force) || noupdate)
                                              +  + ]
                                426                 :             39 :         PrintControlValues(guessed);
                                427                 :                : 
                                428                 :                :     /*
                                429                 :                :      * Adjust fields if required by switches.  (Do this now so that printout,
                                430                 :                :      * if any, includes these values.)
                                431                 :                :      */
 6956 tgl@sss.pgh.pa.us         432         [ +  + ]:            104 :     if (set_xid_epoch != -1)
                                433                 :                :         ControlFile.checkPointCopy.nextXid =
 2354 tmunro@postgresql.or      434                 :             11 :             FullTransactionIdFromEpochAndXid(set_xid_epoch,
 1852 andres@anarazel.de        435                 :             11 :                                              XidFromFullTransactionId(ControlFile.checkPointCopy.nextXid));
                                436                 :                : 
 1503 bruce@momjian.us          437         [ +  + ]:            104 :     if (set_oldest_xid != 0)
                                438                 :                :     {
                                439                 :             10 :         ControlFile.checkPointCopy.oldestXid = set_oldest_xid;
                                440                 :             10 :         ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
                                441                 :                :     }
                                442                 :                : 
                                443         [ +  + ]:            104 :     if (set_xid != 0)
                                444                 :                :         ControlFile.checkPointCopy.nextXid =
 1852 andres@anarazel.de        445                 :             10 :             FullTransactionIdFromEpochAndXid(EpochFromFullTransactionId(ControlFile.checkPointCopy.nextXid),
                                446                 :                :                                              set_xid);
                                447                 :                : 
 3540 mail@joeconway.com        448         [ +  + ]:            104 :     if (set_oldest_commit_ts_xid != 0)
                                449                 :             10 :         ControlFile.checkPointCopy.oldestCommitTsXid = set_oldest_commit_ts_xid;
                                450         [ +  + ]:            104 :     if (set_newest_commit_ts_xid != 0)
                                451                 :              8 :         ControlFile.checkPointCopy.newestCommitTsXid = set_newest_commit_ts_xid;
                                452                 :                : 
 8375 tgl@sss.pgh.pa.us         453         [ +  + ]:            104 :     if (set_oid != 0)
                                454                 :             10 :         ControlFile.checkPointCopy.nextOid = set_oid;
                                455                 :                : 
 7436                           456         [ +  + ]:            104 :     if (set_mxid != 0)
                                457                 :                :     {
                                458                 :             10 :         ControlFile.checkPointCopy.nextMulti = set_mxid;
                                459                 :                : 
 4609 alvherre@alvh.no-ip.      460                 :             10 :         ControlFile.checkPointCopy.oldestMulti = set_oldestmxid;
                                461         [ -  + ]:             10 :         if (ControlFile.checkPointCopy.oldestMulti < FirstMultiXactId)
 4609 alvherre@alvh.no-ip.      462                 :UBC           0 :             ControlFile.checkPointCopy.oldestMulti += FirstMultiXactId;
 4609 alvherre@alvh.no-ip.      463                 :CBC          10 :         ControlFile.checkPointCopy.oldestMultiDB = InvalidOid;
                                464                 :                :     }
                                465                 :                : 
 7395 tgl@sss.pgh.pa.us         466         [ +  + ]:            104 :     if (set_mxoff != -1)
                                467                 :             10 :         ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
                                468                 :                : 
 7565                           469         [ -  + ]:            104 :     if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
                                470                 :                :     {
 7565 tgl@sss.pgh.pa.us         471                 :UBC           0 :         ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
 4590 heikki.linnakangas@i      472                 :              0 :         ControlFile.checkPointCopy.PrevTimeLineID = minXlogTli;
                                473                 :                :     }
                                474                 :                : 
 2722 peter_e@gmx.net           475         [ +  + ]:CBC         104 :     if (set_wal_segsize != 0)
                                476                 :              2 :         ControlFile.xlog_seg_size = WalSegSz;
                                477                 :                : 
  197 msawada@postgresql.o      478         [ +  + ]:            104 :     if (set_char_signedness != -1)
                                479                 :              2 :         ControlFile.default_char_signedness = (set_char_signedness == 1);
                                480                 :                : 
 4822 heikki.linnakangas@i      481         [ +  + ]:            104 :     if (minXlogSegNo > newXlogSegNo)
                                482                 :              3 :         newXlogSegNo = minXlogSegNo;
                                483                 :                : 
  709 peter@eisentraut.org      484         [ +  + ]:            104 :     if (noupdate)
                                485                 :                :     {
                                486                 :             38 :         PrintNewControlValues();
                                487                 :             38 :         exit(0);
                                488                 :                :     }
                                489                 :                : 
                                490                 :                :     /*
                                491                 :                :      * If we had to guess anything, and -f was not given, just print the
                                492                 :                :      * guessed values and exit.
                                493                 :                :      */
                                494   [ +  +  +  + ]:             66 :     if (guessed && !force)
                                495                 :                :     {
 4286 heikki.linnakangas@i      496                 :              1 :         PrintNewControlValues();
  709 peter@eisentraut.org      497                 :              1 :         pg_log_error("not proceeding because control file values were guessed");
                                498                 :              1 :         pg_log_error_hint("If these values seem acceptable, use -f to force reset.");
                                499                 :              1 :         exit(1);
                                500                 :                :     }
                                501                 :                : 
                                502                 :                :     /*
                                503                 :                :      * Don't reset from a dirty pg_control without -f, either.
                                504                 :                :      */
 7035 bruce@momjian.us          505   [ +  +  +  + ]:             65 :     if (ControlFile.state != DB_SHUTDOWNED && !force)
                                506                 :                :     {
  709 peter@eisentraut.org      507                 :              1 :         pg_log_error("database server was not shut down cleanly");
                                508                 :              1 :         pg_log_error_detail("Resetting the write-ahead log might cause data to be lost.");
                                509                 :              1 :         pg_log_error_hint("If you want to proceed anyway, use -f to force reset.");
 8409 peter_e@gmx.net           510                 :              1 :         exit(1);
                                511                 :                :     }
                                512                 :                : 
                                513                 :                :     /*
                                514                 :                :      * Else, do the dirty deed.
                                515                 :                :      */
                                516                 :             64 :     RewriteControlFile();
                                517                 :             64 :     KillExistingXLOG();
 5970 tgl@sss.pgh.pa.us         518                 :             64 :     KillExistingArchiveStatus();
  626 rhaas@postgresql.org      519                 :             64 :     KillExistingWALSummaries();
 8409 peter_e@gmx.net           520                 :             64 :     WriteEmptyXLOG();
                                521                 :                : 
 3039                           522                 :             64 :     printf(_("Write-ahead log reset\n"));
 8409                           523                 :             64 :     return 0;
                                524                 :                : }
                                525                 :                : 
                                526                 :                : 
                                527                 :                : /*
                                528                 :                :  * Look at the version string stored in PG_VERSION and decide if this utility
                                529                 :                :  * can be run safely or not.
                                530                 :                :  *
                                531                 :                :  * We don't want to inject pg_control and WAL files that are for a different
                                532                 :                :  * major version; that can't do anything good.  Note that we don't treat
                                533                 :                :  * mismatching version info in pg_control as a reason to bail out, because
                                534                 :                :  * recovering from a corrupted pg_control is one of the main reasons for this
                                535                 :                :  * program to exist at all.  However, PG_VERSION is unlikely to get corrupted,
                                536                 :                :  * and if it were it would be easy to fix by hand.  So let's make this check
                                537                 :                :  * to prevent simple user errors.
                                538                 :                :  */
                                539                 :                : static void
 3022 tgl@sss.pgh.pa.us         540                 :            105 : CheckDataVersion(void)
                                541                 :                : {
                                542                 :            105 :     const char *ver_file = "PG_VERSION";
                                543                 :                :     FILE       *ver_fd;
                                544                 :                :     char        rawline[64];
                                545                 :                : 
                                546         [ -  + ]:            105 :     if ((ver_fd = fopen(ver_file, "r")) == NULL)
 1247 tgl@sss.pgh.pa.us         547                 :UBC           0 :         pg_fatal("could not open file \"%s\" for reading: %m",
                                548                 :                :                  ver_file);
                                549                 :                : 
                                550                 :                :     /* version number has to be the first line read */
 3022 tgl@sss.pgh.pa.us         551         [ -  + ]:CBC         105 :     if (!fgets(rawline, sizeof(rawline), ver_fd))
                                552                 :                :     {
 3022 tgl@sss.pgh.pa.us         553         [ #  # ]:UBC           0 :         if (!ferror(ver_fd))
 1247                           554                 :              0 :             pg_fatal("unexpected empty file \"%s\"", ver_file);
                                555                 :                :         else
                                556                 :              0 :             pg_fatal("could not read file \"%s\": %m", ver_file);
                                557                 :                :     }
                                558                 :                : 
                                559                 :                :     /* strip trailing newline and carriage return */
 2220 michael@paquier.xyz       560                 :CBC         105 :     (void) pg_strip_crlf(rawline);
                                561                 :                : 
 3022 tgl@sss.pgh.pa.us         562         [ -  + ]:            105 :     if (strcmp(rawline, PG_MAJORVERSION) != 0)
                                563                 :                :     {
 2350 peter@eisentraut.org      564                 :UBC           0 :         pg_log_error("data directory is of wrong version");
 1247 tgl@sss.pgh.pa.us         565                 :              0 :         pg_log_error_detail("File \"%s\" contains \"%s\", which is not compatible with this program's version \"%s\".",
                                566                 :                :                             ver_file, rawline, PG_MAJORVERSION);
 3022                           567                 :              0 :         exit(1);
                                568                 :                :     }
                                569                 :                : 
 3022 tgl@sss.pgh.pa.us         570                 :CBC         105 :     fclose(ver_fd);
                                571                 :            105 : }
                                572                 :                : 
                                573                 :                : 
                                574                 :                : /*
                                575                 :                :  * Try to read the existing pg_control file.
                                576                 :                :  *
                                577                 :                :  * This routine is also responsible for updating old pg_control versions
                                578                 :                :  * to the current format.  (Currently we don't do anything of the sort.)
                                579                 :                :  */
                                580                 :                : static bool
 2028 peter@eisentraut.org      581                 :            104 : read_controlfile(void)
                                582                 :                : {
                                583                 :                :     int         fd;
                                584                 :                :     int         len;
                                585                 :                :     char       *buffer;
                                586                 :                :     pg_crc32c   crc;
                                587                 :                : 
 6191 magnus@hagander.net       588         [ -  + ]:            104 :     if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY | PG_BINARY, 0)) < 0)
                                589                 :                :     {
                                590                 :                :         /*
                                591                 :                :          * If pg_control is not there at all, or we can't read it, the odds
                                592                 :                :          * are we've been handed a bad DataDir path, so give up. User can do
                                593                 :                :          * "touch pg_control" to force us to proceed.
                                594                 :                :          */
 2350 peter@eisentraut.org      595                 :UBC           0 :         pg_log_error("could not open file \"%s\" for reading: %m",
                                596                 :                :                      XLOG_CONTROL_FILE);
 8421 bruce@momjian.us          597         [ #  # ]:              0 :         if (errno == ENOENT)
 1247 tgl@sss.pgh.pa.us         598                 :              0 :             pg_log_error_hint("If you are sure the data directory path is correct, execute\n"
                                599                 :                :                               "  touch %s\n"
                                600                 :                :                               "and try again.",
                                601                 :                :                               XLOG_CONTROL_FILE);
 8421 bruce@momjian.us          602                 :              0 :         exit(1);
                                603                 :                :     }
                                604                 :                : 
                                605                 :                :     /* Use malloc to ensure we have a maxaligned buffer */
 2971 tgl@sss.pgh.pa.us         606                 :CBC         104 :     buffer = (char *) pg_malloc(PG_CONTROL_FILE_SIZE);
                                607                 :                : 
                                608                 :            104 :     len = read(fd, buffer, PG_CONTROL_FILE_SIZE);
 8421 bruce@momjian.us          609         [ -  + ]:            104 :     if (len < 0)
 1247 tgl@sss.pgh.pa.us         610                 :UBC           0 :         pg_fatal("could not read file \"%s\": %m", XLOG_CONTROL_FILE);
 8421 bruce@momjian.us          611                 :CBC         104 :     close(fd);
                                612                 :                : 
                                613         [ +  - ]:            104 :     if (len >= sizeof(ControlFileData) &&
 2999 tgl@sss.pgh.pa.us         614         [ +  + ]:            104 :         ((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION)
                                615                 :                :     {
                                616                 :                :         /* Check the CRC. */
 3959 heikki.linnakangas@i      617                 :            103 :         INIT_CRC32C(crc);
                                618                 :            103 :         COMP_CRC32C(crc,
                                619                 :                :                     buffer,
                                620                 :                :                     offsetof(ControlFileData, crc));
                                621                 :            103 :         FIN_CRC32C(crc);
                                622                 :                : 
 2909 andres@anarazel.de        623         [ +  + ]:            103 :         if (!EQ_CRC32C(crc, ((ControlFileData *) buffer)->crc))
                                624                 :                :         {
                                625                 :                :             /* We will use the data but treat it as guessed. */
 2350 peter@eisentraut.org      626                 :              3 :             pg_log_warning("pg_control exists but has invalid CRC; proceed with caution");
 2909 andres@anarazel.de        627                 :              3 :             guessed = true;
                                628                 :                :         }
                                629                 :                : 
 8421 bruce@momjian.us          630                 :            103 :         memcpy(&ControlFile, buffer, sizeof(ControlFile));
                                631                 :                : 
                                632                 :                :         /* return false if WAL segment size is not valid */
 2722 peter_e@gmx.net           633   [ +  +  +  -  :            103 :         if (!IsValidWalSegSize(ControlFile.xlog_seg_size))
                                        +  -  -  + ]
                                634                 :                :         {
 2350 peter@eisentraut.org      635                 :              3 :             pg_log_warning(ngettext("pg_control specifies invalid WAL segment size (%d byte); proceed with caution",
                                636                 :                :                                     "pg_control specifies invalid WAL segment size (%d bytes); proceed with caution",
                                637                 :                :                                     ControlFile.xlog_seg_size),
                                638                 :                :                            ControlFile.xlog_seg_size);
 2724 peter_e@gmx.net           639                 :              3 :             return false;
                                640                 :                :         }
                                641                 :                : 
 8421 bruce@momjian.us          642                 :            100 :         return true;
                                643                 :                :     }
                                644                 :                : 
                                645                 :                :     /* Looks like it's a mess. */
 2350 peter@eisentraut.org      646                 :              1 :     pg_log_warning("pg_control exists but is broken or wrong version; ignoring it");
 8421 bruce@momjian.us          647                 :              1 :     return false;
                                648                 :                : }
                                649                 :                : 
                                650                 :                : 
                                651                 :                : /*
                                652                 :                :  * Guess at pg_control values when we can't read the old ones.
                                653                 :                :  */
                                654                 :                : static void
 7035                           655                 :              4 : GuessControlValues(void)
                                656                 :                : {
                                657                 :                :     uint64      sysidentifier;
                                658                 :                :     struct timeval tv;
                                659                 :                : 
                                660                 :                :     /*
                                661                 :                :      * Set up a completely default set of pg_control values.
                                662                 :                :      */
                                663                 :              4 :     guessed = true;
 8421                           664                 :              4 :     memset(&ControlFile, 0, sizeof(ControlFile));
                                665                 :                : 
                                666                 :              4 :     ControlFile.pg_control_version = PG_CONTROL_VERSION;
                                667                 :              4 :     ControlFile.catalog_version_no = CATALOG_VERSION_NO;
                                668                 :                : 
                                669                 :                :     /*
                                670                 :                :      * Create a new unique installation identifier, since we can no longer use
                                671                 :                :      * any old XLOG records.  See notes in xlog.c about the algorithm.
                                672                 :                :      */
 7035                           673                 :              4 :     gettimeofday(&tv, NULL);
                                674                 :              4 :     sysidentifier = ((uint64) tv.tv_sec) << 32;
 4120 tgl@sss.pgh.pa.us         675                 :              4 :     sysidentifier |= ((uint64) tv.tv_usec) << 12;
                                676                 :              4 :     sysidentifier |= getpid() & 0xFFF;
                                677                 :                : 
 7035 bruce@momjian.us          678                 :              4 :     ControlFile.system_identifier = sysidentifier;
                                679                 :                : 
 4822 heikki.linnakangas@i      680                 :              4 :     ControlFile.checkPointCopy.redo = SizeOfXLogLongPHD;
 7035 bruce@momjian.us          681                 :              4 :     ControlFile.checkPointCopy.ThisTimeLineID = 1;
 4590 heikki.linnakangas@i      682                 :              4 :     ControlFile.checkPointCopy.PrevTimeLineID = 1;
 4973 simon@2ndQuadrant.co      683                 :              4 :     ControlFile.checkPointCopy.fullPageWrites = false;
                                684                 :                :     ControlFile.checkPointCopy.nextXid =
 2354 tmunro@postgresql.or      685                 :              4 :         FullTransactionIdFromEpochAndXid(0, FirstNormalTransactionId);
 1514 tgl@sss.pgh.pa.us         686                 :              4 :     ControlFile.checkPointCopy.nextOid = FirstGenbkiObjectId;
 7035 bruce@momjian.us          687                 :              4 :     ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
                                688                 :              4 :     ControlFile.checkPointCopy.nextMultiOffset = 0;
 5850 tgl@sss.pgh.pa.us         689                 :              4 :     ControlFile.checkPointCopy.oldestXid = FirstNormalTransactionId;
                                690                 :              4 :     ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
 4609 alvherre@alvh.no-ip.      691                 :              4 :     ControlFile.checkPointCopy.oldestMulti = FirstMultiXactId;
                                692                 :              4 :     ControlFile.checkPointCopy.oldestMultiDB = InvalidOid;
 6411 tgl@sss.pgh.pa.us         693                 :              4 :     ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
 5610                           694                 :              4 :     ControlFile.checkPointCopy.oldestActiveXid = InvalidTransactionId;
                                695                 :                : 
 7035 bruce@momjian.us          696                 :              4 :     ControlFile.state = DB_SHUTDOWNED;
 6411 tgl@sss.pgh.pa.us         697                 :              4 :     ControlFile.time = (pg_time_t) time(NULL);
 7035 bruce@momjian.us          698                 :              4 :     ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
 2141 michael@paquier.xyz       699                 :              4 :     ControlFile.unloggedLSN = FirstNormalUnloggedLSN;
                                700                 :                : 
                                701                 :                :     /* minRecoveryPoint, backupStartPoint and backupEndPoint can be left zero */
                                702                 :                : 
 5610 tgl@sss.pgh.pa.us         703                 :              4 :     ControlFile.wal_level = WAL_LEVEL_MINIMAL;
 4277 fujii@postgresql.org      704                 :              4 :     ControlFile.wal_log_hints = false;
 3930 alvherre@alvh.no-ip.      705                 :              4 :     ControlFile.track_commit_timestamp = false;
 5610 tgl@sss.pgh.pa.us         706                 :              4 :     ControlFile.MaxConnections = 100;
 2398 michael@paquier.xyz       707                 :              4 :     ControlFile.max_wal_senders = 10;
 3197 rhaas@postgresql.org      708                 :              4 :     ControlFile.max_worker_processes = 8;
 5610 tgl@sss.pgh.pa.us         709                 :              4 :     ControlFile.max_prepared_xacts = 0;
                                710                 :              4 :     ControlFile.max_locks_per_xact = 64;
                                711                 :                : 
 7278                           712                 :              4 :     ControlFile.maxAlign = MAXIMUM_ALIGNOF;
                                713                 :              4 :     ControlFile.floatFormat = FLOATFORMAT_VALUE;
 8421 bruce@momjian.us          714                 :              4 :     ControlFile.blcksz = BLCKSZ;
                                715                 :              4 :     ControlFile.relseg_size = RELSEG_SIZE;
 2722 peter_e@gmx.net           716                 :              4 :     ControlFile.xlog_blcksz = XLOG_BLCKSZ;
                                717                 :              4 :     ControlFile.xlog_seg_size = DEFAULT_XLOG_SEG_SIZE;
 8375 tgl@sss.pgh.pa.us         718                 :              4 :     ControlFile.nameDataLen = NAMEDATALEN;
 7466                           719                 :              4 :     ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
 6731                           720                 :              4 :     ControlFile.toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE;
 4111                           721                 :              4 :     ControlFile.loblksize = LOBLKSIZE;
   24 tgl@sss.pgh.pa.us         722                 :GNC           4 :     ControlFile.float8ByVal = true; /* vestigial */
                                723                 :                : 
                                724                 :                :     /*
                                725                 :                :      * XXX eventually, should try to grovel through old XLOG to develop more
                                726                 :                :      * accurate values for TimeLineID, nextXID, etc.
                                727                 :                :      */
 8421 bruce@momjian.us          728                 :CBC           4 : }
                                729                 :                : 
                                730                 :                : 
                                731                 :                : /*
                                732                 :                :  * Print the guessed pg_control values when we had to guess.
                                733                 :                :  *
                                734                 :                :  * NB: this display should be just those fields that will not be
                                735                 :                :  * reset by RewriteControlFile().
                                736                 :                :  */
                                737                 :                : static void
 7035                           738                 :             39 : PrintControlValues(bool guessed)
                                739                 :                : {
                                740         [ +  + ]:             39 :     if (guessed)
                                741                 :              3 :         printf(_("Guessed pg_control values:\n\n"));
                                742                 :                :     else
 4286 heikki.linnakangas@i      743                 :             36 :         printf(_("Current pg_control values:\n\n"));
                                744                 :                : 
 6956 tgl@sss.pgh.pa.us         745                 :             39 :     printf(_("pg_control version number:            %u\n"),
                                746                 :                :            ControlFile.pg_control_version);
                                747                 :             39 :     printf(_("Catalog version number:               %u\n"),
                                748                 :                :            ControlFile.catalog_version_no);
  161 peter@eisentraut.org      749                 :             39 :     printf(_("Database system identifier:           %" PRIu64 "\n"),
                                750                 :                :            ControlFile.system_identifier);
 6956 tgl@sss.pgh.pa.us         751                 :             39 :     printf(_("Latest checkpoint's TimeLineID:       %u\n"),
                                752                 :                :            ControlFile.checkPointCopy.ThisTimeLineID);
 4839 peter_e@gmx.net           753         [ +  + ]:             39 :     printf(_("Latest checkpoint's full_page_writes: %s\n"),
                                754                 :                :            ControlFile.checkPointCopy.fullPageWrites ? _("on") : _("off"));
 3494 mail@joeconway.com        755                 :             39 :     printf(_("Latest checkpoint's NextXID:          %u:%u\n"),
                                756                 :                :            EpochFromFullTransactionId(ControlFile.checkPointCopy.nextXid),
                                757                 :                :            XidFromFullTransactionId(ControlFile.checkPointCopy.nextXid));
 6956 tgl@sss.pgh.pa.us         758                 :             39 :     printf(_("Latest checkpoint's NextOID:          %u\n"),
                                759                 :                :            ControlFile.checkPointCopy.nextOid);
                                760                 :             39 :     printf(_("Latest checkpoint's NextMultiXactId:  %u\n"),
                                761                 :                :            ControlFile.checkPointCopy.nextMulti);
                                762                 :             39 :     printf(_("Latest checkpoint's NextMultiOffset:  %u\n"),
                                763                 :                :            ControlFile.checkPointCopy.nextMultiOffset);
 5850                           764                 :             39 :     printf(_("Latest checkpoint's oldestXID:        %u\n"),
                                765                 :                :            ControlFile.checkPointCopy.oldestXid);
                                766                 :             39 :     printf(_("Latest checkpoint's oldestXID's DB:   %u\n"),
                                767                 :                :            ControlFile.checkPointCopy.oldestXidDB);
 5610                           768                 :             39 :     printf(_("Latest checkpoint's oldestActiveXID:  %u\n"),
                                769                 :                :            ControlFile.checkPointCopy.oldestActiveXid);
 4609 alvherre@alvh.no-ip.      770                 :             39 :     printf(_("Latest checkpoint's oldestMultiXid:   %u\n"),
                                771                 :                :            ControlFile.checkPointCopy.oldestMulti);
                                772                 :             39 :     printf(_("Latest checkpoint's oldestMulti's DB: %u\n"),
                                773                 :                :            ControlFile.checkPointCopy.oldestMultiDB);
 3540 mail@joeconway.com        774                 :             39 :     printf(_("Latest checkpoint's oldestCommitTsXid:%u\n"),
                                775                 :                :            ControlFile.checkPointCopy.oldestCommitTsXid);
                                776                 :             39 :     printf(_("Latest checkpoint's newestCommitTsXid:%u\n"),
                                777                 :                :            ControlFile.checkPointCopy.newestCommitTsXid);
 6956 tgl@sss.pgh.pa.us         778                 :             39 :     printf(_("Maximum data alignment:               %u\n"),
                                779                 :                :            ControlFile.maxAlign);
                                780                 :                :     /* we don't print floatFormat since can't say much useful about it */
                                781                 :             39 :     printf(_("Database block size:                  %u\n"),
                                782                 :                :            ControlFile.blcksz);
                                783                 :             39 :     printf(_("Blocks per segment of large relation: %u\n"),
                                784                 :                :            ControlFile.relseg_size);
                                785                 :             39 :     printf(_("WAL block size:                       %u\n"),
                                786                 :                :            ControlFile.xlog_blcksz);
                                787                 :             39 :     printf(_("Bytes per WAL segment:                %u\n"),
                                788                 :                :            ControlFile.xlog_seg_size);
                                789                 :             39 :     printf(_("Maximum length of identifiers:        %u\n"),
                                790                 :                :            ControlFile.nameDataLen);
                                791                 :             39 :     printf(_("Maximum columns in an index:          %u\n"),
                                792                 :                :            ControlFile.indexMaxKeys);
 6731                           793                 :             39 :     printf(_("Maximum size of a TOAST chunk:        %u\n"),
                                794                 :                :            ControlFile.toast_max_chunk_size);
 4111                           795                 :             39 :     printf(_("Size of a large-object chunk:         %u\n"),
                                796                 :                :            ControlFile.loblksize);
                                797                 :                :     /* This is no longer configurable, but users may still expect to see it: */
 8375                           798                 :             39 :     printf(_("Date/time type storage:               %s\n"),
                                799                 :                :            _("64-bit integers"));
 6347                           800         [ +  - ]:             39 :     printf(_("Float8 argument passing:              %s\n"),
                                801                 :                :            (ControlFile.float8ByVal ? _("by value") : _("by reference")));
 4512 simon@2ndQuadrant.co      802                 :             39 :     printf(_("Data page checksum version:           %u\n"),
                                803                 :                :            ControlFile.data_checksum_version);
  197 msawada@postgresql.o      804         [ +  + ]:             39 :     printf(_("Default char data signedness:         %s\n"),
                                805                 :                :            (ControlFile.default_char_signedness ? _("signed") : _("unsigned")));
 8421 bruce@momjian.us          806                 :             39 : }
                                807                 :                : 
                                808                 :                : 
                                809                 :                : /*
                                810                 :                :  * Print the values to be changed.
                                811                 :                :  */
                                812                 :                : static void
 3675 andres@anarazel.de        813                 :             39 : PrintNewControlValues(void)
                                814                 :                : {
                                815                 :                :     char        fname[MAXFNAMELEN];
                                816                 :                : 
                                817                 :                :     /* This will be always printed in order to keep format same. */
 4286 heikki.linnakangas@i      818                 :             39 :     printf(_("\n\nValues to be changed:\n\n"));
                                819                 :                : 
 2909 andres@anarazel.de        820                 :             39 :     XLogFileName(fname, ControlFile.checkPointCopy.ThisTimeLineID,
                                821                 :                :                  newXlogSegNo, WalSegSz);
 1087 tgl@sss.pgh.pa.us         822                 :             39 :     printf(_("First log segment after reset:        %s\n"), fname);
                                823                 :                : 
 4286 heikki.linnakangas@i      824         [ +  + ]:             39 :     if (set_mxid != 0)
                                825                 :                :     {
                                826                 :              1 :         printf(_("NextMultiXactId:                      %u\n"),
                                827                 :                :                ControlFile.checkPointCopy.nextMulti);
                                828                 :              1 :         printf(_("OldestMultiXid:                       %u\n"),
                                829                 :                :                ControlFile.checkPointCopy.oldestMulti);
                                830                 :              1 :         printf(_("OldestMulti's DB:                     %u\n"),
                                831                 :                :                ControlFile.checkPointCopy.oldestMultiDB);
                                832                 :                :     }
                                833                 :                : 
                                834         [ +  + ]:             39 :     if (set_mxoff != -1)
                                835                 :                :     {
                                836                 :              1 :         printf(_("NextMultiOffset:                      %u\n"),
                                837                 :                :                ControlFile.checkPointCopy.nextMultiOffset);
                                838                 :                :     }
                                839                 :                : 
                                840         [ +  + ]:             39 :     if (set_oid != 0)
                                841                 :                :     {
                                842                 :              1 :         printf(_("NextOID:                              %u\n"),
                                843                 :                :                ControlFile.checkPointCopy.nextOid);
                                844                 :                :     }
                                845                 :                : 
                                846         [ +  + ]:             39 :     if (set_xid != 0)
                                847                 :                :     {
                                848                 :              1 :         printf(_("NextXID:                              %u\n"),
                                849                 :                :                XidFromFullTransactionId(ControlFile.checkPointCopy.nextXid));
                                850                 :              1 :         printf(_("OldestXID:                            %u\n"),
                                851                 :                :                ControlFile.checkPointCopy.oldestXid);
                                852                 :              1 :         printf(_("OldestXID's DB:                       %u\n"),
                                853                 :                :                ControlFile.checkPointCopy.oldestXidDB);
                                854                 :                :     }
                                855                 :                : 
                                856         [ +  + ]:             39 :     if (set_xid_epoch != -1)
                                857                 :                :     {
 4046 peter_e@gmx.net           858                 :              1 :         printf(_("NextXID epoch:                        %u\n"),
                                859                 :                :                EpochFromFullTransactionId(ControlFile.checkPointCopy.nextXid));
                                860                 :                :     }
                                861                 :                : 
 3540 mail@joeconway.com        862         [ +  + ]:             39 :     if (set_oldest_commit_ts_xid != 0)
                                863                 :                :     {
                                864                 :              1 :         printf(_("oldestCommitTsXid:                    %u\n"),
                                865                 :                :                ControlFile.checkPointCopy.oldestCommitTsXid);
                                866                 :                :     }
                                867         [ -  + ]:             39 :     if (set_newest_commit_ts_xid != 0)
                                868                 :                :     {
 3540 mail@joeconway.com        869                 :UBC           0 :         printf(_("newestCommitTsXid:                    %u\n"),
                                870                 :                :                ControlFile.checkPointCopy.newestCommitTsXid);
                                871                 :                :     }
                                872                 :                : 
 2722 peter_e@gmx.net           873         [ +  + ]:CBC          39 :     if (set_wal_segsize != 0)
                                874                 :                :     {
                                875                 :              1 :         printf(_("Bytes per WAL segment:                %u\n"),
                                876                 :                :                ControlFile.xlog_seg_size);
                                877                 :                :     }
 4286 heikki.linnakangas@i      878                 :             39 : }
                                879                 :                : 
                                880                 :                : 
                                881                 :                : /*
                                882                 :                :  * Write out the new pg_control file.
                                883                 :                :  */
                                884                 :                : static void
 7035 bruce@momjian.us          885                 :             64 : RewriteControlFile(void)
                                886                 :                : {
                                887                 :                :     /*
                                888                 :                :      * Adjust fields as needed to force an empty XLOG starting at
                                889                 :                :      * newXlogSegNo.
                                890                 :                :      */
 2616 alvherre@alvh.no-ip.      891                 :             64 :     XLogSegNoOffsetToRecPtr(newXlogSegNo, SizeOfXLogLongPHD, WalSegSz,
                                892                 :                :                             ControlFile.checkPointCopy.redo);
 6411 tgl@sss.pgh.pa.us         893                 :             64 :     ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
                                894                 :                : 
 8421 bruce@momjian.us          895                 :             64 :     ControlFile.state = DB_SHUTDOWNED;
                                896                 :             64 :     ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
 4822 heikki.linnakangas@i      897                 :             64 :     ControlFile.minRecoveryPoint = 0;
 4659                           898                 :             64 :     ControlFile.minRecoveryPointTLI = 0;
 4822                           899                 :             64 :     ControlFile.backupStartPoint = 0;
                                900                 :             64 :     ControlFile.backupEndPoint = 0;
 5134                           901                 :             64 :     ControlFile.backupEndRequired = false;
                                902                 :                : 
                                903                 :                :     /*
                                904                 :                :      * Force the defaults for max_* settings. The values don't really matter
                                905                 :                :      * as long as wal_level='minimal'; the postmaster will reset these fields
                                906                 :                :      * anyway at startup.
                                907                 :                :      */
 5610 tgl@sss.pgh.pa.us         908                 :             64 :     ControlFile.wal_level = WAL_LEVEL_MINIMAL;
 4277 fujii@postgresql.org      909                 :             64 :     ControlFile.wal_log_hints = false;
 3930 alvherre@alvh.no-ip.      910                 :             64 :     ControlFile.track_commit_timestamp = false;
 5610 heikki.linnakangas@i      911                 :             64 :     ControlFile.MaxConnections = 100;
 2398 michael@paquier.xyz       912                 :             64 :     ControlFile.max_wal_senders = 10;
 3197 rhaas@postgresql.org      913                 :             64 :     ControlFile.max_worker_processes = 8;
 5610 heikki.linnakangas@i      914                 :             64 :     ControlFile.max_prepared_xacts = 0;
                                915                 :             64 :     ControlFile.max_locks_per_xact = 64;
                                916                 :                : 
                                917                 :                :     /* The control file gets flushed here. */
 2350 peter@eisentraut.org      918                 :             64 :     update_controlfile(".", &ControlFile, true);
 8421 bruce@momjian.us          919                 :             64 : }
                                920                 :                : 
                                921                 :                : 
                                922                 :                : /*
                                923                 :                :  * Scan existing XLOG files and determine the highest existing WAL address
                                924                 :                :  *
                                925                 :                :  * On entry, ControlFile.checkPointCopy.redo and ControlFile.xlog_seg_size
                                926                 :                :  * are assumed valid (note that we allow the old xlog seg size to differ
                                927                 :                :  * from what we're using).  On exit, newXlogSegNo is set to suitable
                                928                 :                :  * value for the beginning of replacement WAL (in our seg size).
                                929                 :                :  */
                                930                 :                : static void
 6847 tgl@sss.pgh.pa.us         931                 :            104 : FindEndOfXLOG(void)
                                932                 :                : {
                                933                 :                :     DIR        *xldir;
                                934                 :                :     struct dirent *xlde;
                                935                 :                :     uint64      xlogbytepos;
                                936                 :                : 
                                937                 :                :     /*
                                938                 :                :      * Initialize the max() computation using the last checkpoint address from
                                939                 :                :      * old pg_control.  Note that for the moment we are working with segment
                                940                 :                :      * numbering according to the old xlog seg size.
                                941                 :                :      */
 1067 michael@paquier.xyz       942                 :            104 :     XLByteToSeg(ControlFile.checkPointCopy.redo, newXlogSegNo,
                                943                 :                :                 ControlFile.xlog_seg_size);
                                944                 :                : 
                                945                 :                :     /*
                                946                 :                :      * Scan the pg_wal directory to find existing WAL segment files. We assume
                                947                 :                :      * any present have been used; in most scenarios this should be
                                948                 :                :      * conservative, because of xlog.c's attempts to pre-create files.
                                949                 :                :      */
 6847 tgl@sss.pgh.pa.us         950                 :            104 :     xldir = opendir(XLOGDIR);
                                951         [ -  + ]:            104 :     if (xldir == NULL)
 1247 tgl@sss.pgh.pa.us         952                 :UBC           0 :         pg_fatal("could not open directory \"%s\": %m", XLOGDIR);
                                953                 :                : 
 4187 bruce@momjian.us          954         [ +  + ]:CBC         797 :     while (errno = 0, (xlde = readdir(xldir)) != NULL)
                                955                 :                :     {
 3718 fujii@postgresql.org      956   [ +  +  -  + ]:           1110 :         if (IsXLogFileName(xlde->d_name) ||
                                957                 :            417 :             IsPartialXLogFileName(xlde->d_name))
                                958                 :                :         {
                                959                 :                :             TimeLineID  tli;
                                960                 :                :             XLogSegNo   segno;
                                961                 :                : 
                                962                 :                :             /* Use the segment size from the control file */
 1067 michael@paquier.xyz       963                 :            276 :             XLogFromFileName(xlde->d_name, &tli, &segno,
                                964                 :            276 :                              ControlFile.xlog_seg_size);
                                965                 :                : 
                                966                 :                :             /*
                                967                 :                :              * Note: we take the max of all files found, regardless of their
                                968                 :                :              * timelines.  Another possibility would be to ignore files of
                                969                 :                :              * timelines other than the target TLI, but this seems safer.
                                970                 :                :              * Better too large a result than too small...
                                971                 :                :              */
 4822 heikki.linnakangas@i      972         [ +  + ]:            276 :             if (segno > newXlogSegNo)
                                973                 :             27 :                 newXlogSegNo = segno;
                                974                 :                :         }
                                975                 :                :     }
                                976                 :                : 
 6847 tgl@sss.pgh.pa.us         977         [ -  + ]:            104 :     if (errno)
 1247 tgl@sss.pgh.pa.us         978                 :UBC           0 :         pg_fatal("could not read directory \"%s\": %m", XLOGDIR);
                                979                 :                : 
 4187 bruce@momjian.us          980         [ -  + ]:CBC         104 :     if (closedir(xldir))
 1247 tgl@sss.pgh.pa.us         981                 :UBC           0 :         pg_fatal("could not close directory \"%s\": %m", XLOGDIR);
                                982                 :                : 
                                983                 :                :     /*
                                984                 :                :      * Finally, convert to new xlog seg size, and advance by one to ensure we
                                985                 :                :      * are in virgin territory.
                                986                 :                :      */
 4822 heikki.linnakangas@i      987                 :CBC         104 :     xlogbytepos = newXlogSegNo * ControlFile.xlog_seg_size;
 2722 peter_e@gmx.net           988                 :            104 :     newXlogSegNo = (xlogbytepos + ControlFile.xlog_seg_size - 1) / WalSegSz;
 4822 heikki.linnakangas@i      989                 :            104 :     newXlogSegNo++;
 6847 tgl@sss.pgh.pa.us         990                 :            104 : }
                                991                 :                : 
                                992                 :                : 
                                993                 :                : /*
                                994                 :                :  * Remove existing XLOG files
                                995                 :                :  */
                                996                 :                : static void
 8421 bruce@momjian.us          997                 :             64 : KillExistingXLOG(void)
                                998                 :                : {
                                999                 :                :     DIR        *xldir;
                               1000                 :                :     struct dirent *xlde;
                               1001                 :                :     char        path[MAXPGPATH + sizeof(XLOGDIR)];
                               1002                 :                : 
 7369 tgl@sss.pgh.pa.us        1003                 :             64 :     xldir = opendir(XLOGDIR);
 8421 bruce@momjian.us         1004         [ -  + ]:             64 :     if (xldir == NULL)
 1247 tgl@sss.pgh.pa.us        1005                 :UBC           0 :         pg_fatal("could not open directory \"%s\": %m", XLOGDIR);
                               1006                 :                : 
 4187 bruce@momjian.us         1007         [ +  + ]:CBC         406 :     while (errno = 0, (xlde = readdir(xldir)) != NULL)
                               1008                 :                :     {
 3718 fujii@postgresql.org     1009   [ +  +  -  + ]:            599 :         if (IsXLogFileName(xlde->d_name) ||
                               1010                 :            257 :             IsPartialXLogFileName(xlde->d_name))
                               1011                 :                :         {
 3070 peter_e@gmx.net          1012                 :             85 :             snprintf(path, sizeof(path), "%s/%s", XLOGDIR, xlde->d_name);
 8421 bruce@momjian.us         1013         [ -  + ]:             85 :             if (unlink(path) < 0)
 1247 tgl@sss.pgh.pa.us        1014                 :UBC           0 :                 pg_fatal("could not delete file \"%s\": %m", path);
                               1015                 :                :         }
                               1016                 :                :     }
                               1017                 :                : 
 8421 bruce@momjian.us         1018         [ -  + ]:CBC          64 :     if (errno)
 1247 tgl@sss.pgh.pa.us        1019                 :UBC           0 :         pg_fatal("could not read directory \"%s\": %m", XLOGDIR);
                               1020                 :                : 
 4187 bruce@momjian.us         1021         [ -  + ]:CBC          64 :     if (closedir(xldir))
 1247 tgl@sss.pgh.pa.us        1022                 :UBC           0 :         pg_fatal("could not close directory \"%s\": %m", XLOGDIR);
 8421 bruce@momjian.us         1023                 :CBC          64 : }
                               1024                 :                : 
                               1025                 :                : 
                               1026                 :                : /*
                               1027                 :                :  * Remove existing archive status files
                               1028                 :                :  */
                               1029                 :                : static void
 5970 tgl@sss.pgh.pa.us        1030                 :             64 : KillExistingArchiveStatus(void)
                               1031                 :                : {
                               1032                 :                : #define ARCHSTATDIR XLOGDIR "/archive_status"
                               1033                 :                : 
                               1034                 :                :     DIR        *xldir;
                               1035                 :                :     struct dirent *xlde;
                               1036                 :                :     char        path[MAXPGPATH + sizeof(ARCHSTATDIR)];
                               1037                 :                : 
                               1038                 :             64 :     xldir = opendir(ARCHSTATDIR);
                               1039         [ -  + ]:             64 :     if (xldir == NULL)
 1247 tgl@sss.pgh.pa.us        1040                 :UBC           0 :         pg_fatal("could not open directory \"%s\": %m", ARCHSTATDIR);
                               1041                 :                : 
 4187 bruce@momjian.us         1042         [ +  + ]:CBC         192 :     while (errno = 0, (xlde = readdir(xldir)) != NULL)
                               1043                 :                :     {
 3719 fujii@postgresql.org     1044         [ -  + ]:            128 :         if (strspn(xlde->d_name, "0123456789ABCDEF") == XLOG_FNAME_LEN &&
 3719 fujii@postgresql.org     1045         [ #  # ]:UBC           0 :             (strcmp(xlde->d_name + XLOG_FNAME_LEN, ".ready") == 0 ||
 3718                          1046         [ #  # ]:              0 :              strcmp(xlde->d_name + XLOG_FNAME_LEN, ".done") == 0 ||
                               1047         [ #  # ]:              0 :              strcmp(xlde->d_name + XLOG_FNAME_LEN, ".partial.ready") == 0 ||
                               1048         [ #  # ]:              0 :              strcmp(xlde->d_name + XLOG_FNAME_LEN, ".partial.done") == 0))
                               1049                 :                :         {
 3070 peter_e@gmx.net          1050                 :              0 :             snprintf(path, sizeof(path), "%s/%s", ARCHSTATDIR, xlde->d_name);
 5970 tgl@sss.pgh.pa.us        1051         [ #  # ]:              0 :             if (unlink(path) < 0)
 1247                          1052                 :              0 :                 pg_fatal("could not delete file \"%s\": %m", path);
                               1053                 :                :         }
                               1054                 :                :     }
                               1055                 :                : 
 5970 tgl@sss.pgh.pa.us        1056         [ -  + ]:CBC          64 :     if (errno)
 1247 tgl@sss.pgh.pa.us        1057                 :UBC           0 :         pg_fatal("could not read directory \"%s\": %m", ARCHSTATDIR);
                               1058                 :                : 
 4187 bruce@momjian.us         1059         [ -  + ]:CBC          64 :     if (closedir(xldir))
 1247 tgl@sss.pgh.pa.us        1060                 :UBC           0 :         pg_fatal("could not close directory \"%s\": %m", ARCHSTATDIR);
 5970 tgl@sss.pgh.pa.us        1061                 :CBC          64 : }
                               1062                 :                : 
                               1063                 :                : /*
                               1064                 :                :  * Remove existing WAL summary files
                               1065                 :                :  */
                               1066                 :                : static void
  626 rhaas@postgresql.org     1067                 :             64 : KillExistingWALSummaries(void)
                               1068                 :                : {
                               1069                 :                : #define WALSUMMARYDIR XLOGDIR   "/summaries"
                               1070                 :                : #define WALSUMMARY_NHEXCHARS    40
                               1071                 :                : 
                               1072                 :                :     DIR        *xldir;
                               1073                 :                :     struct dirent *xlde;
                               1074                 :                :     char        path[MAXPGPATH + sizeof(WALSUMMARYDIR)];
                               1075                 :                : 
                               1076                 :             64 :     xldir = opendir(WALSUMMARYDIR);
                               1077         [ -  + ]:             64 :     if (xldir == NULL)
  626 rhaas@postgresql.org     1078                 :UBC           0 :         pg_fatal("could not open directory \"%s\": %m", WALSUMMARYDIR);
                               1079                 :                : 
  626 rhaas@postgresql.org     1080         [ +  + ]:CBC         192 :     while (errno = 0, (xlde = readdir(xldir)) != NULL)
                               1081                 :                :     {
                               1082         [ -  + ]:            128 :         if (strspn(xlde->d_name, "0123456789ABCDEF") == WALSUMMARY_NHEXCHARS &&
  626 rhaas@postgresql.org     1083         [ #  # ]:UBC           0 :             strcmp(xlde->d_name + WALSUMMARY_NHEXCHARS, ".summary") == 0)
                               1084                 :                :         {
                               1085                 :              0 :             snprintf(path, sizeof(path), "%s/%s", WALSUMMARYDIR, xlde->d_name);
                               1086         [ #  # ]:              0 :             if (unlink(path) < 0)
                               1087                 :              0 :                 pg_fatal("could not delete file \"%s\": %m", path);
                               1088                 :                :         }
                               1089                 :                :     }
                               1090                 :                : 
  626 rhaas@postgresql.org     1091         [ -  + ]:CBC          64 :     if (errno)
  626 rhaas@postgresql.org     1092                 :UBC           0 :         pg_fatal("could not read directory \"%s\": %m", WALSUMMARYDIR);
                               1093                 :                : 
  626 rhaas@postgresql.org     1094         [ -  + ]:CBC          64 :     if (closedir(xldir))
  626 rhaas@postgresql.org     1095                 :UBC           0 :         pg_fatal("could not close directory \"%s\": %m", ARCHSTATDIR);
  626 rhaas@postgresql.org     1096                 :CBC          64 : }
                               1097                 :                : 
                               1098                 :                : /*
                               1099                 :                :  * Write an empty XLOG file, containing only the checkpoint record
                               1100                 :                :  * already set up in ControlFile.
                               1101                 :                :  */
                               1102                 :                : static void
 8421 bruce@momjian.us         1103                 :             64 : WriteEmptyXLOG(void)
                               1104                 :                : {
                               1105                 :                :     PGAlignedXLogBlock buffer;
                               1106                 :                :     XLogPageHeader page;
                               1107                 :                :     XLogLongPageHeader longpage;
                               1108                 :                :     XLogRecord *record;
                               1109                 :                :     pg_crc32c   crc;
                               1110                 :                :     char        path[MAXPGPATH];
                               1111                 :                :     int         fd;
                               1112                 :                :     int         nbytes;
                               1113                 :                :     char       *recptr;
                               1114                 :                : 
 2562 tgl@sss.pgh.pa.us        1115                 :             64 :     memset(buffer.data, 0, XLOG_BLCKSZ);
                               1116                 :                : 
                               1117                 :                :     /* Set up the XLOG page header */
                               1118                 :             64 :     page = (XLogPageHeader) buffer.data;
 8421 bruce@momjian.us         1119                 :             64 :     page->xlp_magic = XLOG_PAGE_MAGIC;
 7717 tgl@sss.pgh.pa.us        1120                 :             64 :     page->xlp_info = XLP_LONG_HEADER;
                               1121                 :             64 :     page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
 4822 heikki.linnakangas@i     1122                 :             64 :     page->xlp_pageaddr = ControlFile.checkPointCopy.redo - SizeOfXLogLongPHD;
 7717 tgl@sss.pgh.pa.us        1123                 :             64 :     longpage = (XLogLongPageHeader) page;
                               1124                 :             64 :     longpage->xlp_sysid = ControlFile.system_identifier;
 2909 andres@anarazel.de       1125                 :             64 :     longpage->xlp_seg_size = WalSegSz;
 7094 tgl@sss.pgh.pa.us        1126                 :             64 :     longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
                               1127                 :                : 
                               1128                 :                :     /* Insert the initial checkpoint record */
 3943 heikki.linnakangas@i     1129                 :             64 :     recptr = (char *) page + SizeOfXLogLongPHD;
                               1130                 :             64 :     record = (XLogRecord *) recptr;
 4822                          1131                 :             64 :     record->xl_prev = 0;
 7878 tgl@sss.pgh.pa.us        1132                 :             64 :     record->xl_xid = InvalidTransactionId;
 3943 heikki.linnakangas@i     1133                 :             64 :     record->xl_tot_len = SizeOfXLogRecord + SizeOfXLogRecordDataHeaderShort + sizeof(CheckPoint);
 8421 bruce@momjian.us         1134                 :             64 :     record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
                               1135                 :             64 :     record->xl_rmid = RM_XLOG_ID;
                               1136                 :                : 
 3943 heikki.linnakangas@i     1137                 :             64 :     recptr += SizeOfXLogRecord;
 3084 tgl@sss.pgh.pa.us        1138                 :             64 :     *(recptr++) = (char) XLR_BLOCK_ID_DATA_SHORT;
 3943 heikki.linnakangas@i     1139                 :             64 :     *(recptr++) = sizeof(CheckPoint);
                               1140                 :             64 :     memcpy(recptr, &ControlFile.checkPointCopy,
                               1141                 :                :            sizeof(CheckPoint));
                               1142                 :                : 
 3959                          1143                 :             64 :     INIT_CRC32C(crc);
 3943                          1144                 :             64 :     COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
 3959                          1145                 :             64 :     COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
                               1146                 :             64 :     FIN_CRC32C(crc);
 8421 bruce@momjian.us         1147                 :             64 :     record->xl_crc = crc;
                               1148                 :                : 
                               1149                 :                :     /* Write the first page */
 2909 andres@anarazel.de       1150                 :             64 :     XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID,
                               1151                 :                :                  newXlogSegNo, WalSegSz);
                               1152                 :                : 
 8421 bruce@momjian.us         1153                 :             64 :     unlink(path);
                               1154                 :                : 
                               1155                 :             64 :     fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
                               1156                 :                :               pg_file_create_mode);
                               1157         [ -  + ]:             64 :     if (fd < 0)
 1247 tgl@sss.pgh.pa.us        1158                 :UBC           0 :         pg_fatal("could not open file \"%s\": %m", path);
                               1159                 :                : 
 8421 bruce@momjian.us         1160                 :CBC          64 :     errno = 0;
 2562 tgl@sss.pgh.pa.us        1161         [ -  + ]:             64 :     if (write(fd, buffer.data, XLOG_BLCKSZ) != XLOG_BLCKSZ)
                               1162                 :                :     {
                               1163                 :                :         /* if write didn't set errno, assume problem is no disk space */
 8421 bruce@momjian.us         1164         [ #  # ]:UBC           0 :         if (errno == 0)
                               1165                 :              0 :             errno = ENOSPC;
 1247 tgl@sss.pgh.pa.us        1166                 :              0 :         pg_fatal("could not write file \"%s\": %m", path);
                               1167                 :                :     }
                               1168                 :                : 
                               1169                 :                :     /* Fill the rest of the file with zeroes */
 2562 tgl@sss.pgh.pa.us        1170                 :CBC          64 :     memset(buffer.data, 0, XLOG_BLCKSZ);
 2909 andres@anarazel.de       1171         [ +  + ]:         115712 :     for (nbytes = XLOG_BLCKSZ; nbytes < WalSegSz; nbytes += XLOG_BLCKSZ)
                               1172                 :                :     {
 8421 bruce@momjian.us         1173                 :         115648 :         errno = 0;
 2562 tgl@sss.pgh.pa.us        1174         [ -  + ]:         115648 :         if (write(fd, buffer.data, XLOG_BLCKSZ) != XLOG_BLCKSZ)
                               1175                 :                :         {
 8421 bruce@momjian.us         1176         [ #  # ]:UBC           0 :             if (errno == 0)
                               1177                 :              0 :                 errno = ENOSPC;
 1247 tgl@sss.pgh.pa.us        1178                 :              0 :             pg_fatal("could not write file \"%s\": %m", path);
                               1179                 :                :         }
                               1180                 :                :     }
                               1181                 :                : 
 8421 bruce@momjian.us         1182         [ -  + ]:CBC          64 :     if (fsync(fd) != 0)
 1247 tgl@sss.pgh.pa.us        1183                 :UBC           0 :         pg_fatal("fsync error: %m");
                               1184                 :                : 
 8421 bruce@momjian.us         1185                 :CBC          64 :     close(fd);
                               1186                 :             64 : }
                               1187                 :                : 
                               1188                 :                : 
                               1189                 :                : static void
                               1190                 :              1 : usage(void)
                               1191                 :                : {
 3039 peter_e@gmx.net          1192                 :              1 :     printf(_("%s resets the PostgreSQL write-ahead log.\n\n"), progname);
  709 peter@eisentraut.org     1193                 :              1 :     printf(_("Usage:\n"));
                               1194                 :              1 :     printf(_("  %s [OPTION]... DATADIR\n"), progname);
                               1195                 :                : 
                               1196                 :              1 :     printf(_("\nOptions:\n"));
                               1197                 :              1 :     printf(_(" [-D, --pgdata=]DATADIR  data directory\n"));
                               1198                 :              1 :     printf(_("  -f, --force            force update to be done even after unclean shutdown or\n"
                               1199                 :                :              "                         if pg_control values had to be guessed\n"));
                               1200                 :              1 :     printf(_("  -n, --dry-run          no update, just show what would be done\n"));
                               1201                 :              1 :     printf(_("  -V, --version          output version information, then exit\n"));
                               1202                 :              1 :     printf(_("  -?, --help             show this help, then exit\n"));
                               1203                 :                : 
                               1204                 :              1 :     printf(_("\nOptions to override control file values:\n"));
 2723 peter_e@gmx.net          1205                 :              1 :     printf(_("  -c, --commit-timestamp-ids=XID,XID\n"
                               1206                 :                :              "                                   set oldest and newest transactions bearing\n"
                               1207                 :                :              "                                   commit timestamp (zero means no change)\n"));
 1503 bruce@momjian.us         1208                 :              1 :     printf(_("  -e, --epoch=XIDEPOCH             set next transaction ID epoch\n"));
                               1209                 :              1 :     printf(_("  -l, --next-wal-file=WALFILE      set minimum starting location for new WAL\n"));
                               1210                 :              1 :     printf(_("  -m, --multixact-ids=MXID,MXID    set next and oldest multitransaction ID\n"));
                               1211                 :              1 :     printf(_("  -o, --next-oid=OID               set next OID\n"));
                               1212                 :              1 :     printf(_("  -O, --multixact-offset=OFFSET    set next multitransaction offset\n"));
                               1213                 :              1 :     printf(_("  -u, --oldest-transaction-id=XID  set oldest transaction ID\n"));
                               1214                 :              1 :     printf(_("  -x, --next-transaction-id=XID    set next transaction ID\n"));
  130 peter@eisentraut.org     1215                 :              1 :     printf(_("      --char-signedness=OPTION     set char signedness to \"signed\" or \"unsigned\"\n"));
 1503 bruce@momjian.us         1216                 :              1 :     printf(_("      --wal-segsize=SIZE           size of WAL segments, in megabytes\n"));
                               1217                 :                : 
 2017 peter@eisentraut.org     1218                 :              1 :     printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
                               1219                 :              1 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
 8421 bruce@momjian.us         1220                 :              1 : }
        

Generated by: LCOV version 2.4-beta