Re: BUG #19911: Window aggregate over ROWS n FOLLOWING: "unexpected end of tuplestore" after spill

From: Henson Choi <assam258(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: tharakan(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19911: Window aggregate over ROWS n FOLLOWING: "unexpected end of tuplestore" after spill
Date: 2026-11-23 13:22:45
Message-ID: CAAAe_zA6CuLj+348VQebXX2if1OzyF71QJFcDNtF+=qedX0kYQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Tom,

This is registered as CF 7660, and v4 passes CI on all platforms:
https://commitfest.postgresql.org/patch/7660/

v4-0003 has a bug, so I am reporting it before you commit. When a
scroll cursor over Material or CteScan turns around at tuplestore
EOF, it returns wrong rows:

BEGIN;
DECLARE c SCROLL CURSOR FOR SELECT generate_series(1, 5) AS g;
FETCH 3 FROM c;
FETCH BACKWARD 1 FROM c;

On a build without assertions, master returns 2 and v4 returns 1,
with no error. An assert-enabled build fails an assertion instead,
and with a spill it can also raise "could not seek in tuplestore
temporary file". The cause and the fix are below, and the fix is
attached as v5-0005.

> I don't like your 0001. Just because we haven't found a need for
> a tuplestore holding non-heap tuples doesn't mean we never will.
> Even if we wanted to go in that direction, a bug fix requiring
> back-patching is not the place to do it. So I want to drop that.

Agreed.

> Attached v4-0001 corresponds to your 0002. I did a little bit of
> cosmetic work, such as rearranging the fields into what seemed to
> me a more sensible order. But the main change is to add a
> "rowstrimmed" field to track how many rows we've thrown away.

v4-0001: agreed. Keeping tuples equal to the stored count, and the
field order and check placement, make the invariants easier to see.
rowstrimmed is needed: once rows are trimmed and we switch to a file,
v3 would restart numbering at 0 on a backward fetch to the start.

> v4-0002 corresponds to your 0003. I redefined the APIs a bit
> to allow distinguishing between the "past the last tuple" and
> "eof_reached" states.

v4-0002: agreed. Checking against rowstrimmed up front, so that a
false return leaves the state unchanged, and sharing the WRITEFILE
code with READFILE through the fallthrough are both cleaner than v3.
One boundary of seek, left over from my v3, is below.

> v4-0003 is new work to reimplement tuplestore_advance() and
> tuplestore_skiptuples() on top of tuplestore_seek().

v4-0003: I agree with the direction. I first tried the other way
round, building seek on top of skiptuples, and gave up on the
boundaries: the first row, the trimmed-off bound and the start of a
file. Building skiptuples and advance on top of seek is right. But
the bug above is in this patch: it hits the same boundary I hit,
from the other side. The details are below.

> v4-0004 corresponds to your 0004, and is only slightly different.

v4-0004: agreed. With the mark pointer left on the mark row and
markpos starting at 0, it no longer relies on the extra tuple that
trim keeps. And since seek now tells EOF apart, the tell check and
the Assert in window_gettupleslot() can go. It is clearer. One
compatibility point for the back branches is below.

> I'm inclined to keep the code the same in all branches.

Agreed. I will finish checking the back branches and testing with
a wider set of spill cases in the next few days, and report back.

A boundary in seek left over from my v3:

My v3 had the same check. After a trim, tuplestore_seek() accepts
the tuple that trim keeps before the oldest read pointer for a slot:

row: 0 1 2 3 4
[freed] [freed] [kept] [3] [4]
^ ^
| oldest read pointer
kept for a slot

seek(2) v4: true; the next trim fails an assertion
v5-0003: false, as for rows 0 and 1

Before any trim there is no kept tuple: by the same rule it would be
row -1, which does not exist, so the lowest position stays 0. No
caller seeks there today, but tuplestore_seek() is new and exported.

The bug in v4-0003:

The contract of skiptuples is that it is the same as n calls to
gettuple, and Material, CteScan and PersistHoldablePortal() rely on
it. EOF is a virtual position past the last row; a backward gettuple
at EOF steps from it onto the last row and returns that row. In
terms of tell, that step goes from tuples + 1 to tuples.

In the example at the top, FETCH 3 leaves the pointer at EOF
(tell 4); g=4 and 5 are not read from the subplan yet. On FETCH
BACKWARD 1, Material calls tuplestore_advance() backward once and
then does a backward gettuple:

rows: [g=1] [g=2] [g=3]
tell: 0 1 2 3 4 (EOF)

advance gettuple returns
master 4 -> 3 3 -> 2 g=2
v4 4 -> 2 2 -> 1 g=1

v4 breaks this contract. v5-0005 is the fix for it; the details are
in its commit message. Feel free to fold it into 0003, or to drop
0003.

Compatibility in v4-0004:

Since markpos now starts at 0, WinSetMarkPosition(winobj, -1), which
used to be a no-op, raises an error. v5-0007 keeps compatibility by
treating -1 as 0; -2 and below still raise the error, as before.
Whether to apply it to master too, only to the back branches, or not
at all, is your call.

v5-0001, 0002, 0004 and 0006 are your v4-0001 to 0004 unchanged;
v5-0003, 0005 and 0007 are mine, each a fix to the patch just
before it.

Before you decide on committing, I would like a few more days to
review the series further.

Best regards,
Henson

PS: Longer term, trim is spread over physical fields (memtupdeleted,
rowstrimmed, truncated) that differ by storage state, and once the
store spills to a file, later trims record nothing, so nothing stops
a seek back below them. If tuplestore moves to random access with a
block directory, I think trim should become one logical lower bound,
kept apart from the tuple kept for a slot and from freeing storage,
and seek, skiptuples and backward gettuple should be tidied up around
it. Every place that calls them would then need to be reviewed as
well. That is not for this back-patched fix.

Attachment Content-Type Size
v5-0001-Track-absolute-row-numbers-in-tuplestore-read-poi.patch application/octet-stream 15.2 KB
v5-0002-Add-tuplestore_tell-and-tuplestore_seek.patch application/octet-stream 9.7 KB
v5-0003-Do-not-let-tuplestore_seek-land-on-the-tuple-kept.patch application/octet-stream 2.0 KB
v5-0004-Reimplement-tuplestore_advance-and-tuplestore_ski.patch application/octet-stream 4.2 KB
v5-0005-Fix-tuplestore_skiptuples-at-the-boundaries.patch application/octet-stream 7.3 KB
v5-0006-Use-absolute-tuplestore-positions-in-WindowAgg.patch application/octet-stream 8.6 KB
v5-0007-Keep-accepting-a-mark-position-of-1-in-WinSetMark.patch application/octet-stream 1.4 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2026-11-23 14:19:33 Re: BUG #19911: Window aggregate over ROWS n FOLLOWING: "unexpected end of tuplestore" after spill
Previous Message Nisha Moond 2026-11-23 13:10:45 Re: Logical replication commits duplicates in DEFERRABLE unique indexes