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 1vdJQv-00Gaoy-0s for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jan 2026 02:38:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vdJQt-00BQjU-1q for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jan 2026 02:38:00 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vdJQt-00BQjM-0s for pgsql-hackers@lists.postgresql.org; Wed, 07 Jan 2026 02:38:00 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vdJQs-004dqO-1s for pgsql-hackers@lists.postgresql.org; Wed, 07 Jan 2026 02:37:59 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 6072bse31654154; Tue, 6 Jan 2026 21:37:54 -0500 From: Tom Lane To: Richard Guo cc: Eric Ridge , Pg Hackers Subject: Re: Fwd: pg18 bug? SELECT query doesn't work In-reply-to: References: <7900964C-F99E-481E-BEE5-4338774CEB9F@gmail.com> <1607957.1767725532@sss.pgh.pa.us> Comments: In-reply-to Richard Guo message dated "Wed, 07 Jan 2026 11:26:27 +0900" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <1654152.1767753474.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Tue, 06 Jan 2026 21:37:54 -0500 Message-ID: <1654153.1767753474@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Richard Guo writes: > On Wed, Jan 7, 2026 at 3:52 AM Tom Lane wrote: >> I've not probed further than that, but my guess is that now we check >> for set-returning tlist items while the tlist still has grouping Vars, >> thus missing the fact that there's a SRF represented by one of those >> Vars. This prompts us to flatten a subquery we shouldn't have >> flattened (because that ends by introducing a SRF into the outer >> WHERE). > The first part of your diagnosis is correct. This issue is caused by > a failure to notice the SRF in the target list, as the item is hidden > under a grouped Var. This doesn't lead to incorrect subquery > flattening though, since such a subquery must involve grouping, and > is_simple_subquery() would refuse to flatten it. Instead, it > incorrectly indicates that the subquery's restriction clause is safe > to push down, which mistakenly introduces SRFs into the subquery's > WHERE quals. Got it. > (I wonder whether this same issue exists for join alias Vars.) Seems highly likely that we'd have noticed if it did. I think flatten_join_alias_vars happens early enough that no interesting decisions have been made yet. I wonder whether we could fix the current problem by doing grouping-Var expansion earlier? I'm also wondering (don't recall the details of your patch) whether you are repeating eval_const_expressions after grouping-Var expansion. If not, there are going to be bugs there, like failure to handle named args in function calls. That could be another reason to make this happen earlier. regards, tom lane