From 07a9c78d237359ae64a10af407d9629c195e06dc Mon Sep 17 00:00:00 2001
From: Henson Choi <assam258@gmail.com>
Date: Tue, 24 Feb 2026 12:35:14 +0900
Subject: [PATCH 7/8] Refactor create_windowagg_plan to remove bare
 variable-scoping block

---
 src/backend/optimizer/plan/createplan.c | 79 ++++++++++++-------------
 1 file changed, 39 insertions(+), 40 deletions(-)

diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index bbc2c7e71f4..e7aafc89700 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -2484,6 +2484,10 @@ create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
 	Oid		   *ordOperators;
 	Oid		   *ordCollations;
 	ListCell   *lc;
+	List	   *defineVariableList = NIL;
+	List	   *filteredDefineClause = NIL;
+	RPRPattern *compiledPattern = NULL;
+
 
 	/*
 	 * Choice of tlist here is motivated by the fact that WindowAgg will be
@@ -2535,50 +2539,45 @@ create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
 	}
 
 	/* Build RPR pattern and filter defineClause */
+	if (wc->rpPattern)
 	{
-		List	   *defineVariableList = NIL;
-		List	   *filteredDefineClause = NIL;
-		RPRPattern *compiledPattern = NULL;
-
-		if (wc->rpPattern)
-		{
-			List	   *patternVars;
+		List	   *patternVars;
 
-			/*
-			 * Filter defineClause to include only variables used in PATTERN.
-			 * This eliminates unnecessary DEFINE evaluations at runtime.
-			 */
-			patternVars = collectPatternVariables(wc->rpPattern);
-			filteredDefineClause = filterDefineClause(wc->defineClause,
-													  patternVars,
-													  &defineVariableList);
-
-			compiledPattern = buildRPRPattern(wc->rpPattern,
-											  defineVariableList,
-											  wc->rpSkipTo,
-											  wc->frameOptions);
-		}
-
-		/* And finally we can make the WindowAgg node */
-		plan = make_windowagg(tlist,
-							  wc,
-							  partNumCols,
-							  partColIdx,
-							  partOperators,
-							  partCollations,
-							  ordNumCols,
-							  ordColIdx,
-							  ordOperators,
-							  ordCollations,
-							  best_path->runCondition,
-							  wc->rpSkipTo,
-							  compiledPattern,
-							  filteredDefineClause,
-							  best_path->qual,
-							  best_path->topwindow,
-							  subplan);
+		/*
+		 * Filter defineClause to include only variables used in PATTERN. This
+		 * eliminates unnecessary DEFINE evaluations at runtime.
+		 */
+		patternVars = collectPatternVariables(wc->rpPattern);
+		filteredDefineClause = filterDefineClause(wc->defineClause,
+												  patternVars,
+												  &defineVariableList);
+
+		/* Compile and optimize RPR patterns */
+		compiledPattern = buildRPRPattern(wc->rpPattern,
+										  defineVariableList,
+										  wc->rpSkipTo,
+										  wc->frameOptions);
 	}
 
+	/* And finally we can make the WindowAgg node */
+	plan = make_windowagg(tlist,
+						  wc,
+						  partNumCols,
+						  partColIdx,
+						  partOperators,
+						  partCollations,
+						  ordNumCols,
+						  ordColIdx,
+						  ordOperators,
+						  ordCollations,
+						  best_path->runCondition,
+						  wc->rpSkipTo,
+						  compiledPattern,
+						  filteredDefineClause,
+						  best_path->qual,
+						  best_path->topwindow,
+						  subplan);
+
 	copy_generic_path_info(&plan->plan, (Path *) best_path);
 
 	return plan;
-- 
2.50.1 (Apple Git-155)

