From 175d82926ce68a164fd45c0cf9cd68c03342ce0c Mon Sep 17 00:00:00 2001 From: Hayato Kuroda Date: Thu, 18 Jul 2024 10:11:03 +0000 Subject: [PATCH v42 2/2] Extend postgres_fdw_get_connections to return user name --- contrib/postgres_fdw/connection.c | 54 +++++++++++++++---- .../postgres_fdw/expected/postgres_fdw.out | 15 +++--- .../postgres_fdw/postgres_fdw--1.1--1.2.sql | 3 +- contrib/postgres_fdw/sql/postgres_fdw.sql | 7 +-- doc/src/sgml/postgres-fdw.sgml | 12 ++--- src/backend/foreign/foreign.c | 34 ++++++++++++ src/include/foreign/foreign.h | 1 + 7 files changed, 100 insertions(+), 26 deletions(-) diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c index 667d8d4a82..9f5dd9c945 100644 --- a/contrib/postgres_fdw/connection.c +++ b/contrib/postgres_fdw/connection.c @@ -2007,7 +2007,10 @@ pgfdw_finish_abort_cleanup(List *pending_entries, List *cancel_requested, * * If the version is 1.2 and later, this function takes an input parameter, * which indicates the need for a health check. Regarding the returned record, - * this returns two additional values: + * this returns three additional values: + * - user_name - the user mapping name of the active connection. In case the + * foreign server is dropped but the connection is still active, then the + * server name will be NULL in the output. * - used_in_xact - indicates whether the server has been used in a transaction * or not. * - closed - true if the local session seems to be disconnected from other @@ -2019,7 +2022,7 @@ static Datum postgres_fdw_get_connections_internal(FunctionCallInfo fcinfo, enum pgfdwVersion api_version) { -#define POSTGRES_FDW_GET_CONNECTIONS_COLS 4 +#define POSTGRES_FDW_GET_CONNECTIONS_COLS 5 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; HASH_SEQ_STATUS scan; ConnCacheEntry *entry; @@ -2036,6 +2039,7 @@ postgres_fdw_get_connections_internal(FunctionCallInfo fcinfo, ForeignServer *server; Datum values[POSTGRES_FDW_GET_CONNECTIONS_COLS] = {0}; bool nulls[POSTGRES_FDW_GET_CONNECTIONS_COLS] = {0}; + int i = 0; /* We only look for open remote connections */ if (!entry->conn) @@ -2080,20 +2084,52 @@ postgres_fdw_get_connections_internal(FunctionCallInfo fcinfo, Assert(entry->conn && entry->xact_depth > 0 && entry->invalidated); /* Show null, if no server name was found */ - nulls[0] = true; + nulls[i++] = true; } else - values[0] = CStringGetTextDatum(server->servername); + values[i++] = CStringGetTextDatum(server->servername); - values[1] = BoolGetDatum(!entry->invalidated); + /* + * Add user_name to returned tuples if the version is 1.2 or later. + * This helps distinguish which connection is closed by the + * remote peer. + */ + if (api_version == PGFDW_V1_2) + { + UserMapping *user = GetUserMappingFromOid(entry->key, true); - /* Add additional attributes if the version is 1.2 or later */ + /* + * Just like in the foreign server case, user mappings can also + * be dropped in the current explicit transaction. Therefore, the + * same check as in the server case is required. + */ + if (!user) + { + /* + * If we reach here, this entry must have been invalidated in + * pgfdw_inval_callback, same as in the server case. + */ + Assert(entry->conn && entry->xact_depth > 0 && + entry->invalidated); + + nulls[i++] = true; + } + else + values[i++] = CStringGetTextDatum(MappingUserName(user->userid)); + } + + values[i++] = BoolGetDatum(!entry->invalidated); + + /* + * Add "used_in_xact" and "closed" columns if the version is 1.2 or + * later. + */ if (api_version == PGFDW_V1_2) { bool require_verify = PG_GETARG_BOOL(0); /* Has this server been used in the transaction? */ - values[2] = BoolGetDatum(entry->xact_depth > 0); + values[i++] = BoolGetDatum(entry->xact_depth > 0); /* * If requested and possible, check the status of remote connection @@ -2101,9 +2137,9 @@ postgres_fdw_get_connections_internal(FunctionCallInfo fcinfo, * NULL. */ if (require_verify && pgfdw_conn_checkable()) - values[3] = BoolGetDatum(pgfdw_conn_check(entry->conn) != 0); + values[i++] = BoolGetDatum(pgfdw_conn_check(entry->conn) != 0); else - nulls[3] = true; + nulls[i++] = true; } tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out index 22fba7aeb5..3250964244 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -10461,13 +10461,14 @@ 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. -SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid | used_in_xact | closed --------------+-------+--------------+-------- - loopback | f | t | - | f | t | +-- 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 is same as CURRENT_USER", valid, used_in_xact, closed FROM postgres_fdw_get_connections() ORDER BY 1; + server_name | user_name is same as CURRENT_USER | valid | used_in_xact | closed +-------------+-----------------------------------+-------+--------------+-------- + loopback | t | f | t | + | | f | t | (2 rows) -- The invalid connections get closed in pgfdw_xact_callback during commit. 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 9afbc5c39b..5a88433a04 100644 --- a/contrib/postgres_fdw/postgres_fdw--1.1--1.2.sql +++ b/contrib/postgres_fdw/postgres_fdw--1.1--1.2.sql @@ -11,7 +11,8 @@ DROP FUNCTION postgres_fdw_get_connections (); CREATE FUNCTION postgres_fdw_get_connections (IN require_verify boolean DEFAULT false, OUT server_name text, - OUT valid boolean, OUT used_in_xact boolean, OUT closed boolean) + OUT user_name text, OUT valid boolean, OUT used_in_xact boolean, + OUT closed boolean) 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 653d049946..df5033e78c 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -3374,9 +3374,10 @@ SELECT server_name FROM postgres_fdw_get_connections() ORDER BY 1; 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. -SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; +-- 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 is same as CURRENT_USER", valid, used_in_xact, closed 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 diff --git a/doc/src/sgml/postgres-fdw.sgml b/doc/src/sgml/postgres-fdw.sgml index c4b52ddefc..f8467649c0 100644 --- a/doc/src/sgml/postgres-fdw.sgml +++ b/doc/src/sgml/postgres-fdw.sgml @@ -782,8 +782,8 @@ OPTIONS (ADD password_required 'false'); returns setof record - This function returns the foreign server names of all the open - connections that postgres_fdw established from + This function returns the foreign server names and user 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 and has been used within a transaction. valid is returned as false if the @@ -814,10 +814,10 @@ OPTIONS (ADD password_required 'false'); Example usage of the function: postgres=*# SELECT * FROM postgres_fdw_get_connections(true) ORDER BY 1; - server_name | valid | used_in_xact | closed --------------+-------+--------------+-------- - loopback1 | t | t | f - loopback2 | t | t | f + server_name | user_name | valid | used_in_xact | closed +-------------+-----------+-------+--------------+-------- + loopback1 | public | t | t | f + loopback2 | public | t | t | f diff --git a/src/backend/foreign/foreign.c b/src/backend/foreign/foreign.c index f4f35728b4..c7079dd569 100644 --- a/src/backend/foreign/foreign.c +++ b/src/backend/foreign/foreign.c @@ -246,6 +246,40 @@ GetUserMapping(Oid userid, Oid serverid) } +/* + * GetUserMappingFromOid - look up the user mapping by its oid. + */ +UserMapping * +GetUserMappingFromOid(Oid usermappigid, bool missing_ok) +{ + HeapTuple tp; + UserMapping *um; + Form_pg_user_mapping umform; + + tp = SearchSysCache1(USERMAPPINGOID, ObjectIdGetDatum(usermappigid)); + + if (!HeapTupleIsValid(tp)) + { + if (!missing_ok) + elog(ERROR, "cache lookup failed for user mapping %u", + usermappigid); + + return NULL; + } + + umform = ((Form_pg_user_mapping) GETSTRUCT(tp)); + + um = (UserMapping *) palloc(sizeof(UserMapping)); + um->umid = umform->oid; + um->userid = umform->umuser; + um->serverid = umform->umserver; + + ReleaseSysCache(tp); + + return um; +} + + /* * GetForeignTable - look up the foreign table definition by relation oid. */ diff --git a/src/include/foreign/foreign.h b/src/include/foreign/foreign.h index 82b8153100..b3941edac0 100644 --- a/src/include/foreign/foreign.h +++ b/src/include/foreign/foreign.h @@ -71,6 +71,7 @@ extern ForeignServer *GetForeignServerByName(const char *srvname, bool missing_ok); extern UserMapping *GetUserMapping(Oid userid, Oid serverid); extern ForeignDataWrapper *GetForeignDataWrapper(Oid fdwid); +extern UserMapping *GetUserMappingFromOid(Oid usermappigid, bool missing_ok); extern ForeignDataWrapper *GetForeignDataWrapperExtended(Oid fdwid, bits16 flags); extern ForeignDataWrapper *GetForeignDataWrapperByName(const char *fdwname, -- 2.43.0