diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c index 8a8d3b4481..6e5396ed89 100644 --- a/contrib/postgres_fdw/connection.c +++ b/contrib/postgres_fdw/connection.c @@ -2111,8 +2111,8 @@ pgfdw_finish_abort_cleanup(List *pending_entries, List *cancel_requested, /* Number of output arguments (columns) for various API versions */ #define POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_1 2 -#define POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_2 5 -#define POSTGRES_FDW_GET_CONNECTIONS_COLS 5 /* maximum of above */ +#define POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_2 6 +#define POSTGRES_FDW_GET_CONNECTIONS_COLS 6 /* maximum of above */ /* * Internal function used by postgres_fdw_get_connections variants. @@ -2135,6 +2135,7 @@ pgfdw_finish_abort_cleanup(List *pending_entries, List *cancel_requested, * user name will be NULL in the output. * - used_in_xact - true if the connection is used in the current transaction. * - closed - true if the connection is closed. + * - remote_backend_pid - return remote server backend pid * * No records are returned when there are no cached connections at all. */ @@ -2261,6 +2262,8 @@ postgres_fdw_get_connections_internal(FunctionCallInfo fcinfo, if (api_version >= PGFDW_V1_2) { bool check_conn = PG_GETARG_BOOL(0); + int remote_backend_pid; + int is_conn_closed = 0; /* Is this connection used in the current transaction? */ values[i++] = BoolGetDatum(entry->xact_depth > 0); @@ -2270,7 +2273,20 @@ postgres_fdw_get_connections_internal(FunctionCallInfo fcinfo, * whether the connection is closed. Otherwise, return NULL. */ if (check_conn && pgfdw_conn_checkable()) - values[i++] = BoolGetDatum(pgfdw_conn_check(entry->conn) != 0); + { + is_conn_closed = pgfdw_conn_check(entry->conn); + values[i++] = BoolGetDatum( is_conn_closed != 0); + } + else + nulls[i++] = true; + + /* + * If a connection status is not closed and remote backend + * ID is valid, return remote backend ID. Otherwise, return NULL. + */ + remote_backend_pid = PQbackendPID(entry->conn); + if ((is_conn_closed != 1) && (remote_backend_pid != 0)) + values[i++] = remote_backend_pid; else nulls[i++] = true; } diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out index daa3b1d7a6..eb54aacd8c 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -10589,13 +10589,15 @@ 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 and user name -- for loopback3 should be NULL because both server and user mapping were --- dropped. -SELECT server_name, user_name = CURRENT_USER as "user_name = CURRENT_USER", valid, used_in_xact, closed +-- dropped. remote_backend_pid will continue to return available as it fetch remote +-- server backend pid from cached connections. +SELECT server_name, user_name = CURRENT_USER as "user_name = CURRENT_USER", valid, used_in_xact, closed, +CASE WHEN remote_backend_pid IS NOT NULL then 'available' ELSE 'not available' END AS remote_backend_pid FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | user_name = CURRENT_USER | valid | used_in_xact | closed --------------+--------------------------+-------+--------------+-------- - loopback | t | f | t | - | | f | t | + server_name | user_name = CURRENT_USER | valid | used_in_xact | closed | remote_backend_pid +-------------+--------------------------+-------+--------------+--------+-------------------- + loopback | t | f | t | | available + | | f | t | | available (2 rows) -- The invalid connections get closed in pgfdw_xact_callback during commit. @@ -12436,11 +12438,12 @@ SELECT 1 FROM ft1 LIMIT 1; -- Since the remote server is still connected, "closed" should be FALSE, -- or NULL if the connection status check is not available. -SELECT CASE WHEN closed IS NOT true THEN 1 ELSE 0 END +SELECT server_name, CASE WHEN closed IS false THEN 'false' ELSE 'true' END AS remote_conn_closed, +CASE WHEN remote_backend_pid IS NOT NULL then 'available' ELSE 'not available' END AS remote_backend_pid FROM postgres_fdw_get_connections(true); - case ------- - 1 + server_name | remote_conn_closed | remote_backend_pid +-------------+--------------------+-------------------- + loopback | false | available (1 row) -- After terminating the remote backend, since the connection is closed, @@ -12450,11 +12453,12 @@ DO $$ BEGIN PERFORM pg_terminate_backend(pid, 180000) FROM pg_stat_activity WHERE application_name = 'fdw_conn_check'; END $$; -SELECT CASE WHEN closed IS NOT false THEN 1 ELSE 0 END +SELECT server_name, CASE WHEN closed IS false THEN 'false' ELSE 'true' END AS remote_conn_closed, +CASE WHEN remote_backend_pid IS NOT NULL THEN 'available' ELSE 'not available' END AS remote_backend_pid FROM postgres_fdw_get_connections(true); - case ------- - 1 + server_name | remote_conn_closed | remote_backend_pid +-------------+--------------------+-------------------- + loopback | true | not available (1 row) -- Clean up diff --git a/contrib/postgres_fdw/postgres_fdw--1.1--1.2.sql b/contrib/postgres_fdw/postgres_fdw--1.1--1.2.sql index 81aad4fcda..98b4c69dbb 100644 --- a/contrib/postgres_fdw/postgres_fdw--1.1--1.2.sql +++ b/contrib/postgres_fdw/postgres_fdw--1.1--1.2.sql @@ -12,7 +12,7 @@ DROP FUNCTION postgres_fdw_get_connections (); CREATE FUNCTION postgres_fdw_get_connections ( IN check_conn boolean DEFAULT false, OUT server_name text, OUT user_name text, OUT valid boolean, OUT used_in_xact boolean, - OUT closed boolean) + OUT closed boolean, OUT remote_backend_pid INTEGER) RETURNS SETOF record AS 'MODULE_PATHNAME', 'postgres_fdw_get_connections_1_2' LANGUAGE C STRICT PARALLEL RESTRICTED; diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql index 1598d9e086..483e9a3a64 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -3410,8 +3410,10 @@ DROP SERVER loopback3 CASCADE; -- List all the existing cached connections. loopback and loopback3 -- should be output as invalid connections. Also the server name and user name -- for loopback3 should be NULL because both server and user mapping were --- dropped. -SELECT server_name, user_name = CURRENT_USER as "user_name = CURRENT_USER", valid, used_in_xact, closed +-- dropped. remote_backend_pid will continue to return available as it fetch remote +-- server backend pid from cached connections. +SELECT server_name, user_name = CURRENT_USER as "user_name = CURRENT_USER", valid, used_in_xact, closed, +CASE WHEN remote_backend_pid IS NOT NULL then 'available' ELSE 'not available' END AS remote_backend_pid FROM postgres_fdw_get_connections() ORDER BY 1; -- The invalid connections get closed in pgfdw_xact_callback during commit. COMMIT; @@ -4288,7 +4290,8 @@ SELECT 1 FROM ft1 LIMIT 1; -- Since the remote server is still connected, "closed" should be FALSE, -- or NULL if the connection status check is not available. -SELECT CASE WHEN closed IS NOT true THEN 1 ELSE 0 END +SELECT server_name, CASE WHEN closed IS false THEN 'false' ELSE 'true' END AS remote_conn_closed, +CASE WHEN remote_backend_pid IS NOT NULL then 'available' ELSE 'not available' END AS remote_backend_pid FROM postgres_fdw_get_connections(true); -- After terminating the remote backend, since the connection is closed, @@ -4298,7 +4301,8 @@ DO $$ BEGIN PERFORM pg_terminate_backend(pid, 180000) FROM pg_stat_activity WHERE application_name = 'fdw_conn_check'; END $$; -SELECT CASE WHEN closed IS NOT false THEN 1 ELSE 0 END +SELECT server_name, CASE WHEN closed IS false THEN 'false' ELSE 'true' END AS remote_conn_closed, +CASE WHEN remote_backend_pid IS NOT NULL THEN 'available' ELSE 'not available' END AS remote_backend_pid FROM postgres_fdw_get_connections(true); -- Clean up