From ddc2504dbcf2af97b58a91b071768c17774d58d1 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Tue, 26 Aug 2014 02:54:53 +0200
Subject: [PATCH] Don't hardcode contrib_regression dbname in postgres_fdw and
 dblink tests.

That allows parallel installcheck to succeed inside contrib/. The
output is not particularly pretty unless make's -O option to
synchronize the output is used.

There's other tests, outside contrib, that use a hardcoded,
non-unique, database name. Those prohibit paralell installcheck to be
used across more directories; but that's something for a separate
patch.
---
 contrib/dblink/Makefile                       |  3 --
 contrib/dblink/expected/dblink.out            | 43 ++++++++++--------
 contrib/dblink/sql/dblink.sql                 | 45 +++++++++++--------
 contrib/postgres_fdw/Makefile                 |  3 --
 .../postgres_fdw/expected/postgres_fdw.out    |  8 +++-
 contrib/postgres_fdw/sql/postgres_fdw.sql     |  8 +++-
 6 files changed, 64 insertions(+), 46 deletions(-)

diff --git a/contrib/dblink/Makefile b/contrib/dblink/Makefile
index e5d0cd6bf00..f5ab1641dbd 100644
--- a/contrib/dblink/Makefile
+++ b/contrib/dblink/Makefile
@@ -14,9 +14,6 @@ REGRESS = paths dblink
 REGRESS_OPTS = --dlpath=$(top_builddir)/src/test/regress
 EXTRA_CLEAN = sql/paths.sql expected/paths.out
 
-# the db name is hard-coded in the tests
-override USE_MODULE_DB =
-
 ifdef USE_PGXS
 PG_CONFIG = pg_config
 PGXS := $(shell $(PG_CONFIG) --pgxs)
diff --git a/contrib/dblink/expected/dblink.out b/contrib/dblink/expected/dblink.out
index eee31a249f3..60cca8254d8 100644
--- a/contrib/dblink/expected/dblink.out
+++ b/contrib/dblink/expected/dblink.out
@@ -88,9 +88,12 @@ SELECT dblink_build_sql_delete('"MySchema"."Foo"','1 2',2,'{"0", "a"}');
  DELETE FROM "MySchema"."Foo" WHERE f1 = '0' AND f2 = 'a'
 (1 row)
 
+CREATE FUNCTION connection_parameters() RETURNS text LANGUAGE SQL AS $f$
+       SELECT $$dbname='$$||current_database()||$$'$$;
+$f$;
 -- regular old dblink
 SELECT *
-FROM dblink('dbname=contrib_regression','SELECT * FROM foo') AS t(a int, b text, c text[])
+FROM dblink(connection_parameters(),'SELECT * FROM foo') AS t(a int, b text, c text[])
 WHERE t.a > 7;
  a | b |     c      
 ---+---+------------
@@ -116,9 +119,9 @@ DECLARE
 	detail text;
 BEGIN
 	PERFORM wait_pid(crash_pid)
