[PATCH] Little refactoring of portalcmds.c

Started by Aleksander Alekseev3 months ago3 messages
#1Aleksander Alekseev
aleksander@tigerdata.com
1 attachment(s)

Hi,

The proposed patch places some repetitive code in a helper function.
The value of this change is arguably not that high but it makes the
code a bit neater IMO.

--
Best regards,
Aleksander Alekseev

Attachments:

v1-0001-Refactor-portalcmds.c.patchtext/x-patch; charset=US-ASCII; name=v1-0001-Refactor-portalcmds.c.patchDownload
From af051406d09dedf87bde8802d7f7dea0ab4458ec Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@tigerdata.com>
Date: Wed, 8 Oct 2025 16:54:17 +0300
Subject: [PATCH v1] Refactor portalcmds.c

Place some repetitive code in a helper function.

Author: Aleksander Alekseev <aleksander@tigerdata.com>
Reviewed-by: TODO FIXME
Discussion: TODO FIXME
---
 src/backend/commands/portalcmds.c | 40 +++++++++++++------------------
 1 file changed, 16 insertions(+), 24 deletions(-)

diff --git a/src/backend/commands/portalcmds.c b/src/backend/commands/portalcmds.c
index ec96c2efcd3..d74a01c01a3 100644
--- a/src/backend/commands/portalcmds.c
+++ b/src/backend/commands/portalcmds.c
@@ -36,6 +36,19 @@
 #include "utils/memutils.h"
 #include "utils/snapmgr.h"
 
+/*
+ * Check that cursor name is not empty, which would conflict with protocol-level
+ * unnamed portal.
+ */
+static void
+check_cursor_name(const char *name)
+{
+	if (!name || name[0] == '\0')
+		ereport(ERROR,
+				(errcode(ERRCODE_INVALID_CURSOR_NAME),
+				 errmsg("invalid cursor name: must not be empty")));
+}
+
 
 /*
  * PerformCursorOpen
@@ -53,14 +66,7 @@ PerformCursorOpen(ParseState *pstate, DeclareCursorStmt *cstmt, ParamListInfo pa
 	MemoryContext oldContext;
 	char	   *queryString;
 
-	/*
-	 * Disallow empty-string cursor name (conflicts with protocol-level
-	 * unnamed portal).
-	 */
-	if (!cstmt->portalname || cstmt->portalname[0] == '\0')
-		ereport(ERROR,
-				(errcode(ERRCODE_INVALID_CURSOR_NAME),
-				 errmsg("invalid cursor name: must not be empty")));
+	check_cursor_name(cstmt->portalname);
 
 	/*
 	 * If this is a non-holdable cursor, we require that this statement has
@@ -182,14 +188,7 @@ PerformPortalFetch(FetchStmt *stmt,
 	Portal		portal;
 	uint64		nprocessed;
 
-	/*
-	 * Disallow empty-string cursor name (conflicts with protocol-level
-	 * unnamed portal).
-	 */
-	if (!stmt->portalname || stmt->portalname[0] == '\0')
-		ereport(ERROR,
-				(errcode(ERRCODE_INVALID_CURSOR_NAME),
-				 errmsg("invalid cursor name: must not be empty")));
+	check_cursor_name(stmt->portalname);
 
 	/* get the portal from the portal name */
 	portal = GetPortalByName(stmt->portalname);
@@ -233,14 +232,7 @@ PerformPortalClose(const char *name)
 		return;
 	}
 
-	/*
-	 * Disallow empty-string cursor name (conflicts with protocol-level
-	 * unnamed portal).
-	 */
-	if (name[0] == '\0')
-		ereport(ERROR,
-				(errcode(ERRCODE_INVALID_CURSOR_NAME),
-				 errmsg("invalid cursor name: must not be empty")));
+	check_cursor_name(name);
 
 	/*
 	 * get the portal from the portal name
-- 
2.43.0

#2Quan Zongliang
quanzongliang@yeah.net
In reply to: Aleksander Alekseev (#1)
Re: [PATCH] Little refactoring of portalcmds.c

On 10/8/25 10:02 PM, Aleksander Alekseev wrote:

Hi,

The proposed patch places some repetitive code in a helper function.
The value of this change is arguably not that high but it makes the
code a bit neater IMO.

It also reduces the ease of reading the code.
Just add a function for a single if statement. I don't think it's necessary.

Regards,
Quan Zongliang

#3wenhui qiu
qiuwenhuifx@gmail.com
In reply to: Quan Zongliang (#2)
Re: [PATCH] Little refactoring of portalcmds.c

Hi Aleksander Alekseev

The proposed patch places some repetitive code in a helper function.
The value of this change is arguably not that high but it makes the
code a bit neater IMO.

I agree with your suggestion to refactor the duplicated code into a
function.

Thanks

On Mon, Oct 27, 2025 at 2:03 PM Quan Zongliang <quanzongliang@yeah.net>
wrote:

Show quoted text

On 10/8/25 10:02 PM, Aleksander Alekseev wrote:

Hi,

The proposed patch places some repetitive code in a helper function.
The value of this change is arguably not that high but it makes the
code a bit neater IMO.

It also reduces the ease of reading the code.
Just add a function for a single if statement. I don't think it's
necessary.

Regards,
Quan Zongliang