Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d9db324

Browse files
committed
In pg_upgrade, check that the binary and data directories are the same
major version. Backpatch to 9.1. Dan McGee
1 parent c6635bd commit d9db324

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

contrib/pg_upgrade/check.c

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static void check_for_prepared_transactions(ClusterInfo *cluster);
2020
static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
2121
static void check_for_reg_data_type_usage(ClusterInfo *cluster);
2222
static void check_for_support_lib(ClusterInfo *cluster);
23+
static void get_bin_version(ClusterInfo *cluster);
2324

2425

2526
void
@@ -217,6 +218,8 @@ output_completion_banner(char *deletion_script_file_name)
217218
void
218219
check_cluster_versions(void)
219220
{
221+
prep_status("Checking cluster versions");
222+
220223
/* get old and new cluster versions */
221224
old_cluster.major_version = get_major_server_version(&old_cluster);
222225
new_cluster.major_version = get_major_server_version(&new_cluster);
@@ -236,10 +239,26 @@ check_cluster_versions(void)
236239

237240
/*
238241
* We can't allow downgrading because we use the target pg_dumpall, and
239-
* pg_dumpall cannot operate on new datbase versions, only older versions.
242+
* pg_dumpall cannot operate on new database versions, only older versions.
240243
*/
241244
if (old_cluster.major_version > new_cluster.major_version)
242245
pg_log(PG_FATAL, "This utility cannot be used to downgrade to older major PostgreSQL versions.\n");
246+
247+
/* get old and new binary versions */
248+
get_bin_version(&old_cluster);
249+
get_bin_version(&new_cluster);
250+
251+
/* Ensure binaries match the designated data directories */
252+
if (GET_MAJOR_VERSION(old_cluster.major_version) !=
253+
GET_MAJOR_VERSION(old_cluster.bin_version))
254+
pg_log(PG_FATAL,
255+
"Old cluster data and binary directories are from different major versions.\n");
256+
if (GET_MAJOR_VERSION(new_cluster.major_version) !=
257+
GET_MAJOR_VERSION(new_cluster.bin_version))
258+
pg_log(PG_FATAL,
259+
"New cluster data and binary directories are from different major versions.\n");
260+
261+
check_ok();
243262
}
244263

245264

@@ -756,3 +775,32 @@ check_for_support_lib(ClusterInfo *cluster)
756775

757776
fclose(lib_test);
758777
}
778+
779+
780+
static void
781+
get_bin_version(ClusterInfo *cluster)
782+
{
783+
char cmd[MAXPGPATH], cmd_output[MAX_STRING];
784+
FILE *output;
785+
int pre_dot, post_dot;
786+
787+
snprintf(cmd, sizeof(cmd), "\"%s/pg_ctl\" --version", cluster->bindir);
788+
789+
if ((output = popen(cmd, "r")) == NULL)
790+
pg_log(PG_FATAL, "Could not get pg_ctl version data: %s\n",
791+
getErrorText(errno));
792+
793+
fgets(cmd_output, sizeof(cmd_output), output);
794+
795+
pclose(output);
796+
797+
/* Remove trailing newline */
798+
if (strchr(cmd_output, '\n') != NULL)
799+
*strchr(cmd_output, '\n') = '\0';
800+
801+
if (sscanf(cmd_output, "%*s %*s %d.%d", &pre_dot, &post_dot) != 2)
802+
pg_log(PG_FATAL, "could not get version from %s\n", cmd);
803+
804+
cluster->bin_version = (pre_dot * 100 + post_dot) * 100;
805+
}
806+

contrib/pg_upgrade/pg_upgrade.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ typedef struct
184184
unsigned short port; /* port number where postmaster is waiting */
185185
uint32 major_version; /* PG_VERSION of cluster */
186186
char major_version_str[64]; /* string PG_VERSION of cluster */
187+
uint32 bin_version; /* version returned from pg_ctl */
187188
Oid pg_database_oid; /* OID of pg_database relation */
188189
char *tablespace_suffix; /* directory specification */
189190
} ClusterInfo;

0 commit comments

Comments
 (0)