From 98fd7bab2c1b4d8c74704fb38ce7d2222156f7a6 Mon Sep 17 00:00:00 2001 From: Zhan Balbaev Date: Wed, 25 May 2022 16:09:30 +0300 Subject: [PATCH 1/4] Set sysid via pg_resetwal --- .vscode/settings.json | 7 +++++ src/bin/pg_resetwal/pg_resetwal.c | 51 +++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000000..0244b4e8ebfe2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "pg_authid.h": "c", + "builtins.h": "c", + "slot.h": "c" + } +} \ No newline at end of file diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c index 611edf6ade2f2..e073737b1fc65 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 ReadControlFile(void); static void GuessControlValues(void); @@ -137,7 +139,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) { @@ -328,6 +330,22 @@ main(int argc, char *argv[]) } break; + case 's': + if (optarg) + { + 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); @@ -492,6 +510,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. @@ -596,6 +617,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. * @@ -687,9 +728,6 @@ ReadControlFile(void) static void GuessControlValues(void) { - uint64 sysidentifier; - struct timeval tv; - /* * Set up a completely default set of pg_control values. */ @@ -701,14 +739,14 @@ 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; @@ -1332,6 +1370,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")); From 7348902d5508b1915d292809ed81833af807e844 Mon Sep 17 00:00:00 2001 From: Zhan Balbaev Date: Wed, 8 Jun 2022 17:45:46 +0300 Subject: [PATCH 2/4] Fix undeclared identifier --- src/bin/pg_resetwal/pg_resetwal.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c index e073737b1fc65..ac671b050e4de 100644 --- a/src/bin/pg_resetwal/pg_resetwal.c +++ b/src/bin/pg_resetwal/pg_resetwal.c @@ -741,10 +741,6 @@ GuessControlValues(void) * Create a new unique installation identifier, since we can no longer use * 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 = GenerateSystemIdentifier(); From a92e3acafb1dd9089da51722dbf7b71c1ef43b26 Mon Sep 17 00:00:00 2001 From: Zhan Balbaev Date: Wed, 8 Jun 2022 19:29:38 +0300 Subject: [PATCH 3/4] Fix long_options --- src/bin/pg_resetwal/pg_resetwal.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c index ac671b050e4de..752bead9ff774 100644 --- a/src/bin/pg_resetwal/pg_resetwal.c +++ b/src/bin/pg_resetwal/pg_resetwal.c @@ -107,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} }; @@ -139,7 +140,7 @@ main(int argc, char *argv[]) } - while ((c = getopt_long(argc, argv, "c:D:e:fl:m:no:O:u:x:s::", 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) { @@ -331,7 +332,7 @@ main(int argc, char *argv[]) break; case 's': - if (optarg) + if (endptr == optarg || *endptr != '\0') { if (sscanf(optarg, UINT64_FORMAT, &set_sysid) != 1) { @@ -1366,7 +1367,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(_(" -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")); From ac333b660f42852a8de1ff797078a3d7948337b6 Mon Sep 17 00:00:00 2001 From: Zhan Balbaev Date: Thu, 9 Jun 2022 11:49:51 +0300 Subject: [PATCH 4/4] Fix spaces and tabs --- src/bin/pg_resetwal/pg_resetwal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c index 752bead9ff774..4e565f168a6b9 100644 --- a/src/bin/pg_resetwal/pg_resetwal.c +++ b/src/bin/pg_resetwal/pg_resetwal.c @@ -1367,7 +1367,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(_(" -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"));