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-18 03:51:33
Message-ID: CAAAe_zC=2KN_seGKj+G6s7S5-33b6tZ9s_VhA_4PusAdD3bhCw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> writes:
> I've got very little faith in your proposed patch though. WindowAgg
> maintains a bunch of read pointers and is less than consistent about
> which one is the "active" one when it calls tuplestore_puttupleslot(),
> so I suspect that there are more places than this one that would need
> resync logic if we follow this fix approach.

Agreed, v1 depended on that. tuplestore does need improving, and
once row pattern recognition has settled, I'd like to rework it,
together with the performance side, so that several positions can be
read at once through multiple read buffers. For the problem at hand,
though, I'd like to propose a simpler fix that, unlike v1, does not
create the EOF state at all.

WinSetMarkPosition() skipped the aggregate's read pointer past the
end of the stored rows. A skip past the end leaves the pointer at
EOF. While it is at EOF and still the active pointer, it moves along
as the spool adds rows, but seekpos does not, so the two no longer
agree.

v2 limits the skip so that it cannot reach EOF: the read pointer
stops on the last stored row. Since the read pointer can never reach
EOF, seekpos and the pointer can no longer disagree.

An assertion in window_gettupleslot() checks that the pointer it uses
is not at EOF.

This does not depend on which pointer is active. On a write,
tuplestore moves a read pointer only if it is at EOF; a pointer
anywhere else stays where it is, active or not. The skip above is the
only way a WindowObject's read pointer reaches EOF. The frame head,
frame tail, group tail and current-row pointers keep no seekpos, and
reach EOF only at the real end of the partition, after which no more
rows are written.

To check this, I made spool_tuples() report any WindowObject read
pointer at EOF before it writes. On 2900 window queries and a set of
random ones at work_mem = 64kB, it found the active aggregate pointer
at EOF thousands of times with v1, and never with v2.

The regression test is the same as in v1. With assertions enabled,
it fails without the fix and passes with it on master and on REL_14
to REL_19.

> I think what we need to do is what you called a mitigation but seems
> to me to be a full fix: get rid of WindowAgg's local tracking of
> tuplestore positions in favor of extending the tuplestore API with
> some more-appropriate operations.

I agree that the tuplestore API is the right direction, and I'll
include it, without the extra layer, when I rework tuplestore as
mentioned above. As that is likely to take quite a while, would you
consider v2 first, as a small fix without side effects?

Regards,
Henson

Attachment Content-Type Size
v2-0001-Don-t-move-a-window-read-pointer-past-the-stored-.patch application/octet-stream 4.4 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Tom Lane 2026-11-18 02:57:42 Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub-