From bb5ece58679a4c62b4b0e08c294c3a4f4c762b58 Mon Sep 17 00:00:00 2001
From: Daniel Gustafsson <daniel@yesql.se>
Date: Mon, 25 Mar 2019 23:49:18 +0100
Subject: [PATCH 1/3] Only allow upgrades by the same exact version new bindir

Extend the check for bin_version to ensure the upgrade is using
the exact same version of binaries as pg_upgrade.
---
 src/bin/pg_upgrade/check.c |  2 ++
 src/bin/pg_upgrade/exec.c  | 18 +++++++++++++-----
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/bin/pg_upgrade/check.c b/src/bin/pg_upgrade/check.c
index fc5aa7010f..4bafd15715 100644
--- a/src/bin/pg_upgrade/check.c
+++ b/src/bin/pg_upgrade/check.c
@@ -283,6 +283,8 @@ check_cluster_versions(void)
 	if (GET_MAJOR_VERSION(new_cluster.major_version) !=
 		GET_MAJOR_VERSION(new_cluster.bin_version))
 		pg_fatal("New cluster data and binary directories are from different major versions.\n");
+	if (new_cluster.bin_version != PG_VERSION_NUM)
+		pg_fatal("New cluster binaries and upgrade utility are from different versions.\n");
 
 	check_ok();
 }
diff --git a/src/bin/pg_upgrade/exec.c b/src/bin/pg_upgrade/exec.c
index 043373f9a5..a42e1ac6d3 100644
--- a/src/bin/pg_upgrade/exec.c
+++ b/src/bin/pg_upgrade/exec.c
@@ -34,8 +34,10 @@ get_bin_version(ClusterInfo *cluster)
 	char		cmd[MAXPGPATH],
 				cmd_output[MAX_STRING];
 	FILE	   *output;
+	int			matches;
 	int			v1 = 0,
-				v2 = 0;
+				v2 = 0,
+				v3 = 0;
 
 	snprintf(cmd, sizeof(cmd), "\"%s/pg_ctl\" --version", cluster->bindir);
 
@@ -46,17 +48,23 @@ get_bin_version(ClusterInfo *cluster)
 
 	pclose(output);
 
-	if (sscanf(cmd_output, "%*s %*s %d.%d", &v1, &v2) < 1)
+	matches = sscanf(cmd_output, "%*s %*s %d.%d.%d", &v1, &v2, &v3);
+	if (matches < 1)
 		pg_fatal("could not get pg_ctl version output from %s\n", cmd);
 
-	if (v1 < 10)
+	if (matches == 3)
 	{
 		/* old style, e.g. 9.6.1 */
-		cluster->bin_version = v1 * 10000 + v2 * 100;
+		cluster->bin_version = v1 * 10000 + v2 * 100 + v3;
 	}
-	else
+	else if (matches == 2)
 	{
 		/* new style, e.g. 10.1 */
+		cluster->bin_version = v1 * 10000 + v2;
+	}
+	else
+	{
+		/* new style pre-release, e.g. 12devel */
 		cluster->bin_version = v1 * 10000;
 	}
 }
-- 
2.14.1.145.gb3622a4ee

