2023年7月3日(月) 18:22 Peter Eisentraut <pe...@eisentraut.org>:
>
> On 23.06.23 09:45, Ian Lawrence Barwick wrote:
> >       if (!HeapTupleIsValid(tp))
> > +     {
> > +             ForeignServer *server = GetForeignServer(serverid);
> > +
> >               ereport(ERROR,
> >                               (errcode(ERRCODE_UNDEFINED_OBJECT),
> > -                              errmsg("user mapping not found for \"%s\"",
> > -                                             MappingUserName(userid))));
> > +                              errmsg("user mapping not found for user 
> > \"%s\", server \"%s\"",
> > +                                             MappingUserName(userid),
> > +                                             server->servername)));
> > +     }
>
> What if the foreign server does not exist either?  Then this would show
> a "cache lookup failed" error message, which I think we should avoid.
>
> There is existing logic for handling this in
> get_object_address_usermapping().

Apologies, missed this response somewhere. Does the attached fix that?

Regards

Ian Barwick
commit 9b01a96a5a4fb816668c86c254b67dbf1083e4d5
Author: Ian Barwick <barw...@gmail.com>
Date:   Sun Jul 23 05:49:13 2023 +0900

    Improve "user mapping not found" error message
    
    Display the name of the foreign server for which the user mapping
    was not found.

diff --git a/src/backend/foreign/foreign.c b/src/backend/foreign/foreign.c
index ca3ad55b62..3432eb2841 100644
--- a/src/backend/foreign/foreign.c
+++ b/src/backend/foreign/foreign.c
@@ -217,10 +217,22 @@ GetUserMapping(Oid userid, Oid serverid)
 	}
 
 	if (!HeapTupleIsValid(tp))
+	{
+		ForeignServer *server = GetForeignServerExtended(serverid,
+														 FSV_MISSING_OK);
+
+		if (!server)
+			ereport(ERROR,
+					(errcode(ERRCODE_UNDEFINED_OBJECT),
+					 errmsg("user mapping not found for user \"%s\"",
+							MappingUserName(userid))));
+
 		ereport(ERROR,
 				(errcode(ERRCODE_UNDEFINED_OBJECT),
-				 errmsg("user mapping not found for \"%s\"",
-						MappingUserName(userid))));
+				 errmsg("user mapping not found for user \"%s\", server \"%s\"",
+						MappingUserName(userid),
+						server->servername)));
+	}
 
 	um = (UserMapping *) palloc(sizeof(UserMapping));
 	um->umid = ((Form_pg_user_mapping) GETSTRUCT(tp))->oid;

Reply via email to