]> granicus.if.org Git - postgresql/blobdiff - src/bin/pg_resetxlog/pg_resetxlog.c
Properly check for readdir/closedir() failures
[postgresql] / src / bin / pg_resetxlog / pg_resetxlog.c
index 15613832792ccb7048ba6035ea3a106580e0960d..af2cec7e932cd6d392b4968f84fa2831739843d0 100644 (file)
  * step 2 ...
  *
  *
- * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/bin/pg_resetxlog/pg_resetxlog.c,v 1.53 2006/10/04 00:30:05 momjian Exp $
+ * src/bin/pg_resetxlog/pg_resetxlog.c
  *
  *-------------------------------------------------------------------------
  */
+
+/*
+ * We have to use postgres.h not postgres_fe.h here, because there's so much
+ * backend-only stuff in the XLOG include files we need.  But we need a
+ * frontend-ish environment otherwise. Hence this ugly hack.
+ */
+#define FRONTEND 1
+
 #include "postgres.h"
 
 #include <dirent.h>
 #include <sys/time.h>
 #include <time.h>
 #include <unistd.h>
-#ifdef HAVE_GETOPT_H
-#include <getopt.h>
-#endif
 
 #include "access/transam.h"
+#include "access/tuptoaster.h"
 #include "access/multixact.h"
 #include "access/xlog_internal.h"
 #include "catalog/catversion.h"
 #include "catalog/pg_control.h"
-
-extern int     optind;
-extern char *optarg;
+#include "common/fe_memutils.h"
+#include "pg_getopt.h"
 
 
 static ControlFileData ControlFile;            /* pg_control values */
-static uint32 newXlogId,
-                       newXlogSeg;                     /* ID/Segment of new XLOG segment */
+static XLogSegNo newXlogSegNo; /* new XLOG segment # */
 static bool guessed = false;   /* T if we had to guess at any values */
 static const char *progname;
+static uint32 set_xid_epoch = (uint32) -1;
+static TransactionId set_xid = 0;
+static Oid     set_oid = 0;
+static MultiXactId set_mxid = 0;
+static MultiXactOffset set_mxoff = (MultiXactOffset) -1;
+static uint32 minXlogTli = 0;
+static XLogSegNo minXlogSegNo = 0;
 
 static bool ReadControlFile(void);
 static void GuessControlValues(void);
 static void PrintControlValues(bool guessed);
+static void PrintNewControlValues(void);
 static void RewriteControlFile(void);
+static void FindEndOfXLOG(void);
 static void KillExistingXLOG(void);
+static void KillExistingArchiveStatus(void);
 static void WriteEmptyXLOG(void);
 static void usage(void);
 
@@ -71,22 +85,13 @@ main(int argc, char *argv[])
        int                     c;
        bool            force = false;
        bool            noupdate = false;
-       uint32          set_xid_epoch = -1;
-       TransactionId set_xid = 0;
-       Oid                     set_oid = 0;
-       MultiXactId set_mxid = 0;
-       MultiXactOffset set_mxoff = -1;
-       uint32          minXlogTli = 0,
-                               minXlogId = 0,
-                               minXlogSeg = 0;
+       MultiXactId set_oldestmxid = 0;
        char       *endptr;
        char       *endptr2;
-       char       *endptr3;
        char       *DataDir;
        int                     fd;
-       char            path[MAXPGPATH];
 
-       set_pglocale_pgservice(argv[0], "pg_resetxlog");
+       set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_resetxlog"));
 
        progname = get_progname(argv[0]);
 
@@ -164,7 +169,15 @@ main(int argc, char *argv[])
 
                        case 'm':
                                set_mxid = strtoul(optarg, &endptr, 0);
-                               if (endptr == optarg || *endptr != '\0')
+                               if (endptr == optarg || *endptr != ',')
+                               {
+                                       fprintf(stderr, _("%s: invalid argument for option -m\n"), progname);
+                                       fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
+                                       exit(1);
+                               }
+
+                               set_oldestmxid = strtoul(endptr + 1, &endptr2, 0);
+                               if (endptr2 == endptr + 1 || *endptr2 != '\0')
                                {
                                        fprintf(stderr, _("%s: invalid argument for option -m\n"), progname);
                                        fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
@@ -175,6 +188,17 @@ main(int argc, char *argv[])
                                        fprintf(stderr, _("%s: multitransaction ID (-m) must not be 0\n"), progname);
                                        exit(1);
                                }
+
+                               /*
+                                * XXX It'd be nice to have more sanity checks here, e.g. so
+                                * that oldest is not wrapped around w.r.t. nextMulti.
+                                */
+                               if (set_oldestmxid == 0)
+                               {
+                                       fprintf(stderr, _("%s: oldest multitransaction ID (-m) must not be 0\n"),
+                                                       progname);
+                                       exit(1);
+                               }
                                break;
 
                        case 'O':
@@ -193,27 +217,13 @@ main(int argc, char *argv[])
                                break;
 
                        case 'l':
-                               minXlogTli = strtoul(optarg, &endptr, 0);
-                               if (endptr == optarg || *endptr != ',')
-                               {
-                                       fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
-                                       fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
-                                       exit(1);
-                               }
-                               minXlogId = strtoul(endptr + 1, &endptr2, 0);
-                               if (endptr2 == endptr + 1 || *endptr2 != ',')
-                               {
-                                       fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
-                                       fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
-                                       exit(1);
-                               }
-                               minXlogSeg = strtoul(endptr2 + 1, &endptr3, 0);
-                               if (endptr3 == endptr2 + 1 || *endptr3 != '\0')
+                               if (strspn(optarg, "01234567890ABCDEFabcdef") != 24)
                                {
                                        fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
                                        fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
                                        exit(1);
                                }
+                               XLogFromFileName(optarg, &minXlogTli, &minXlogSegNo);
                                break;
 
                        default:
@@ -259,13 +269,12 @@ main(int argc, char *argv[])
         * Check for a postmaster lock file --- if there is one, refuse to
         * proceed, on grounds we might be interfering with a live installation.
         */
-       snprintf(path, MAXPGPATH, "%s/postmaster.pid", DataDir);
-
-       if ((fd = open(path, O_RDONLY, 0)) < 0)
+       if ((fd = open("postmaster.pid", O_RDONLY, 0)) < 0)
        {
                if (errno != ENOENT)
                {
-                       fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), progname, path, strerror(errno));
+                       fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
+                                       progname, "postmaster.pid", strerror(errno));
                        exit(1);
                }
        }
@@ -273,7 +282,7 @@ main(int argc, char *argv[])
        {
                fprintf(stderr, _("%s: lock file \"%s\" exists\n"
                                                  "Is a server running?  If not, delete the lock file and try again.\n"),
-                               progname, path);
+                               progname, "postmaster.pid");
                exit(1);
        }
 
@@ -283,6 +292,18 @@ main(int argc, char *argv[])
        if (!ReadControlFile())
                GuessControlValues();
 
+       /*
+        * Also look at existing segment files to set up newXlogSegNo
+        */
+       FindEndOfXLOG();
+
+       /*
+        * If we're not going to proceed with the reset, print the current control
+        * file parameters.
+        */
+       if ((guessed && !force) || noupdate)
+               PrintControlValues(guessed);
+
        /*
         * Adjust fields if required by switches.  (Do this now so that printout,
         * if any, includes these values.)
@@ -291,35 +312,54 @@ main(int argc, char *argv[])
                ControlFile.checkPointCopy.nextXidEpoch = set_xid_epoch;
 
        if (set_xid != 0)
+       {
                ControlFile.checkPointCopy.nextXid = set_xid;
 
+               /*
+                * For the moment, just set oldestXid to a value that will force
+                * immediate autovacuum-for-wraparound.  It's not clear whether adding
+                * user control of this is useful, so let's just do something that's
+                * reasonably safe.  The magic constant here corresponds to the
+                * maximum allowed value of autovacuum_freeze_max_age.
+                */
+               ControlFile.checkPointCopy.oldestXid = set_xid - 2000000000;
+               if (ControlFile.checkPointCopy.oldestXid < FirstNormalTransactionId)
+                       ControlFile.checkPointCopy.oldestXid += FirstNormalTransactionId;
+               ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
+       }
+
        if (set_oid != 0)
                ControlFile.checkPointCopy.nextOid = set_oid;
 
        if (set_mxid != 0)
+       {
                ControlFile.checkPointCopy.nextMulti = set_mxid;
 
+               ControlFile.checkPointCopy.oldestMulti = set_oldestmxid;
+               if (ControlFile.checkPointCopy.oldestMulti < FirstMultiXactId)
+                       ControlFile.checkPointCopy.oldestMulti += FirstMultiXactId;
+               ControlFile.checkPointCopy.oldestMultiDB = InvalidOid;
+       }
+
        if (set_mxoff != -1)
                ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
 
        if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
-               ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
-
-       if (minXlogId > ControlFile.logId ||
-               (minXlogId == ControlFile.logId &&
-                minXlogSeg > ControlFile.logSeg))
        {
-               ControlFile.logId = minXlogId;
-               ControlFile.logSeg = minXlogSeg;
+               ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
+               ControlFile.checkPointCopy.PrevTimeLineID = minXlogTli;
        }
 
+       if (minXlogSegNo > newXlogSegNo)
+               newXlogSegNo = minXlogSegNo;
+
        /*
         * If we had to guess anything, and -f was not given, just print the
         * guessed values and exit.  Also print if -n is given.
         */
        if ((guessed && !force) || noupdate)
        {
-               PrintControlValues(guessed);
+               PrintNewControlValues();
                if (!noupdate)
                {
                        printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
@@ -335,7 +375,7 @@ main(int argc, char *argv[])
        if (ControlFile.state != DB_SHUTDOWNED && !force)
        {
                printf(_("The database server was not shut down cleanly.\n"
-                                "Resetting the transaction log may cause data to be lost.\n"
+                          "Resetting the transaction log might cause data to be lost.\n"
                                 "If you want to proceed anyway, use -f to force reset.\n"));
                exit(1);
        }
@@ -345,6 +385,7 @@ main(int argc, char *argv[])
         */
        RewriteControlFile();
        KillExistingXLOG();
+       KillExistingArchiveStatus();
        WriteEmptyXLOG();
 
        printf(_("Transaction log reset\n"));
@@ -366,7 +407,7 @@ ReadControlFile(void)
        char       *buffer;
        pg_crc32        crc;
 
-       if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY, 0)) < 0)
+       if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY | PG_BINARY, 0)) < 0)
        {
                /*
                 * If pg_control is not there at all, or we can't read it, the odds
@@ -384,7 +425,7 @@ ReadControlFile(void)
        }
 
        /* Use malloc to ensure we have a maxaligned buffer */
-       buffer = (char *) malloc(PG_CONTROL_SIZE);
+       buffer = (char *) pg_malloc(PG_CONTROL_SIZE);
 
        len = read(fd, buffer, PG_CONTROL_SIZE);
        if (len < 0)
@@ -435,7 +476,6 @@ GuessControlValues(void)
 {
        uint64          sysidentifier;
        struct timeval tv;
-       char       *localeptr;
 
        /*
         * Set up a completely default set of pg_control values.
@@ -456,22 +496,35 @@ GuessControlValues(void)
 
        ControlFile.system_identifier = sysidentifier;
 
-       ControlFile.checkPointCopy.redo.xlogid = 0;
-       ControlFile.checkPointCopy.redo.xrecoff = SizeOfXLogLongPHD;
-       ControlFile.checkPointCopy.undo = ControlFile.checkPointCopy.redo;
+       ControlFile.checkPointCopy.redo = SizeOfXLogLongPHD;
        ControlFile.checkPointCopy.ThisTimeLineID = 1;
+       ControlFile.checkPointCopy.PrevTimeLineID = 1;
+       ControlFile.checkPointCopy.fullPageWrites = false;
        ControlFile.checkPointCopy.nextXidEpoch = 0;
-       ControlFile.checkPointCopy.nextXid = (TransactionId) 514;       /* XXX */
+       ControlFile.checkPointCopy.nextXid = FirstNormalTransactionId;
        ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
        ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
        ControlFile.checkPointCopy.nextMultiOffset = 0;
-       ControlFile.checkPointCopy.time = time(NULL);
+       ControlFile.checkPointCopy.oldestXid = FirstNormalTransactionId;
+       ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
+       ControlFile.checkPointCopy.oldestMulti = FirstMultiXactId;
+       ControlFile.checkPointCopy.oldestMultiDB = InvalidOid;
+       ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
+       ControlFile.checkPointCopy.oldestActiveXid = InvalidTransactionId;
 
        ControlFile.state = DB_SHUTDOWNED;
-       ControlFile.time = time(NULL);
-       ControlFile.logId = 0;
-       ControlFile.logSeg = 1;
+       ControlFile.time = (pg_time_t) time(NULL);
        ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
+       ControlFile.unloggedLSN = 1;
+
+       /* minRecoveryPoint, backupStartPoint and backupEndPoint can be left zero */
+
+       ControlFile.wal_level = WAL_LEVEL_MINIMAL;
+       ControlFile.wal_log_hints = false;
+       ControlFile.MaxConnections = 100;
+       ControlFile.max_worker_processes = 8;
+       ControlFile.max_prepared_xacts = 0;
+       ControlFile.max_locks_per_xact = 64;
 
        ControlFile.maxAlign = MAXIMUM_ALIGNOF;
        ControlFile.floatFormat = FLOATFORMAT_VALUE;
@@ -481,27 +534,14 @@ GuessControlValues(void)
        ControlFile.xlog_seg_size = XLOG_SEG_SIZE;
        ControlFile.nameDataLen = NAMEDATALEN;
        ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
+       ControlFile.toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE;
 #ifdef HAVE_INT64_TIMESTAMP
-       ControlFile.enableIntTimes = TRUE;
+       ControlFile.enableIntTimes = true;
 #else
-       ControlFile.enableIntTimes = FALSE;
+       ControlFile.enableIntTimes = false;
 #endif
-       ControlFile.localeBuflen = LOCALE_NAME_BUFLEN;
-
-       localeptr = setlocale(LC_COLLATE, "");
-       if (!localeptr)
-       {
-               fprintf(stderr, _("%s: invalid LC_COLLATE setting\n"), progname);
-               exit(1);
-       }
-       StrNCpy(ControlFile.lc_collate, localeptr, LOCALE_NAME_BUFLEN);
-       localeptr = setlocale(LC_CTYPE, "");
-       if (!localeptr)
-       {
-               fprintf(stderr, _("%s: invalid LC_CTYPE setting\n"), progname);
-               exit(1);
-       }
-       StrNCpy(ControlFile.lc_ctype, localeptr, LOCALE_NAME_BUFLEN);
+       ControlFile.float4ByVal = FLOAT4PASSBYVAL;
+       ControlFile.float8ByVal = FLOAT8PASSBYVAL;
 
        /*
         * XXX eventually, should try to grovel through old XLOG to develop more
@@ -524,7 +564,7 @@ PrintControlValues(bool guessed)
        if (guessed)
                printf(_("Guessed pg_control values:\n\n"));
        else
-               printf(_("pg_control values:\n\n"));
+               printf(_("Current pg_control values:\n\n"));
 
        /*
         * Format system_identifier separately to keep platform-dependent format
@@ -539,12 +579,10 @@ PrintControlValues(bool guessed)
                   ControlFile.catalog_version_no);
        printf(_("Database system identifier:           %s\n"),
                   sysident_str);
-       printf(_("Current log file ID:                  %u\n"),
-                  ControlFile.logId);
-       printf(_("Next log file segment:                %u\n"),
-                  ControlFile.logSeg);
        printf(_("Latest checkpoint's TimeLineID:       %u\n"),
                   ControlFile.checkPointCopy.ThisTimeLineID);
+       printf(_("Latest checkpoint's full_page_writes: %s\n"),
+                  ControlFile.checkPointCopy.fullPageWrites ? _("on") : _("off"));
        printf(_("Latest checkpoint's NextXID:          %u/%u\n"),
                   ControlFile.checkPointCopy.nextXidEpoch,
                   ControlFile.checkPointCopy.nextXid);
@@ -554,6 +592,16 @@ PrintControlValues(bool guessed)
                   ControlFile.checkPointCopy.nextMulti);
        printf(_("Latest checkpoint's NextMultiOffset:  %u\n"),
                   ControlFile.checkPointCopy.nextMultiOffset);
+       printf(_("Latest checkpoint's oldestXID:        %u\n"),
+                  ControlFile.checkPointCopy.oldestXid);
+       printf(_("Latest checkpoint's oldestXID's DB:   %u\n"),
+                  ControlFile.checkPointCopy.oldestXidDB);
+       printf(_("Latest checkpoint's oldestActiveXID:  %u\n"),
+                  ControlFile.checkPointCopy.oldestActiveXid);
+       printf(_("Latest checkpoint's oldestMultiXid:   %u\n"),
+                  ControlFile.checkPointCopy.oldestMulti);
+       printf(_("Latest checkpoint's oldestMulti's DB: %u\n"),
+                  ControlFile.checkPointCopy.oldestMultiDB);
        printf(_("Maximum data alignment:               %u\n"),
                   ControlFile.maxAlign);
        /* we don't print floatFormat since can't say much useful about it */
@@ -569,14 +617,70 @@ PrintControlValues(bool guessed)
                   ControlFile.nameDataLen);
        printf(_("Maximum columns in an index:          %u\n"),
                   ControlFile.indexMaxKeys);
+       printf(_("Maximum size of a TOAST chunk:        %u\n"),
+                  ControlFile.toast_max_chunk_size);
        printf(_("Date/time type storage:               %s\n"),
                   (ControlFile.enableIntTimes ? _("64-bit integers") : _("floating-point numbers")));
-       printf(_("Maximum length of locale name:        %u\n"),
-                  ControlFile.localeBuflen);
-       printf(_("LC_COLLATE:                           %s\n"),
-                  ControlFile.lc_collate);
-       printf(_("LC_CTYPE:                             %s\n"),
-                  ControlFile.lc_ctype);
+       printf(_("Float4 argument passing:              %s\n"),
+                  (ControlFile.float4ByVal ? _("by value") : _("by reference")));
+       printf(_("Float8 argument passing:              %s\n"),
+                  (ControlFile.float8ByVal ? _("by value") : _("by reference")));
+       printf(_("Data page checksum version:           %u\n"),
+                  ControlFile.data_checksum_version);
+}
+
+
+/*
+ * Print the values to be changed.
+ */
+static void
+PrintNewControlValues()
+{
+       char            fname[MAXFNAMELEN];
+
+       /* This will be always printed in order to keep format same. */
+       printf(_("\n\nValues to be changed:\n\n"));
+
+       XLogFileName(fname, ControlFile.checkPointCopy.ThisTimeLineID, newXlogSegNo);
+       printf(_("First log segment after reset:        %s\n"), fname);
+
+       if (set_mxid != 0)
+       {
+               printf(_("NextMultiXactId:                      %u\n"),
+                          ControlFile.checkPointCopy.nextMulti);
+               printf(_("OldestMultiXid:                       %u\n"),
+                          ControlFile.checkPointCopy.oldestMulti);
+               printf(_("OldestMulti's DB:                     %u\n"),
+                          ControlFile.checkPointCopy.oldestMultiDB);
+       }
+
+       if (set_mxoff != -1)
+       {
+               printf(_("NextMultiOffset:                      %u\n"),
+                          ControlFile.checkPointCopy.nextMultiOffset);
+       }
+
+       if (set_oid != 0)
+       {
+               printf(_("NextOID:                              %u\n"),
+                          ControlFile.checkPointCopy.nextOid);
+       }
+
+       if (set_xid != 0)
+       {
+               printf(_("NextXID:                              %u\n"),
+                          ControlFile.checkPointCopy.nextXid);
+               printf(_("OldestXID:                            %u\n"),
+                          ControlFile.checkPointCopy.oldestXid);
+               printf(_("OldestXID's DB:                       %u\n"),
+                          ControlFile.checkPointCopy.oldestXidDB);
+       }
+
+       if (set_xid_epoch != -1)
+       {
+               printf(_("NextXID Epoch:                        %u\n"),
+                          ControlFile.checkPointCopy.nextXidEpoch);
+       }
 }
 
 
