pg_resetxlog.c 22.4 KB
Newer Older
Bruce Momjian's avatar
Bruce Momjian committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*-------------------------------------------------------------------------
 *
 * pg_resetxlog.c
 *	  A utility to "zero out" the xlog when it's corrupt beyond recovery.
 *	  Can also rebuild pg_control if needed.
 *
 * 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 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 ...
 *
 *
23
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
Bruce Momjian's avatar
Bruce Momjian committed
24 25
 * Portions Copyright (c) 1994, Regents of the University of California
 *
26
 * $PostgreSQL: pgsql/src/bin/pg_resetxlog/pg_resetxlog.c,v 1.37 2005/10/03 00:28:42 tgl Exp $
Bruce Momjian's avatar
Bruce Momjian committed
27 28 29 30 31 32
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include <dirent.h>
33
#include <fcntl.h>
Bruce Momjian's avatar
Bruce Momjian committed
34
#include <locale.h>
35 36 37 38
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
39 40 41
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
Bruce Momjian's avatar
Bruce Momjian committed
42

43
#include "access/multixact.h"
Bruce Momjian's avatar
Bruce Momjian committed
44
#include "access/xlog.h"
45
#include "access/xlog_internal.h"
Bruce Momjian's avatar
Bruce Momjian committed
46 47 48
#include "catalog/catversion.h"
#include "catalog/pg_control.h"

Tom Lane's avatar
Tom Lane committed
49 50 51 52
extern int	optind;
extern char *optarg;


Bruce Momjian's avatar
Bruce Momjian committed
53 54 55 56
static ControlFileData ControlFile;		/* pg_control values */
static uint32 newXlogId,
			newXlogSeg;			/* ID/Segment of new XLOG segment */
static bool guessed = false;	/* T if we had to guess at any values */
57
static const char *progname;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

static bool ReadControlFile(void);
static void GuessControlValues(void);
static void PrintControlValues(bool guessed);
static void RewriteControlFile(void);
static void KillExistingXLOG(void);
static void WriteEmptyXLOG(void);
static void usage(void);


