Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xTV15-00000000Mgc-29KG for pgsql-bugs@arkaria.postgresql.org; Wed, 18 Nov 2026 02:03:19 +0000 Received: from localhost ([127.0.0.1]:53676 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xTV14-00000008BmB-3nbX for pgsql-bugs@arkaria.postgresql.org; Wed, 18 Nov 2026 02:03:18 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]:37368) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xTV14-00000008Blt-2jBS for pgsql-bugs@lists.postgresql.org; Wed, 18 Nov 2026 02:03:18 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xTV12-00000000GRt-1bzl for pgsql-bugs@lists.postgresql.org; Wed, 18 Nov 2026 02:03:18 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 6AI23DWW601851; Tue, 17 Nov 2026 21:03:13 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 sss.pgh.pa.us 6AI23DWW601851 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sss.pgh.pa.us; s=default; t=1794967393; bh=Xqc2vaF3yXU9vyRnfpsOhbE9YTr++woFiKqb5S8+5FQ=; h=From:To:cc:Subject:In-reply-to:References:Date:From; b=GVNIID7qKhY6fvdWYgR3jKu/fHbTJPTmjk809pnia895KiV3vbIxB2+pkHFQ996Ua tApUDhjQ2EXPSzLfsN9IjQXSndjegFBVg90cpGqRZLpvOr+M1LrckUawcD205iY4WG b1eHGl281zhiUsZ1c8fpBk6Ol/TA+K2GcssEUdaE= From: Tom Lane To: assam258@gmail.com cc: tharakan@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19911: Window aggregate over ROWS n FOLLOWING: "unexpected end of tuplestore" after spill In-reply-to: References: <19911-991c431cc0ba50d1@postgresql.org> Comments: In-reply-to Henson Choi message dated "Wed, 18 Nov 2026 10:05:20 +0900" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <601849.1794967393.1@sss.pgh.pa.us> Date: Tue, 17 Nov 2026 21:03:13 -0500 Message-ID: <601850.1794967393@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Henson Choi writes: > Thanks for the report. I can reproduce it on master, and the attached > patch fixes it. Below are the cause and fix, why this kind of bug > keeps turning up in WindowAgg, and two follow-up ideas for master. I've spent the last little while poking at this problem too. I concur with your root-cause diagnosis: WindowAgg's dead reckoning about where the tuplestore read pointers are positioned is getting out of sync with their actual positions. 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. I experimented with some alternative fix ideas, but couldn't come up with anything very workable. 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. In particular, if tuplestore had an API call to the effect of "set the read pointer to absolute tuple number N", we could get rid of a lot of silliness in WindowAgg. Having a call to read out the position as an absolute tuple number might help too, but I'm not sure we'd even need it if we had the first one. The reason that tuplestore hasn't got that already is long obsolete: I think it's mainly that we wanted to support tuplestores with more than 4G tuples on machines without int64 arithmetic, so dealing in absolute rather than relative tuple numbers didn't work. There are some fine points to be dealt with, like compensating for "tuplestore_trim" operations, but I think it's not going to be too complicated. And fortunately we never exposed the tuplestore state structs in any header, so there's no ABI breakage risk to worry about from adding or expanding fields. I would not bother with the intermediate management layer that you talked about; that seems like it's just adding bureaucracy. regards, tom lane