@@ -590,38 +694,38 @@ RewriteControlFile(void)
        char            buffer[PG_CONTROL_SIZE];                /* need not be aligned */
 
        /*
-        * Adjust fields as needed to force an empty XLOG starting at the next
-        * available segment.
+        * Adjust fields as needed to force an empty XLOG starting at
+        * newXlogSegNo.
         */
-       newXlogId = ControlFile.logId;
-       newXlogSeg = ControlFile.logSeg;
+       XLogSegNoOffsetToRecPtr(newXlogSegNo, SizeOfXLogLongPHD,
+                                                       ControlFile.checkPointCopy.redo);
+       ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
 
-       /* adjust in case we are changing segment size */
-       newXlogSeg *= ControlFile.xlog_seg_size;
-       newXlogSeg = (newXlogSeg + XLogSegSize - 1) / XLogSegSize;
+       ControlFile.state = DB_SHUTDOWNED;
+       ControlFile.time = (pg_time_t) time(NULL);
+       ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
+       ControlFile.prevCheckPoint = 0;
+       ControlFile.minRecoveryPoint = 0;
+       ControlFile.minRecoveryPointTLI = 0;
+       ControlFile.backupStartPoint = 0;
+       ControlFile.backupEndPoint = 0;
+       ControlFile.backupEndRequired = false;
 