int
main(int argc, char *argv[])
{
	int			c;
	bool		force = false;
	bool		noupdate = false;
	TransactionId set_xid = 0;
75
	Oid			set_oid = 0;
76
	MultiXactId	set_mxid = 0;
77
	MultiXactOffset set_mxoff = -1;
78 79
	uint32		minXlogTli = 0,
				minXlogId = 0,
80
				minXlogSeg = 0;
81 82
	char	   *endptr;
	char	   *endptr2;
83
	char	   *endptr3;
84 85 86 87
	char	   *DataDir;
	int			fd;
	char		path[MAXPGPATH];

88
	set_pglocale_pgservice(argv[0], "pg_resetxlog");
89

90
	progname = get_progname(argv[0]);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

	if (argc > 1)
	{
		if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
		{
			usage();
			exit(0);
		}
		if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
		{
			puts("pg_resetxlog (PostgreSQL) " PG_VERSION);
			exit(0);
		}
	}


107
	while ((c = getopt(argc, argv, "fl:m:no:O:x:")) != -1)
108 109 110 111 112 113 114 115 116 117 118 119
	{
		switch (c)
		{
			case 'f':
				force = true;
				break;

			case 'n':
				noupdate = true;
				break;

			case 'x':
120 121 122
				set_xid = strtoul(optarg, &endptr, 0);
				if (endptr == optarg || *endptr != '\0')
				{
123 124
					fprintf(stderr, _("%s: invalid argument for option -x\n"), progname);
					fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
125 126
					exit(1);
				}
127 128 129 130 131 132 133
				if (set_xid == 0)
				{
					fprintf(stderr, _("%s: transaction ID (-x) must not be 0\n"), progname);
					exit(1);
				}
				break;

134 135 136 137
			case 'o':
				set_oid = strtoul(optarg, &endptr, 0);
				if (endptr == optarg || *endptr != '\0')
				{
138 139
					fprintf(stderr, _("%s: invalid argument for option -o\n"), progname);
					fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
140 141 142 143 144 145 146 147 148
					exit(1);
				}
				if (set_oid == 0)
				{
					fprintf(stderr, _("%s: OID (-o) must not be 0\n"), progname);
					exit(1);
				}
				break;

149 150 151 152 153 154 155 156 157 158
			case 'm':
				set_mxid = strtoul(optarg, &endptr, 0);
				if (endptr == optarg || *endptr != '\0')
				{
					fprintf(stderr, _("%s: invalid argument for option -m\n"), progname);
					fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
					exit(1);
				}
				if (set_mxid == 0)
				{
Peter Eisentraut's avatar
Peter Eisentraut committed
159
					fprintf(stderr, _("%s: multitransaction ID (-m) must not be 0\n"), progname);
160 161 162 163
					exit(1);
				}
				break;

164 165 166 167 168 169 170 171 172 173
			case 'O':
				set_mxoff = strtoul(optarg, &endptr, 0);
				if (endptr == optarg || *endptr != '\0')
				{
					fprintf(stderr, _("%s: invalid argument for option -O\n"), progname);
					fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
					exit(1);
				}
				if (set_mxoff == -1)
				{
Peter Eisentraut's avatar
Peter Eisentraut committed
174
					fprintf(stderr, _("%s: multitransaction offset (-O) must not be -1\n"), progname);
175 176 177 178
					exit(1);
				}
				break;

179
			case 'l':
180
				minXlogTli = strtoul(optarg, &endptr, 0);
181 182
				if (endptr == optarg || *endptr != ',')
				{
183 184
					fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
					fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
185 186
					exit(1);
				}
187 188 189 190 191 192 193 194 195
				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')
196
				{
197 198
					fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
					fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
199 200 201 202 203
					exit(1);
				}
				break;

			default:
204
				fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
205 206 207 208 209 210 211
				exit(1);
		}
	}

	if (optind == argc)
	{
		fprintf(stderr, _("%s: no data directory specified\n"), progname);
212
		fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
213 214 215
		exit(1);
	}

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
 	/*
	 * Don't allow pg_resetxlog to be run as root, to avoid
	 * overwriting the ownership of files in the data directory. We
	 * need only check for root -- any other user won't have
	 * sufficient permissions to modify files in the data directory.
	 */
#ifndef WIN32
#ifndef __BEOS__				/* no root check on BeOS */
	if (geteuid() == 0)
	{
		fprintf(stderr, _("%s: cannot be executed by \"root\"\n"),
				progname);
		fprintf(stderr, _("You must run %s as the PostgreSQL superuser.\n"),
				progname);
		exit(1);
	}
#endif
#endif

235
	DataDir = argv[optind];
236 237 238 239 240 241 242

	if (chdir(DataDir) < 0)
	{
		fprintf(stderr, _("%s: could not change directory to \"%s\": %s\n"),
				progname, DataDir, strerror(errno));
		exit(1);
	}
243 244 245 246 247 248 249 250 251 252 253 254

	/*
	 * 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 (errno != ENOENT)
		{
255
			fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), progname, path, strerror(errno));
256 257 258 259 260
			exit(1);
		}
	}
	else
	{
261 262
		fprintf(stderr, _("%s: lock file \"%s\" exists\n"
						  "Is a server running?  If not, delete the lock file and try again.\n"),
263 264 265 266 267 268 269 270 271 272
				progname, path);
		exit(1);
	}

	/*
	 * Attempt to read the existing pg_control file
	 */
	if (!ReadControlFile())
		GuessControlValues();

273 274 275 276 277 278 279 280 281 282
	/*
	 * Adjust fields if required by switches.  (Do this now so that
	 * printout, if any, includes these values.)
	 */
	if (set_xid != 0)
		ControlFile.checkPointCopy.nextXid = set_xid;

	if (set_oid != 0)
		ControlFile.checkPointCopy.nextOid = set_oid;

283 284 285
	if (set_mxid != 0)
		ControlFile.checkPointCopy.nextMulti = set_mxid;

286 287 288
	if (set_mxoff != -1)
		ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;

289 290 291
	if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
		ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;

292 293 294 295 296 297 298 299
	if (minXlogId > ControlFile.logId ||
		(minXlogId == ControlFile.logId &&
		 minXlogSeg > ControlFile.logSeg))
	{
		ControlFile.logId = minXlogId;
		ControlFile.logSeg = minXlogSeg;
	}

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	/*
	 * 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);
		if (!noupdate)
		{
			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)
	{
		printf(_("The database server was not shut down cleanly.\n"
Bruce Momjian's avatar
Bruce Momjian committed
322 323
			 "Resetting the transaction log may cause data to be lost.\n"
			 "If you want to proceed anyway, use -f to force reset.\n"));
324 325 326 327 328 329 330 331 332 333 334 335 336
		exit(1);
	}

	/*
	 * Else, do the dirty deed.
	 */
	RewriteControlFile();
	KillExistingXLOG();
	WriteEmptyXLOG();

	printf(_("Transaction log reset\n"));
	return 0;
}
Bruce Momjian's avatar
Bruce Momjian committed
337 338 339 340 341 342


