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-24 14:46:44
Message-ID: CAAAe_zBmyjM1QgB-cE8XLrXOj4hm=-WTJTc+ZFoha3+2kO4Ajw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Tom,

> Sure, no urgency.

Thanks. Here is v6, with one new patch (v6-0002), the back-branch
check, and a measurement of the file-mode cost that v4-0003 brings down.

v6 numbering (v6-0002 is new, v6-0004 and v6-0006 have the changes
below, the rest are unchanged from v5):

v6 v5 v4
0001 0001 0001 Track absolute row numbers
0002 (new) Fix new read pointer position in READFILE
0003 0002 0002 Add tuplestore_tell() and tuplestore_seek()
0004 0003 Do not let seek land on the tuple kept by a trim
0005 0004 0003 Reimplement advance() and skiptuples()
0006 0005 Fix skiptuples() at the boundaries
0007 0006 0004 Use absolute tuplestore positions in WindowAgg
0008 0007 Keep accepting a mark position of -1

I have reviewed the whole series again and, as far as I can see, found
no defects other than the two below.

v6-0002:

tuplestore_alloc_read_pointer() makes the new pointer by copying read
pointer 0. In READFILE state the active pointer does not maintain its
file/offset (its position is the temp file's seek point), so if pointer
0 is active, a stale file/offset is copied.

v6-0002 takes the position with BufFileTell() in that case, as
tuplestore_copy_read_pointer() already does. The problem is in master
too, but no current caller is affected: they allocate pointers before
storing any tuple, or rewind the new pointer right away.

So it cannot be reproduced from SQL, and I have not added a regression
test. Calling the API directly does reproduce it: in a spilled store,
read 100 rows with pointer 0, then allocate a new pointer and select
it; master returns the 1st row, v6 the 101st.

v6-0004:

v5-0003 rejected a seek to the tuple a trim keeps for a slot, but only
while the store was in memory. Once the store spills, that tuple is the
first one in the file, at rowstrimmed, and seek accepted it. v6-0004
rejects it in both states, and v6-0006 (skiptuples) uses the same lower
bound.

Back branches:

I applied v6 to REL_15 through REL_19 and ran the core regression
tests with assertions enabled; all pass. The logic is the same in
every branch, and only the following adjustments are needed:

- REL_19: applies as is
- REL_18 and older: off_t instead of pgoff_t, as the BufFile API there
takes off_t, and /* FALLTHROUGH */ instead of pg_fallthrough;
v6-0007 needs context adjustments, as IGNORE NULLS is not there
- REL_17 and older: WindowAgg creates a tuplestore per partition
instead of reusing one, so the Assert in begin_partition() that the
store was cleared is dropped, and markpos is reset where the read
pointers are allocated; v6-0001 needs context adjustments, as
tuplestore_get_stats() is not there
- REL_15: also context adjustments

When you think the patches are ready on master, let me know and I will
prepare the back-branch patches.

> One thing I wondered about after sending v4 was whether
> the definition of rowstrimmed should change to be the total number of
> rows trimmed all the time (so that even before dumptuples, it would
> include memtupdeleted).

I have not changed the definition in v6. The current definition is
enough to deal with the defects, but the storage work below would most
likely be master only, so the back branches would keep the current
structure. I have a first draft of the change, but I will take some
time to audit it further for defects.

Performance:

For the worst case I used the query recorded in the XXX comment in
nodeWindowAgg.c of the row pattern recognition patch (CF 4460). One
read pointer goes back and forth between the match start and the row
being advanced on every row, so once the store spills, skiptuples moves
over the whole match on every row:

SET work_mem = '64kB';
SELECT first_value(id) OVER w, last_value(id) OVER w
FROM generate_series(1, 8000) AS id
WINDOW w AS (
ORDER BY id
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
AFTER MATCH SKIP PAST LAST ROW
PATTERN (S A+)
DEFINE A AS id >= FIRST(id));

Here rows is the length of the series. Release builds, median of three
runs, comparing the RPR branch with and without v6-0001 to v6-0006 (the
patches that touch only tuplestore):

rows spilled: before after in memory: before after
2000 554 ms 257 ms 0.83 ms 0.77 ms
4000 4657 ms 2287 ms 1.17 ms 1.17 ms
8000 22453 ms 11095 ms 1.91 ms 1.97 ms

The spilled case takes about half the time, since skipping no longer
needlessly reads the tuples it passes over; it only looks at their
length words. In memory there is no difference, and the results are the
same in every case. Doubling the rows still takes five to nine times as
long: each step is cheaper, but the number of steps is the same. Unlike
the defects, this is hard to solve within the current structure. To
address it, I would like to redesign the file-mode storage layout as a
separate patch in the first half of next year, and that would redefine
trim and the fields behind it to fit the new layout. The ideas I have
in mind are:

- memory: recently written blocks and blocks read back share one
buffer pool, with the blocks under each read pointer pinned, so the
recent part is still read from memory after a spill
- file layout: an index replaces the length word after each tuple and
gives, for each fixed-size block, the row number and file/offset of
the first tuple starting in it; only the index block not yet written
out and a few recently used ones are kept in memory
- trim: trimmed rows can no longer be reached, in the file as well,
and segments whose rows are all trimmed are removed
- read pointers: the block of the most recently read tuple stays in
memory, and tuple positions within it are reused once computed, so
nearby tuples are reached quickly
- tuple form: besides MinimalTuple, tuples in pinned blocks are also
cached in deformed form, so they can be used without deforming them
again
- callers: they move to an API that takes the read pointer as an
argument, so the active pointer no longer needs switching, and the
code of every caller is simplified along with it; instead of
copy=false, tuple lifetime is managed either by the read pointer or
by the consumer, which holds its own pin

Best regards,
Henson

PS: I will be traveling from Korea to Tokyo for PGConf.Japan, so from
Wednesday through Sunday this week I may not be able to reply.

Attachment Content-Type Size
v6-0001-Track-absolute-row-numbers-in-tuplestore-read-poi.patch application/octet-stream 15.2 KB
v6-0002-Fix-new-tuplestore-read-pointer-position-in-READF.patch application/octet-stream 1.7 KB
v6-0003-Add-tuplestore_tell-and-tuplestore_seek.patch application/octet-stream 9.7 KB
v6-0004-Do-not-let-tuplestore_seek-land-on-the-tuple-kept.patch application/octet-stream 3.0 KB
v6-0005-Reimplement-tuplestore_advance-and-tuplestore_ski.patch application/octet-stream 4.2 KB
v6-0006-Fix-tuplestore_skiptuples-at-the-boundaries.patch application/octet-stream 7.3 KB
v6-0007-Use-absolute-tuplestore-positions-in-WindowAgg.patch application/octet-stream 8.6 KB
v6-0008-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 Raja Sai pranav 2026-11-24 15:11:17 Re: BUG #19895: dblink() never returns and spins at 100% CPU when the remote command is COPY ... TO STDOUT;
Previous Message Daniel Gustafsson 2026-11-24 14:17:57 Re: macaddr8 accepts one trailing character after a complete address