LCOV - code coverage report
Current view: top level - src/bin/pg_resetwal - pg_resetwal.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12devel Lines: 374 557 67.1 %
Date: 2018-09-20 15:21:09 Functions: 12 12 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13