/*
 * Try to read the existing pg_control file.
 *
 * This routine is also responsible for updating old pg_control versions
343
 * to the current format.  (Currently we don't do anything of the sort.)
Bruce Momjian's avatar
Bruce Momjian committed
344 345 346 347 348 349 350
 */
static bool
ReadControlFile(void)
{
	int			fd;
	int			len;
	char	   *buffer;
351
	pg_crc32	crc;
Bruce Momjian's avatar
Bruce Momjian committed
352

353
	if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY)) < 0)
Bruce Momjian's avatar
Bruce Momjian committed
354 355 356 357 358 359
	{
		/*
		 * If pg_control is not there at all, or we can't read it, the
		 * odds are we've been handed a bad DataDir path, so give up. User
		 * can do "touch pg_control" to force us to proceed.
		 */
360
		fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
361
				progname, XLOG_CONTROL_FILE, strerror(errno));
Bruce Momjian's avatar
Bruce Momjian committed
362
		if (errno == ENOENT)
363
			fprintf(stderr, _("If you are sure the data directory path is correct, execute\n"
364 365
							  "  touch %s\n"
							  "and try again.\n"),
366
					XLOG_CONTROL_FILE);
Bruce Momjian's avatar
Bruce Momjian committed
367 368 369 370 371 372 373 374 375
		exit(1);
	}

	/* Use malloc to ensure we have a maxaligned buffer */
	buffer = (char *) malloc(BLCKSZ);

	len = read(fd, buffer, BLCKSZ);
	if (len < 0)
	{
376
		fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
377
				progname, XLOG_CONTROL_FILE, strerror(errno));