-       /* be sure we wrap around correctly at end of a logfile */
-       NextLogSeg(newXlogId, newXlogSeg);
+       /*
+        * Force the defaults for max_* settings. The values don't really matter
+        * as long as wal_level='minimal'; the postmaster will reset these fields
+        * anyway at startup.
+        */
+       ControlFile.wal_level = WAL_LEVEL_MINIMAL;
+       ControlFile.wal_log_hints = false;
+       ControlFile.MaxConnections = 100;
+       ControlFile.max_worker_processes = 8;
+       ControlFile.max_prepared_xacts = 0;
+       ControlFile.max_locks_per_xact = 64;
 
        /* Now we can force the recorded xlog seg size to the right thing. */
        ControlFile.xlog_seg_size = XLogSegSize;
 
-       ControlFile.checkPointCopy.redo.xlogid = newXlogId;
-       ControlFile.checkPointCopy.redo.xrecoff =
-               newXlogSeg * XLogSegSize + SizeOfXLogLongPHD;
-       ControlFile.checkPointCopy.undo = ControlFile.checkPointCopy.redo;
-       ControlFile.checkPointCopy.time = time(NULL);
-
-       ControlFile.state = DB_SHUTDOWNED;
-       ControlFile.time = time(NULL);
-       ControlFile.logId = newXlogId;
-       ControlFile.logSeg = newXlogSeg + 1;
-       ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
-       ControlFile.prevCheckPoint.xlogid = 0;
-       ControlFile.prevCheckPoint.xrecoff = 0;
-       ControlFile.minRecoveryPoint.xlogid = 0;
-       ControlFile.minRecoveryPoint.xrecoff = 0;
-
        /* Contents are protected with a CRC */
        INIT_CRC32(ControlFile.crc);
        COMP_CRC32(ControlFile.crc,
@@ -680,6 +784,97 @@ RewriteControlFile(void)
 }
 
 
