From 7e2b1c03ce24e8fefa2080c0f1f8cfbb86ce664e Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sat, 11 May 2013 14:22:18 -0400
Subject: [PATCH] Make pg_upgrade's test script attempt to select a
 non-conflicting port.

Previously, the port number used in this test script was hard-wired at
pg_upgrade's default of 50432; which is not so great because parallel build
runs might conflict.  Commit 3d53173e20d151341f894f79d556768c845ba3e4
removed this setting for the postmasters started by the script proper
(not by pg_upgrade), which didn't do anything to fix that problem and also
guaranteed a failure if there was a live postmaster at the build's default
port number.  Instead, select a non-conflicting temporary port number in
the same way that pg_regress.c does.  (Its method isn't entirely
bulletproof, but given the lack of complaints I'm not going to worry
about that today.)

In passing, unset MAKEFLAGS and MAKELEVEL to avoid problems with the
script's internal invocations of make, for the same reason pg_regress.c
does: it could cause problems in a parallel make.
---
 contrib/pg_upgrade/test.sh | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)

diff --git a/contrib/pg_upgrade/test.sh b/contrib/pg_upgrade/test.sh
index 9b4b132d23b..8bc6a00b538 100644
--- a/contrib/pg_upgrade/test.sh
+++ b/contrib/pg_upgrade/test.sh
@@ -13,6 +13,11 @@ set -e
 
 : ${MAKE=make}
 
+# Guard against parallel make issues (see comments in pg_regress.c)
+unset MAKEFLAGS
+unset MAKELEVEL
+
+# Set listen_addresses desirably
 testhost=`uname -s`
 
 case $testhost in
@@ -65,6 +70,12 @@ PGDATA="$BASE_PGDATA.old"
 export PGDATA
 rm -rf "$BASE_PGDATA" "$PGDATA"
 
+logdir=$PWD/log
+rm -rf "$logdir"
+mkdir "$logdir"
+
+# Clear out any environment vars that might cause libpq to connect to
+# the wrong postmaster (cf pg_regress.c)
 unset PGDATABASE
 unset PGUSER
 unset PGSERVICE
@@ -74,9 +85,23 @@ unset PGCONNECT_TIMEOUT
 unset PGHOST
 unset PGHOSTADDR
 
-logdir=$PWD/log
-rm -rf "$logdir"
-mkdir "$logdir"
+# Select a non-conflicting port number, similarly to pg_regress.c
+PG_VERSION_NUM=`grep '#define PG_VERSION_NUM' $newsrc/src/include/pg_config.h | awk '{print $3}'`
+PGPORT=`expr $PG_VERSION_NUM % 16384 + 49152`
+export PGPORT
+
+i=0
+while psql -X postgres </dev/null 2>/dev/null
+do
+	i=`expr $i + 1`
+	if [ $i -eq 16 ]
+	then
+		echo port $PGPORT apparently in use
+		exit 1
+	fi
+	PGPORT=`expr $PGPORT + 1`
+	export PGPORT
+done
 
 # enable echo so the user can see what is being executed
 set -x
@@ -123,7 +148,7 @@ PGDATA=$BASE_PGDATA
 
 initdb -N
 
-pg_upgrade -d "${PGDATA}.old" -D "${PGDATA}" -b "$oldbindir" -B "$bindir"
+pg_upgrade -d "${PGDATA}.old" -D "${PGDATA}" -b "$oldbindir" -B "$bindir" -p "$PGPORT" -P "$PGPORT"
 
 pg_ctl start -l "$logdir/postmaster2.log" -o "$POSTMASTER_OPTS" -w
 
-- 
GitLab