From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: tharakan@gmail.com
Subject: BUG #19911: Window aggregate over ROWS n FOLLOWING: "unexpected end of tuplestore" after spill
Date: Tue, 17 Nov 2026 13:18:49 +0000
Message-ID: <19911-991c431cc0ba50d1@postgresql.org> (raw)
The following bug has been logged on the website:
Bug reference: 19911
Logged by: Robins Tharakan
Email address: tharakan@gmail.com
PostgreSQL version: Unsupported/Unknown
Operating system: Debian 13 (x86_64)
Description:
A window aggregate whose ROWS frame starts at an offset FOLLOWING fails
with an internal error when the rows before the frame's start don't fit
in work_mem:
ERROR: XX000: unexpected end of tuplestore
LOCATION: window_gettupleslot, nodeWindowAgg.c:3358
The query should return its rows: with work_mem large enough for the
partition (4MB for the first query below), it does. It fails the same
way for max(), sum() and count(*), with or without PARTITION BY or
ORDER BY. first_value() and nth_value(), RANGE and GROUPS frames, and
frames starting at an offset PRECEDING give the same rows at 64kB as at
64MB. A Valgrind run didn't report much else interesting.
SQL
===
SET work_mem = '64kB';
SELECT count(x), max(x)
FROM (SELECT max(g) OVER (ROWS BETWEEN 2000 FOLLOWING
AND UNBOUNDED FOLLOWING) AS x
FROM generate_series(1, 2002) g) s;
-- expected: 2 | 2002 (as with work_mem = '4MB')
-- got: ERROR: XX000: unexpected end of tuplestore
Commit
======
Reproduces on master (11591f273e6) and on every supported branch,
including v14 (853b35f35b1). A release build (no assertions, -O2)
raises the same error.
Error Log
=======
ERROR: unexpected end of tuplestore
STATEMENT: SELECT count(x), max(x)
FROM (SELECT max(g) OVER (ROWS BETWEEN 2000 FOLLOWING
AND UNBOUNDED FOLLOWING) AS x
FROM generate_series(1, 2002) g) s;
Backtrace
========
#0 window_gettupleslot (winobj=..., pos=2001, slot=...)
at nodeWindowAgg.c:3358
#1 eval_windowaggregates (winstate=...) at nodeWindowAgg.c:982
#2 ExecWindowAgg (pstate=...) at nodeWindowAgg.c:2431
#3 ExecProcNodeFirst (node=...) at execProcnode.c:469
#4 ExecProcNode (node=...) at executor.h:327
#5 fetch_input_tuple (aggstate=...) at nodeAgg.c:564
#6 agg_retrieve_direct (aggstate=...) at nodeAgg.c:2464
#7 ExecAgg (pstate=...) at nodeAgg.c:2279
At #0, for the first output row (currentpos 0, frameheadpos 2000): the
fetch of row 2000 has just returned g = 2002, and the read pointer is
at EOF with seekpos 2000.
Found using SQLsmith, SQLReduce, creduce and Claude.
-
Robins Tharakan
Databricks
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: noreply@postgresql.org, tharakan@gmail.com
Subject: Re: BUG #19911: Window aggregate over ROWS n FOLLOWING: "unexpected end of tuplestore" after spill
In-Reply-To: <19911-991c431cc0ba50d1@postgresql.org>
References: <19911-991c431cc0ba50d1@postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox