From fe186bda78c4a0ccbb691489c744e6317f648434 Mon Sep 17 00:00:00 2001 From: Fujii Masao Date: Mon, 3 Mar 2025 08:51:30 +0900 Subject: 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 Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/CAPhYifF25q5xUQWXETfKwhc0YVa_6+tfG9Kw4bCvCjpCWxYs2A@mail.gmail.com --- contrib/postgres_fdw/connection.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'contrib/postgres_fdw/connection.c') diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c index 8a8d3b4481f..8ef9702c05c 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. @@ -2128,13 +2128,15 @@ pgfdw_finish_abort_cleanup(List *pending_entries, List *cancel_requested, * * For API version 1.2 and later, this function takes an input parameter * to check a connection status and returns the following - * additional values along with the three values from version 1.1: + * additional values along with the four values from version 1.1: * * - user_name - the local user name of the active connection. In case the * user mapping is dropped but the connection is still active, then the * 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 - process ID of the remote backend, on the foreign + * server, handling the connection. * * No records are returned when there are no cached connections at all. */ @@ -2273,6 +2275,9 @@ postgres_fdw_get_connections_internal(FunctionCallInfo fcinfo, values[i++] = BoolGetDatum(pgfdw_conn_check(entry->conn) != 0); else nulls[i++] = true; + + /* Return process ID of remote backend */ + values[i++] = Int32GetDatum(PQbackendPID(entry->conn)); } tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); -- cgit v1.2.3