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

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: tharakan(at)gmail(dot)com
Subject: BUG #19911: Window aggregate over ROWS n FOLLOWING: "unexpected end of tuplestore" after spill
Date: 2026-11-17 13:18:49
Message-ID: 19911-991c431cc0ba50d1@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19911
Logged by: Robins Tharakan
Email address: tharakan(at)gmail(dot)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

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2026-11-17 14:49:34 Re: PG 18 JIT regression: int4range hash breaks multi-key GROUP BY
Previous Message Nitin Motiani 2026-11-17 12:03:35 Re: BUG #19737: Empty `JSON_OBJECT` cannot use documented `ON NULL` or unique-key clauses