On Wed, 2026-03-11 at 17:59 -0700, Jeff Davis wrote:
> That leaves some awkwardness, but I don't immediately see a cleaner
> way
> to do it. Suggestions welcome.
I did see a way to avoid the volatile entirely, it just needs a
redundant MemoryContextSwtichTo(). I think this is cleaner overall,
attached.
Regards,
Jeff Davis
From ae3beb8493888e1ff8c23813bb56e4abc4e8b723 Mon Sep 17 00:00:00 2001
From: Jeff Davis <[email protected]>
Date: Thu, 12 Mar 2026 18:04:35 -0700
Subject: [PATCH v21] Clean up PG_TRY()/PG_FINALLY().
In ForeignServerConnectionString(), refactor the PG_TRY()/PG_FINALLY()
blocks to avoid the need for volatile at all. The only downside is a
redundant MemoryContextSwitchTo() in the success case.
Reported-by: Andres Freund <[email protected]>
Discussion: https://postgr.es/m/xvdjrdqnpap3uq7owbaox3r7p5gf7sv62aaqf2ju3vb6yglatr@kvvwhoudrlxq
---
src/backend/foreign/foreign.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/src/backend/foreign/foreign.c b/src/backend/foreign/foreign.c
index 160cf6f51c9..cf10a8c00f9 100644
--- a/src/backend/foreign/foreign.c
+++ b/src/backend/foreign/foreign.c
@@ -225,7 +225,6 @@ ForeignServerConnectionString(Oid userid, Oid serverid)
{
MemoryContext tempContext;
MemoryContext oldcxt;
- text *volatile connection_text = NULL;
char *result = NULL;
/*
@@ -243,6 +242,7 @@ ForeignServerConnectionString(Oid userid, Oid serverid)
{
ForeignServer *server;
ForeignDataWrapper *fdw;
+ text *connection_text = NULL;
Datum connection_datum;
server = GetForeignServer(serverid);
@@ -262,14 +262,15 @@ ForeignServerConnectionString(Oid userid, Oid serverid)
PointerGetDatum(NULL));
connection_text = DatumGetTextPP(connection_datum);
+
+ MemoryContextSwitchTo(oldcxt);
+ result = text_to_cstring((text *) connection_text);
}
PG_FINALLY();
{
+ /* no-op on success path */
MemoryContextSwitchTo(oldcxt);
- if (connection_text)
- result = text_to_cstring((text *) connection_text);
-
MemoryContextDelete(tempContext);
}
PG_END_TRY();
--
2.43.0