Bruce Momjian's avatar
Bruce Momjian committed
378 379 380 381 382 383 384 385
		exit(1);
	}
	close(fd);

	if (len >= sizeof(ControlFileData) &&
		((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION)
	{
		/* Check the CRC. */
386 387 388 389 390
		INIT_CRC32(crc);
		COMP_CRC32(crc,
				   buffer,
				   offsetof(ControlFileData, crc));
		FIN_CRC32(crc);
Bruce Momjian's avatar
Bruce Momjian committed
391

392
		if (EQ_CRC32(crc, ((ControlFileData *) buffer)->crc))
Bruce Momjian's avatar
Bruce Momjian committed
393 394 395 396 397 398
		{
			/* Valid data... */
			memcpy(&ControlFile, buffer, sizeof(ControlFile));
			return true;
		}

399 400
		fprintf(stderr, _("%s: pg_control exists but has invalid CRC; proceed with caution\n"),
				progname);
Bruce Momjian's avatar
Bruce Momjian committed
401 402 403 404 405 406 407
		/* We will use the data anyway, but treat it as guessed. */
		memcpy(&ControlFile, buffer, sizeof(ControlFile));
		guessed = true;
		return true;
	}

	/* Looks like it's a mess. */
408 409
	fprintf(stderr, _("%s: pg_control exists but is broken or unknown version; ignoring it\n"),
			progname);
Bruce Momjian's avatar
Bruce Momjian committed
410 411 412 413 414 415 416 417 418 419
	return false;
}


/*
 * Guess at pg_control values when we can't read the old ones.
 */
static void
GuessControlValues(void)
{
420 421
	uint64		sysidentifier;
	struct timeval tv;
Bruce Momjian's avatar
Bruce Momjian committed
422 423 424 425 426 427 428 429 430 431 432
	char	   *localeptr;

	/*
	 * 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;

433 434 435 436 437 438 439 440 441 442
	/*
	 * 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.system_identifier = sysidentifier;

Bruce Momjian's avatar
Bruce Momjian committed
443
	ControlFile.checkPointCopy.redo.xlogid = 0;
444
	ControlFile.checkPointCopy.redo.xrecoff = SizeOfXLogLongPHD;
Bruce Momjian's avatar
Bruce Momjian committed
445
	ControlFile.checkPointCopy.undo = ControlFile.checkPointCopy.redo;
446
	ControlFile.checkPointCopy.ThisTimeLineID = 1;
Bruce Momjian's avatar
Bruce Momjian committed
447
	ControlFile.checkPointCopy.nextXid = (TransactionId) 514;	/* XXX */
448
	ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
449
	ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
450
	ControlFile.checkPointCopy.nextMultiOffset = 0;
Bruce Momjian's avatar
Bruce Momjian committed
451 452 453 454 455 456 457 458
	ControlFile.checkPointCopy.time = time(NULL);

	ControlFile.state = DB_SHUTDOWNED;
	ControlFile.time = time(NULL);
	ControlFile.logId = 0;
	ControlFile.logSeg = 1;
	ControlFile.checkPoint = ControlFile.checkPointCopy.redo;

459 460
	ControlFile.maxAlign = MAXIMUM_ALIGNOF;
	ControlFile.floatFormat = FLOATFORMAT_VALUE;
Bruce Momjian's avatar
Bruce Momjian committed
461 462
	ControlFile.blcksz = BLCKSZ;
	ControlFile.relseg_size = RELSEG_SIZE;
463
	ControlFile.xlog_seg_size = XLOG_SEG_SIZE;
464
	ControlFile.nameDataLen = NAMEDATALEN;
465
	ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
466 467 468 469 470 471 472
#ifdef HAVE_INT64_TIMESTAMP
	ControlFile.enableIntTimes = TRUE;
#else
	ControlFile.enableIntTimes = FALSE;
#endif
	ControlFile.localeBuflen = LOCALE_NAME_BUFLEN;

Bruce Momjian's avatar
Bruce Momjian committed
473 474 475
	localeptr = setlocale(LC_COLLATE, "");
	if (!localeptr)
	{
476
		fprintf(stderr, _("%s: invalid LC_COLLATE setting\n"), progname);
Bruce Momjian's avatar
Bruce Momjian committed
477 478 479 480 481 482
		exit(1);
	}
	StrNCpy(ControlFile.lc_collate, localeptr, LOCALE_NAME_BUFLEN);
	localeptr = setlocale(LC_CTYPE, "");
	if (!localeptr)
	{
483
		fprintf(stderr, _("%s: invalid LC_CTYPE setting\n"), progname);
Bruce Momjian's avatar
Bruce Momjian committed
484 485 486 487 488 489
		exit(1);
	}
	StrNCpy(ControlFile.lc_ctype, localeptr, LOCALE_NAME_BUFLEN);

	/*
	 * XXX eventually, should try to grovel through old XLOG to develop
490
	 * more accurate values for TimeLineID, nextXID, etc.
Bruce Momjian's avatar
Bruce Momjian committed
491 492 493 494 495 496 497 498 499 500 501 502 503
	 */
}


/*
 * 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(bool guessed)
{
504 505
	char		sysident_str[32];

506 507 508 509 510
	if (guessed)
		printf(_("Guessed pg_control values:\n\n"));
	else
		printf(_("pg_control values:\n\n"));

511
	/*
Bruce Momjian's avatar
Bruce Momjian committed
512 513
	 * Format system_identifier separately to keep platform-dependent
	 * format code out of the translatable message string.
514 515 516 517
	 */
	snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
			 ControlFile.system_identifier);

518 519
	printf(_("pg_control version number:            %u\n"), ControlFile.pg_control_version);
	printf(_("Catalog version number:               %u\n"), ControlFile.catalog_version_no);
520
	printf(_("Database system identifier:           %s\n"), sysident_str);
521 522
	printf(_("Current log file ID:                  %u\n"), ControlFile.logId);
	printf(_("Next log file segment:                %u\n"), ControlFile.logSeg);
523
	printf(_("Latest checkpoint's TimeLineID:       %u\n"), ControlFile.checkPointCopy.ThisTimeLineID);
