On 4/21/25 07:46, Frédéric Yhuel wrote:
I can try to implement Tom's idea if we have a consensus.


v3 attached. Would that do?
From 07d331ba0f91b999fcefd12696bfc1eda7e8f20f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Yhuel?= <frederic.yh...@dalibo.com>
Date: Fri, 18 Apr 2025 13:20:52 +0200
Subject: [PATCH v3] fix reporting of temp files usage

when extended protocol is used with unnamed portals
---
 src/backend/tcop/postgres.c        | 19 +++++++++++++------
 src/backend/utils/mmgr/portalmem.c |  4 ++++
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index dc4c600922d..1b0c98828c8 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -1018,7 +1018,11 @@ exec_simple_query(const char *query_string)
 	bool		was_logged = false;
 	bool		use_implicit_block;
 	char		msec_str[32];
+	Portal		portal;
 
+	portal = GetPortalByName("");
+	if (PortalIsValid(portal))
+		PortalDrop(portal, false);
 	/*
 	 * Report query to various monitoring facilities.
 	 */
@@ -1672,6 +1676,12 @@ exec_bind_message(StringInfo input_message)
 					 errmsg("unnamed prepared statement does not exist")));
 	}
 
+	if (portal_name[0] == '\0')
+	{
+		portal = GetPortalByName(portal_name);
+		if (PortalIsValid(portal))
+			PortalDrop(portal, false);
+	}
 	/*
 	 * Report query to various monitoring facilities.
 	 */
@@ -1749,13 +1759,10 @@ exec_bind_message(StringInfo input_message)
 				 errdetail_abort()));
 
 	/*
-	 * Create the portal.  Allow silent replacement of an existing portal only
-	 * if the unnamed portal is specified.
+	 * Create the portal.  Don't allow silent replacement of an existing named portal.
+	 * An unnamed portal should have been removed already.
 	 */
-	if (portal_name[0] == '\0')
-		portal = CreatePortal(portal_name, true, true);
-	else
-		portal = CreatePortal(portal_name, false, false);
+	portal = CreatePortal(portal_name, false, false);
 
 	/*
 	 * Prepare to copy stuff into the portal's memory context.  We do all this
diff --git a/src/backend/utils/mmgr/portalmem.c b/src/backend/utils/mmgr/portalmem.c
index e3526e78064..246e711db81 100644
--- a/src/backend/utils/mmgr/portalmem.c
+++ b/src/backend/utils/mmgr/portalmem.c
@@ -27,6 +27,7 @@
 #include "utils/memutils.h"
 #include "utils/snapmgr.h"
 #include "utils/timestamp.h"
+#include "tcop/tcopprot.h"
 
 /*
  * Estimate of the maximum number of open portals a user would have,
@@ -488,6 +489,9 @@ PortalDrop(Portal portal, bool isTopCommit)
 				(errcode(ERRCODE_INVALID_CURSOR_STATE),
 				 errmsg("cannot drop active portal \"%s\"", portal->name)));
 
+	if (portal->queryDesc)
+		debug_query_string = portal->queryDesc->sourceText;
+
 	/*
 	 * Allow portalcmds.c to clean up the state it knows about, in particular
 	 * shutting down the executor if still active.  This step potentially runs
-- 
2.47.2

Reply via email to