diff --git a/contrib/pg_upgrade/.gitignore b/contrib/pg_upgrade/.gitignore
index 03ec737bf2b993d040a6971b5ddae0bb506f81a6..79c8cdb540e3a092f1ea85ac1d6dc77c897fceaf 100644
--- a/contrib/pg_upgrade/.gitignore
+++ b/contrib/pg_upgrade/.gitignore
@@ -1 +1,5 @@
 /pg_upgrade
+# Generated by test suite
+delete_old_cluster.sh
+/log/
+/tmp_check/
diff --git a/contrib/pg_upgrade/Makefile b/contrib/pg_upgrade/Makefile
index 8f3fd7c9bbf29ca227ae77280130ebc46db30d58..7f97b3d48451c9a0fbed05464a4f1c02cf17141e 100644
--- a/contrib/pg_upgrade/Makefile
+++ b/contrib/pg_upgrade/Makefile
@@ -21,3 +21,11 @@ top_builddir = ../..
 include $(top_builddir)/src/Makefile.global
 include $(top_srcdir)/contrib/contrib-global.mk
 endif
+
+check: test.sh
+	MAKE=$(MAKE) bindir=$(bindir) libdir=$(libdir) $(SHELL) $< --install
+
+installcheck: test.sh
+	MAKE=$(MAKE) bindir=$(bindir) libdir=$(libdir) $(SHELL) $<
+
+EXTRA_CLEAN = delete_old_cluster.sh log/ tmp_check/
diff --git a/contrib/pg_upgrade/TESTING b/contrib/pg_upgrade/TESTING
index f685c938c5f1424b709acebd61f4f734eac4ca6b..d38b205f49c2385bdbff778b5dc24022d11a63e8 100644
--- a/contrib/pg_upgrade/TESTING
+++ b/contrib/pg_upgrade/TESTING
@@ -62,3 +62,22 @@ steps:
 
 7)  Diff the regression database dump file with the regression dump
     file loaded into the old server.
