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 1vdWtu-001o8L-1i for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jan 2026 17:00:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vdWtt-00Ewlp-1E for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jan 2026 17:00:50 +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 1vdWtt-00Ewlg-0F for pgsql-hackers@lists.postgresql.org; Wed, 07 Jan 2026 17:00:49 +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 1vdWts-004kBT-1B for pgsql-hackers@lists.postgresql.org; Wed, 07 Jan 2026 17:00:49 +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 607H0gt31752325; Wed, 7 Jan 2026 12:00:42 -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> <1654153.1767753474@sss.pgh.pa.us> Comments: In-reply-to Richard Guo message dated "Wed, 07 Jan 2026 16:31:27 +0900" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <1752323.1767805242.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Wed, 07 Jan 2026 12:00:42 -0500 Message-ID: <1752324.1767805242@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 11:37 AM Tom Lane wrote: >> 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. > Currently we're not repeating eval_const_expressions after the > grouping-Var expansion, but I fail to wrap my head around why that > would be a problem. What I was mainly concerned about was whether the replacement expressions ever got passed through eval_const_expressions(). I see now that they do, at planner.c:1069. It's still not really desirable that this is done separately; for example, I think it breaks the assumption that we will have AND/OR flatness everywhere. But I think that only leads to possible inefficiencies not wrong answers. And I do take your point about needing to preserve the separate identities of these subexpressions. So let's let that go for now. The main problem, as you say, is that allpaths.c is coming to conclusions about the contents of output expressions of the subquery without having done any of this. The only really simple answer I can see is to make a copy of the subquery's tlist and apply these transformations to it before we run the subquery_is_pushdown_safe logic. That's ... ugly. Perhaps another idea could be to shove the responsibility for this down into subquery_planner (or make it call a callback at the right point), and handle transferring of parent restriction clauses into HAVING only after we've finished preprocessing the subquery's tlist. That's an uncomfortably big change to be making in a released branch, but it might still be a better way than duplicating preprocessing. regards, tom lane