From 3e2988ec7ad99c20a3015652b97d1ac5727acb33 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Sat, 7 Nov 2026 18:34:12 -0500 Subject: [PATCH v1] Don't read past a ROWS frame end in IGNORE NULLS scan. Commit 69b20fdd20d checks the partition end before calling row_is_in_frame(), which spools the row being tested. For ROWS frames row_is_in_frame() can tell from the position alone that the row is out of frame, so we now read and evaluate one extra row. Call row_is_in_frame() first and check the partition end only when it does not return -1. Reported-by: Fujii Masao Discussion: https://postgr.es/m/CAHGQGwFTmdD5T+7z8kZekrjVkA=Z2yNhq43ff3AuoxPVxDXP1A@mail.gmail.com Backpatch-through: 19 --- src/backend/executor/nodeWindowAgg.c | 11 ++++++----- src/test/regress/expected/window.out | 13 +++++++++++++ src/test/regress/sql/window.sql | 8 ++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/backend/executor/nodeWindowAgg.c b/src/backend/executor/nodeWindowAgg.c index 8d80f2cc023..ceec084aaf1 100644 --- a/src/backend/executor/nodeWindowAgg.c +++ b/src/backend/executor/nodeWindowAgg.c @@ -3477,6 +3477,11 @@ ignorenulls_getfuncarginframe(WindowObject winobj, int argno, if (abs_pos < 0) goto out_of_frame; + /* check whether row is in frame */ + inframe = row_is_in_frame(winobj, abs_pos, slot, true); + if (inframe == -1) + goto out_of_frame; + /* * Likewise, stop at the end of the partition, which row_is_in_frame * expects its caller to have checked. It cannot always detect that @@ -3489,11 +3494,7 @@ ignorenulls_getfuncarginframe(WindowObject winobj, int argno, if (abs_pos >= winstate->spooled_rows) goto out_of_frame; - /* check whether row is in frame */ - inframe = row_is_in_frame(winobj, abs_pos, slot, true); - if (inframe == -1) - goto out_of_frame; - else if (inframe == 0) + if (inframe == 0) goto advance; if (isout) diff --git a/src/test/regress/expected/window.out b/src/test/regress/expected/window.out index 112551c350b..49d8489198e 100644 --- a/src/test/regress/expected/window.out +++ b/src/test/regress/expected/window.out @@ -6250,6 +6250,19 @@ ORDER BY p, x; 2 | | (3 rows) +-- the IGNORE NULLS scan must not read the row after a ROWS frame end +WITH t AS MATERIALIZED ( + SELECT NULLIF(i, 1) / (2 - i) AS v + FROM generate_series(1, 2) AS g(i) +) +SELECT first_value(v) IGNORE NULLS OVER (ROWS CURRENT ROW) +FROM t +LIMIT 1; + first_value +------------- + +(1 row) + -- volatile arguments cannot use the IGNORE NULLS nullness cache CREATE TEMPORARY SEQUENCE null_treatment_seq; CREATE FUNCTION pg_temp.volatile_null(i int) RETURNS int diff --git a/src/test/regress/sql/window.sql b/src/test/regress/sql/window.sql index e848557174b..3a4e01b9119 100644 --- a/src/test/regress/sql/window.sql +++ b/src/test/regress/sql/window.sql @@ -2302,6 +2302,14 @@ SELECT p, x, FROM (VALUES (1, 1), (1, NULL), (2, NULL)) v(p, x) WINDOW w AS (PARTITION BY p ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) ORDER BY p, x; +-- the IGNORE NULLS scan must not read the row after a ROWS frame end +WITH t AS MATERIALIZED ( + SELECT NULLIF(i, 1) / (2 - i) AS v + FROM generate_series(1, 2) AS g(i) +) +SELECT first_value(v) IGNORE NULLS OVER (ROWS CURRENT ROW) +FROM t +LIMIT 1; -- volatile arguments cannot use the IGNORE NULLS nullness cache CREATE TEMPORARY SEQUENCE null_treatment_seq; -- 2.37.1 (Apple Git-137.1)