diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c index f8d5ecb6adba4..e13cb08951c45 100644 --- a/src/bin/pg_resetwal/pg_resetwal.c +++ b/src/bin/pg_resetwal/pg_resetwal.c @@ -75,7 +75,9 @@ static uint32 minXlogTli = 0; static XLogSegNo minXlogSegNo = 0; static int WalSegSz; static int set_wal_segsize; +static uint64 set_sysid = 0; +static uint64 GenerateSystemIdentifier(void); static void CheckDataVersion(void); static bool read_controlfile(void); static void GuessControlValues(void); @@ -105,6 +107,7 @@ main(int argc, char *argv[]) {"oldest-transaction-id", required_argument, NULL, 'u'}, {"next-transaction-id", required_argument, NULL, 'x'}, {"wal-segsize", required_argument, NULL, 1}, + {"sysid", required_argument, NULL, 's'}, {NULL, 0, NULL, 0} }; @@ -137,7 +140,7 @@ main(int argc, char *argv[]) } - while ((c = getopt_long(argc, argv, "c:D:e:fl:m:no:O:u:x:", long_options, NULL)) != -1) + while ((c = getopt_long(argc, argv, "c:D:e:fl:m:no:O:u:x:s:", long_options, NULL)) != -1) { switch (c) { @@ -323,6 +326,22 @@ main(int argc, char *argv[]) } break; + case 's': + if (endptr == optarg || *endptr != '\0') + { + if (sscanf(optarg, UINT64_FORMAT, &set_sysid) != 1) + { + fprintf(stderr, _("%s: invalid argument for option -s\n"), progname); + fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); + exit(1); + } + } + else + { + set_sysid = GenerateSystemIdentifier(); + } + break; + default: fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); @@ -489,6 +508,9 @@ main(int argc, char *argv[]) if (minXlogSegNo > newXlogSegNo) newXlogSegNo = minXlogSegNo; + if (set_sysid != 0) + ControlFile.system_identifier = set_sysid; + /* * If we had to guess anything, and -f was not given, just print the * guessed values and exit. Also print if -n is given. @@ -580,6 +602,26 @@ CheckDataVersion(void) } +/* + * Create a new unique installation identifier. + * + * See notes in resetwal.c about the algorithm. + */ +static uint64 +GenerateSystemIdentifier(void) +{ + uint64 sysidentifier; + struct timeval tv; + + gettimeofday(&tv, NULL); + sysidentifier = ((uint64) tv.tv_sec) << 32; + sysidentifier |= ((uint64) tv.tv_usec) << 12; + sysidentifier |= getpid() & 0xFFF; + + return sysidentifier; +} + + /* * Try to read the existing pg_control file. * @@ -666,9 +708,6 @@ read_controlfile(void) static void GuessControlValues(void) { - uint64 sysidentifier; - struct timeval tv; - /* * Set up a completely default set of pg_control values. */ @@ -680,14 +719,9 @@ GuessControlValues(void) /* * Create a new unique installation identifier, since we can no longer use - * any old XLOG records. See notes in xlog.c about the algorithm. + * any old XLOG records. */ - gettimeofday(&tv, NULL); - sysidentifier = ((uint64) tv.tv_sec) << 32; - sysidentifier |= ((uint64) tv.tv_usec) << 12; - sysidentifier |= getpid() & 0xFFF; - - ControlFile.system_identifier = sysidentifier; + ControlFile.system_identifier = GenerateSystemIdentifier(); ControlFile.checkPointCopy.redo = SizeOfXLogLongPHD; ControlFile.checkPointCopy.ThisTimeLineID = 1; @@ -1230,6 +1264,7 @@ usage(void) printf(_(" -O, --multixact-offset=OFFSET set next multitransaction offset\n")); printf(_(" -u, --oldest-transaction-id=XID set oldest transaction ID\n")); printf(_(" -V, --version output version information, then exit\n")); + printf(_(" -s [SYSID] set system identifier (or generate one)\n")); printf(_(" -x, --next-transaction-id=XID set next transaction ID\n")); printf(_(" --wal-segsize=SIZE size of WAL segments, in megabytes\n")); printf(_(" -?, --help show this help, then exit\n"));