From 75c7ac00130ea4d38444f833a5b9b743592e9591 Mon Sep 17 00:00:00 2001
From: Dipesh Dhameliya <dipeshdhameliya125@gmail.com>
Date: Mon, 5 May 2025 10:27:57 +0530
Subject: [PATCH] Allow parallelism for plpgsql return expression after commit
 556f7b7

With the commit 556f7b7, we unintentionally blocked parallelism to
evaluate plpgsql return expression because maxtuples = 2 is being passed
to exec_run_select(...) from exec_eval_expr(...) to evaluate the return
expression of the plpgsql function.

Idea to fix this issue to pass maxtuples = 0. It is safe to do it
because number of processed rows is anyway being checked later in
exec_eval_expr(...). But with this, there is not real caller remained
which calls exec_run_select(...) with maxtuple != 0 so updated definition
of exec_run_select(...) to remove maxtuples argument.

Signed-off-by: Dipesh Dhameliya <dipeshdhameliya125@gmail.com>
---
 src/pl/plpgsql/src/pl_exec.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c
index bb99781c56e..75a589bfd59 100644
--- a/src/pl/plpgsql/src/pl_exec.c
+++ b/src/pl/plpgsql/src/pl_exec.c
@@ -385,7 +385,7 @@ static Datum exec_eval_expr(PLpgSQL_execstate *estate,
 							Oid *rettype,
 							int32 *rettypmod);
 static int	exec_run_select(PLpgSQL_execstate *estate,
-							PLpgSQL_expr *expr, long maxtuples, Portal *portalP);
+							PLpgSQL_expr *expr, Portal *portalP);
 static int	exec_for_query(PLpgSQL_execstate *estate, PLpgSQL_stmt_forq *stmt,
 						   Portal portal, bool prefetch_ok);
 static ParamListInfo setup_param_list(PLpgSQL_execstate *estate,
@@ -2181,7 +2181,7 @@ exec_stmt_perform(PLpgSQL_execstate *estate, PLpgSQL_stmt_perform *stmt)
 {
 	PLpgSQL_expr *expr = stmt->expr;
 
-	(void) exec_run_select(estate, expr, 0, NULL);
+	(void) exec_run_select(estate, expr, NULL);
 	exec_set_found(estate, (estate->eval_processed != 0));
 	exec_eval_cleanup(estate);
 
@@ -2844,7 +2844,7 @@ exec_stmt_fors(PLpgSQL_execstate *estate, PLpgSQL_stmt_fors *stmt)
 	/*
 	 * Open the implicit cursor for the statement using exec_run_select
 	 */
-	exec_run_select(estate, stmt->query, 0, &portal);
+	exec_run_select(estate, stmt->query, &portal);
 
 	/*
 	 * Execute the loop
@@ -5703,7 +5703,7 @@ exec_eval_expr(PLpgSQL_execstate *estate,
 	/*
 	 * Else do it the hard way via exec_run_select
 	 */
-	rc = exec_run_select(estate, expr, 2, NULL);
+	rc = exec_run_select(estate, expr, NULL);
 	if (rc != SPI_OK_SELECT)
 		ereport(ERROR,
 				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
@@ -5761,7 +5761,7 @@ exec_eval_expr(PLpgSQL_execstate *estate,
  */
 static int
 exec_run_select(PLpgSQL_execstate *estate,
-				PLpgSQL_expr *expr, long maxtuples, Portal *portalP)
+				PLpgSQL_expr *expr, Portal *portalP)
 {
 	ParamListInfo paramLI;
 	int			rc;
@@ -5810,7 +5810,7 @@ exec_run_select(PLpgSQL_execstate *estate,
 	 * Execute the query
 	 */
 	rc = SPI_execute_plan_with_paramlist(expr->plan, paramLI,
-										 estate->readonly_func, maxtuples);
+										 estate->readonly_func, 0);
 	if (rc != SPI_OK_SELECT)
 	{
 		/*
-- 
2.42.0

