* A utility to "zero out" the xlog when it's corrupt beyond recovery.
* Can also rebuild pg_control if needed.
*
- * The theory of reset operation is fairly simple:
+ * The theory of operation is fairly simple:
* 1. Read the existing pg_control (which will include the last
* checkpoint record). If it is an old format then update to
* current format.
- * 2. If pg_control is corrupt, attempt to rebuild the values,
- * by scanning the old xlog; if it fail then try to guess it.
+ * 2. If pg_control is corrupt, attempt to intuit reasonable values,
+ * by scanning the old xlog if necessary.
* 3. Modify pg_control to reflect a "shutdown" state with a checkpoint
* record at the start of xlog.
* 4. Flush the existing xlog files and write a new segment with
* just a checkpoint record in it. The new segment is positioned
* just past the end of the old xlog, so that existing LSNs in
* data pages will appear to be "in the past".
+ * This is all pretty straightforward except for the intuition part of
+ * step 2 ...
*
- * The algorithm of restoring the pg_control value from old xlog file:
- * 1. Retrieve all of the active xlog files from xlog direcotry into a list
- * by increasing order, according their timeline, log id, segment id.
- * 2. Search the list to find the oldest xlog file of the lastest time line.
- * 3. Search the records from the oldest xlog file of latest time line
- * to the latest xlog file of latest time line, if the checkpoint record
- * has been found, update the latest checkpoint and previous checkpoint.
- * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
+ *
+ * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
+ * 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.h"
#include "access/xlog_internal.h"
#include "catalog/catversion.h"
#include "catalog/pg_control.h"
-
-#define GUESS 0
-#define WAL 1
-
-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 uint64 sysidentifier=-1;
-
-/*
- * We use a list to store the active xlog files we had found in the
- * xlog directory in increasing order according the time line, logid,
- * segment id.
- *
- */
-typedef struct XLogFileName {
- TimeLineID tli;
- uint32 logid;
- uint32 seg;
- char fname[256];
- struct XLogFileName *next;
-} XLogFileName;
-
-/* The list head */
-static XLogFileName *xlogfilelist=NULL;
-
-/* LastXLogfile is the latest file in the latest time line,
- CurXLogfile is the oldest file in the lastest time line
- */
-static XLogFileName *CurXLogFile, *LastXLogFile;
-
-/* The last checkpoint found in xlog file.*/
-static CheckPoint lastcheckpoint;
-
-/* The last and previous checkpoint pointers found in xlog file.*/
-static XLogRecPtr prevchkp, lastchkp;
-
-/* the database state.*/
-static DBState state=DB_SHUTDOWNED;
-
-/* the total checkpoint numbers which had been found in the xlog file.*/
-static int found_checkpoint=0;
-
+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 bool RestoreControlValues(int mode);
-static void PrintControlValues(void);
-static void UpdateCtlFile4Reset(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);
-static void GetXLogFiles(void);
-static bool ValidXLogFileName(char * fname);
-static bool ValidXLogFileHeader(XLogFileName *segfile);
-static bool ValidXLOGPageHeader(XLogPageHeader hdr, uint tli, uint id, uint seg);
-static bool CmpXLogFileOT(XLogFileName * f1, XLogFileName *f2);
-static bool IsNextSeg(XLogFileName *prev, XLogFileName *cur);
-static void InsertXLogFile( char * fname );
-static bool ReadXLogPage(void);
-static bool RecordIsValid(XLogRecord *record, XLogRecPtr recptr);
-static bool FetchRecord(void);
-static void UpdateCheckPoint(XLogRecord *record);
-static void SelectStartXLog(void);
-static int SearchLastCheckpoint(void);
-static int OpenXLogFile(XLogFileName *sf);
-static void CleanUpList(XLogFileName *list);
int
main(int argc, char *argv[])
{
int c;
bool force = false;
- bool restore = false;
bool noupdate = false;
- 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];
- bool ctlcorrupted = false;
- bool PidLocked = false;
-
- set_pglocale_pgservice(argv[0], "pg_resetxlog");
+
+ set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_resetxlog"));
progname = get_progname(argv[0]);
}
- while ((c = getopt(argc, argv, "fl:m:no:O:x:r")) != -1)
+ while ((c = getopt(argc, argv, "fl:m:no:O:x:e:")) != -1)
{
switch (c)
{
case 'f':
force = true;
break;
-
- case 'r':
- restore = true;
- break;
-
+
case 'n':
noupdate = true;
break;
+ case 'e':
+ set_xid_epoch = strtoul(optarg, &endptr, 0);
+ if (endptr == optarg || *endptr != '\0')
+ {
+ fprintf(stderr, _("%s: invalid argument for option -e\n"), progname);
+ fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
+ exit(1);
+ }
+ if (set_xid_epoch == -1)
+ {
+ fprintf(stderr, _("%s: transaction ID epoch (-e) must not be -1\n"), progname);
+ exit(1);
+ }
+ break;
+
case 'x':
set_xid = strtoul(optarg, &endptr, 0);
if (endptr == optarg || *endptr != '\0')
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);
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':
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:
* 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)
+ 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);
}
}
else
{
- PidLocked = true;
+ fprintf(stderr, _("%s: lock file \"%s\" exists\n"
+ "Is a server running? If not, delete the lock file and try again.\n"),
+ progname, "postmaster.pid");
+ exit(1);
}
/*
* Attempt to read the existing pg_control file
*/
if (!ReadControlFile())
- {
- /* The control file has been corruptted.*/
- ctlcorrupted = true;
- }
+ 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.)
*/
+ if (set_xid_epoch != -1)
+ 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;
}
- /* retore the broken control file from WAL file.*/
- if (restore)
- {
-
- /* If the control fine is fine, don't touch it.*/
- if ( !ctlcorrupted )
- {
- printf(_("\nThe control file seems fine, not need to restore it.\n"));
- printf(_("If you want to restore it anyway, use -f option, but this also will reset the log file.\n"));
- exit(0);
- }
-
-
- /* Try to restore control values from old xlog file, or complain it.*/
- if (RestoreControlValues(WAL))
- {
- /* Success in restoring the checkpoint information from old xlog file.*/
-
- /* Print it out.*/
- PrintControlValues();
-
- /* In case the postmaster is crashed.
- * But it may be dangerous for the living one.
- * It may need a more good way.
- */
- if (PidLocked)
- {
- ControlFile.state = DB_IN_PRODUCTION;
- }
- /* Write the new control file. */
- RewriteControlFile();
- printf(_("\nThe control file had been restored.\n"));
- }
- else
- {
- /* Fail in restoring the checkpoint information from old xlog file. */
- printf(_("\nCan not restore the control file from XLog file..\n"));
- printf(_("\nIf you want to restore it anyway, use -f option to guess the information, but this also will reset the log file.\n"));
- }
+ if (minXlogSegNo > newXlogSegNo)
+ newXlogSegNo = minXlogSegNo;
- exit(0);
-
- }
- if (PidLocked)
- {
- fprintf(stderr, _("%s: lock file \"%s\" exists\n"
- "Is a server running? If not, delete the lock file and try again.\n"),
- progname, path);
- exit(1);
-
- }
/*
- * Print out the values in control file if -n is given. if the control file is
- * corrupted, then inform user to restore it first.
+ * 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 (noupdate)
+ if ((guessed && !force) || noupdate)
{
- if (!ctlcorrupted)
+ PrintNewControlValues();
+ if (!noupdate)
{
- /* The control file is fine, print the values out.*/
- PrintControlValues();
- exit(0);
- }
- else{
- /* The control file is corrupted.*/
- printf(_("The control file had been corrupted.\n"));
- printf(_("Please use -r option to restore it first.\n"));
+ printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
exit(1);
- }
+ }
+ else
+ exit(0);
}
/*
* Don't reset from a dirty pg_control without -f, either.
*/
- if (ControlFile.state != DB_SHUTDOWNED && !force && !ctlcorrupted)
+ 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);
}
-/*
- * Try to reset the xlog file.
+ /*
+ * Else, do the dirty deed.
*/
-
- /* If the control file is corrupted, and -f option is given, resotre it first.*/
- if ( ctlcorrupted )
- {
- if (force)
- {
- if (!RestoreControlValues(WAL))
- {
- printf(_("fails to recover the control file from old xlog files, so we had to guess it.\n"));
- RestoreControlValues(GUESS);
- }
- printf(_("Restored the control file from old xlog files.\n"));
- }
- else
- {
- printf(_("Control file corrupted.\nIf you want to proceed anyway, use -f to force reset.\n"));
- exit(1);
- }
- }
-
- /* Reset the xlog fille.*/
- UpdateCtlFile4Reset();
RewriteControlFile();
KillExistingXLOG();
+ KillExistingArchiveStatus();
WriteEmptyXLOG();
- printf(_("Transaction log reset\n"));
+
+ printf(_("Transaction log reset\n"));
return 0;
}
char *buffer;
pg_crc32 crc;
- if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY)) < 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
}
/* 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)
progname);
/* We will use the data anyway, but treat it as guessed. */
memcpy(&ControlFile, buffer, sizeof(ControlFile));
+ guessed = true;
return true;
}
}
-
-
/*
- * Restore the pg_control values by scanning old xlog files or by guessing it.
- *
- * Input parameter:
- * WAL: Restore the pg_control values by scanning old xlog files.
- * GUESS: Restore the pg_control values by guessing.
- * Return:
- * TRUE: success in restoring.
- * FALSE: fail to restore the values.
- *
+ * Guess at pg_control values when we can't read the old ones.
*/
-static bool
-RestoreControlValues(int mode)
+static void
+GuessControlValues(void)
{
+ uint64 sysidentifier;
struct timeval tv;
- char *localeptr;
- bool successed=true;
/*
* Set up a completely default set of pg_control values.
*/
+ guessed = true;
memset(&ControlFile, 0, sizeof(ControlFile));
ControlFile.pg_control_version = PG_CONTROL_VERSION;
ControlFile.catalog_version_no = CATALOG_VERSION_NO;
- /*
- * update the checkpoint value in control file,by searching
- * xlog segment file, or just guessing it.
+ /*
+ * Create a new unique installation identifier, since we can no longer use
+ * any old XLOG records. See notes in xlog.c about the algorithm.
*/
- if (mode == WAL)
- {
- int result = SearchLastCheckpoint();
- if ( result > 0 ) /* The last checkpoint had been found. */
- {
- ControlFile.checkPointCopy = lastcheckpoint;
- ControlFile.checkPoint = lastchkp;
- ControlFile.prevCheckPoint = prevchkp;
- ControlFile.logId = LastXLogFile->logid;
- ControlFile.logSeg = LastXLogFile->seg + 1;
- ControlFile.checkPointCopy.ThisTimeLineID = LastXLogFile->tli;
- ControlFile.state = state;
- } else successed = false;
-
- /* Clean up the list. */
- CleanUpList(xlogfilelist);
-
- }
-
- if (mode == GUESS)
- {
- ControlFile.checkPointCopy.redo.xlogid = 0;
- ControlFile.checkPointCopy.redo.xrecoff = SizeOfXLogLongPHD;
- ControlFile.checkPointCopy.undo = ControlFile.checkPointCopy.redo;
- ControlFile.checkPointCopy.nextXid = (TransactionId) 514; /* XXX */
- ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
- ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
- ControlFile.checkPointCopy.nextMultiOffset = 0;
- ControlFile.checkPointCopy.time = time(NULL);
- ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
- /*
- * Create a new unique installation identifier, since we can no longer
- * use any old XLOG records. See notes in xlog.c about the algorithm.
- */
- gettimeofday(&tv, NULL);
- sysidentifier = ((uint64) tv.tv_sec) << 32;
- sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec);
- ControlFile.state = DB_SHUTDOWNED;
-
- }
+ gettimeofday(&tv, NULL);
+ sysidentifier = ((uint64) tv.tv_sec) << 32;
+ sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec);
- ControlFile.time = time(NULL);
ControlFile.system_identifier = sysidentifier;
+
+ ControlFile.checkPointCopy.redo = SizeOfXLogLongPHD;
+ ControlFile.checkPointCopy.ThisTimeLineID = 1;
+ ControlFile.checkPointCopy.PrevTimeLineID = 1;
+ ControlFile.checkPointCopy.fullPageWrites = false;
+ ControlFile.checkPointCopy.nextXidEpoch = 0;
+ ControlFile.checkPointCopy.nextXid = FirstNormalTransactionId;
+ ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
+ ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
+ ControlFile.checkPointCopy.nextMultiOffset = 0;
+ 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 = (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;
ControlFile.blcksz = BLCKSZ;
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;
+ ControlFile.float4ByVal = FLOAT4PASSBYVAL;
+ ControlFile.float8ByVal = FLOAT8PASSBYVAL;
- 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);
-
- return successed;
+ /*
+ * XXX eventually, should try to grovel through old XLOG to develop more
+ * accurate values for TimeLineID, nextXID, etc.
+ */
}
/*
- * Print the out pg_control values.
+ * Print the guessed pg_control values when we had to guess.
*
* NB: this display should be just those fields that will not be
* reset by RewriteControlFile().
*/
static void
-PrintControlValues(void)
+PrintControlValues(bool guessed)
{
char sysident_str[32];
- printf(_("pg_control values:\n\n"));
+ if (guessed)
+ printf(_("Guessed pg_control values:\n\n"));
+ else
+ printf(_("Current pg_control values:\n\n"));
/*
* Format system_identifier separately to keep platform-dependent format
snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
ControlFile.system_identifier);
- printf(_("pg_control version number: %u\n"), ControlFile.pg_control_version);
- printf(_("Catalog version number: %u\n"), 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 NextXID: %u\n"), ControlFile.checkPointCopy.nextXid);
- printf(_("Latest checkpoint's NextOID: %u\n"), ControlFile.checkPointCopy.nextOid);
- printf(_("Latest checkpoint's NextMultiXactId: %u\n"), ControlFile.checkPointCopy.nextMulti);
- printf(_("Latest checkpoint's NextMultiOffset: %u\n"), ControlFile.checkPointCopy.nextMultiOffset);
- printf(_("Maximum data alignment: %u\n"), ControlFile.maxAlign);
+ printf(_("pg_control version number: %u\n"),
+ ControlFile.pg_control_version);
+ printf(_("Catalog version number: %u\n"),
+ ControlFile.catalog_version_no);
+ printf(_("Database system identifier: %s\n"),
+ sysident_str);
+ 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);
+ printf(_("Latest checkpoint's NextOID: %u\n"),
+ ControlFile.checkPointCopy.nextOid);
+ printf(_("Latest checkpoint's NextMultiXactId: %u\n"),
+ 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 */
- printf(_("Database block size: %u\n"), ControlFile.blcksz);
- printf(_("Blocks per segment of large relation: %u\n"), ControlFile.relseg_size);
- printf(_("WAL block size: %u\n"), ControlFile.xlog_blcksz);
- printf(_("Bytes per WAL segment: %u\n"), ControlFile.xlog_seg_size);
- printf(_("Maximum length of identifiers: %u\n"), ControlFile.nameDataLen);
- printf(_("Maximum columns in an index: %u\n"), ControlFile.indexMaxKeys);
+ printf(_("Database block size: %u\n"),
+ ControlFile.blcksz);
+ printf(_("Blocks per segment of large relation: %u\n"),
+ ControlFile.relseg_size);
+ printf(_("WAL block size: %u\n"),
+ ControlFile.xlog_blcksz);
+ printf(_("Bytes per WAL segment: %u\n"),
+ ControlFile.xlog_seg_size);
+ printf(_("Maximum length of identifiers: %u\n"),
+ 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);
}
+
/*
-* Update the control file before reseting it.
-*/
-static void
-UpdateCtlFile4Reset(void)
+ * Print the values to be changed.
+ */
+static void
+PrintNewControlValues()
{
- /*
- * Adjust fields as needed to force an empty XLOG starting at the next
- * available segment.
- */
- newXlogId = ControlFile.logId;
- newXlogSeg = ControlFile.logSeg;
+ char fname[MAXFNAMELEN];
- /* adjust in case we are changing segment size */
- newXlogSeg *= ControlFile.xlog_seg_size;
- newXlogSeg = (newXlogSeg + XLogSegSize - 1) / XLogSegSize;
+ /* This will be always printed in order to keep format same. */
+ printf(_("\n\nValues to be changed:\n\n"));
- /* be sure we wrap around correctly at end of a logfile */
- NextLogSeg(newXlogId, newXlogSeg);
+ XLogFileName(fname, ControlFile.checkPointCopy.ThisTimeLineID, newXlogSegNo);
+ printf(_("First log segment after reset: %s\n"), fname);
- /* Now we can force the recorded xlog seg size to the right thing. */
- ControlFile.xlog_seg_size = XLogSegSize;
+ 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);
+ }
- ControlFile.checkPointCopy.redo.xlogid = newXlogId;
- ControlFile.checkPointCopy.redo.xrecoff =
- newXlogSeg * XLogSegSize + SizeOfXLogLongPHD;
- ControlFile.checkPointCopy.undo = ControlFile.checkPointCopy.redo;
- ControlFile.checkPointCopy.time = time(NULL);
+ if (set_mxoff != -1)
+ {
+ printf(_("NextMultiOffset: %u\n"),
+ ControlFile.checkPointCopy.nextMultiOffset);
+ }
- 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;
+ 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);
+ }
}
+
/*
* Write out the new pg_control file.
*/
RewriteControlFile(void)
{
int fd;
- char buffer[PG_CONTROL_SIZE]; /* need not be aligned */
+ char buffer[PG_CONTROL_SIZE]; /* need not be aligned */
+
+ /*
+ * Adjust fields as needed to force an empty XLOG starting at
+ * newXlogSegNo.
+ */
+ XLogSegNoOffsetToRecPtr(newXlogSegNo, SizeOfXLogLongPHD,
+ ControlFile.checkPointCopy.redo);
+ ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
+
+ 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;
+
+ /*
+ * 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;
/* Contents are protected with a CRC */
INIT_CRC32(ControlFile.crc);
}
+/*
+ * 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
*/
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)
exit(1);
}
}
- errno = 0;
}
+
#ifdef WIN32
- /*
- * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
- * released version
- */
+ /* 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);
+ }
+
+ if (closedir(xldir))
+ {
+ fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
progname, XLOGDIR, strerror(errno));
exit(1);
}
- closedir(xldir);
}
/*
- * Write an empty XLOG file, containing only the checkpoint record
- * already set up in ControlFile.
+ * Remove existing archive status files
*/
static void
-WriteEmptyXLOG(void)
+KillExistingArchiveStatus(void)
{
- char *buffer;
- XLogPageHeader page;
- XLogLongPageHeader longpage;
- XLogRecord *record;
- pg_crc32 crc;
+ DIR *xldir;
+ struct dirent *xlde;
char path[MAXPGPATH];
- int fd;
- int nbytes;
- /* Use malloc() to ensure buffer is MAXALIGNED */
- buffer = (char *) malloc(XLOG_BLCKSZ);
- page = (XLogPageHeader) buffer;
- memset(buffer, 0, XLOG_BLCKSZ);
+#define ARCHSTATDIR XLOGDIR "/archive_status"
- /* Set up the XLOG page header */
- 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;
- longpage = (XLogLongPageHeader) page;
- longpage->xlp_sysid = ControlFile.system_identifier;
+ 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);
+ }
+}
+
+
+/*
+ * Write an empty XLOG file, containing only the checkpoint record
+ * already set up in ControlFile.
+ */
+static void
+WriteEmptyXLOG(void)
+{
+ char *buffer;
+ XLogPageHeader page;
+ XLogLongPageHeader longpage;
+ XLogRecord *record;
+ pg_crc32 crc;
+ char path[MAXPGPATH];
+ int fd;
+ int nbytes;
+
+ /* Use malloc() to ensure buffer is MAXALIGNED */
+ buffer = (char *) pg_malloc(XLOG_BLCKSZ);
+ page = (XLogPageHeader) buffer;
+ memset(buffer, 0, XLOG_BLCKSZ);
+
+ /* Set up the XLOG page header */
+ page->xlp_magic = XLOG_PAGE_MAGIC;
+ page->xlp_info = XLP_LONG_HEADER;
+ page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
+ page->xlp_pageaddr = ControlFile.checkPointCopy.redo - SizeOfXLogLongPHD;
+ longpage = (XLogLongPageHeader) page;
+ longpage->xlp_sysid = ControlFile.system_identifier;
longpage->xlp_seg_size = XLogSegSize;
longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
/* 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);
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);
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 reset xlog to be done, if the control file is corrupted, then try to restore it.\n"));
- printf(_(" -r restore the pg_control file from old XLog files, resets is not done..\n"));
- printf(_(" -l TLI,FILE,SEG force minimum WAL starting location for new transaction log\n"));
- printf(_(" -n show extracted control values of existing pg_control file.\n"));
- printf(_(" -m multiXID set next multi transaction ID\n"));
- printf(_(" -o OID set next OID\n"));
- printf(_(" -O multiOffset set next multi transaction offset\n"));
- printf(_(" -x XID set next transaction ID\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"));
}
-
-
-
-/*
- * The following routines are mainly used for getting pg_control values
- * from the xlog file.
- */
-
- /* some local varaibles.*/
-static int logFd=0; /* kernel FD for current input file */
-static int logRecOff; /* offset of next record in page */
-static char pageBuffer[BLCKSZ]; /* current page */
-static XLogRecPtr curRecPtr; /* logical address of current record */
-static XLogRecPtr prevRecPtr; /* logical address of previous record */
-static char *readRecordBuf = NULL; /* ReadRecord result area */
-static uint32 readRecordBufSize = 0;
-static int32 logPageOff; /* offset of current page in file */
-static uint32 logId; /* current log file id */
-static uint32 logSeg; /* current log file segment */
-static uint32 logTli; /* current log file timeline */
-
-/*
- * Get existing XLOG files
- */
-static void
-GetXLogFiles(void)
-{
- DIR *xldir;
- struct dirent *xlde;
-
- /* Open the xlog direcotry.*/
- xldir = opendir(XLOGDIR);
- if (xldir == NULL)
- {
- fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
- progname, XLOGDIR, strerror(errno));
- exit(1);
- }
-
- /* Search the directory, insert the segment files into the xlogfilelist.*/
- errno = 0;
- while ((xlde = readdir(xldir)) != NULL)
- {
- if (ValidXLogFileName(xlde->d_name)) {
- /* XLog file is found, insert it into the xlogfilelist.*/
- InsertXLogFile(xlde->d_name);
- };
- errno = 0;
- }
-#ifdef WIN32
- if (GetLastError() == ERROR_NO_MORE_FILES)
- errno = 0;
-#endif
-
- if (errno)
- {
- fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
- progname, XLOGDIR, strerror(errno));
- exit(1);
- }
- closedir(xldir);
-}
-
-/*
- * Insert a file while had been found in the xlog folder into xlogfilelist.
- * The xlogfile list is matained in a increasing order.
- *
- * The input parameter is the name of the xlog file, the name is assumpted
- * valid.
- */
-static void
-InsertXLogFile( char * fname )
-{
- XLogFileName * NewSegFile, *Curr, *Prev;
- bool append2end = false;
-
- /* Allocate a new node for the new file. */
- NewSegFile = (XLogFileName *) malloc(sizeof(XLogFileName));
- strcpy(NewSegFile->fname,fname); /* setup the name */
- /* extract the time line, logid, and segment number from the name.*/
- sscanf(fname, "%8x%8x%8x", &(NewSegFile->tli), &(NewSegFile->logid), &(NewSegFile->seg));
- NewSegFile->next = NULL;
-
- /* Ensure the xlog file is active and valid.*/
- if (! ValidXLogFileHeader(NewSegFile))
- {
- free(NewSegFile);
- return;
- }
-
- /* the list is empty.*/
- if ( xlogfilelist == NULL ) {
- xlogfilelist = NewSegFile;
- return;
- };
-
- /* try to search the list and find the insert point. */
- Prev=Curr=xlogfilelist;
- while( CmpXLogFileOT(NewSegFile, Curr))
- {
- /* the node is appended to the end of the list.*/
- if (Curr->next == NULL)
- {
- append2end = true;
- break;
- }
- Prev=Curr;
- Curr = Curr->next;
- }
-
- /* Insert the new node to the list.*/
- if ( append2end )
- {
- /* We need to append the new node to the end of the list */
- Curr->next = NewSegFile;
- }
- else
- {
- NewSegFile->next = Curr;
- /* prev should not be the list head. */
- if ( Prev != NULL && Prev != xlogfilelist)
- {
- Prev->next = NewSegFile;
- }
- }
- /* Update the list head if it is needed.*/
- if ((Curr == xlogfilelist) && !append2end)
- {
- xlogfilelist = NewSegFile;
- }
-
-}
-
-/*
- * compare two xlog file from their name to see which one is latest.
- *
- * Return true for file 2 is the lastest file.
- *
- */
-static bool
-CmpXLogFileOT(XLogFileName * f1, XLogFileName *f2)
-{
- if (f2->tli >= f1->tli)
- {
- if (f2->logid >= f1->logid)
- {
- if (f2->seg > f1->seg) return false;
- }
- }
- return true;
-
-}
-
-/* check is two segment file is continous.*/
-static bool
-IsNextSeg(XLogFileName *prev, XLogFileName *cur)
-{
- uint32 logid, logseg;
-
- if (prev->tli != cur->tli) return false;
-
- logid = prev->logid;
- logseg = prev->seg;
- NextLogSeg(logid, logseg);
-
- if ((logid == cur->logid) && (logseg == cur->seg)) return true;
-
- return false;
-
-}
-
-
-/*
-* Select the oldest xlog file in the latest time line.
-*/
-static void
-SelectStartXLog( void )
-{
- XLogFileName *tmp;
- CurXLogFile = xlogfilelist;
-
- if (xlogfilelist == NULL)
- {
- return;
- }
-
- tmp=LastXLogFile=CurXLogFile=xlogfilelist;
-
- while(tmp->next != NULL)
- {
-
- /*
- * we should ensure that from the first to
- * the last segment file is continous.
- * */
- if (!IsNextSeg(tmp, tmp->next))
- {
- CurXLogFile = tmp->next;
- }
- tmp=tmp->next;
- }
-
- LastXLogFile = tmp;
-
-}
-
-/*
- * Check if the file is a valid xlog file.
- *
- * Return true for the input file is a valid xlog file.
- *
- * The input parameter is the name of the xlog file.
- *
- */
-static bool
-ValidXLogFileName(char * fname)
-{
- uint logTLI, logId, logSeg;
- if (strlen(fname) != 24 ||
- strspn(fname, "0123456789ABCDEF") != 24 ||
- sscanf(fname, "%8x%8x%8x", &logTLI, &logId, &logSeg) != 3)
- return false;
- return true;
-
-}
-
-/* Ensure the xlog file is active and valid.*/
-static bool
-ValidXLogFileHeader(XLogFileName *segfile)
-{
- int fd;
- char buffer[BLCKSZ];
- char path[MAXPGPATH];
- size_t nread;
-
- snprintf(path, MAXPGPATH, "%s/%s", XLOGDIR, segfile->fname);
- fd = open(path, O_RDONLY | PG_BINARY, 0);
- if (fd < 0)
- {
- return false;
- }
- nread = read(fd, buffer, BLCKSZ);
- if (nread == BLCKSZ)
- {
- XLogPageHeader hdr = (XLogPageHeader)buffer;
-
- if (ValidXLOGPageHeader(hdr, segfile->tli, segfile->logid, segfile->seg))
- {
- return true;
- }
-
- }
- return false;
-
-}
-static bool
-ValidXLOGPageHeader(XLogPageHeader hdr, uint tli, uint id, uint seg)
-{
- XLogRecPtr recaddr;
-
- if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
- {
- return false;
- }
- if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
- {
- return false;
- }
- if (hdr->xlp_info & XLP_LONG_HEADER)
- {
- XLogLongPageHeader longhdr = (XLogLongPageHeader) hdr;
-
- if (longhdr->xlp_seg_size != XLogSegSize)
- {
- return false;
- }
- /* Get the system identifier from the segment file header.*/
- sysidentifier = ((XLogLongPageHeader) pageBuffer)->xlp_sysid;
- }
-
- recaddr.xlogid = id;
- recaddr.xrecoff = seg * XLogSegSize + logPageOff;
- if (!XLByteEQ(hdr->xlp_pageaddr, recaddr))
- {
- return false;
- }
-
- if (hdr->xlp_tli != tli)
- {
- return false;
- }
- return true;
-}
-
-
-/* Read another page, if possible */
-static bool
-ReadXLogPage(void)
-{
- size_t nread;
-
- /* Need to advance to the new segment file.*/
- if ( logPageOff >= XLogSegSize )
- {
- close(logFd);
- logFd = 0;
- }
-
- /* Need to open the segement file.*/
- if ((logFd <= 0) && (CurXLogFile != NULL))
- {
- if (OpenXLogFile(CurXLogFile) < 0)
- {
- return false;
- }
- CurXLogFile = CurXLogFile->next;
- }
-
- /* Read a page from the openning segement file.*/
- nread = read(logFd, pageBuffer, BLCKSZ);
-
- if (nread == BLCKSZ)
- {
- logPageOff += BLCKSZ;
- if (ValidXLOGPageHeader( (XLogPageHeader)pageBuffer, logTli, logId, logSeg))
- return true;
- }
-
- return false;
-}
-
-/*
- * CRC-check an XLOG record. We do not believe the contents of an XLOG
- * record (other than to the minimal extent of computing the amount of
- * data to read in) until we've checked the CRCs.
- *
- * We assume all of the record has been read into memory at *record.
- */
-static bool
-RecordIsValid(XLogRecord *record, XLogRecPtr recptr)
-{
- pg_crc32 crc;
- int i;
- uint32 len = record->xl_len;
- BkpBlock bkpb;
- char *blk;
-
- /* First the rmgr data */
- INIT_CRC32(crc);
- COMP_CRC32(crc, XLogRecGetData(record), len);
-
- /* Add in the backup blocks, if any */
- blk = (char *) XLogRecGetData(record) + len;
- for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
- {
- uint32 blen;
-
- if (!(record->xl_info & XLR_SET_BKP_BLOCK(i)))
- continue;
-
- memcpy(&bkpb, blk, sizeof(BkpBlock));
- if (bkpb.hole_offset + bkpb.hole_length > BLCKSZ)
- {
- return false;
- }
- blen = sizeof(BkpBlock) + BLCKSZ - bkpb.hole_length;
- COMP_CRC32(crc, blk, blen);
- blk += blen;
- }
-
- /* Check that xl_tot_len agrees with our calculation */
- if (blk != (char *) record + record->xl_tot_len)
- {
- return false;
- }
-
- /* Finally include the record header */
- COMP_CRC32(crc, (char *) record + sizeof(pg_crc32),
- SizeOfXLogRecord - sizeof(pg_crc32));
- FIN_CRC32(crc);
-
- if (!EQ_CRC32(record->xl_crc, crc))
- {
- return false;
- }
-
- return true;
-}
-
-
-
-/*
- * Attempt to read an XLOG record into readRecordBuf.
- */
-static bool
-FetchRecord(void)
-{
- char *buffer;
- XLogRecord *record;
- XLogContRecord *contrecord;
- uint32 len, total_len;
-
-
- while (logRecOff <= 0 || logRecOff > BLCKSZ - SizeOfXLogRecord)
- {
- /* Need to advance to new page */
- if (! ReadXLogPage())
- {
- return false;
- }
-
- logRecOff = XLogPageHeaderSize((XLogPageHeader) pageBuffer);
- if ((((XLogPageHeader) pageBuffer)->xlp_info & ~XLP_LONG_HEADER) != 0)
- {
- /* Check for a continuation record */
- if (((XLogPageHeader) pageBuffer)->xlp_info & XLP_FIRST_IS_CONTRECORD)
- {
- contrecord = (XLogContRecord *) (pageBuffer + logRecOff);
- logRecOff += MAXALIGN(contrecord->xl_rem_len + SizeOfXLogContRecord);
- }
- }
- }
-
- curRecPtr.xlogid = logId;
- curRecPtr.xrecoff = logSeg * XLogSegSize + logPageOff + logRecOff;
- record = (XLogRecord *) (pageBuffer + logRecOff);
-
- if (record->xl_len == 0)
- {
- return false;
- }
-
- total_len = record->xl_tot_len;
-
- /*
- * Allocate or enlarge readRecordBuf as needed. To avoid useless
- * small increases, round its size to a multiple of BLCKSZ, and make
- * sure it's at least 4*BLCKSZ to start with. (That is enough for all
- * "normal" records, but very large commit or abort records might need
- * more space.)
- */
- if (total_len > readRecordBufSize)
- {
- uint32 newSize = total_len;
-
- newSize += BLCKSZ - (newSize % BLCKSZ);
- newSize = Max(newSize, 4 * BLCKSZ);
- if (readRecordBuf)
- free(readRecordBuf);
- readRecordBuf = (char *) malloc(newSize);
- if (!readRecordBuf)
- {
- readRecordBufSize = 0;
- return false;
- }
- readRecordBufSize = newSize;
- }
-
- buffer = readRecordBuf;
- len = BLCKSZ - curRecPtr.xrecoff % BLCKSZ; /* available in block */
- if (total_len > len)
- {
- /* Need to reassemble record */
- uint32 gotlen = len;
-
- memcpy(buffer, record, len);
- record = (XLogRecord *) buffer;
- buffer += len;
- for (;;)
- {
- uint32 pageHeaderSize;
-
- if (!ReadXLogPage())
- {
- return false;
- }
- if (!(((XLogPageHeader) pageBuffer)->xlp_info & XLP_FIRST_IS_CONTRECORD))
- {
- return false;
- }
- pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) pageBuffer);
- contrecord = (XLogContRecord *) (pageBuffer + pageHeaderSize);
- if (contrecord->xl_rem_len == 0 ||
- total_len != (contrecord->xl_rem_len + gotlen))
- {
- return false;
- }
- len = BLCKSZ - pageHeaderSize - SizeOfXLogContRecord;
- if (contrecord->xl_rem_len > len)
- {
- memcpy(buffer, (char *)contrecord + SizeOfXLogContRecord, len);
- gotlen += len;
- buffer += len;
- continue;
- }
- memcpy(buffer, (char *) contrecord + SizeOfXLogContRecord,
- contrecord->xl_rem_len);
- logRecOff = MAXALIGN(pageHeaderSize + SizeOfXLogContRecord + contrecord->xl_rem_len);
- break;
- }
- if (!RecordIsValid(record, curRecPtr))
- {
- return false;
- }
- return true;
- }
- /* Record is contained in this page */
- memcpy(buffer, record, total_len);
- record = (XLogRecord *) buffer;
- logRecOff += MAXALIGN(total_len);
- if (!RecordIsValid(record, curRecPtr))
- {
-
- return false;
- }
- return true;
-}
-
-/*
- * if the record is checkpoint, update the lastest checkpoint record.
- */
-static void
-UpdateCheckPoint(XLogRecord *record)
-{
- uint8 info = record->xl_info & ~XLR_INFO_MASK;
-
- if ((info == XLOG_CHECKPOINT_SHUTDOWN) ||
- (info == XLOG_CHECKPOINT_ONLINE))
- {
- CheckPoint *chkpoint = (CheckPoint*) XLogRecGetData(record);
- prevchkp = lastchkp;
- lastchkp = curRecPtr;
- lastcheckpoint = *chkpoint;
-
- /* update the database state.*/
- switch(info)
- {
- case XLOG_CHECKPOINT_SHUTDOWN:
- state = DB_SHUTDOWNED;
- break;
- case XLOG_CHECKPOINT_ONLINE:
- state = DB_IN_PRODUCTION;
- break;
- }
- found_checkpoint ++ ;
- }
-}
-
-static int
-OpenXLogFile(XLogFileName *sf)
-{
-
- char path[MAXPGPATH];
-
- if ( logFd > 0 ) close(logFd);
-
- /* Open a Xlog segment file. */
- snprintf(path, MAXPGPATH, "%s/%s", XLOGDIR, sf->fname);
- logFd = open(path, O_RDONLY | PG_BINARY, 0);
-
- if (logFd < 0)
- {
- fprintf(stderr, _("%s: Can not open xlog file %s.\n"), progname,path);
- return -1;
- }
-
- /* Setup the parameter for searching. */
- logPageOff = -BLCKSZ; /* so 1st increment in readXLogPage gives 0 */
- logRecOff = 0;
- logId = sf->logid;
- logSeg = sf->seg;
- logTli = sf->tli;
- return logFd;
-}
-
-/*
- * Search the lastest checkpoint in the lastest XLog segment file.
- *
- * The return value is the total checkpoints which had been found
- * in the XLog segment file.
- */
-static int
-SearchLastCheckpoint(void)
-{
-
- /* retrive all of the active xlog files from xlog direcotry
- * into a list by increasing order, according their timeline,
- * log id, segment id.
- */
- GetXLogFiles();
-
- /* Select the oldest segment file in the lastest time line.*/
- SelectStartXLog();
-
- /* No segment file was found.*/
- if ( CurXLogFile == NULL )
- {
- return 0;
- }
-
- /* initial it . */
- logFd=logId=logSeg=logTli=0;
-
- /*
- * Search the XLog segment file from beginning to end,
- * if checkpoint record is found, then update the
- * latest check point.
- */
- while (FetchRecord())
- {
- /* To see if the record is checkpoint record. */
- if (((XLogRecord *) readRecordBuf)->xl_rmid == RM_XLOG_ID)
- UpdateCheckPoint((XLogRecord *) readRecordBuf);
- prevRecPtr = curRecPtr;
- }
-
- /* We can not know clearly if we had reached the end.
- * But just check if we reach the last segment file,
- * if it is not, then some problem there.
- * (We need a better way to know the abnormal broken during the search)
- */
- if ((logId != LastXLogFile->logid) && (logSeg != LastXLogFile->seg))
- {
- return 0;
- }
-
- /*
- * return the checkpoints which had been found yet,
- * let others know how much checkpointes are found.
- */
- return found_checkpoint;
-}
-
-/* Clean up the allocated list.*/
-static void
-CleanUpList(XLogFileName *list)
-{
-
- XLogFileName *tmp;
- tmp = list;
- while(list != NULL)
- {
- tmp=list->next;
- free(list);
- list=tmp;
- }
-
-}
-