Skip to content

Commit b877761

Browse files
committed
pg_upgrade: Fix problems caused by renaming pg_resetxlog.
Commit 85c1132 renamed pg_resetxlog to pg_resetwal, but didn't make pg_upgrade smart enough to cope with the situation. Michael Paquier, per a complaint from Jeff Janes
1 parent 6d16ecc commit b877761

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

src/bin/pg_upgrade/check.c

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
2626
static void check_for_reg_data_type_usage(ClusterInfo *cluster);
2727
static void check_for_jsonb_9_4_usage(ClusterInfo *cluster);
2828
static void check_for_pg_role_prefix(ClusterInfo *cluster);
29-
static void get_bin_version(ClusterInfo *cluster);
3029
static char *get_canonical_locale_name(int category, const char *locale);
3130

3231

@@ -241,10 +240,6 @@ check_cluster_versions(void)
241240
if (old_cluster.major_version > new_cluster.major_version)
242241
pg_fatal("This utility cannot be used to downgrade to older major PostgreSQL versions.\n");
243242

244-
/* get old and new binary versions */
245-
get_bin_version(&old_cluster);
246-
get_bin_version(&new_cluster);
247-
248243
/* Ensure binaries match the designated data directories */
249244
if (GET_MAJOR_VERSION(old_cluster.major_version) !=
250245
GET_MAJOR_VERSION(old_cluster.bin_version))
@@ -1080,34 +1075,6 @@ check_for_pg_role_prefix(ClusterInfo *cluster)
10801075
check_ok();
10811076
}
10821077

1083-
static void
1084-
get_bin_version(ClusterInfo *cluster)
1085-
{
1086-
char cmd[MAXPGPATH],
1087-
cmd_output[MAX_STRING];
1088-
FILE *output;
1089-
int pre_dot = 0,
1090-
post_dot = 0;
1091-
1092-
snprintf(cmd, sizeof(cmd), "\"%s/pg_ctl\" --version", cluster->bindir);
1093-
1094-
if ((output = popen(cmd, "r")) == NULL ||
1095-
fgets(cmd_output, sizeof(cmd_output), output) == NULL)
1096-
pg_fatal("could not get pg_ctl version data using %s: %s\n",
1097-
cmd, strerror(errno));
1098-
1099-
pclose(output);
1100-
1101-
/* Remove trailing newline */
1102-
if (strchr(cmd_output, '\n') != NULL)
1103-
*strchr(cmd_output, '\n') = '\0';
1104-
1105-
if (sscanf(cmd_output, "%*s %*s %d.%d", &pre_dot, &post_dot) < 1)
1106-
pg_fatal("could not get version from %s\n", cmd);
1107-
1108-
cluster->bin_version = (pre_dot * 100 + post_dot) * 100;
1109-
}
1110-
11111078

11121079
/*
11131080
* get_canonical_locale_name

src/bin/pg_upgrade/controldata.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ get_control_data(ClusterInfo *cluster, bool live_check)
7070
uint32 tli = 0;
7171
uint32 logid = 0;
7272
uint32 segno = 0;
73+
char *resetwal_bin;
7374

7475

7576
/*
@@ -111,9 +112,14 @@ get_control_data(ClusterInfo *cluster, bool live_check)
111112
pg_putenv("LC_ALL", NULL);
112113
pg_putenv("LC_MESSAGES", "C");
113114

115+
/* pg_resetxlog has been renamed to pg_resetwal in version 10 */
116+
if (GET_MAJOR_VERSION(cluster->bin_version) < 1000)
117+
resetwal_bin = "pg_resetxlog\" -n";
118+
else
119+
resetwal_bin = "pg_resetwal\" -n";
114120
snprintf(cmd, sizeof(cmd), "\"%s/%s \"%s\"",
115121
cluster->bindir,
116-
live_check ? "pg_controldata\"" : "pg_resetwal\" -n",
122+
live_check ? "pg_controldata\"" : resetwal_bin,
117123
cluster->pgdata);
118124
fflush(stdout);
119125
fflush(stderr);

src/bin/pg_upgrade/exec.c

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,48 @@
1616

1717
static void check_data_dir(ClusterInfo *cluster);
1818
static void check_bin_dir(ClusterInfo *cluster);
19+
static void get_bin_version(ClusterInfo *cluster);
1920
static void validate_exec(const char *dir, const char *cmdName);
2021

2122
#ifdef WIN32
2223
static int win32_check_directory_write_permissions(void);
2324
#endif
2425

2526

27+
/*
28+
* get_bin_version
29+
*
30+
* Fetch versions of binaries for cluster.
31+
*/
32+
static void
33+
get_bin_version(ClusterInfo *cluster)
34+
{
35+
char cmd[MAXPGPATH],
36+
cmd_output[MAX_STRING];
37+
FILE *output;
38+
int pre_dot = 0,
39+
post_dot = 0;
40+
41+
snprintf(cmd, sizeof(cmd), "\"%s/pg_ctl\" --version", cluster->bindir);
42+
43+
if ((output = popen(cmd, "r")) == NULL ||
44+
fgets(cmd_output, sizeof(cmd_output), output) == NULL)
45+
pg_fatal("could not get pg_ctl version data using %s: %s\n",
46+
cmd, strerror(errno));
47+
48+
pclose(output);
49+
50+
/* Remove trailing newline */
51+
if (strchr(cmd_output, '\n') != NULL)
52+
*strchr(cmd_output, '\n') = '\0';
53+
54+
if (sscanf(cmd_output, "%*s %*s %d.%d", &pre_dot, &post_dot) < 1)
55+
pg_fatal("could not get version from %s\n", cmd);
56+
57+
cluster->bin_version = (pre_dot * 100 + post_dot) * 100;
58+
}
59+
60+
2661
/*
2762
* exec_prog()
2863
* Execute an external program with stdout/stderr redirected, and report
@@ -335,7 +370,20 @@ check_bin_dir(ClusterInfo *cluster)
335370

336371
validate_exec(cluster->bindir, "postgres");
337372
validate_exec(cluster->bindir, "pg_ctl");
338-
validate_exec(cluster->bindir, "pg_resetwal");
373+
374+
/*
375+
* Fetch the binary versions after checking for the existence of pg_ctl,
376+
* this gives a correct error if the binary used itself for the version
377+
* fetching is broken.
378+
*/
379+
get_bin_version(&old_cluster);
380+
get_bin_version(&new_cluster);
381+
382+
/* pg_resetxlog has been renamed to pg_resetwal in version 10 */
383+
if (GET_MAJOR_VERSION(cluster->bin_version) < 1000)
384+
validate_exec(cluster->bindir, "pg_resetxlog");
385+
else
386+
validate_exec(cluster->bindir, "pg_resetwal");
339387
if (cluster == &new_cluster)
340388
{
341389
/* these are only needed in the new cluster */

0 commit comments

Comments
 (0)