3131extern Datum pg_options_to_table (PG_FUNCTION_ARGS );
3232extern Datum postgresql_fdw_validator (PG_FUNCTION_ARGS );
3333
34- static HeapTuple find_user_mapping (Oid userid , Oid serverid );
34+ static HeapTuple find_user_mapping (Oid userid , Oid serverid , bool missing_ok );
3535
3636/*
3737 * GetForeignDataWrapper - look up the foreign-data wrapper by OID.
@@ -223,7 +223,7 @@ GetUserMapping(Oid userid, Oid serverid)
223223 bool isnull ;
224224 UserMapping * um ;
225225
226- tp = find_user_mapping (userid , serverid );
226+ tp = find_user_mapping (userid , serverid , false );
227227
228228 um = (UserMapping * ) palloc (sizeof (UserMapping ));
229229 um -> umid = HeapTupleGetOid (tp );
@@ -250,14 +250,23 @@ GetUserMapping(Oid userid, Oid serverid)
250250 *
251251 * If no mapping is found for the supplied user, we also look for
252252 * PUBLIC mappings (userid == InvalidOid).
253+ *
254+ * If missing_ok is true, the function returns InvalidOid when it does not find
255+ * required user mapping. Otherwise, find_user_mapping() throws error if it
256+ * does not find required user mapping.
253257 */
254258Oid
255- GetUserMappingId (Oid userid , Oid serverid )
259+ GetUserMappingId (Oid userid , Oid serverid , bool missing_ok )
256260{
257261 HeapTuple tp ;
258262 Oid umid ;
259263
260- tp = find_user_mapping (userid , serverid );
264+ tp = find_user_mapping (userid , serverid , missing_ok );
265+
266+ Assert (missing_ok || tp );
267+
268+ if (!tp && missing_ok )
269+ return InvalidOid ;
261270
262271 /* Extract the Oid */
263272 umid = HeapTupleGetOid (tp );
@@ -273,9 +282,13 @@ GetUserMappingId(Oid userid, Oid serverid)
273282 *
274283 * If no mapping is found for the supplied user, we also look for
275284 * PUBLIC mappings (userid == InvalidOid).
285+ *
286+ * If missing_ok is true, the function returns NULL, if it does not find
287+ * the required user mapping. Otherwise, it throws error if it does not
288+ * find the required user mapping.
276289 */
277290static HeapTuple
278- find_user_mapping (Oid userid , Oid serverid )
291+ find_user_mapping (Oid userid , Oid serverid , bool missing_ok )
279292{
280293 HeapTuple tp ;
281294
@@ -292,10 +305,15 @@ find_user_mapping(Oid userid, Oid serverid)
292305 ObjectIdGetDatum (serverid ));
293306
294307 if (!HeapTupleIsValid (tp ))
295- ereport (ERROR ,
296- (errcode (ERRCODE_UNDEFINED_OBJECT ),
297- errmsg ("user mapping not found for \"%s\"" ,
298- MappingUserName (userid ))));
308+ {
309+ if (missing_ok )
310+ return NULL ;
311+ else
312+ ereport (ERROR ,
313+ (errcode (ERRCODE_UNDEFINED_OBJECT ),
314+ errmsg ("user mapping not found for \"%s\"" ,
315+ MappingUserName (userid ))));
316+ }
299317
300318 return tp ;
301319}
0 commit comments