nocfbot-0062-Invalidate-the-row-pattern-nav-slot-cache-when-a-.txt

text/plain

Filename: nocfbot-0062-Invalidate-the-row-pattern-nav-slot-cache-when-a-.txt
Type: text/plain
Part: 60
Message: Re: Row pattern recognition
From 9d9e6f677f1015c2555f718cf4350e95ea5dc5d4 Mon Sep 17 00:00:00 2001
From: Henson Choi <assam258@gmail.com>
Date: Mon, 8 Jun 2026 12:44:45 +0900
Subject: [PATCH 62/68] Invalidate the row pattern nav slot cache when a window
 partition changes

release_partition clears the per-partition tuplestore, leaving the cached
nav slot position (which is partition-relative) stale.  This is not a live
bug: nfa_evaluate_row resets nav_slot_pos to -1 before each row's DEFINE
evaluation, the only path that fetches nav slots, so a stale position never
produces a cache hit.  Reset it here too for consistency with the rest of the
per-partition cleanup.  Also note in visit_nav_plan that a constant
LAST(x, 0) is conservatively flagged match-start-dependent, causing a
harmless redundant re-evaluation.
---
 src/backend/executor/nodeWindowAgg.c    | 3 +++
 src/backend/optimizer/plan/createplan.c | 7 ++++++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/backend/executor/nodeWindowAgg.c b/src/backend/executor/nodeWindowAgg.c
index 8a3535e03f9..576abff3ce6 100644
--- a/src/backend/executor/nodeWindowAgg.c
+++ b/src/backend/executor/nodeWindowAgg.c
@@ -1574,6 +1574,9 @@ release_partition(WindowAggState *winstate)
 	winstate->nfaLastProcessedRow = -1;
 	winstate->nfaStatesActive = 0;
 	winstate->nfaContextsActive = 0;
+
+	/* Invalidate the nav slot position cache for the new partition. */
+	winstate->nav_slot_pos = -1;
 }
 
 /*
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index c5b0857ff20..5608d71cbe2 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -2693,7 +2693,12 @@ visit_nav_plan(NavTraversal *t, RPRNavExpr *nav)
 		}
 	}
 
-	/* Match-start dependency: classify the outer nav kind. */
+	/*
+	 * Match-start dependency: classify the outer nav kind.  A constant
+	 * LAST(x, 0) is conservatively included (offset_arg is a non-NULL Const),
+	 * causing a harmless extra re-evaluation; since LAST(x, 0) is the current
+	 * row, its result is independent of the match start.
+	 */
 	if (nav->kind == RPR_NAV_FIRST ||
 		(nav->kind == RPR_NAV_LAST && nav->offset_arg != NULL) ||
 		nav->kind == RPR_NAV_PREV_FIRST ||
-- 
2.50.1 (Apple Git-155)