524 525
	printf(_("Latest checkpoint's NextXID:          %u\n"), ControlFile.checkPointCopy.nextXid);
	printf(_("Latest checkpoint's NextOID:          %u\n"), ControlFile.checkPointCopy.nextOid);
526
	printf(_("Latest checkpoint's NextMultiXactId:  %u\n"), ControlFile.checkPointCopy.nextMulti);
527
	printf(_("Latest checkpoint's NextMultiOffset:  %u\n"), ControlFile.checkPointCopy.nextMultiOffset);
528 529
	printf(_("Maximum data alignment:               %u\n"), ControlFile.maxAlign);
	/* we don't print floatFormat since can't say much useful about it */
530 531
	printf(_("Database block size:                  %u\n"), ControlFile.blcksz);
	printf(_("Blocks per segment of large relation: %u\n"), ControlFile.relseg_size);
532
	printf(_("Maximum length of identifiers:        %u\n"), ControlFile.nameDataLen);
533
	printf(_("Maximum columns in an index:          %u\n"), ControlFile.indexMaxKeys);
534
	printf(_("Date/time type storage:               %s\n"),
535
		   (ControlFile.enableIntTimes ? _("64-bit integers") : _("floating-point numbers")));
536
	printf(_("Maximum length of locale name:        %u\n"), ControlFile.localeBuflen);
537 538
	printf(_("LC_COLLATE:                           %s\n"), ControlFile.lc_collate);
	printf(_("LC_CTYPE:                             %s\n"), ControlFile.lc_ctype);
Bruce Momjian's avatar
Bruce Momjian committed
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
}


/*
 * Write out the new pg_control file.
 */
static void
RewriteControlFile(void)
{
	int			fd;
	char		buffer[BLCKSZ]; /* need not be aligned */

	/*
	 * Adjust fields as needed to force an empty XLOG starting at the next
	 * available segment.
	 */
	newXlogId = ControlFile.logId;
	newXlogSeg = ControlFile.logSeg;
557 558 559

	/* adjust in case we are changing segment size */
	newXlogSeg *= ControlFile.xlog_seg_size;
Bruce Momjian's avatar
Bruce Momjian committed
560
	newXlogSeg = (newXlogSeg + XLogSegSize - 1) / XLogSegSize;
561

Bruce Momjian's avatar
Bruce Momjian committed
562 563 564
	/* be sure we wrap around correctly at end of a logfile */
	NextLogSeg(newXlogId, newXlogSeg);

565 566 567
	/* Now we can force the recorded xlog seg size to the right thing. */
	ControlFile.xlog_seg_size = XLogSegSize;

Bruce Momjian's avatar
Bruce Momjian committed
568 569
	ControlFile.checkPointCopy.redo.xlogid = newXlogId;
	ControlFile.checkPointCopy.redo.xrecoff =
570
		newXlogSeg * XLogSegSize + SizeOfXLogLongPHD;
Bruce Momjian's avatar
Bruce Momjian committed
571 572 573 574 575 576 577 578 579 580 581 582
	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;

	/* Contents are protected with a CRC */
583 584 585 586 587
	INIT_CRC32(ControlFile.crc);
	COMP_CRC32(ControlFile.crc,
			   (char *) &ControlFile,
			   offsetof(ControlFileData, crc));
	FIN_CRC32(ControlFile.crc);
Bruce Momjian's avatar
Bruce Momjian committed
588 589 590 591 592 593 594 595 596 597

	/*
	 * We write out BLCKSZ bytes into pg_control, zero-padding the excess
	 * over sizeof(ControlFileData).  This reduces the odds of
	 * premature-EOF errors when reading pg_control.  We'll still fail
	 * when we check the contents of the file, but hopefully with a more
	 * specific error than "couldn't read pg_control".
	 */
	if (sizeof(ControlFileData) > BLCKSZ)
	{
598 599 600
		fprintf(stderr,
				_("%s: internal error -- sizeof(ControlFileData) is too large ... fix xlog.c\n"),
				progname);
Bruce Momjian's avatar
Bruce Momjian committed
601 602 603 604 605 606
		exit(1);
	}

	memset(buffer, 0, BLCKSZ);
	memcpy(buffer, &ControlFile, sizeof(ControlFileData));

607
	unlink(XLOG_CONTROL_FILE);
Bruce Momjian's avatar
Bruce Momjian committed
608

609 610 611
	fd = open(XLOG_CONTROL_FILE,
			  O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
			  S_IRUSR | S_IWUSR);
Bruce Momjian's avatar
Bruce Momjian committed
612 613
	if (fd < 0)
	{
614 615
		fprintf(stderr, _("%s: could not create pg_control file: %s\n"),
				progname, strerror(errno));
Bruce Momjian's avatar
Bruce Momjian committed
616 617 618 619 620 621 622 623 624
		exit(1);
	}

	errno = 0;
	if (write(fd, buffer, BLCKSZ) != BLCKSZ)
	{
		/* if write didn't set errno, assume problem is no disk space */
		if (errno == 0)
			errno = ENOSPC;
625 626
		fprintf(stderr, _("%s: could not write pg_control file: %s\n"),
				progname, strerror(errno));
Bruce Momjian's avatar
Bruce Momjian committed
627 628 629 630 631
		exit(1);
	}

	if (fsync(fd) != 0)
	{
632
		fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
Bruce Momjian's avatar
Bruce Momjian committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
		exit(1);
	}

	close(fd);
}


