From 12df4c5961b6940733a5ef7feb72808f2585c390 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sun, 22 Nov 2026 18:54:52 -0500
Subject: [PATCH v4 4/4] Use absolute tuplestore positions in WindowAgg.

WindowAgg kept its own idea of where each read pointer was (seekpos)
and moved the pointers by relative skips.  However, its tracking was
inaccurate.  WinSetMarkPosition() could advance the read pointer past
the stored rows, leaving it at EOF.  An active pointer at EOF follows
later writes while seekpos does not, so window_gettupleslot() then
failed with "unexpected end of tuplestore".

Rather than trying to fix up that logic, let's remove seekpos and
position the pointers with tuplestore_tell() and tuplestore_seek().

Bug: #19911
Reported-by: Robins Tharakan <tharakan@gmail.com>
Author: Henson Choi <assam258@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19911-991c431cc0ba50d1@postgresql.org
Backpatch-through: 15
---
 src/backend/executor/nodeWindowAgg.c | 100 +++++++++------------------
 src/test/regress/expected/window.out |  12 ++++
 src/test/regress/sql/window.sql      |   8 +++
 3 files changed, 51 insertions(+), 69 deletions(-)

diff --git a/src/backend/executor/nodeWindowAgg.c b/src/backend/executor/nodeWindowAgg.c
index 8d80f2cc023..1b2b008ec9d 100644
--- a/src/backend/executor/nodeWindowAgg.c
+++ b/src/backend/executor/nodeWindowAgg.c
@@ -70,8 +70,7 @@ typedef struct WindowObjectData
 	void	   *localmem;		/* WinGetPartitionLocalMemory's chunk */
 	int			markptr;		/* tuplestore mark pointer for this fn */
 	int			readptr;		/* tuplestore read pointer for this fn */
-	int64		markpos;		/* row that markptr is positioned on */
-	int64		seekpos;		/* row that readptr is positioned on */
+	int64		markpos;		/* oldest row this fn may still fetch */
 	uint8	  **notnull_info;	/* not null info for each func args */
 	int64	   *num_notnull_info;	/* track size (number of tuples in
 									 * partition) of the notnull_info array
@@ -1294,9 +1293,15 @@ begin_partition(WindowAggState *winstate)
 		}
 	}
 
-	/* Create new tuplestore if not done already. */
+	/*
+	 * Create new tuplestore if not done already.  If we do have one, check
+	 * that it was cleared by release_partition; this is important since we
+	 * depend on absolute row numbering within the tuplestore.
+	 */
 	if (unlikely(winstate->buffer == NULL))
 		prepare_tuplestore(winstate);
+	else
+		Assert(tuplestore_tuple_count(winstate->buffer) == 0);
 
 	winstate->next_partition = false;
 
@@ -1304,16 +1309,15 @@ begin_partition(WindowAggState *winstate)
 	{
 		WindowObject agg_winobj = winstate->agg_winobj;
 
-		/* reset mark and see positions for aggregate functions */
-		agg_winobj->markpos = -1;
-		agg_winobj->seekpos = -1;
+		/* Reset mark position for aggregate functions */
+		agg_winobj->markpos = 0;
 
 		/* Also reset the row counters for aggregates */
 		winstate->aggregatedbase = 0;
 		winstate->aggregatedupto = 0;
 	}
 