+/*
+ * Scan existing XLOG files and determine the highest existing WAL address
+ *
+ * On entry, ControlFile.checkPointCopy.redo and ControlFile.xlog_seg_size
+ * are assumed valid (note that we allow the old xlog seg size to differ
+ * from what we're using).  On exit, newXlogId and newXlogSeg are set to
+ * suitable values for the beginning of replacement WAL (in our seg size).
+ */
+static void
+FindEndOfXLOG(void)
+{
+       DIR                *xldir;
+       struct dirent *xlde;
+       uint64          segs_per_xlogid;
+       uint64          xlogbytepos;
+
+       /*
+        * Initialize the max() computation using the last checkpoint address from
+        * old pg_control.      Note that for the moment we are working with segment
+        * numbering according to the old xlog seg size.
+        */
+       segs_per_xlogid = (UINT64CONST(0x0000000100000000) / ControlFile.xlog_seg_size);
+       newXlogSegNo = ControlFile.checkPointCopy.redo / ControlFile.xlog_seg_size;
+
+       /*
+        * Scan the pg_xlog directory to find existing WAL segment files. We
+        * assume any present have been used; in most scenarios this should be
+        * conservative, because of xlog.c's attempts to pre-create files.
+        */
+       xldir = opendir(XLOGDIR);
+       if (xldir == NULL)
+       {
+               fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
+                               progname, XLOGDIR, strerror(errno));
+               exit(1);
+       }
+
+       while (errno = 0, (xlde = readdir(xldir)) != NULL)
+       {
+               if (strlen(xlde->d_name) == 24 &&
+                       strspn(xlde->d_name, "0123456789ABCDEF") == 24)
+               {
+                       unsigned int tli,
+                                               log,
+                                               seg;
+                       XLogSegNo       segno;
+
+                       sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
+                       segno = ((uint64) log) * segs_per_xlogid + seg;
+
+                       /*
+                        * Note: we take the max of all files found, regardless of their
+                        * timelines.  Another possibility would be to ignore files of
+                        * timelines other than the target TLI, but this seems safer.
+                        * Better too large a result than too small...
+                        */
+                       if (segno > newXlogSegNo)
+                               newXlogSegNo = segno;
+               }
+       }
+
+#ifdef WIN32
+       /* Bug in old Mingw dirent.c;  fixed in mingw-runtime-3.2, 2003-10-10 */
+       if (GetLastError() == ERROR_NO_MORE_FILES)
+               errno = 0;
+#endif
+
+       if (errno)
+       {
+               fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
+                               progname, XLOGDIR, strerror(errno));
+               exit(1);
+       }
+
+       if (closedir(xldir))
+       {
+               fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
+                               progname, XLOGDIR, strerror(errno));
+               exit(1);
+       }
+
+       /*
+        * Finally, convert to new xlog seg size, and advance by one to ensure we
+        * are in virgin territory.
+        */
+       xlogbytepos = newXlogSegNo * ControlFile.xlog_seg_size;
+       newXlogSegNo = (xlogbytepos + XLogSegSize - 1) / XLogSegSize;
+       newXlogSegNo++;
+}
+
+
 /*
  * Remove existing XLOG files
  */
