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