pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tatsuo Ishii <ishii@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix infinite loop in first_value()/nth_value() with IGNORE NULLS
Date: Fri, 06 Nov 2026 00:04:19 +0000
Message-ID: <E1xP7RL-00000000Nbx-1aR6@gemulon.postgresql.org> (raw)

Fix infinite loop in first_value()/nth_value() with IGNORE NULLS.

ignorenulls_getfuncarginframe() walks the frame row by row looking for
the n-th non-null value.  It calls row_is_in_frame() on each position
without first checking that the position is inside the partition,
although row_is_in_frame() requires its caller to have done so ("The
caller must have already determined that the row is in the
partition").  When the frame ends at UNBOUNDED FOLLOWING,
row_is_in_frame() has no end-of-frame test, and with EXCLUDE GROUP (or
EXCLUDE TIES, for rows other than the current one) and no ORDER BY it
returns 0 ("excluded") for every position, including positions past
the last row.  The loop then never calls window_gettupleslot(), which
is what would have noticed the end of the partition, and it advances
abs_pos forever.  The fix checks the partition end before calling
row_is_in_frame(), the same way window_gettupleslot() does
(spool_tuples(), then compare with spooled_rows).  It also adds
CHECK_FOR_INTERRUPTS() at the top of the loop: the loop can pass over
many excluded or known-NULL rows without fetching any tuple, and
window_gettupleslot() is where the interrupt check normally happens.

Author: BHUVAN CHILUKUTI <c.bhuvan@iitg.ac.in>
Reviewed-by: shihao zhong <zhong950419@gmail.com>
Reviewed-by: Tatsuo Ishii <ishii@postgresql.org>
Discussion: https://postgr.es/m/PN0P287MB09038F3BD9921EBACF5646F6A7632%40PN0P287MB0903.INDP287.PROD.OUTLOOK.COM
Backpatch-through: 19

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/69b20fdd20de68b0f0eaa855669a6657c65c8533

Modified Files
--------------
src/backend/executor/nodeWindowAgg.c | 15 ++++++++++++++
src/test/regress/expected/window.out | 40 ++++++++++++++++++++++++++++++++++++
src/test/regress/sql/window.sql      | 20 ++++++++++++++++++
3 files changed, 75 insertions(+)



view thread (4+ messages)  latest in thread

Message-ID: <E1xP7RL-00000000Nbx-1aR6@gemulon.postgresql.org>
Permalink:  ../E1xP7RL-00000000Nbx-1aR6@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xP7RL-00000000Nbx-1aR6@gemulon.postgresql.org

 · 

reply

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-committers@postgresql.org
  Cc: ishii@postgresql.org
  Subject: Re: pgsql: Fix infinite loop in first_value()/nth_value() with IGNORE NULLS
  In-Reply-To: <E1xP7RL-00000000Nbx-1aR6@gemulon.postgresql.org>
  References: <E1xP7RL-00000000Nbx-1aR6@gemulon.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