| 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-30 07:29:13 |
| Message-ID: | CAAAe_zCwbSn9Q0OtPddn5+Pvquj5xPQ0XqxmSfwUdNF+1SEO3A@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
Hi Tom,
Here is v7. As you suggested, it now holds onto the tuple kept by a
trim separately, which replaces v6-0004, and v6-0008 is dropped.
v7 numbering (v7-0002 and v7-0005 have changed, v7-0007 to v7-0009 are
new, the rest are unchanged from v6; v7-0009 is comment hygiene only):
v7 v6 v4
0001 0001 0001 Track absolute row numbers
0002 0002 Fix new read pointer position in READFILE
0003 0003 0002 Add tuplestore_tell() and tuplestore_seek()
0004 0005 0003 Reimplement advance() and skiptuples()
0005 0006 Fix skiptuples() at the boundaries
0006 0007 0004 Use absolute tuplestore positions in WindowAgg
0007 (new) Hold the tuple kept by trim outside the store
0008 (new) Make rowstrimmed count all trimmed rows
0009 (new) Fix outdated comments in tuplestore.c
0004 (dropped, replaced by v7-0007)
0008 (dropped)
> v6-0002: Good catch. I concur that this error is probably not
> reachable from any in-core code, but we should fix it for the
> possible benefit of extensions. The proposed one-line comment
> feels a bit too terse, otherwise fine.
I expanded the comment along the lines of the one in
tuplestore_copy_read_pointer().
> v6-0004: this definitely makes me feel like I chose the wrong
> definition for rowstrimmed, ie it should include memtupdeleted in
> TSS_INMEM state. You sure it's preferable to leave it this way?
> I'm also feeling uncomfortable with the logic around whether the
> extra untrimmed tuple counts as a legal move destination. That
> all seems squishy and easy to get wrong; I wonder if we should
> do it another way, e.g. explicitly hold onto that tuple via a
> separate pointer until the next tuplestore operation.
Agreed, and done in v7-0007. Trim now removes all the tuples before
the oldest read pointer from the store, but keeps the last of them in a
new field, heldtuple, instead of freeing it. That made some things
unnecessary, so I removed them:
- v6-0004: the held tuple is not in the store and so not a position
to move to, so seek and skiptuples need no special case.
- truncated: with rowstrimmed changed to the definition you proposed
(v7-0008), it is the same as rowstrimmed > 0.
It is released not at the next operation but at the next trim that
releases more tuples, at dumptuples(), or at clear or end, since calls
such as tuplestore_ateof() can come between moving the mark and the
next fetch. That is the same lifetime as before.
There is one behavior change: a backward read that used to reach the
kept tuple now gets NULL, and fails the existing assertion that no trim
has happened, as that read is past the truncation point documented at
tuplestore_set_eflags(). WindowAgg did that before it switched to
absolute positions (rank() in the box, polygon, triggers and window
regression tests, triggers through information_schema.triggers), so
v7-0007 comes after v7-0006. No other core caller moves back past the
oldest read pointer.
Since a trim can now leave the store empty, v7-0007 also makes
tuplestore_set_eflags() and tuplestore_alloc_read_pointer() check tuples
instead of memtupcount. Otherwise an extension could add REWIND after
such a trim and hit the rowstrimmed == 0 assertion in
tuplestore_rescan(); master raises the "too late" error there.
> v6-0006: I agree I messed up on the step-backwards-from-EOF case,
> but somehow this coding feels uglier than the approach I used
> before. In particular, the forward case wasn't broken but this
> way of coding it doesn't seem like an improvement to me. Maybe
> it's all in the eye of the beholder.
v7-0005 keeps the coding of v7-0004 and fixes three things:
- Backward from EOF, the first step (from EOF onto the last tuple)
counts as one.
- It leaves EOF through tuplestore_seek() instead of clearing
eof_reached by hand, since in WRITEFILE state the file position of
a pointer at EOF is not maintained.
- The forward case was off in one respect: with R tuples left, a skip
of R + 1 returns true, where the original code returned false.
PersistHoldablePortal() depends on that. With v7-0004, a WITH HOLD
cursor whose re-execution at commit returns one row fewer than its
position (c6 in portals.sql) commits without error and goes on
fetching from the shorter result, while master raises "unexpected
end of tuple stream". The comparison is now < in both directions.
> v6-0008: I do not see a need for this.
Agreed, dropped.
> I agree that we need to do some additional performance work here,
> but this sketch seems a trifle over-ambitious. I had also thought
> of the idea of keeping an index of block-starting tuples, and I
> think we ought to see what that will buy us, but I'm less on board
> with the rest of this.
OK. I will start with the index of block-starting tuples and see what
it buys us, and bring that up separately.
What each test catches, run one by one on assert-enabled builds of
master and of v7-0004, before the fixes in v7-0005:
portals.sql, in v7-0005:
- c3: turning around at EOF in memory while the Materialize input is
not done. v7-0004 fails the assertion row == base + current.
- c4, first part: leaving EOF in WRITEFILE state. v7-0004 raises
"could not seek in tuplestore temporary file".
- c5: the same through a CTE reader whose file position was saved for
a SubPlan reader. v7-0004 returns wrong rows.
- c6 and c8: a WITH HOLD cursor whose re-execution returns one row
fewer than its position, in memory and spilled to a file. v7-0004
commits without error.
- c4, second part, and c7: coverage only, for the READFILE branch
that sets row = rowstrimmed and for a forward skip of exactly R in
a file.
window.sql:
- 2000 FOLLOWING, in v7-0006: the reported case of #19911. master
and v7-0004 raise "unexpected end of tuplestore".
- trim before spill, in v7-0007: coverage only, for dumptuples()
releasing the held tuple and a file starting at rowstrimmed > 0.
All of them pass on master except 2000 FOLLOWING.
For the back branches, the same adjustments as for v6 apply, plus one
in v7-0007: in REL_17 and older, clear and end do not reset the memory
context, so heldtuple must be freed explicitly there. Whether v7-0007
and v7-0008 should go to the back branches at all is your call; without
them, the back branches would need v6-0004 instead.
Best regards,
Henson
| Attachment | Content-Type | Size |
|---|---|---|
| v7-0001-Track-absolute-row-numbers-in-tuplestore-read-poi.patch | application/octet-stream | 15.2 KB |
| v7-0002-Fix-new-tuplestore-read-pointer-position-in-READF.patch | application/octet-stream | 1.7 KB |
| v7-0003-Add-tuplestore_tell-and-tuplestore_seek.patch | application/octet-stream | 9.7 KB |
| v7-0004-Reimplement-tuplestore_advance-and-tuplestore_ski.patch | application/octet-stream | 4.1 KB |
| v7-0005-Fix-tuplestore_skiptuples-at-the-boundaries.patch | application/octet-stream | 9.4 KB |
| v7-0006-Use-absolute-tuplestore-positions-in-WindowAgg.patch | application/octet-stream | 8.5 KB |
| v7-0007-Hold-the-tuple-kept-by-tuplestore_trim-outside-th.patch | application/octet-stream | 10.4 KB |
| v7-0008-Make-rowstrimmed-count-all-trimmed-rows.patch | application/octet-stream | 8.5 KB |
| v7-0009-Fix-outdated-comments-in-tuplestore.c.patch | application/octet-stream | 3.5 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Michael Paquier | 2026-11-30 08:17:12 | Re: BUG #19909: BRIN redo: BufferIsPinned assert in brin_xlog_insert_update() if index was dropped or truncated |
| Previous Message | David G. Johnston | 2026-11-30 06:10:48 | Re: BUG #19871: JSON_TABLE evaluates each PASSING argument once per column - a named variable $v gets different valu |