From aeeb82bee861dceb9eae3d8a46226d878c20d2cd Mon Sep 17 00:00:00 2001 From: Hayato Kuroda Date: Thu, 18 Jul 2024 10:11:03 +0000 Subject: [PATCH v41 2/2] Extend postgres_fdw_get_connections to return user name --- contrib/postgres_fdw/connection.c | 33 +++++++++++++++--- .../postgres_fdw/expected/postgres_fdw.out | 13 +++---- .../postgres_fdw/postgres_fdw--1.1--1.2.sql | 3 +- contrib/postgres_fdw/sql/postgres_fdw.sql | 5 +-- doc/src/sgml/postgres-fdw.sgml | 12 +++---- src/backend/foreign/foreign.c | 34 +++++++++++++++++++ src/include/foreign/foreign.h | 1 + 7 files changed, 81 insertions(+), 20 deletions(-) diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c index edc2f5eb46..18abb0994e 100644 --- a/contrib/postgres_fdw/connection.c +++ b/contrib/postgres_fdw/connection.c @@ -2008,7 +2008,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 - user mapping 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. * - 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 @@ -2020,7 +2023,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; @@ -2092,9 +2095,29 @@ postgres_fdw_get_connections_internal(FunctionCallInfo fcinfo, if (api_version == PGFDW_V1_2) { bool require_verify = PG_GETARG_BOOL(1); + UserMapping *user = GetUserMappingFromOid(entry->key, true); + + /* + * The same as in the foreign server case, user mappings can also + * be dropped in the current explicit transaction, so the same + * check as in the server case is required. + */ + if (user) + values[2] = CStringGetTextDatum(MappingUserName(user->userid)); + else + { + /* + * If we reach here, this entry must have been invalidated in + * pgfdw_inval_callback, same as server case. + */ + Assert(entry->conn && entry->xact_depth > 0 && + entry->invalidated); + + nulls[2] = true; + } /* Has this server been used in the transaction? */ - values[2] = BoolGetDatum(entry->xact_depth > 0); + values[3] = BoolGetDatum(entry->xact_depth > 0); /* * If requested and the connection is not invalidated, check the @@ -2103,12 +2126,12 @@ postgres_fdw_get_connections_internal(FunctionCallInfo fcinfo, */ if (require_verify && !entry->invalidated && entry->conn) { - values[3] = BoolGetDatum(pgfdw_conn_checkable() ? + values[4] = BoolGetDatum(pgfdw_conn_checkable() ? pgfdw_conn_check(entry->conn) != 0 : false); } else - nulls[3] = true; + nulls[4] = 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 4fcf73070c..ece6b4cf29 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -10432,13 +10432,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. +-- 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 * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid | used_in_xact | closed --------------+-------+--------------+-------- - loopback | f | t | - | f | t | + server_name | valid | user_name | used_in_xact | closed +-------------+-------+-----------+--------------+-------- + loopback | f | hayato | 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 6af10110ca..0b477a2082 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 valid boolean, OUT user_name text, 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 fa9c4dd36f..12d254d7f0 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -3357,8 +3357,9 @@ 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. +-- 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 * FROM postgres_fdw_get_connections() ORDER BY 1; -- The invalid connections get closed in pgfdw_xact_callback during commit. COMMIT; diff --git a/doc/src/sgml/postgres-fdw.sgml b/doc/src/sgml/postgres-fdw.sgml index fa2b50450d..dab796f37f 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 @@ -815,10 +815,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 | valid | user_name | used_in_xact | closed +-------------+-------+-----------+--------------+-------- + loopback1 | t | public | t | f + loopback2 | t | public | 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