postgres_fdw: Use psql variables for connection parameters

Several statements need to reference the current connection's current
database name and current port value.  Until now, this has been
accomplished by creating dynamic SQL statements inside of a DO block,
which is not as easy to parse.  It also takes away some of the
granularity of any error messages that might occur, making debugging
harder.

By capturing the connection-specific settings into psql variables, it
becomes possible to write simpler SQL statements for the FDW objects.
This eliminates most of DO blocks used in this test, making it a bit
more readable and shorter.

Author: Author: Corey Huinker <corey.huinker@gmail.com>
Discussion: https://postgr.es/m/CADkLM=cpUiJ3QF7aUthTvaVMmgQcm7QqZBRMDLhBRTR+gJX-Og@mail.gmail.com
master
Michael Paquier 2 weeks ago
parent 317c117d6d
commit 36aed19fd9
  1. 42
      contrib/postgres_fdw/expected/postgres_fdw.out
  2. 44
      contrib/postgres_fdw/sql/postgres_fdw.sql

@ -2,23 +2,16 @@
-- create FDW objects -- create FDW objects
-- =================================================================== -- ===================================================================
CREATE EXTENSION postgres_fdw; CREATE EXTENSION postgres_fdw;
SELECT current_database() AS current_database,
current_setting('port') AS current_port
\gset
CREATE SERVER testserver1 FOREIGN DATA WRAPPER postgres_fdw; CREATE SERVER testserver1 FOREIGN DATA WRAPPER postgres_fdw;
DO $d$ CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw
BEGIN OPTIONS (dbname :'current_database', port :'current_port');
EXECUTE $$CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw CREATE SERVER loopback2 FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname '$$||current_database()||$$', OPTIONS (dbname :'current_database', port :'current_port');
port '$$||current_setting('port')||$$' CREATE SERVER loopback3 FOREIGN DATA WRAPPER postgres_fdw
)$$; OPTIONS (dbname :'current_database', port :'current_port');
EXECUTE $$CREATE SERVER loopback2 FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname '$$||current_database()||$$',
port '$$||current_setting('port')||$$'
)$$;
EXECUTE $$CREATE SERVER loopback3 FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname '$$||current_database()||$$',
port '$$||current_setting('port')||$$'
)$$;
END;
$d$;
CREATE USER MAPPING FOR public SERVER testserver1 CREATE USER MAPPING FOR public SERVER testserver1
OPTIONS (user 'value', password 'value'); OPTIONS (user 'value', password 'value');
CREATE USER MAPPING FOR CURRENT_USER SERVER loopback; CREATE USER MAPPING FOR CURRENT_USER SERVER loopback;
@ -235,12 +228,7 @@ SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should work
ALTER SERVER loopback OPTIONS (SET dbname 'no such database'); ALTER SERVER loopback OPTIONS (SET dbname 'no such database');
SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should fail SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should fail
ERROR: could not connect to server "loopback" ERROR: could not connect to server "loopback"
DO $d$ ALTER SERVER loopback OPTIONS (SET dbname :'current_database');
BEGIN
EXECUTE $$ALTER SERVER loopback
OPTIONS (SET dbname '$$||current_database()||$$')$$;
END;
$d$;
SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should work again SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should work again
c3 | c4 c3 | c4
-------+------------------------------ -------+------------------------------
@ -10643,14 +10631,8 @@ SHOW is_superuser;
(1 row) (1 row)
-- This will be OK, we can create the FDW -- This will be OK, we can create the FDW
DO $d$ CREATE SERVER loopback_nopw FOREIGN DATA WRAPPER postgres_fdw
BEGIN OPTIONS (dbname :'current_database', port :'current_port');
EXECUTE $$CREATE SERVER loopback_nopw FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname '$$||current_database()||$$',
port '$$||current_setting('port')||$$'
)$$;
END;
$d$;
-- But creation of user mappings for non-superusers should fail -- But creation of user mappings for non-superusers should fail
CREATE USER MAPPING FOR public SERVER loopback_nopw; CREATE USER MAPPING FOR public SERVER loopback_nopw;
CREATE USER MAPPING FOR CURRENT_USER SERVER loopback_nopw; CREATE USER MAPPING FOR CURRENT_USER SERVER loopback_nopw;

@ -4,24 +4,17 @@
CREATE EXTENSION postgres_fdw; CREATE EXTENSION postgres_fdw;
CREATE SERVER testserver1 FOREIGN DATA WRAPPER postgres_fdw; SELECT current_database() AS current_database,
DO $d$ current_setting('port') AS current_port
BEGIN \gset
EXECUTE $$CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname '$$||current_database()||$$',
port '$$||current_setting('port')||$$'
)$$;
EXECUTE $$CREATE SERVER loopback2 FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname '$$||current_database()||$$',
port '$$||current_setting('port')||$$'
)$$;
EXECUTE $$CREATE SERVER loopback3 FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname '$$||current_database()||$$',
port '$$||current_setting('port')||$$'
)$$;
END;
$d$;
CREATE SERVER testserver1 FOREIGN DATA WRAPPER postgres_fdw;
CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname :'current_database', port :'current_port');
CREATE SERVER loopback2 FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname :'current_database', port :'current_port');
CREATE SERVER loopback3 FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname :'current_database', port :'current_port');
CREATE USER MAPPING FOR public SERVER testserver1 CREATE USER MAPPING FOR public SERVER testserver1
OPTIONS (user 'value', password 'value'); OPTIONS (user 'value', password 'value');
CREATE USER MAPPING FOR CURRENT_USER SERVER loopback; CREATE USER MAPPING FOR CURRENT_USER SERVER loopback;
@ -233,12 +226,7 @@ ALTER FOREIGN TABLE ft2 ALTER COLUMN c1 OPTIONS (column_name 'C 1');
SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should work SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should work
ALTER SERVER loopback OPTIONS (SET dbname 'no such database'); ALTER SERVER loopback OPTIONS (SET dbname 'no such database');
SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should fail SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should fail
DO $d$ ALTER SERVER loopback OPTIONS (SET dbname :'current_database');
BEGIN
EXECUTE $$ALTER SERVER loopback
OPTIONS (SET dbname '$$||current_database()||$$')$$;
END;
$d$;
SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should work again SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should work again
-- Test that alteration of user mapping options causes reconnection -- Test that alteration of user mapping options causes reconnection
@ -3375,14 +3363,8 @@ SET ROLE regress_nosuper;
SHOW is_superuser; SHOW is_superuser;
-- This will be OK, we can create the FDW -- This will be OK, we can create the FDW
DO $d$ CREATE SERVER loopback_nopw FOREIGN DATA WRAPPER postgres_fdw
BEGIN OPTIONS (dbname :'current_database', port :'current_port');
EXECUTE $$CREATE SERVER loopback_nopw FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname '$$||current_database()||$$',
port '$$||current_setting('port')||$$'
)$$;
END;
$d$;
-- But creation of user mappings for non-superusers should fail -- But creation of user mappings for non-superusers should fail
CREATE USER MAPPING FOR public SERVER loopback_nopw; CREATE USER MAPPING FOR public SERVER loopback_nopw;

Loading…
Cancel
Save