-	/* reset mark and seek positions for each real window function */
+	/* Reset mark position for each real window function */
 	for (int i = 0; i < numfuncs; i++)
 	{
 		WindowStatePerFunc perfuncstate = &(winstate->perfunc[i]);
@@ -1322,8 +1326,7 @@ begin_partition(WindowAggState *winstate)
 		{
 			WindowObject winobj = perfuncstate->winobj;
 
-			winobj->markpos = -1;
-			winobj->seekpos = -1;
+			winobj->markpos = 0;
 
 			/* reset null map */
 			if (winobj->ignore_nulls == IGNORE_NULLS ||
@@ -3307,59 +3310,19 @@ window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
 	tuplestore_select_read_pointer(winstate->buffer, winobj->readptr);
 
 	/*
-	 * Advance or rewind until we are within one tuple of the one we want.
+	 * Position the read pointer on the wanted row by its absolute number; the
+	 * partition's rows are numbered from 0 in the tuplestore.
 	 */
-	if (winobj->seekpos < pos - 1)
-	{
-		if (!tuplestore_skiptuples(winstate->buffer,
-								   pos - 1 - winobj->seekpos,
-								   true))
-			elog(ERROR, "unexpected end of tuplestore");
-		winobj->seekpos = pos - 1;
-	}
-	else if (winobj->seekpos > pos + 1)
-	{
-		if (!tuplestore_skiptuples(winstate->buffer,
-								   winobj->seekpos - (pos + 1),
-								   false))
-			elog(ERROR, "unexpected end of tuplestore");
-		winobj->seekpos = pos + 1;
-	}
-	else if (winobj->seekpos == pos)
-	{
-		/*
-		 * There's no API to refetch the tuple at the current position.  We
-		 * have to move one tuple forward, and then one backward.  (We don't
-		 * do it the other way because we might try to fetch the row before
-		 * our mark, which isn't allowed.)  XXX this case could stand to be
-		 * optimized.
-		 */
-		tuplestore_advance(winstate->buffer, true);
-		winobj->seekpos++;
-	}
+	if (!tuplestore_seek(winstate->buffer, pos))
+		elog(ERROR, "unexpected end of tuplestore");
 
 	/*
-	 * Now we should be on the tuple immediately before or after the one we
-	 * want, so just fetch forwards or backwards as appropriate.
-	 *
 	 * Notice that we tell tuplestore_gettupleslot to make a physical copy of
 	 * the fetched tuple.  This ensures that the slot's contents remain valid
 	 * through manipulations of the tuplestore, which some callers depend on.
 	 */
-	if (winobj->seekpos > pos)
-	{
-		if (!tuplestore_gettupleslot(winstate->buffer, false, true, slot))
-			elog(ERROR, "unexpected end of tuplestore");
-		winobj->seekpos--;
-	}
-	else
-	{
-		if (!tuplestore_gettupleslot(winstate->buffer, true, true, slot))
-			elog(ERROR, "unexpected end of tuplestore");
-		winobj->seekpos++;
-	}
-
-	Assert(winobj->seekpos == pos);
+	if (!tuplestore_gettupleslot(winstate->buffer, true, true, slot))
+		elog(ERROR, "unexpected end of tuplestore");
 
 	MemoryContextSwitchTo(oldcontext);
 
@@ -3776,28 +3739,27 @@ void
 WinSetMarkPosition(WindowObject winobj, int64 markpos)
 {
 	WindowAggState *winstate;
+	int64		target;
 
 	Assert(WindowObjectIsValid(winobj));
 	winstate = winobj->winstate;
 
 	if (markpos < winobj->markpos)
 		elog(ERROR, "cannot move WindowObject's mark position backward");
+	winobj->markpos = markpos;
+	/* Don't try to move the mark pointer past current EOF */
+	target = tuplestore_tuple_count(winstate->buffer);
+	target = Min(markpos, target);
 	tuplestore_select_read_pointer(winstate->buffer, winobj->markptr);
-	if (markpos > winobj->markpos)
-	{
-		tuplestore_skiptuples(winstate->buffer,
-							  markpos - winobj->markpos,
-							  true);
-		winobj->markpos = markpos;
-	}
+	Assert(tuplestore_tell(winstate->buffer) <= target);
+	if (!tuplestore_seek(winstate->buffer, target))
+		elog(ERROR, "unexpected end of tuplestore");
+	Assert(!tuplestore_ateof(winstate->buffer));
 	tuplestore_select_read_pointer(winstate->buffer, winobj->readptr);
-	if (markpos > winobj->seekpos)
-	{
-		tuplestore_skiptuples(winstate->buffer,
-							  markpos - winobj->seekpos,
-							  true);
-		winobj->seekpos = markpos;
-	}
+	if (tuplestore_tell(winstate->buffer) < target &&
+		!tuplestore_seek(winstate->buffer, target))
+		elog(ERROR, "unexpected end of tuplestore");
+	Assert(!tuplestore_ateof(winstate->buffer));
 }
 
 /*
diff --git a/src/test/regress/expected/window.out b/src/test/regress/expected/window.out
index 112551c350b..061caa685d7 100644
--- a/src/test/regress/expected/window.out
+++ b/src/test/regress/expected/window.out
@@ -1452,6 +1452,18 @@ FROM tenk1 WHERE unique1 < 10;
      |       7 |    3
 (10 rows)
 
+-- frame head on the next row, not yet stored, when the tuplestore spills
+SET work_mem = '64kB';
+SELECT count(*), sum(s) FROM
+  (SELECT sum(i) OVER (ORDER BY i ROWS BETWEEN 2000 FOLLOWING
+                       AND UNBOUNDED FOLLOWING) s
+   FROM generate_series(1, 2002) i) t;
+ count | sum  
+-------+------
+  2002 | 6005
+(1 row)
+
+RESET work_mem;
 -- RANGE offset PRECEDING/FOLLOWING tests
 SELECT sum(unique1) over (order by four range between 2::int8 preceding and 1::int2 preceding),
 	unique1, four
diff --git a/src/test/regress/sql/window.sql b/src/test/regress/sql/window.sql
index e848557174b..c4ff185d815 100644
--- a/src/test/regress/sql/window.sql
+++ b/src/test/regress/sql/window.sql
@@ -356,6 +356,14 @@ SELECT sum(unique1) over (ORDER BY four groups between 9223372036854775807 follo
 	unique1, four
 FROM tenk1 WHERE unique1 < 10;
 
+-- frame head on the next row, not yet stored, when the tuplestore spills
+SET work_mem = '64kB';
+SELECT count(*), sum(s) FROM
+  (SELECT sum(i) OVER (ORDER BY i ROWS BETWEEN 2000 FOLLOWING
+                       AND UNBOUNDED FOLLOWING) s
+   FROM generate_series(1, 2002) i) t;
+RESET work_mem;
+
 -- RANGE offset PRECEDING/FOLLOWING tests
 
 SELECT sum(unique1) over (order by four range between 2::int8 preceding and 1::int2 preceding),
-- 
2.52.0

