Skip to content

Commit 5e5f4fc

Browse files
committed
postgres_fdw: Save foreign server OID in connection cache entry.
The foreign server OID stored in the connection cache entry is used as a lookup key to directly get the server name. Previously since the connection cache entry did not have the server OID, postgres_fdw had to get the server OID at first from user mapping before getting the server name. So if the corresponding user mapping was dropped, postgres_fdw could raise the error "cache lookup failed for user mapping" while looking up user mapping and fail to get the server name even though the server had not been dropped yet. Author: Bharath Rupireddy Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/CALj2ACVRZPUB7ZwqLn-6DY8C_UmPs6084gSpHA92YBv++1AJXA@mail.gmail.com
1 parent 8e396a7 commit 5e5f4fc

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

contrib/postgres_fdw/connection.c

+3-9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ typedef struct ConnCacheEntry
5757
bool have_error; /* have any subxacts aborted in this xact? */
5858
bool changing_xact_state; /* xact state change in process */
5959
bool invalidated; /* true if reconnect is pending */
60+
Oid serverid; /* foreign server OID used to get server name */
6061
uint32 server_hashvalue; /* hash value of foreign server OID */
6162
uint32 mapping_hashvalue; /* hash value of user mapping OID */
6263
} ConnCacheEntry;
@@ -273,6 +274,7 @@ make_new_connection(ConnCacheEntry *entry, UserMapping *user)
273274
entry->have_error = false;
274275
entry->changing_xact_state = false;
275276
entry->invalidated = false;
277+
entry->serverid = server->serverid;
276278
entry->server_hashvalue =
277279
GetSysCacheHashValue1(FOREIGNSERVEROID,
278280
ObjectIdGetDatum(server->serverid));
@@ -1138,8 +1140,6 @@ pgfdw_inval_callback(Datum arg, int cacheid, uint32 hashvalue)
11381140
static void
11391141
pgfdw_reject_incomplete_xact_state_change(ConnCacheEntry *entry)
11401142
{
1141-
HeapTuple tup;
1142-
Form_pg_user_mapping umform;
11431143
ForeignServer *server;
11441144

11451145
/* nothing to do for inactive entries and entries of sane state */
@@ -1150,13 +1150,7 @@ pgfdw_reject_incomplete_xact_state_change(ConnCacheEntry *entry)
11501150
disconnect_pg_server(entry);
11511151

11521152
/* find server name to be shown in the message below */
1153-
tup = SearchSysCache1(USERMAPPINGOID,
1154-
ObjectIdGetDatum(entry->key));
1155-
if (!HeapTupleIsValid(tup))
1156-
elog(ERROR, "cache lookup failed for user mapping %u", entry->key);
1157-
umform = (Form_pg_user_mapping) GETSTRUCT(tup);
1158-
server = GetForeignServer(umform->umserver);
1159-
ReleaseSysCache(tup);
1153+
server = GetForeignServer(entry->serverid);
11601154

11611155
ereport(ERROR,
11621156
(errcode(ERRCODE_CONNECTION_EXCEPTION),

0 commit comments

Comments
 (0)