diff options
author | Fujii Masao <fujii@postgresql.org> | 2025-03-03 08:51:30 +0900 |
---|---|---|
committer | Fujii Masao <fujii@postgresql.org> | 2025-03-03 08:51:30 +0900 |
commit | fe186bda78c4a0ccbb691489c744e6317f648434 (patch) | |
tree | 95083aa57d720f90a49fc19c6680a55ef5fc9de8 /contrib/postgres_fdw/sql/postgres_fdw.sql | |
parent | 15a79c73111f0c9738ee81b796f7de5bfeb3aedc (diff) | |
download | postgresql-fe186bda78c4a0ccbb691489c744e6317f648434.tar.gz postgresql-fe186bda78c4a0ccbb691489c744e6317f648434.zip |
postgres_fdw: Extend postgres_fdw_get_connections to return remote backend PID.
This commit adds a new "remote_backend_pid" output column to
the postgres_fdw_get_connections function. It returns the process ID of
the remote backend, on the foreign server, handling the connection.
This enhancement is useful for troubleshooting, monitoring, and reporting.
For example, if a connection is unexpectedly closed by the foreign server,
the remote backend's PID can help diagnose the cause.
No extension version bump is needed, as commit c297a47c5f already
handled it for v18~.
Author: Sagar Dilip Shedge <sagar.shedge92@gmail.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/CAPhYifF25q5xUQWXETfKwhc0YVa_6+tfG9Kw4bCvCjpCWxYs2A@mail.gmail.com
Diffstat (limited to 'contrib/postgres_fdw/sql/postgres_fdw.sql')
-rw-r--r-- | contrib/postgres_fdw/sql/postgres_fdw.sql | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql index 1598d9e0862..d45e9f8ab52 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -3410,9 +3410,14 @@ 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 -FROM postgres_fdw_get_connections() ORDER BY 1; +-- dropped. In this test, the PIDs of remote backends can be gathered from +-- pg_stat_activity, and remote_backend_pid should match one of those PIDs. +SELECT server_name, user_name = CURRENT_USER as "user_name = CURRENT_USER", + valid, used_in_xact, closed, + remote_backend_pid = ANY(SELECT pid FROM pg_stat_activity + WHERE backend_type = 'client backend' AND pid <> pg_backend_pid()) + as remote_backend_pid + FROM postgres_fdw_get_connections() ORDER BY 1; -- The invalid connections get closed in pgfdw_xact_callback during commit. COMMIT; -- All cached connections were closed while committing above xact, so no @@ -4288,17 +4293,26 @@ 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 +-- In this test, the remote backend handling this connection should have +-- application_name set to "fdw_conn_check", so remote_backend_pid should +-- match the PID from the pg_stat_activity entry with that application_name. +SELECT server_name, + CASE WHEN closed IS NOT true THEN false ELSE true END AS closed, + remote_backend_pid = (SELECT pid FROM pg_stat_activity + WHERE application_name = 'fdw_conn_check') AS remote_backend_pid FROM postgres_fdw_get_connections(true); -- After terminating the remote backend, since the connection is closed, -- "closed" should be TRUE, or NULL if the connection status check --- is not available. +-- is not available. Despite the termination, remote_backend_pid should +-- still show the non-zero PID of the terminated remote backend. 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 NOT false THEN true ELSE false END AS closed, + remote_backend_pid <> 0 AS remote_backend_pid FROM postgres_fdw_get_connections(true); -- Clean up |