+
+The shell script test.sh in this directory performs more or less this
+procedure.  You can invoke it by running
+
+    gmake check
+
+or by running
+
+    gmake installcheck
+
+if "gmake install" (or "gmake install-world") were done beforehand.
+When invoked without arguments, it will run an upgrade from the
+version in this source tree to a new instance of the same version.  To
+test an upgrade from a different version, invoke it like this:
+
+    gmake installcheck oldbindir=...otherversion/bin oldsrc=...somewhere/postgresql
+
+In this case, you will have to manually eyeball the resulting dump
+diff for version-specific differences, as explained above.
diff --git a/contrib/pg_upgrade/test.sh b/contrib/pg_upgrade/test.sh
new file mode 100644
index 0000000000000000000000000000000000000000..9aebee94b49d4d9368aee749df843f752705289a
--- /dev/null
+++ b/contrib/pg_upgrade/test.sh
@@ -0,0 +1,123 @@
+#!/bin/sh
+
+# contrib/pg_upgrade/test.sh
+#
+# Test driver for pg_upgrade.  Initializes a new database cluster,
+# runs the regression tests (to put in some data), runs pg_dumpall,
+# runs pg_upgrade, runs pg_dumpall again, compares the dumps.
+#
+# Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
+# Portions Copyright (c) 1994, Regents of the University of California
+
+set -e
+
+: ${MAKE=make}
+: ${PGPORT=50432}
+export PGPORT
+
+temp_root=$PWD/tmp_check
+
+if [ "$1" = '--install' ]; then
+	temp_install=$temp_root/install
+	bindir=$temp_install/$bindir
+	libdir=$temp_install/$libdir
+
+	"$MAKE" -s -C ../.. install DESTDIR="$temp_install"
+	"$MAKE" -s -C ../pg_upgrade_support install DESTDIR="$temp_install"
+	"$MAKE" -s -C . install DESTDIR="$temp_install"
+
+	# platform-specific magic to find the shared libraries; see pg_regress.c
+	LD_LIBRARY_PATH=$libdir:$LD_LIBRARY_PATH
+	export LD_LIBRARY_PATH
+	DYLD_LIBRARY_PATH=$libdir:$DYLD_LIBRARY_PATH
+	export DYLD_LIBRARY_PATH
+	LIBPATH=$libdir:$LIBPATH
+	export LIBPATH
+	PATH=$libdir:$PATH
+
+	# We need to make it use psql from our temporary installation,
+	# because otherwise the installcheck run below would try to
+	# use psql from the proper installation directory, which might
+	# be outdated or missing.
+	EXTRA_REGRESS_OPTS=--psqldir=$bindir
+	export EXTRA_REGRESS_OPTS
+fi
+
+: ${oldbindir=$bindir}
+
+: ${oldsrc=../..}
+oldsrc=`cd "$oldsrc" && pwd`
+newsrc=`cd ../.. && pwd`
+
+PATH=$bindir:$PATH
+export PATH
+
+PGDATA=$temp_root/data
+export PGDATA
+rm -rf "$PGDATA" "$PGDATA".old
+
+logdir=$PWD/log
+rm -rf "$logdir"
+mkdir "$logdir"
+
+set -x
+
+$oldbindir/initdb
+$oldbindir/pg_ctl start -l "$logdir/postmaster1.log" -w
+if "$MAKE" -C "$oldsrc" installcheck; then
+	pg_dumpall >"$temp_root"/dump1.sql || pg_dumpall1_status=$?
+	if [ "$newsrc" != "$oldsrc" ]; then
+		oldpgversion=`psql -A -t -d regression -c "SHOW server_version_num"`
+		fix_sql=""
+		case $oldpgversion in
+			804??)
+				fix_sql="UPDATE pg_proc SET probin = replace(probin::text, '$oldsrc', '$newsrc')::bytea WHERE probin LIKE '$oldsrc%'; DROP FUNCTION public.myfunc(integer);"
+				;;
+			900??)
+				fix_sql="SET bytea_output TO escape; UPDATE pg_proc SET probin = replace(probin::text, '$oldsrc', '$newsrc')::bytea WHERE probin LIKE '$oldsrc%';"
+				;;
+			901??)
+				fix_sql="UPDATE pg_proc SET probin = replace(probin, '$oldsrc', '$newsrc') WHERE probin LIKE '$oldsrc%';"
+				;;
+		esac
+		psql -d regression -c "$fix_sql;" || psql_fix_sql_status=$?
+
+		mv "$temp_root"/dump1.sql "$temp_root"/dump1.sql.orig
+		sed "s;$oldsrc;$newsrc;g" "$temp_root"/dump1.sql.orig >"$temp_root"/dump1.sql
+	fi
+else
+	make_installcheck_status=$?
+fi
+$oldbindir/pg_ctl -m fast stop
+if [ -n "$make_installcheck_status" ]; then
+	exit 1
+fi
+if [ -n "$psql_fix_sql_status" ]; then
+	exit 1
+fi
+if [ -n "$pg_dumpall1_status" ]; then
+	echo "pg_dumpall of pre-upgrade database cluster failed"
+	exit 1
+fi
+
+mv "${PGDATA}" "${PGDATA}.old"
+
+initdb
+
+pg_upgrade -d "${PGDATA}.old" -D "${PGDATA}" -b "$oldbindir" -B "$bindir"
+
+pg_ctl start -l "$logdir/postmaster2.log" -w
+pg_dumpall >"$temp_root"/dump2.sql || pg_dumpall2_status=$?
+pg_ctl -m fast stop
+if [ -n "$pg_dumpall2_status" ]; then
+	echo "pg_dumpall of post-upgrade database cluster failed"
+	exit 1
+fi
+
+if diff -q "$temp_root"/dump1.sql "$temp_root"/dump2.sql; then
+	echo PASSED
+	exit 0
+else
+	echo "dumps were not identical"
+	exit 1
+fi
diff --git a/src/makefiles/pgxs.mk b/src/makefiles/pgxs.mk
index 84a296a60d76d2ae718798f5cb94aedf949b36f9..7dc874240ac04de03298faed3cb0955f1761c04f 100644
--- a/src/makefiles/pgxs.mk
+++ b/src/makefiles/pgxs.mk
@@ -207,7 +207,7 @@ ifdef OBJS
 	rm -f $(OBJS)
 endif
 ifdef EXTRA_CLEAN
-	rm -f $(EXTRA_CLEAN)
+	rm -rf $(EXTRA_CLEAN)
 endif
 ifdef REGRESS
 # things created by various check targets
diff --git a/src/test/regress/GNUmakefile b/src/test/regress/GNUmakefile
index 90aea6cc82aa43d64430740a868040e541a6458b..ec293914ec95d162b96e5e8ea8d15a5dcdcce2d7 100644
--- a/src/test/regress/GNUmakefile
+++ b/src/test/regress/GNUmakefile
@@ -132,7 +132,7 @@ tablespace-setup:
 ## Run tests
 ##
 
-REGRESS_OPTS = --dlpath=.
+REGRESS_OPTS = --dlpath=. $(EXTRA_REGRESS_OPTS)
 
 check: all tablespace-setup
 	$(pg_regress_check) $(REGRESS_OPTS) --schedule=$(srcdir)/parallel_schedule $(MAXCONNOPT) $(TEMP_CONF) $(EXTRA_TESTS)