@@ -698,8 +893,7 @@ KillExistingXLOG(void)
                exit(1);
        }
 
-       errno = 0;
-       while ((xlde = readdir(xldir)) != NULL)
+       while (errno = 0, (xlde = readdir(xldir)) != NULL)
        {
                if (strlen(xlde->d_name) == 24 &&
                        strspn(xlde->d_name, "0123456789ABCDEF") == 24)
@@ -712,25 +906,85 @@ KillExistingXLOG(void)
                                exit(1);
                        }
                }
-               errno = 0;
        }
-#ifdef WIN32
 
-       /*
-        * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
-        * released version
-        */
+#ifdef WIN32
+       /* Bug in old Mingw dirent.c;  fixed in mingw-runtime-3.2, 2003-10-10 */
        if (GetLastError() == ERROR_NO_MORE_FILES)
                errno = 0;
 #endif
 
        if (errno)
        {
-               fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
+               fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
                                progname, XLOGDIR, strerror(errno));
                exit(1);
        }
-       closedir(xldir);
+
+       if (closedir(xldir))
+       {
+               fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
+                               progname, XLOGDIR, strerror(errno));
+               exit(1);
+       }
+}
+
+
+/*
+ * Remove existing archive status files
+ */
+static void
+KillExistingArchiveStatus(void)
+{
+       DIR                *xldir;
+       struct dirent *xlde;
+       char            path[MAXPGPATH];
+
+#define ARCHSTATDIR XLOGDIR "/archive_status"
+
+       xldir = opendir(ARCHSTATDIR);
+       if (xldir == NULL)
+       {
+               fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
+                               progname, ARCHSTATDIR, strerror(errno));
+               exit(1);
+       }
+
+       while (errno = 0, (xlde = readdir(xldir)) != NULL)
+       {
+               if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
+                       (strcmp(xlde->d_name + 24, ".ready") == 0 ||
+                        strcmp(xlde->d_name + 24, ".done") == 0))
+               {
+                       snprintf(path, MAXPGPATH, "%s/%s", ARCHSTATDIR, xlde->d_name);
+                       if (unlink(path) < 0)
+                       {
+                               fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
+                                               progname, path, strerror(errno));
+                               exit(1);
+                       }
+               }
+       }
+
+#ifdef WIN32
+       /* Bug in old Mingw dirent.c;  fixed in mingw-runtime-3.2, 2003-10-10 */
+       if (GetLastError() == ERROR_NO_MORE_FILES)
+               errno = 0;
+#endif
+
+       if (errno)
+       {
+               fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
+                               progname, ARCHSTATDIR, strerror(errno));
+               exit(1);
+       }
+
+       if (closedir(xldir))
+       {
+               fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
+                               progname, ARCHSTATDIR, strerror(errno));
+               exit(1);
+       }
 }
 
 
