From 8e249f01bb50e5db303b7d14e6a3f3409001bee6 Mon Sep 17 00:00:00 2001 From: Bharath Rupireddy Date: Fri, 29 Jan 2021 15:27:04 +0530 Subject: [PATCH v1] Don't return valid state in postgres_fdw_get_connections Don't show the the cached connection's valid state in postgres_fdw_get_connections output, as it can be inconsistent due to internal events such as invalidations. Also, make tests added by 708d165 and 411ae64 so that they return consistent results even when sys cache invalidations occur too frequently and the cached connections get invalidated in pgfdw_inval_callback. --- contrib/postgres_fdw/connection.c | 26 +- .../postgres_fdw/expected/postgres_fdw.out | 223 ++++++------------ .../postgres_fdw/postgres_fdw--1.0--1.1.sql | 7 +- contrib/postgres_fdw/sql/postgres_fdw.sql | 89 +++---- doc/src/sgml/postgres-fdw.sgml | 22 +- 5 files changed, 123 insertions(+), 244 deletions(-) diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c index ee0b4acf0b..436f9bfc4f 100644 --- a/contrib/postgres_fdw/connection.c +++ b/contrib/postgres_fdw/connection.c @@ -1350,19 +1350,14 @@ exit: ; * List active foreign server connections. * * This function takes no input parameter and returns setof record made of - * following values: - * - server_name - server name of active connection. In case the foreign server - * is dropped but still the connection is active, then the server name will - * be NULL in output. - * - valid - true/false representing whether the connection is valid or not. - * Note that the connections can get invalidated in pgfdw_inval_callback. + * server_name i.e. server name of active connection. * * No records are returned when there are no cached connections at all. */ Datum postgres_fdw_get_connections(PG_FUNCTION_ARGS) { -#define POSTGRES_FDW_GET_CONNECTIONS_COLS 2 +#define POSTGRES_FDW_GET_CONNECTIONS_COLS 1 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; TupleDesc tupdesc; Tuplestorestate *tupstore; @@ -1430,9 +1425,11 @@ postgres_fdw_get_connections(PG_FUNCTION_ARGS) * * Even though the server is dropped in the current transaction, the * cache can still have associated active connection entry, say we - * call such connections dangling. Since we can not fetch the server - * name from system catalogs for dangling connections, instead we show - * NULL value for server name in output. + * call such connections dangling. We can not fetch the server name + * from system catalogs for dangling connections and the corresponding + * cache entries would have been invalidated in pgfdw_inval_callback at + * the end of drop server command. Hence the dangling connections are + * not worth showing in the output, we skip them. * * We could have done better by storing the server name in the cache * entry instead of server oid so that it could be used in the output. @@ -1457,14 +1454,15 @@ postgres_fdw_get_connections(PG_FUNCTION_ARGS) */ Assert(entry->conn && entry->xact_depth > 0 && entry->invalidated); - /* Show null, if no server name was found */ - nulls[0] = true; + /* + * It doesn't make sense to show this entry in the output with a + * NULL server_name as it will be closed at the xact end. + */ + continue; } else values[0] = CStringGetTextDatum(server->servername); - values[1] = BoolGetDatum(!entry->invalidated); - tuplestore_putvalues(tupstore, tupdesc, values, nulls); } diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out index 07e06e5bf7..549649e7c0 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -17,10 +17,6 @@ DO $d$ OPTIONS (dbname '$$||current_database()||$$', port '$$||current_setting('port')||$$' )$$; - EXECUTE $$CREATE SERVER loopback4 FOREIGN DATA WRAPPER postgres_fdw - OPTIONS (dbname '$$||current_database()||$$', - port '$$||current_setting('port')||$$' - )$$; END; $d$; CREATE USER MAPPING FOR public SERVER testserver1 @@ -28,7 +24,6 @@ CREATE USER MAPPING FOR public SERVER testserver1 CREATE USER MAPPING FOR CURRENT_USER SERVER loopback; CREATE USER MAPPING FOR CURRENT_USER SERVER loopback2; CREATE USER MAPPING FOR public SERVER loopback3; -CREATE USER MAPPING FOR public SERVER loopback4; -- =================================================================== -- create objects used through FDW loopback server -- =================================================================== @@ -144,11 +139,6 @@ CREATE FOREIGN TABLE ft7 ( c2 int NOT NULL, c3 text ) SERVER loopback3 OPTIONS (schema_name 'S 1', table_name 'T 4'); -CREATE FOREIGN TABLE ft8 ( - c1 int NOT NULL, - c2 int NOT NULL, - c3 text -) SERVER loopback4 OPTIONS (schema_name 'S 1', table_name 'T 4'); -- =================================================================== -- tests for validator -- =================================================================== @@ -220,8 +210,7 @@ ALTER FOREIGN TABLE ft2 ALTER COLUMN c1 OPTIONS (column_name 'C 1'); public | ft5 | loopback | (schema_name 'S 1', table_name 'T 4') | public | ft6 | loopback2 | (schema_name 'S 1', table_name 'T 4') | public | ft7 | loopback3 | (schema_name 'S 1', table_name 'T 4') | - public | ft8 | loopback4 | (schema_name 'S 1', table_name 'T 4') | -(7 rows) +(6 rows) -- Test that alteration of server options causes reconnection -- Remote's errors might be non-English, so hide them to ensure stable results @@ -9066,15 +9055,21 @@ DROP PROCEDURE terminate_backend_and_wait(text); -- ============================================================================= -- test connection invalidation cases and postgres_fdw_get_connections function -- ============================================================================= --- This test case is for closing the connection in pgfdw_xact_callback -BEGIN; --- List all the existing cached connections. Only loopback2 should be output. -SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid --------------+------- - loopback2 | t +-- Let's ensure to close all the existing cached connections. +SELECT 1 FROM postgres_fdw_disconnect_all(); + ?column? +---------- + 1 (1 row) +-- No cached connections, so no records should be output. +SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; + server_name +------------- +(0 rows) + +-- This test case is for closing the connection in pgfdw_xact_callback +BEGIN; -- Connection xact depth becomes 1 i.e. the connection is in midst of the xact. SELECT 1 FROM ft1 LIMIT 1; ?column? @@ -9088,56 +9083,44 @@ SELECT 1 FROM ft7 LIMIT 1; 1 (1 row) --- List all the existing cached connections. loopback and loopback3 --- also should be output as valid connections. +-- List all the existing cached connections. loopback and loopback3 should be +-- output. SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid --------------+------- - loopback | t - loopback2 | t - loopback3 | t -(3 rows) + server_name +------------- + loopback + loopback3 +(2 rows) --- Connection is not closed at the end of the alter statement in --- pgfdw_inval_callback. That's because the connection is in midst of this --- xact, it is just marked as invalid. +-- Connections are not closed at the end of the alter statement in +-- pgfdw_inval_callback. That's because the connections are in midst of this +-- xact, they are just marked as invalid. ALTER SERVER loopback OPTIONS (ADD use_remote_estimate 'off'); DROP SERVER loopback3 CASCADE; NOTICE: drop cascades to 2 other objects DETAIL: drop cascades to user mapping for public on server loopback3 drop cascades to foreign table ft7 --- List all the existing cached connections. loopback and loopback3 --- should be output as invalid connections. Also the server name for --- loopback3 should be NULL because the server was dropped. +-- List all the existing cached connections. loopback should be output. +-- loopback3 is not shown in the output because the server is dropped. SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid --------------+------- - loopback | f - loopback2 | t - | f -(3 rows) + server_name +------------- + loopback +(1 row) --- The invalid connection gets closed in pgfdw_xact_callback during commit. +-- The above invalid connections get closed in pgfdw_xact_callback during commit. COMMIT; --- List all the existing cached connections. loopback and loopback3 --- should not be output because they should be closed at the end of --- the above transaction. +-- All cached connections were closed while committing above xact, so no +-- records should be output. SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid --------------+------- - loopback2 | t -(1 row) + server_name +------------- +(0 rows) -- ======================================================================= -- test postgres_fdw_disconnect and postgres_fdw_disconnect_all functions -- ======================================================================= --- Return true as all cached connections are closed. -SELECT postgres_fdw_disconnect_all(); - postgres_fdw_disconnect_all ------------------------------ - t -(1 row) - +BEGIN; -- Ensure to cache loopback connection. SELECT 1 FROM ft1 LIMIT 1; ?column? @@ -9145,7 +9128,6 @@ SELECT 1 FROM ft1 LIMIT 1; 1 (1 row) -BEGIN; -- Ensure to cache loopback2 connection. SELECT 1 FROM ft6 LIMIT 1; ?column? @@ -9156,65 +9138,30 @@ SELECT 1 FROM ft6 LIMIT 1; -- List all the existing cached connections. loopback and loopback2 should be -- output. SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid --------------+------- - loopback | t - loopback2 | t + server_name +------------- + loopback + loopback2 (2 rows) --- Issue a warning and return false as loopback2 connection is still in use and +-- Issue a warning and return false as loopback connection is still in use and -- can not be closed. -SELECT postgres_fdw_disconnect('loopback2'); -WARNING: cannot close connection for server "loopback2" because it is still in use +SELECT postgres_fdw_disconnect('loopback'); +WARNING: cannot close connection for server "loopback" because it is still in use postgres_fdw_disconnect ------------------------- f (1 row) --- Close loopback connection, return true and issue a warning as loopback2 --- connection is still in use and can not be closed. -SELECT postgres_fdw_disconnect_all(); -WARNING: cannot close connection for server "loopback2" because it is still in use - postgres_fdw_disconnect_all ------------------------------ - t -(1 row) - --- List all the existing cached connections. loopback2 should be output. -SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid --------------+------- - loopback2 | t -(1 row) - --- Ensure to cache loopback connection. -SELECT 1 FROM ft1 LIMIT 1; - ?column? ----------- - 1 -(1 row) - --- Ensure to cache loopback4 connection. -SELECT 1 FROM ft8 LIMIT 1; - ?column? ----------- - 1 -(1 row) - --- List all the existing cached connections. loopback, loopback2, loopback4 --- should be output. +-- List all the existing cached connections. loopback and loopback2 should be +-- output. SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid --------------+------- - loopback | t - loopback2 | t - loopback4 | t -(3 rows) + server_name +------------- + loopback + loopback2 +(2 rows) -DROP SERVER loopback4 CASCADE; -NOTICE: drop cascades to 2 other objects -DETAIL: drop cascades to user mapping for public on server loopback4 -drop cascades to foreign table ft8 -- Return false as connections are still in use, warnings are issued. -- But disable warnings temporarily because the order of them is not stable. SET client_min_messages = 'ERROR'; @@ -9226,42 +9173,17 @@ SELECT postgres_fdw_disconnect_all(); RESET client_min_messages; COMMIT; --- Close loopback2 connection and return true. -SELECT postgres_fdw_disconnect('loopback2'); - postgres_fdw_disconnect -------------------------- - t -(1 row) - --- List all the existing cached connections. loopback should be output. -SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid --------------+------- - loopback | t -(1 row) - --- Return false as loopback2 connectin is closed already. -SELECT postgres_fdw_disconnect('loopback2'); - postgres_fdw_disconnect -------------------------- - f -(1 row) - --- Return an error as there is no foreign server with given name. -SELECT postgres_fdw_disconnect('unknownserver'); -ERROR: server "unknownserver" does not exist --- Close loopback connection and return true. -SELECT postgres_fdw_disconnect_all(); - postgres_fdw_disconnect_all ------------------------------ - t +-- Let's ensure to close all the existing cached connections. +SELECT 1 FROM postgres_fdw_disconnect_all(); + ?column? +---------- + 1 (1 row) --- List all the existing cached connections. No connection exists, so NULL --- should be output. +-- No cached connections, so no records should be output. SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid --------------+------- + server_name +------------- (0 rows) -- ============================================================================= @@ -9271,6 +9193,7 @@ CREATE ROLE regress_multi_conn_user1 SUPERUSER; CREATE ROLE regress_multi_conn_user2 SUPERUSER; CREATE USER MAPPING FOR regress_multi_conn_user1 SERVER loopback; CREATE USER MAPPING FOR regress_multi_conn_user2 SERVER loopback; +BEGIN; -- Will cache loopback connection with user mapping for regress_multi_conn_user1 SET ROLE regress_multi_conn_user1; SELECT 1 FROM ft1 LIMIT 1; @@ -9291,24 +9214,24 @@ SELECT 1 FROM ft1 LIMIT 1; RESET ROLE; -- Should output two connections for loopback server SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid --------------+------- - loopback | t - loopback | t + server_name +------------- + loopback + loopback (2 rows) --- Close loopback connections and return true. -SELECT postgres_fdw_disconnect('loopback'); - postgres_fdw_disconnect -------------------------- - t +COMMIT; +-- Let's ensure to close all the existing cached connections. +SELECT 1 FROM postgres_fdw_disconnect_all(); + ?column? +---------- + 1 (1 row) --- List all the existing cached connections. No connection exists, so NULL --- should be output. +-- No cached connections, so no records should be output. SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid --------------+------- + server_name +------------- (0 rows) -- Clean up diff --git a/contrib/postgres_fdw/postgres_fdw--1.0--1.1.sql b/contrib/postgres_fdw/postgres_fdw--1.0--1.1.sql index ed4ca378d4..58b8e54147 100644 --- a/contrib/postgres_fdw/postgres_fdw--1.0--1.1.sql +++ b/contrib/postgres_fdw/postgres_fdw--1.0--1.1.sql @@ -3,9 +3,10 @@ -- complain if script is sourced in psql, rather than via ALTER EXTENSION \echo Use "ALTER EXTENSION postgres_fdw UPDATE TO '1.1'" to load this file. \quit -CREATE FUNCTION postgres_fdw_get_connections (OUT server_name text, - OUT valid boolean) -RETURNS SETOF record +CREATE TYPE postgres_fdw_get_connections_result_type AS (server_name text); + +CREATE FUNCTION postgres_fdw_get_connections () +RETURNS SETOF postgres_fdw_get_connections_result_type AS 'MODULE_PATHNAME' LANGUAGE C STRICT PARALLEL RESTRICTED; diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql index 647192cf6a..6ef965d4bd 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -19,10 +19,6 @@ DO $d$ OPTIONS (dbname '$$||current_database()||$$', port '$$||current_setting('port')||$$' )$$; - EXECUTE $$CREATE SERVER loopback4 FOREIGN DATA WRAPPER postgres_fdw - OPTIONS (dbname '$$||current_database()||$$', - port '$$||current_setting('port')||$$' - )$$; END; $d$; @@ -31,7 +27,6 @@ CREATE USER MAPPING FOR public SERVER testserver1 CREATE USER MAPPING FOR CURRENT_USER SERVER loopback; CREATE USER MAPPING FOR CURRENT_USER SERVER loopback2; CREATE USER MAPPING FOR public SERVER loopback3; -CREATE USER MAPPING FOR public SERVER loopback4; -- =================================================================== -- create objects used through FDW loopback server @@ -158,12 +153,6 @@ CREATE FOREIGN TABLE ft7 ( c3 text ) SERVER loopback3 OPTIONS (schema_name 'S 1', table_name 'T 4'); -CREATE FOREIGN TABLE ft8 ( - c1 int NOT NULL, - c2 int NOT NULL, - c3 text -) SERVER loopback4 OPTIONS (schema_name 'S 1', table_name 'T 4'); - -- =================================================================== -- tests for validator -- =================================================================== @@ -2723,79 +2712,58 @@ DROP PROCEDURE terminate_backend_and_wait(text); -- ============================================================================= -- test connection invalidation cases and postgres_fdw_get_connections function -- ============================================================================= +-- Let's ensure to close all the existing cached connections. +SELECT 1 FROM postgres_fdw_disconnect_all(); +-- No cached connections, so no records should be output. +SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; -- This test case is for closing the connection in pgfdw_xact_callback BEGIN; --- List all the existing cached connections. Only loopback2 should be output. -SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; -- Connection xact depth becomes 1 i.e. the connection is in midst of the xact. SELECT 1 FROM ft1 LIMIT 1; SELECT 1 FROM ft7 LIMIT 1; --- List all the existing cached connections. loopback and loopback3 --- also should be output as valid connections. +-- List all the existing cached connections. loopback and loopback3 should be +-- output. SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; --- Connection is not closed at the end of the alter statement in --- pgfdw_inval_callback. That's because the connection is in midst of this --- xact, it is just marked as invalid. +-- Connections are not closed at the end of the alter statement in +-- pgfdw_inval_callback. That's because the connections are in midst of this +-- xact, they are just marked as invalid. ALTER SERVER loopback OPTIONS (ADD use_remote_estimate 'off'); DROP SERVER loopback3 CASCADE; --- List all the existing cached connections. loopback and loopback3 --- should be output as invalid connections. Also the server name for --- loopback3 should be NULL because the server was dropped. +-- List all the existing cached connections. loopback should be output. +-- loopback3 is not shown in the output because the server is dropped. SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; --- The invalid connection gets closed in pgfdw_xact_callback during commit. +-- The above invalid connections get closed in pgfdw_xact_callback during commit. COMMIT; --- List all the existing cached connections. loopback and loopback3 --- should not be output because they should be closed at the end of --- the above transaction. +-- All cached connections were closed while committing above xact, so no +-- records should be output. SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; -- ======================================================================= -- test postgres_fdw_disconnect and postgres_fdw_disconnect_all functions -- ======================================================================= --- Return true as all cached connections are closed. -SELECT postgres_fdw_disconnect_all(); +BEGIN; -- Ensure to cache loopback connection. SELECT 1 FROM ft1 LIMIT 1; -BEGIN; -- Ensure to cache loopback2 connection. SELECT 1 FROM ft6 LIMIT 1; -- List all the existing cached connections. loopback and loopback2 should be -- output. SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; --- Issue a warning and return false as loopback2 connection is still in use and +-- Issue a warning and return false as loopback connection is still in use and -- can not be closed. -SELECT postgres_fdw_disconnect('loopback2'); --- Close loopback connection, return true and issue a warning as loopback2 --- connection is still in use and can not be closed. -SELECT postgres_fdw_disconnect_all(); --- List all the existing cached connections. loopback2 should be output. -SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; --- Ensure to cache loopback connection. -SELECT 1 FROM ft1 LIMIT 1; --- Ensure to cache loopback4 connection. -SELECT 1 FROM ft8 LIMIT 1; --- List all the existing cached connections. loopback, loopback2, loopback4 --- should be output. +SELECT postgres_fdw_disconnect('loopback'); +-- List all the existing cached connections. loopback and loopback2 should be +-- output. SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; -DROP SERVER loopback4 CASCADE; -- Return false as connections are still in use, warnings are issued. -- But disable warnings temporarily because the order of them is not stable. SET client_min_messages = 'ERROR'; SELECT postgres_fdw_disconnect_all(); RESET client_min_messages; COMMIT; --- Close loopback2 connection and return true. -SELECT postgres_fdw_disconnect('loopback2'); --- List all the existing cached connections. loopback should be output. -SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; --- Return false as loopback2 connectin is closed already. -SELECT postgres_fdw_disconnect('loopback2'); --- Return an error as there is no foreign server with given name. -SELECT postgres_fdw_disconnect('unknownserver'); --- Close loopback connection and return true. -SELECT postgres_fdw_disconnect_all(); --- List all the existing cached connections. No connection exists, so NULL --- should be output. +-- Let's ensure to close all the existing cached connections. +SELECT 1 FROM postgres_fdw_disconnect_all(); +-- No cached connections, so no records should be output. SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; -- ============================================================================= @@ -2806,24 +2774,21 @@ CREATE ROLE regress_multi_conn_user2 SUPERUSER; CREATE USER MAPPING FOR regress_multi_conn_user1 SERVER loopback; CREATE USER MAPPING FOR regress_multi_conn_user2 SERVER loopback; +BEGIN; -- Will cache loopback connection with user mapping for regress_multi_conn_user1 SET ROLE regress_multi_conn_user1; SELECT 1 FROM ft1 LIMIT 1; RESET ROLE; - -- Will cache loopback connection with user mapping for regress_multi_conn_user2 SET ROLE regress_multi_conn_user2; SELECT 1 FROM ft1 LIMIT 1; RESET ROLE; - -- Should output two connections for loopback server SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - --- Close loopback connections and return true. -SELECT postgres_fdw_disconnect('loopback'); - --- List all the existing cached connections. No connection exists, so NULL --- should be output. +COMMIT; +-- Let's ensure to close all the existing cached connections. +SELECT 1 FROM postgres_fdw_disconnect_all(); +-- No cached connections, so no records should be output. SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; -- Clean up diff --git a/doc/src/sgml/postgres-fdw.sgml b/doc/src/sgml/postgres-fdw.sgml index 8d6abd4c54..82e4f13995 100644 --- a/doc/src/sgml/postgres-fdw.sgml +++ b/doc/src/sgml/postgres-fdw.sgml @@ -497,27 +497,19 @@ OPTIONS (ADD password_required 'false'); - postgres_fdw_get_connections(OUT server_name text, OUT valid boolean) returns setof record + postgres_fdw_get_connections(OUT server_name text) returns setof record This function returns the foreign server names of all the open connections that postgres_fdw established from - the local session to the foreign servers. It also returns whether - each connection is valid or not. false is returned - if the foreign server connection is used in the current local - transaction but its foreign server or user mapping is changed or - dropped (Note that server name of an invalid connection will be - NULL if the server is dropped), - and then such invalid connection will be closed at - the end of that transaction. true is returned - otherwise. If there are no open connections, no record is returned. - Example usage of the function: + the local session to the foreign servers. If there are no open + connections, no record is returned. Example usage of the function: postgres=# SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid --------------+------- - loopback1 | t - loopback2 | f + server_name +------------- + loopback1 + loopback2 -- 2.25.1