From e7fdc06f43a8a0233d9a97d17883eb64b92343f9 Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Wed, 18 Nov 2026 08:21:59 +0900 Subject: [PATCH v2] Don't move a window read pointer past the stored rows When the frame head of a window aggregate is the next row, not yet stored, WinSetMarkPosition() moved the aggregate's read pointer past the stored rows and left it at EOF. A read pointer at EOF follows the rows written while it is active. Once the tuplestore has spilled to disk, the next spool reads the rest of the partition, so the pointer ended up at its end while seekpos stayed put, and a later fetch failed with "unexpected end of tuplestore". Fix by moving the read pointer no further than the last stored row, where it stays put as rows are added, and assert that a WindowObject's read pointer is never at EOF when it is used. Reported-by: Robins Tharakan Author: Henson Choi Discussion: https://postgr.es/m/19911-991c431cc0ba50d1@postgresql.org Backpatch-through: 14 --- src/backend/executor/nodeWindowAgg.c | 24 ++++++++++++++++++++---- src/test/regress/expected/window.out | 12 ++++++++++++ src/test/regress/sql/window.sql | 8 ++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/backend/executor/nodeWindowAgg.c b/src/backend/executor/nodeWindowAgg.c index 8d80f2cc023..a5508d20005 100644 --- a/src/backend/executor/nodeWindowAgg.c +++ b/src/backend/executor/nodeWindowAgg.c @@ -3306,6 +3306,9 @@ window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot) tuplestore_select_read_pointer(winstate->buffer, winobj->readptr); + /* seekpos is only valid for a pointer not at EOF; see WinSetMarkPosition */ + Assert(!tuplestore_ateof(winstate->buffer)); + /* * Advance or rewind until we are within one tuple of the one we want. */ @@ -3790,13 +3793,26 @@ WinSetMarkPosition(WindowObject winobj, int64 markpos) true); winobj->markpos = markpos; } + + /* + * Bring the read pointer up to the mark, but don't skip it past the rows + * stored so far: a pointer at EOF follows the rows written while it is + * active, which seekpos would not reflect. Stopped on the last stored + * row, it stays put. The mark pointer is never active while rows are + * written, so if it reaches EOF it is just held one row back. + */ tuplestore_select_read_pointer(winstate->buffer, winobj->readptr); if (markpos > winobj->seekpos) { - tuplestore_skiptuples(winstate->buffer, - markpos - winobj->seekpos, - true); - winobj->seekpos = markpos; + int64 target = Min(markpos, winstate->spooled_rows - 1); + + if (target > winobj->seekpos) + { + tuplestore_skiptuples(winstate->buffer, + target - winobj->seekpos, + true); + winobj->seekpos = target; + } } } 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.54.0 (Apple Git-157)