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.96) (envelope-from ) id 1wB0Xf-000eH9-1X for pgsql-hackers@arkaria.postgresql.org; Fri, 10 Apr 2026 01:20:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wB0Wd-008dtC-2w for pgsql-hackers@arkaria.postgresql.org; Fri, 10 Apr 2026 01:19:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wB0Wd-008dt4-21 for pgsql-hackers@lists.postgresql.org; Fri, 10 Apr 2026 01:19:12 +0000 Received: from meldrar.postgresql.org ([2a02:c0:301:0:ffff::31]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wB0Wb-00000000Gzw-3b8H for pgsql-hackers@postgresql.org; Fri, 10 Apr 2026 01:19:12 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Content-Transfer-Encoding:Content-Type: Mime-Version:References:In-Reply-To:From:Subject:Cc:To:Message-Id:Date:Sender :Reply-To:Content-ID:Content-Description; bh=Gfq3HLi+SEEfjSaRdRKtmgPsJDGOrIl9kTiWvlY4PEg=; b=d8IZQDdvxVfE+ULNqL0zHBGKgX KXGWxBitA7eoEUVs4y0NHlkRmPtcKnXyrH66dDKjEi9buIFavNAOR3XxlbvHHtpRrRRiXu7WVQoGx UARenzBCtu/K/olrfXgQRyMEc1P+LKslMqcsjFn40zRO4207ynp25G5T6B9CMiEL0KkOJWPe955Nd eVrSHxSqUjGWAT7sFaVilGbNLqTnhPmDndU6w9l2udbhJkAk47e+MYK5HwbM2xe+z/RyxpV4JIQCp rbBS+MYcU4hrp6FR3LgG7kamc9A1GDAOtVAAReqaJosqJAcWLYcf2IL3msg0SXcY2P2ZPS5TsMhSn qi5qdIlA==; Received: from [2409:11:4120:300:9320:d8d7:e50e:a6cc] (helo=localhost) by meldrar.postgresql.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wB0WT-000pAB-2e; Fri, 10 Apr 2026 01:19:03 +0000 Date: Fri, 10 Apr 2026 10:18:47 +0900 (JST) Message-Id: <20260410.101847.595894887122872457.ishii@postgresql.org> To: assam258@gmail.com Cc: zsolt.parragi@percona.com, sjjang112233@gmail.com, vik@postgresfriends.org, er@xs4all.nl, jacob.champion@enterprisedb.com, david.g.johnston@gmail.com, peter@eisentraut.org, pgsql-hackers@postgresql.org Subject: Re: Row pattern recognition From: Tatsuo Ishii In-Reply-To: References: <20260330.133428.1197197778850463943.ishii@postgresql.org> X-Mailer: Mew version 6.8 on Emacs 29.3 Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2409:11:4120:300:9320:d8d7:e50e:a6cc (failed) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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