/*
 * Remove existing XLOG files
 */
static void
KillExistingXLOG(void)
{
	DIR		   *xldir;
	struct dirent *xlde;
	char		path[MAXPGPATH];

650
	xldir = opendir(XLOGDIR);
Bruce Momjian's avatar
Bruce Momjian committed
651 652
	if (xldir == NULL)
	{
653
		fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
654
				progname, XLOGDIR, strerror(errno));
Bruce Momjian's avatar
Bruce Momjian committed
655 656 657 658 659 660
		exit(1);
	}

	errno = 0;
	while ((xlde = readdir(xldir)) != NULL)
	{
661 662
		if (strlen(xlde->d_name) == 24 &&
			strspn(xlde->d_name, "0123456789ABCDEF") == 24)
Bruce Momjian's avatar
Bruce Momjian committed
663
		{
664
			snprintf(path, MAXPGPATH, "%s/%s", XLOGDIR, xlde->d_name);
Bruce Momjian's avatar
Bruce Momjian committed
665 666
			if (unlink(path) < 0)
			{
667
				fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
668
						progname, path, strerror(errno));
Bruce Momjian's avatar
Bruce Momjian committed
669 670 671 672 673
				exit(1);
			}
		}
		errno = 0;
	}
674
#ifdef WIN32
Bruce Momjian's avatar
Bruce Momjian committed
675 676 677 678 679

	/*
	 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
	 * not in released version
	 */
680 681 682
	if (GetLastError() == ERROR_NO_MORE_FILES)
		errno = 0;
#endif
683

Bruce Momjian's avatar
Bruce Momjian committed
684 685
	if (errno)
	{
686
		fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
687
				progname, XLOGDIR, strerror(errno));
Bruce Momjian's avatar
Bruce Momjian committed
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
		exit(1);
	}
	closedir(xldir);
}


/*
 * Write an empty XLOG file, containing only the checkpoint record
 * already set up in ControlFile.
 */
