nocfbot-0011-define-cost.txt
text/plain
Filename: nocfbot-0011-define-cost.txt
Type: text/plain
Part: 0
Message:
Re: Row pattern recognition
From 613d2b9cc310f9e55c71ef16555b9c42401c46b9 Mon Sep 17 00:00:00 2001
From: Henson Choi <assam258@gmail.com>
Date: Sun, 1 Mar 2026 19:09:27 +0900
Subject: [PATCH] Add RPR DEFINE expression cost to WindowAgg cost estimation
---
src/backend/optimizer/path/costsize.c | 29 +++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
index 89ca4e08bf1..a10fb802d0e 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -103,6 +103,7 @@
#include "optimizer/placeholder.h"
#include "optimizer/plancat.h"
#include "optimizer/restrictinfo.h"
+#include "optimizer/rpr.h"
#include "parser/parsetree.h"
#include "utils/lsyscache.h"
#include "utils/selfuncs.h"
@@ -3227,7 +3228,35 @@ cost_windowagg(Path *path, PlannerInfo *root,
* many rows the window function will fetch, it's hard to do better. In
* any case, it's a good estimate for all the built-in window functions,
* so we'll just do this for now.
+ *
+ * Moreover, if row pattern recognition is used, we charge the DEFINE
+ * expressions once per tuple for each variable that appears in PATTERN.
*/
+ if (winclause->rpPattern)
+ {
+ List *pattern_vars;
+ ListCell *lc2;
+ QualCost defcosts;
+
+ pattern_vars = collectPatternVariables(winclause->rpPattern);
+
+ foreach(lc2, pattern_vars)
+ {
+ char *ptname = strVal(lfirst(lc2));
+
+ foreach_node(TargetEntry, def, winclause->defineClause)
+ {
+ if (!strcmp(ptname, def->resname))
+ {
+ cost_qual_eval_node(&defcosts, (Node *) def->expr, root);
+ startup_cost += defcosts.startup;
+ total_cost += defcosts.per_tuple * input_tuples;
+ }
+ }
+ }
+ list_free_deep(pattern_vars);
+ }
+
foreach(lc, windowFuncs)
{
WindowFunc *wfunc = lfirst_node(WindowFunc, lc);
--
2.50.1 (Apple Git-155)