-	FROM dblink('dbname=contrib_regression', $$
+	FROM dblink(connection_parameters(), $$
 		SELECT pg_backend_pid() FROM dblink(
-			'service=test_ldap dbname=contrib_regression',
+			'service=test_ldap '||connection_parameters(),
 			-- This string concatenation is a hack to shoehorn a
 			-- set_pgservicefile call into the SQL statement.
 			'SELECT 1' || set_pgservicefile('pg_service.conf')
@@ -131,7 +134,7 @@ EXCEPTION WHEN OTHERS THEN
 END
 $pl$;
 -- create a persistent connection
-SELECT dblink_connect('dbname=contrib_regression');
+SELECT dblink_connect(connection_parameters());
  dblink_connect 
 ----------------
  OK
@@ -267,14 +270,14 @@ WHERE t.a > 7;
 ERROR:  connection not available
 -- put more data into our slave table, first using arbitrary connection syntax
 -- but truncate the actual return value so we can use diff to check for success
-SELECT substr(dblink_exec('dbname=contrib_regression','INSERT INTO foo VALUES(10,''k'',''{"a10","b10","c10"}'')'),1,6);
+SELECT substr(dblink_exec(connection_parameters(),'INSERT INTO foo VALUES(10,''k'',''{"a10","b10","c10"}'')'),1,6);
  substr 
 --------
  INSERT
 (1 row)
 
 -- create a persistent connection
-SELECT dblink_connect('dbname=contrib_regression');
+SELECT dblink_connect(connection_parameters());
  dblink_connect 
 ----------------
  OK
@@ -374,7 +377,7 @@ ERROR:  could not establish connection
 DETAIL:  missing "=" after "myconn" in connection info string
 
 -- create a named persistent connection
-SELECT dblink_connect('myconn','dbname=contrib_regression');
+SELECT dblink_connect('myconn',connection_parameters());
  dblink_connect 
 ----------------
  OK
@@ -403,10 +406,10 @@ CONTEXT:  Error occurred on dblink connection named "myconn": could not execute
 
 -- create a second named persistent connection
 -- should error with "duplicate connection name"
-SELECT dblink_connect('myconn','dbname=contrib_regression');
+SELECT dblink_connect('myconn',connection_parameters());
 ERROR:  duplicate connection name
 -- create a second named persistent connection with a new name
-SELECT dblink_connect('myconn2','dbname=contrib_regression');
+SELECT dblink_connect('myconn2',connection_parameters());
  dblink_connect 
 ----------------
  OK
@@ -601,7 +604,7 @@ ERROR:  could not establish connection
 DETAIL:  missing "=" after "myconn" in connection info string
 
 -- create a named persistent connection
-SELECT dblink_connect('myconn','dbname=contrib_regression');
+SELECT dblink_connect('myconn',connection_parameters());
  dblink_connect 
 ----------------
  OK
@@ -677,7 +680,7 @@ SELECT dblink_disconnect('myconn');
 SELECT dblink_disconnect('myconn');
 ERROR:  connection "myconn" not available
 -- test asynchronous queries
-SELECT dblink_connect('dtest1', 'dbname=contrib_regression');
+SELECT dblink_connect('dtest1', connection_parameters());
  dblink_connect 
 ----------------
  OK
@@ -690,7 +693,7 @@ SELECT * from
   1
 (1 row)
 
-SELECT dblink_connect('dtest2', 'dbname=contrib_regression');
+SELECT dblink_connect('dtest2', connection_parameters());
  dblink_connect 
 ----------------
  OK
@@ -703,7 +706,7 @@ SELECT * from
   1
 (1 row)
 
-SELECT dblink_connect('dtest3', 'dbname=contrib_regression');
+SELECT dblink_connect('dtest3', connection_parameters());
  dblink_connect 
 ----------------
  OK
@@ -777,7 +780,7 @@ SELECT * from result;
  10 | k  | {a10,b10,c10}
 (11 rows)
 
-SELECT dblink_connect('dtest1', 'dbname=contrib_regression');
+SELECT dblink_connect('dtest1', connection_parameters());
  dblink_connect 
 ----------------
  OK
@@ -810,8 +813,12 @@ SELECT dblink_disconnect('dtest1');
 
 -- test foreign data wrapper functionality
 CREATE ROLE dblink_regression_test;
-CREATE SERVER fdtest FOREIGN DATA WRAPPER dblink_fdw
-  OPTIONS (dbname 'contrib_regression');
+DO $d$
+    BEGIN
+        EXECUTE $$CREATE SERVER fdtest FOREIGN DATA WRAPPER dblink_fdw
+            OPTIONS (dbname '$$||current_database()||$$')$$;
+    END;
+$d$;
 CREATE USER MAPPING FOR public SERVER fdtest
   OPTIONS (server 'localhost');  -- fail, can't specify server here
 ERROR:  invalid option "server"
@@ -854,7 +861,7 @@ DROP USER dblink_regression_test;
 DROP USER MAPPING FOR public SERVER fdtest;
 DROP SERVER fdtest;
 -- test asynchronous notifications
-SELECT dblink_connect('dbname=contrib_regression');
+SELECT dblink_connect(connection_parameters());
  dblink_connect 
 ----------------
  OK
@@ -943,7 +950,7 @@ SELECT dblink_build_sql_delete('test_dropped', '1', 1,
 SET datestyle = ISO, MDY;
 SET intervalstyle = postgres;
 SET timezone = UTC;
-SELECT dblink_connect('myconn','dbname=contrib_regression');
+SELECT dblink_connect('myconn',connection_parameters());
  dblink_connect 
 ----------------
  OK
diff --git a/contrib/dblink/sql/dblink.sql b/contrib/dblink/sql/dblink.sql
index 500c684a91c..d00e7d1c41a 100644
--- a/contrib/dblink/sql/dblink.sql
+++ b/contrib/dblink/sql/dblink.sql
@@ -55,9 +55,13 @@ SELECT dblink_build_sql_update('"MySchema"."Foo"','1 2',2,'{"0", "a"}','{"99", "
 -- build a delete statement based on a local tuple,
 SELECT dblink_build_sql_delete('"MySchema"."Foo"','1 2',2,'{"0", "a"}');
 
+CREATE FUNCTION connection_parameters() RETURNS text LANGUAGE SQL AS $f$
+       SELECT $$dbname='$$||current_database()||$$'$$;
+$f$;
+
 -- regular old dblink
 SELECT *
-FROM dblink('dbname=contrib_regression','SELECT * FROM foo') AS t(a int, b text, c text[])
+FROM dblink(connection_parameters(),'SELECT * FROM foo') AS t(a int, b text, c text[])
 WHERE t.a > 7;
 
 -- should generate "connection not available" error
@@ -78,9 +82,9 @@ DECLARE
 	detail text;
 BEGIN
 	PERFORM wait_pid(crash_pid)
-	FROM dblink('dbname=contrib_regression', $$
+	FROM dblink(connection_parameters(), $$
 		SELECT pg_backend_pid() FROM dblink(
-			'service=test_ldap dbname=contrib_regression',
+			'service=test_ldap '||connection_parameters(),
 			-- This string concatenation is a hack to shoehorn a
 			-- set_pgservicefile call into the SQL statement.
 			'SELECT 1' || set_pgservicefile('pg_service.conf')
@@ -94,7 +98,7 @@ END
 $pl$;
 
 -- create a persistent connection
-SELECT dblink_connect('dbname=contrib_regression');
+SELECT dblink_connect(connection_parameters());
 
 -- use the persistent connection
 SELECT *
@@ -155,10 +159,10 @@ WHERE t.a > 7;
 
 -- put more data into our slave table, first using arbitrary connection syntax
 -- but truncate the actual return value so we can use diff to check for success
-SELECT substr(dblink_exec('dbname=contrib_regression','INSERT INTO foo VALUES(10,''k'',''{"a10","b10","c10"}'')'),1,6);
+SELECT substr(dblink_exec(connection_parameters(),'INSERT INTO foo VALUES(10,''k'',''{"a10","b10","c10"}'')'),1,6);
 
 -- create a persistent connection
-SELECT dblink_connect('dbname=contrib_regression');
+SELECT dblink_connect(connection_parameters());
 
 -- put more data into our slave table, using persistent connection syntax
 -- but truncate the actual return value so we can use diff to check for success
@@ -204,7 +208,7 @@ FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[])
 WHERE t.a > 7;
 
 -- create a named persistent connection
-SELECT dblink_connect('myconn','dbname=contrib_regression');
+SELECT dblink_connect('myconn',connection_parameters());
 
 -- use the named persistent connection
 SELECT *
@@ -218,10 +222,10 @@ WHERE t.a > 7;
 
 -- create a second named persistent connection
 -- should error with "duplicate connection name"
-SELECT dblink_connect('myconn','dbname=contrib_regression');
+SELECT dblink_connect('myconn',connection_parameters());
 
 -- create a second named persistent connection with a new name
-SELECT dblink_connect('myconn2','dbname=contrib_regression');
+SELECT dblink_connect('myconn2',connection_parameters());
 
 -- use the second named persistent connection
 SELECT *
@@ -307,7 +311,7 @@ FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[])
 WHERE t.a > 7;
 
 -- create a named persistent connection
-SELECT dblink_connect('myconn','dbname=contrib_regression');
+SELECT dblink_connect('myconn',connection_parameters());
 
 -- put more data into our slave table, using named persistent connection syntax
 -- but truncate the actual return value so we can use diff to check for success
@@ -341,15 +345,15 @@ SELECT dblink_disconnect('myconn');
 SELECT dblink_disconnect('myconn');
 
 -- test asynchronous queries
-SELECT dblink_connect('dtest1', 'dbname=contrib_regression');
+SELECT dblink_connect('dtest1', connection_parameters());
 SELECT * from
  dblink_send_query('dtest1', 'select * from foo where f1 < 3') as t1;
 
-SELECT dblink_connect('dtest2', 'dbname=contrib_regression');
+SELECT dblink_connect('dtest2', connection_parameters());
 SELECT * from
  dblink_send_query('dtest2', 'select * from foo where f1 > 2 and f1 < 7') as t1;
 
-SELECT dblink_connect('dtest3', 'dbname=contrib_regression');
+SELECT dblink_connect('dtest3', connection_parameters());
 SELECT * from
  dblink_send_query('dtest3', 'select * from foo where f1 > 6') as t1;
 
@@ -378,7 +382,7 @@ SELECT dblink_disconnect('dtest3');
 
 SELECT * from result;
 
-SELECT dblink_connect('dtest1', 'dbname=contrib_regression');
+SELECT dblink_connect('dtest1', connection_parameters());
 SELECT * from
  dblink_send_query('dtest1', 'select * from foo where f1 < 3') as t1;
 
@@ -388,8 +392,13 @@ SELECT dblink_disconnect('dtest1');
 
 -- test foreign data wrapper functionality
 CREATE ROLE dblink_regression_test;
-CREATE SERVER fdtest FOREIGN DATA WRAPPER dblink_fdw
-  OPTIONS (dbname 'contrib_regression');
+DO $d$
+    BEGIN
+        EXECUTE $$CREATE SERVER fdtest FOREIGN DATA WRAPPER dblink_fdw
+            OPTIONS (dbname '$$||current_database()||$$')$$;
+    END;
+$d$;
+
 CREATE USER MAPPING FOR public SERVER fdtest
   OPTIONS (server 'localhost');  -- fail, can't specify server here
 CREATE USER MAPPING FOR public SERVER fdtest OPTIONS (user :'USER');
@@ -412,7 +421,7 @@ DROP USER MAPPING FOR public SERVER fdtest;
 DROP SERVER fdtest;
 
 -- test asynchronous notifications
-SELECT dblink_connect('dbname=contrib_regression');
+SELECT dblink_connect(connection_parameters());
 
 --should return listen
 SELECT dblink_exec('LISTEN regression');
@@ -458,7 +467,7 @@ SELECT dblink_build_sql_delete('test_dropped', '1', 1,
 SET datestyle = ISO, MDY;
 SET intervalstyle = postgres;
 SET timezone = UTC;
-SELECT dblink_connect('myconn','dbname=contrib_regression');
+SELECT dblink_connect('myconn',connection_parameters());
 SELECT dblink_exec('myconn', 'SET datestyle = GERMAN, DMY;');
 
 -- single row synchronous case
diff --git a/contrib/postgres_fdw/Makefile b/contrib/postgres_fdw/Makefile
index 7c660955caf..a9e380d6be4 100644
--- a/contrib/postgres_fdw/Makefile
+++ b/contrib/postgres_fdw/Makefile
@@ -13,9 +13,6 @@ DATA = postgres_fdw--1.0.sql
 
 REGRESS = postgres_fdw
 
-# the db name is hard-coded in the tests
-override USE_MODULE_DB =
-
 ifdef USE_PGXS
 PG_CONFIG = pg_config
 PGXS := $(shell $(PG_CONFIG) --pgxs)
diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out
index 7eead58cffb..5fe752a1bbe 100644
--- a/contrib/postgres_fdw/expected/postgres_fdw.out
+++ b/contrib/postgres_fdw/expected/postgres_fdw.out
@@ -3,8 +3,12 @@
 -- ===================================================================
 CREATE EXTENSION postgres_fdw;
 CREATE SERVER testserver1 FOREIGN DATA WRAPPER postgres_fdw;
-CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw
-  OPTIONS (dbname 'contrib_regression');
+DO $d$
+    BEGIN
+        EXECUTE $$CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw
+            OPTIONS (dbname '$$||current_database()||$$');$$;
+    END;
+$d$;
 CREATE USER MAPPING FOR public SERVER testserver1
 	OPTIONS (user 'value', password 'value');
 CREATE USER MAPPING FOR CURRENT_USER SERVER loopback;
diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql
index 9f54359be58..ba5ef91f6a0 100644
--- a/contrib/postgres_fdw/sql/postgres_fdw.sql
+++ b/contrib/postgres_fdw/sql/postgres_fdw.sql
@@ -5,8 +5,12 @@
 CREATE EXTENSION postgres_fdw;
 
 CREATE SERVER testserver1 FOREIGN DATA WRAPPER postgres_fdw;
-CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw
-  OPTIONS (dbname 'contrib_regression');
+DO $d$
+    BEGIN
+        EXECUTE $$CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw
+            OPTIONS (dbname '$$||current_database()||$$');$$;
+    END;
+$d$;
 
 CREATE USER MAPPING FOR public SERVER testserver1
 	OPTIONS (user 'value', password 'value');
-- 
GitLab