@@ -751,7 +1005,7 @@ WriteEmptyXLOG(void)
        int                     nbytes;
 
        /* Use malloc() to ensure buffer is MAXALIGNED */
-       buffer = (char *) malloc(XLOG_BLCKSZ);
+       buffer = (char *) pg_malloc(XLOG_BLCKSZ);
        page = (XLogPageHeader) buffer;
        memset(buffer, 0, XLOG_BLCKSZ);
 
@@ -759,10 +1013,7 @@ WriteEmptyXLOG(void)
        page->xlp_magic = XLOG_PAGE_MAGIC;
        page->xlp_info = XLP_LONG_HEADER;
        page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
-       page->xlp_pageaddr.xlogid =
-               ControlFile.checkPointCopy.redo.xlogid;
-       page->xlp_pageaddr.xrecoff =
-               ControlFile.checkPointCopy.redo.xrecoff - SizeOfXLogLongPHD;
+       page->xlp_pageaddr = ControlFile.checkPointCopy.redo - SizeOfXLogLongPHD;
        longpage = (XLogLongPageHeader) page;
        longpage->xlp_sysid = ControlFile.system_identifier;
        longpage->xlp_seg_size = XLogSegSize;
@@ -770,8 +1021,7 @@ WriteEmptyXLOG(void)
 
        /* Insert the initial checkpoint record */
        record = (XLogRecord *) ((char *) page + SizeOfXLogLongPHD);
-       record->xl_prev.xlogid = 0;
-       record->xl_prev.xrecoff = 0;
+       record->xl_prev = 0;
        record->xl_xid = InvalidTransactionId;
        record->xl_tot_len = SizeOfXLogRecord + sizeof(CheckPoint);
        record->xl_len = sizeof(CheckPoint);
@@ -782,14 +1032,12 @@ WriteEmptyXLOG(void)
 
        INIT_CRC32(crc);
        COMP_CRC32(crc, &ControlFile.checkPointCopy, sizeof(CheckPoint));
-       COMP_CRC32(crc, (char *) record + sizeof(pg_crc32),
-                          SizeOfXLogRecord - sizeof(pg_crc32));
+       COMP_CRC32(crc, (char *) record, offsetof(XLogRecord, xl_crc));
        FIN_CRC32(crc);
        record->xl_crc = crc;
 
        /* Write the first page */
-       XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID,
-                                newXlogId, newXlogSeg);
+       XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID, newXlogSegNo);
 
        unlink(path);
 
@@ -844,15 +1092,15 @@ usage(void)
        printf(_("%s resets the PostgreSQL transaction log.\n\n"), progname);
        printf(_("Usage:\n  %s [OPTION]... DATADIR\n\n"), progname);
        printf(_("Options:\n"));
-       printf(_("  -f              force update to be done\n"));
-       printf(_("  -l TLI,FILE,SEG force minimum WAL starting location for new transaction log\n"));
-       printf(_("  -m XID          set next multitransaction ID\n"));
-       printf(_("  -n              no update, just show extracted control values (for testing)\n"));
-       printf(_("  -o OID          set next OID\n"));
-       printf(_("  -O OFFSET       set next multitransaction offset\n"));
-       printf(_("  -x XID          set next transaction ID\n"));
-       printf(_("  -e XIDEPOCH     set next transaction ID epoch\n"));
-       printf(_("  --help          show this help, then exit\n"));
-       printf(_("  --version       output version information, then exit\n"));
+       printf(_("  -e XIDEPOCH      set next transaction ID epoch\n"));
+       printf(_("  -f               force update to be done\n"));
+       printf(_("  -l XLOGFILE      force minimum WAL starting location for new transaction log\n"));
+       printf(_("  -m MXID,MXID     set next and oldest multitransaction ID\n"));
+       printf(_("  -n               no update, just show what would be done (for testing)\n"));
+       printf(_("  -o OID           set next OID\n"));
+       printf(_("  -O OFFSET        set next multitransaction offset\n"));
+       printf(_("  -V, --version    output version information, then exit\n"));
+       printf(_("  -x XID           set next transaction ID\n"));
+       printf(_("  -?, --help       show this help, then exit\n"));
        printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
 }