Re: Row pattern recognition

From: Tatsuo Ishii <ishii(at)postgresql(dot)org>
To: assam258(at)gmail(dot)com
Cc: zsolt(dot)parragi(at)percona(dot)com, sjjang112233(at)gmail(dot)com, vik(at)postgresfriends(dot)org, er(at)xs4all(dot)nl, jacob(dot)champion(at)enterprisedb(dot)com, david(dot)g(dot)johnston(at)gmail(dot)com, peter(at)eisentraut(dot)org, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Row pattern recognition
Date: 2026-04-10 01:18:47
Message-ID: 20260410.101847.595894887122872457.ishii@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Henson,

> 0006: Fix DEFINE expression handling in RPR window
> planning (revised)
>
> Integration tests in 0007 revealed two crashes:
> (1) subquery wrapping with outer aggregate causes
> WindowAgg removal when RPR window function output
> is unused, (2) RPR and non-RPR windows coexisting
> causes SIGSEGV from RPRNavExpr propagating to the
> wrong WindowAgg. This version extends the fix to
> extract only Var nodes via pull_var_clause()
> instead of adding the whole DEFINE expression to
> the targetlist. The allpaths.c guard is extended
> to also preserve columns referenced by DEFINE
> clauses.

I took a look at this and have a question.

--- a/src/backend/optimizer/path/allpaths.c
+++ b/src/backend/optimizer/path/allpaths.c
@@ -4750,6 +4750,74 @@ remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel,
if (contain_volatile_functions(texpr))
continue;

+ /*
+ * If any RPR window clause references this column in its DEFINE
+ * clause, don't remove it. The DEFINE expression needs these columns
+ * in the tuplestore slot for pattern matching evaluation, even if the
+ * outer query doesn't reference them.
+ */

Before this, there's a check in the function:

if (tle->ressortgroupref || tle->resjunk)
continue;

Below is the query in the regression test that is suppoed to trigger
the error. In this case column "i" in the target list should have
resjunk flag to be set to true. So I thought the if statenment above
becomes true and the column i is not removed from subquery's target
list. Am I missing something?

The test case in the patch:

-- Subquery wrapping: RPR window inside outer aggregate.
-- Tests that WindowAgg is not removed by remove_unused_subquery_outputs()
-- when DEFINE clause contains PREV/NEXT.
--
-- PREV in DEFINE + outer aggregate

--EXPLAIN
SELECT count(*) FROM (
SELECT count(*) OVER w FROM generate_series(1,10) i
WINDOW w AS (
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
PATTERN (A+)
DEFINE A AS i > PREV(i)
)
) t;

Also I think removal of the WindowAgg node is fine here because it's
not necessary to calculate the result of the sub query at
all. Actually the query above runs fine on v46. I guess some of the
incremental patches caused this to stop working. Can you elaborate?

Regards,
--
Tatsuo Ishii
SRA OSS K.K.
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tender Wang 2026-04-10 01:29:48 Re: Bug: var_is_nonnullable() gives wrong results for old/new in RETURNING
Previous Message Peter Smith 2026-04-10 01:05:09 Re: Add missing period to HINT messages