Skip to content

Set custom sysid 13.7 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: release_13_7
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 45 additions & 10 deletions src/bin/pg_resetwal/pg_resetwal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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}
};

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*/
Expand All @@ -682,12 +721,7 @@ 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.
*/
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;
Expand Down Expand Up @@ -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"));
Expand Down