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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: assam258(at)gmail(dot)com
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 01:06:02
Message-ID: 742405.1795395962@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Henson Choi <assam258(at)gmail(dot)com> writes:
>> In particular, if tuplestore had an API call to the
>> effect of "set the read pointer to absolute tuple number N", we
>> could get rid of a lot of silliness in WindowAgg. Having a call
>> to read out the position as an absolute tuple number might help
>> too, but I'm not sure we'd even need it if we had the first one.

> v3 follows that direction. There are four patches.

I looked this over and did some more work with it.

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.

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.
You were right that we could get away without that so far as
the INMEM state is concerned, but if we trim some rows and then
switch to file-based operations, your code would have misnumbered
the surviving rows. Having it also simplifies some boundary checks
in tuplestore_seek.

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-0003 is new work to reimplement tuplestore_advance() and
tuplestore_skiptuples() on top of tuplestore_seek(). The
main reasons to do this are to shorten the code and provide
additional test coverage for tuplestore_seek, but I think that
this should also make these operations a bit cheaper in file mode,
since we'll avoid creating/pfreeing tuples.

v4-0004 corresponds to your 0004, and is only slightly different.
One point I didn't like is having WindowAgg know about the fact
that tuplestore keeps an additional tuple before what it's told
it can trim off. That's not documented as part of its API and
it's something we might well change.

> v3 is for master. For the back branches, I'd like your view on
> whether to use this series or v2. It applies to REL_19 as is; 14 to
> 18 need small context adjustments.

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

regards, tom lane

Attachment Content-Type Size
v4-0001-Track-absolute-row-numbers-in-tuplestore-read-poi.patch text/x-diff 15.2 KB
v4-0002-Add-tuplestore_tell-and-tuplestore_seek.patch text/x-diff 9.7 KB
v4-0003-Reimplement-tuplestore_advance-and-tuplestore_ski.patch text/x-diff 4.2 KB
v4-0004-Use-absolute-tuplestore-positions-in-WindowAgg.patch text/x-diff 8.5 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Raja Sai pranav 2026-11-22 19:10:50 Re: BUG #19930: amcheck might report btree index corruption after recovery of a mid-deletion state