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