diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index ff47d5c4e7b4677f1f6efbfafa5f634531b9d8ad..b4a2f9c8795258f7e5716873281bb2ed0a1d220f 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -512,16 +512,21 @@ submake-libpgport:
 #
 # Testing support
 
-PL_TESTDB = pl_regression
-CONTRIB_TESTDB = contrib_regression
-ifneq ($(MODULE_big),)
-  CONTRIB_TESTDB_MODULE = contrib_regression_$(MODULE_big)
-else
-  ifneq ($(MODULES),)
-    CONTRIB_TESTDB_MODULE = contrib_regression_$(MODULES)
+ifneq ($(USE_MODULE_DB),)
+  PL_TESTDB = pl_regression_$(NAME)
+  # Replace this with $(or ...) if we ever require GNU make 3.81.
+  ifneq ($(MODULE_big),)
+    CONTRIB_TESTDB=contrib_regression_$(MODULE_big)
   else
-    CONTRIB_TESTDB_MODULE = contrib_regression
+    ifneq ($(MODULES),)
+      CONTRIB_TESTDB=contrib_regression_$(word 1,$(MODULES))
+    else
+      CONTRIB_TESTDB=contrib_regression_$(word 1,$(REGRESS))
+    endif
   endif
+else
+  PL_TESTDB = pl_regression
+  CONTRIB_TESTDB = contrib_regression
 endif
 
 ifdef NO_LOCALE
diff --git a/src/makefiles/pgxs.mk b/src/makefiles/pgxs.mk
index faa7dd9b987ed0a60fd79cf4d39168fae5ed4225..424741f9d5cc6ccb429b46802a30959d794902f4 100644
--- a/src/makefiles/pgxs.mk
+++ b/src/makefiles/pgxs.mk
@@ -256,12 +256,7 @@ distclean maintainer-clean: clean
 
 ifdef REGRESS
 
-# Select database to use for running the tests
-ifneq ($(USE_MODULE_DB),)
-  REGRESS_OPTS += --dbname=$(CONTRIB_TESTDB_MODULE)
-else
-  REGRESS_OPTS += --dbname=$(CONTRIB_TESTDB)
-endif
+REGRESS_OPTS += --dbname=$(CONTRIB_TESTDB)
 
 # where to find psql for running the tests
 PSQLDIR = $(bindir)