nocfbot-0005-Fix-mark-handling-for-last_value-under-RPR.txt

text/plain

Filename: nocfbot-0005-Fix-mark-handling-for-last_value-under-RPR.txt
Type: text/plain
Part: 4
Message: Re: Row pattern recognition
From 344fbb8a5d39a53f7ce6ff924a06d4e4d3ec2b57 Mon Sep 17 00:00:00 2001
From: Henson Choi <assam258@gmail.com>
Date: Wed, 25 Mar 2026 00:37:50 +0900
Subject: [PATCH 5/6] Fix mark handling for last_value() under RPR

Enable mark advancement in window_last_value() for
better tuplestore memory usage in non-RPR cases, while
adding a guard in WinGetFuncArgInFrame to suppress it
for RPR SEEK_TAIL to prevent position invalidation
from reduced frame shifts.
---
 src/backend/executor/nodeWindowAgg.c | 8 ++++++++
 src/backend/utils/adt/windowfuncs.c  | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/backend/executor/nodeWindowAgg.c b/src/backend/executor/nodeWindowAgg.c
index 185d7a0d5ae..6d696af6fd4 100644
--- a/src/backend/executor/nodeWindowAgg.c
+++ b/src/backend/executor/nodeWindowAgg.c
@@ -4703,6 +4703,14 @@ WinGetFuncArgInFrame(WindowObject winobj, int argno,
 
 	Assert(WindowObjectIsValid(winobj));
 	winstate = winobj->winstate;
+
+	/*
+	 * RPR reduced frames may shift; don't advance mark from tail seeks to
+	 * avoid invalidating tuplestore read positions.
+	 */
+	if (set_mark && winstate->rpPattern != NULL && seektype == WINDOW_SEEK_TAIL)
+		set_mark = false;
+
 	econtext = winstate->ss.ps.ps_ExprContext;
 	slot = winstate->temp_slot_1;
 
diff --git a/src/backend/utils/adt/windowfuncs.c b/src/backend/utils/adt/windowfuncs.c
index efb60c99052..74ef109f72e 100644
--- a/src/backend/utils/adt/windowfuncs.c
+++ b/src/backend/utils/adt/windowfuncs.c
@@ -682,7 +682,7 @@ window_last_value(PG_FUNCTION_ARGS)
 
 	WinCheckAndInitializeNullTreatment(winobj, true, fcinfo);
 	result = WinGetFuncArgInFrame(winobj, 0,
-								  0, WINDOW_SEEK_TAIL, false,
+								  0, WINDOW_SEEK_TAIL, true,
 								  &isnull, NULL);
 	if (isnull)
 		PG_RETURN_NULL();
-- 
2.50.1 (Apple Git-155)