From bf09932dbc0c3ab73bfd5566f5f69a32d709278a Mon Sep 17 00:00:00 2001
From: Jeff Davis <jeff@j-davis.com>
Date: Fri, 13 Mar 2026 13:24:18 -0700
Subject: [PATCH v22] Eliminate PG_TRY()/PG_CATCH() in
 ForeignServerConnectionString().

The error path is cleaned up for all callers, so only worry about the
success path. In the case of a logical worker, it will just exit.

Suggested-by: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/xvdjrdqnpap3uq7owbaox3r7p5gf7sv62aaqf2ju3vb6yglatr%40kvvwhoudrlxq
---
 src/backend/foreign/foreign.c | 53 ++++++++++++++---------------------
 1 file changed, 21 insertions(+), 32 deletions(-)

diff --git a/src/backend/foreign/foreign.c b/src/backend/foreign/foreign.c
index cf10a8c00f9..cd2d85df811 100644
--- a/src/backend/foreign/foreign.c
+++ b/src/backend/foreign/foreign.c
@@ -225,7 +225,11 @@ ForeignServerConnectionString(Oid userid, Oid serverid)
 {
 	MemoryContext tempContext;
 	MemoryContext oldcxt;
-	char	   *result = NULL;
+	ForeignServer *server;
+	ForeignDataWrapper *fdw;
+	text	   *connection_text;
+	Datum		connection_datum;
+	char	   *result;
 
 	/*
 	 * GetForeignServer, GetForeignDataWrapper, and the connection function
@@ -238,42 +242,27 @@ ForeignServerConnectionString(Oid userid, Oid serverid)
 
 	oldcxt = MemoryContextSwitchTo(tempContext);
 
-	PG_TRY();
-	{
-		ForeignServer *server;
-		ForeignDataWrapper *fdw;
-		text	   *connection_text = NULL;
-		Datum		connection_datum;
-
-		server = GetForeignServer(serverid);
-		fdw = GetForeignDataWrapper(server->fdwid);
-
-		if (!OidIsValid(fdw->fdwconnection))
-			ereport(ERROR,
-					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-					 errmsg("foreign data wrapper \"%s\" does not support subscription connections",
-							fdw->fdwname),
-					 errdetail("Foreign data wrapper must be defined with CONNECTION specified.")));
+	server = GetForeignServer(serverid);
+	fdw = GetForeignDataWrapper(server->fdwid);
 
+	if (!OidIsValid(fdw->fdwconnection))
+		ereport(ERROR,
+				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+				 errmsg("foreign data wrapper \"%s\" does not support subscription connections",
+						fdw->fdwname),
+				 errdetail("Foreign data wrapper must be defined with CONNECTION specified.")));
 
-		connection_datum = OidFunctionCall3(fdw->fdwconnection,
-											ObjectIdGetDatum(userid),
-											ObjectIdGetDatum(serverid),
-											PointerGetDatum(NULL));
+	connection_datum = OidFunctionCall3(fdw->fdwconnection,
+										ObjectIdGetDatum(userid),
+										ObjectIdGetDatum(serverid),
+										PointerGetDatum(NULL));
 
-		connection_text = DatumGetTextPP(connection_datum);
+	connection_text = DatumGetTextPP(connection_datum);
 
-		MemoryContextSwitchTo(oldcxt);
-		result = text_to_cstring((text *) connection_text);
-	}
-	PG_FINALLY();
-	{
-		/* no-op on success path */
-		MemoryContextSwitchTo(oldcxt);
+	MemoryContextSwitchTo(oldcxt);
+	result = text_to_cstring(connection_text);
 
-		MemoryContextDelete(tempContext);
-	}
-	PG_END_TRY();
+	MemoryContextDelete(tempContext);
 
 	return result;
 }
-- 
2.43.0

