From c281cde98debf6256ff3d682c3f764d6d73b8020 Mon Sep 17 00:00:00 2001 From: Hayato Kuroda Date: Tue, 4 Apr 2023 16:19:06 +0000 Subject: [PATCH v38 3/3] Extend postgres_fdw_get_connections to return user name --- contrib/postgres_fdw/connection.c | 10 ++++-- .../postgres_fdw/expected/postgres_fdw.out | 2 +- .../postgres_fdw/postgres_fdw--1.1--1.2.sql | 8 +++++ contrib/postgres_fdw/sql/postgres_fdw.sql | 2 +- doc/src/sgml/postgres-fdw.sgml | 32 +++++++++---------- src/backend/foreign/foreign.c | 28 ++++++++++++++++ src/include/foreign/foreign.h | 1 + 7 files changed, 63 insertions(+), 20 deletions(-) diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c index 5cd886bb2d..b67082ae4d 100644 --- a/contrib/postgres_fdw/connection.c +++ b/contrib/postgres_fdw/connection.c @@ -1648,7 +1648,7 @@ pgfdw_finish_pre_subcommit_cleanup(List *pending_entries, int curlevel) Datum postgres_fdw_get_connections(PG_FUNCTION_ARGS) { -#define POSTGRES_FDW_GET_CONNECTIONS_COLS 2 +#define POSTGRES_FDW_GET_CONNECTIONS_COLS 3 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; HASH_SEQ_STATUS scan; ConnCacheEntry *entry; @@ -1710,11 +1710,17 @@ postgres_fdw_get_connections(PG_FUNCTION_ARGS) /* Show null, if no server name was found */ nulls[0] = true; + nulls[1] = true; } else + { + UserMapping *user = GetUserMappingFromOid(entry->key); + values[0] = CStringGetTextDatum(server->servername); + values[1] = CStringGetTextDatum(MappingUserName(user->userid)); + } - values[1] = BoolGetDatum(!entry->invalidated); + values[2] = BoolGetDatum(!entry->invalidated); 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 4723b2c5ce..1ee5a65aca 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -10014,7 +10014,7 @@ 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; +SELECT server_name, valid FROM postgres_fdw_get_connections() ORDER BY 1; server_name | valid -------------+------- loopback | f 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 78ee82cc74..8e6aa3e949 100644 --- a/contrib/postgres_fdw/postgres_fdw--1.1--1.2.sql +++ b/contrib/postgres_fdw/postgres_fdw--1.1--1.2.sql @@ -3,6 +3,14 @@ -- complain if script is sourced in psql, rather than via ALTER EXTENSION \echo Use "ALTER EXTENSION postgres_fdw UPDATE TO '1.2'" to load this file. \quit +DROP FUNCTION postgres_fdw_get_connections (); + +CREATE FUNCTION postgres_fdw_get_connections (OUT server_name text, + OUT user_name text, OUT valid boolean) +RETURNS SETOF record +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT PARALLEL RESTRICTED; + CREATE FUNCTION postgres_fdw_verify_connection (IN server_name text, IN user_name text) RETURNS bool diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql index bce5d25aa7..8a9297b29a 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -3153,7 +3153,7 @@ 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; +SELECT server_name, valid 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 b4bc45b74f..e57520629e 100644 --- a/doc/src/sgml/postgres-fdw.sgml +++ b/doc/src/sgml/postgres-fdw.sgml @@ -761,27 +761,27 @@ OPTIONS (ADD password_required 'false'); - postgres_fdw_get_connections(OUT server_name text, OUT valid boolean) returns setof record + postgres_fdw_get_connections(OUT server_name text, OUT user_name text, OUT valid boolean) returns setof record - This function returns the foreign server 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 or not. false is returned - if the foreign server connection is used in the current local - transaction but its foreign server or user mapping is changed or - dropped (Note that server name of an invalid connection will be - NULL if the server is dropped), - and then such invalid connection will be closed at - the end of that transaction. true is returned - otherwise. If there are no open connections, no record is returned. + 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 or not. false is returned if the + foreign server connection is used in the current local transaction but + its foreign server or user mapping is changed or dropped (Note that + server name and user name of an invalid connection will be + NULL if the server is dropped), and then such invalid + connection will be closed at the end of that transaction. + true is returned otherwise. If there are no open + connections, no record is returned. Example usage of the function: postgres=# SELECT * FROM postgres_fdw_get_connections() ORDER BY 1; - server_name | valid --------------+------- - loopback1 | t - loopback2 | f + server_name | user_name | validvalid +-------------+------------------------ + loopback1 | postgres | t + loopback2 | | f diff --git a/src/backend/foreign/foreign.c b/src/backend/foreign/foreign.c index dca02271dc..2dd7789fe6 100644 --- a/src/backend/foreign/foreign.c +++ b/src/backend/foreign/foreign.c @@ -242,6 +242,34 @@ GetUserMapping(Oid userid, Oid serverid) return um; } +/* + * GetUserMappingFromOid - look up the user mapping by its oid. + */ +UserMapping * +GetUserMappingFromOid(Oid usermappigid) +{ + HeapTuple tp; + UserMapping *um; + Form_pg_user_mapping umform; + + tp = SearchSysCache1(USERMAPPINGOID, + ObjectIdGetDatum(usermappigid)); + + if (!HeapTupleIsValid(tp)) + elog(ERROR, "cache lookup failed for user mapping %u", usermappigid); + + 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 5256d4d91f..25c9d40699 100644 --- a/src/include/foreign/foreign.h +++ b/src/include/foreign/foreign.h @@ -70,6 +70,7 @@ extern ForeignServer *GetForeignServerExtended(Oid serverid, extern ForeignServer *GetForeignServerByName(const char *srvname, bool missing_ok); extern UserMapping *GetUserMapping(Oid userid, Oid serverid); +extern UserMapping *GetUserMappingFromOid(Oid usermappigid); extern ForeignDataWrapper *GetForeignDataWrapper(Oid fdwid); extern ForeignDataWrapper *GetForeignDataWrapperExtended(Oid fdwid, bits16 flags); -- 2.27.0