static void
WriteEmptyXLOG(void)
{
	char	   *buffer;
	XLogPageHeader page;
703
	XLogLongPageHeader longpage;
Bruce Momjian's avatar
Bruce Momjian committed
704
	XLogRecord *record;
705
	pg_crc32	crc;
Bruce Momjian's avatar
Bruce Momjian committed
706 707 708 709 710 711 712 713
	char		path[MAXPGPATH];
	int			fd;
	int			nbytes;

	/* Use malloc() to ensure buffer is MAXALIGNED */
	buffer = (char *) malloc(BLCKSZ);
	page = (XLogPageHeader) buffer;
	memset(buffer, 0, BLCKSZ);
714 715

	/* Set up the XLOG page header */
Bruce Momjian's avatar
Bruce Momjian committed
716
	page->xlp_magic = XLOG_PAGE_MAGIC;
717 718
	page->xlp_info = XLP_LONG_HEADER;
	page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
Bruce Momjian's avatar
Bruce Momjian committed
719 720 721
	page->xlp_pageaddr.xlogid =
		ControlFile.checkPointCopy.redo.xlogid;
	page->xlp_pageaddr.xrecoff =
722 723 724 725
		ControlFile.checkPointCopy.redo.xrecoff - SizeOfXLogLongPHD;
	longpage = (XLogLongPageHeader) page;
	longpage->xlp_sysid = ControlFile.system_identifier;
	longpage->xlp_seg_size = XLogSegSize;
726

727 728
	/* Insert the initial checkpoint record */
	record = (XLogRecord *) ((char *) page + SizeOfXLogLongPHD);
Bruce Momjian's avatar
Bruce Momjian committed
729 730
	record->xl_prev.xlogid = 0;
	record->xl_prev.xrecoff = 0;
731
	record->xl_xid = InvalidTransactionId;
732
	record->xl_tot_len = SizeOfXLogRecord + sizeof(CheckPoint);
Bruce Momjian's avatar
Bruce Momjian committed
733 734 735 736 737 738
	record->xl_len = sizeof(CheckPoint);
	record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
	record->xl_rmid = RM_XLOG_ID;
	memcpy(XLogRecGetData(record), &ControlFile.checkPointCopy,
		   sizeof(CheckPoint));

739 740 741 742 743
	INIT_CRC32(crc);
	COMP_CRC32(crc, &ControlFile.checkPointCopy, sizeof(CheckPoint));
	COMP_CRC32(crc, (char *) record + sizeof(pg_crc32),
			   SizeOfXLogRecord - sizeof(pg_crc32));
	FIN_CRC32(crc);
Bruce Momjian's avatar
Bruce Momjian committed
744 745 746
	record->xl_crc = crc;

	/* Write the first page */
747 748
	XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID,
				 newXlogId, newXlogSeg);
Bruce Momjian's avatar
Bruce Momjian committed
749 750 751 752 753 754 755

	unlink(path);

	fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
			  S_IRUSR | S_IWUSR);
	if (fd < 0)
	{
756
		fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
757
				progname, path, strerror(errno));
Bruce Momjian's avatar
Bruce Momjian committed
758 759 760 761 762 763 764 765 766
		exit(1);
	}

	errno = 0;
	if (write(fd, buffer, BLCKSZ) != BLCKSZ)
	{
		/* if write didn't set errno, assume problem is no disk space */
		if (errno == 0)
			errno = ENOSPC;
767
		fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
768
				progname, path, strerror(errno));
Bruce Momjian's avatar
Bruce Momjian committed
769 770 771 772 773 774 775 776 777 778 779 780
		exit(1);
	}

	/* Fill the rest of the file with zeroes */
	memset(buffer, 0, BLCKSZ);
	for (nbytes = BLCKSZ; nbytes < XLogSegSize; nbytes += BLCKSZ)
	{
		errno = 0;
		if (write(fd, buffer, BLCKSZ) != BLCKSZ)
		{
			if (errno == 0)
				errno = ENOSPC;
781
			fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
782
					progname, path, strerror(errno));
Bruce Momjian's avatar
Bruce Momjian committed
783 784 785 786 787 788
			exit(1);
		}
	}

	if (fsync(fd) != 0)
	{
789
		fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
Bruce Momjian's avatar
Bruce Momjian committed
790 791 792 793 794 795 796 797 798 799
		exit(1);
	}

	close(fd);
}


static void
usage(void)
{
800
	printf(_("%s resets the PostgreSQL transaction log.\n\n"), progname);
801
	printf(_("Usage:\n  %s [OPTION]... DATADIR\n\n"), progname);
802
	printf(_("Options:\n"));
803
	printf(_("  -f              force update to be done\n"));
804
	printf(_("  -l TLI,FILE,SEG force minimum WAL starting location for new transaction log\n"));
Peter Eisentraut's avatar
Peter Eisentraut committed
805
	printf(_("  -m XID          set next multitransaction ID\n"));
806 807
	printf(_("  -n              no update, just show extracted control values (for testing)\n"));
	printf(_("  -o OID          set next OID\n"));
Peter Eisentraut's avatar
Peter Eisentraut committed
808
	printf(_("  -O OFFSET       set next multitransaction offset\n"));
809 810 811
	printf(_("  -x XID          set next transaction ID\n"));
	printf(_("  --help          show this help, then exit\n"));
	printf(_("  --version       output version information, then exit\n"));
812
	printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
Bruce Momjian's avatar
Bruce Momjian committed
813
}