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.94.2) (envelope-from ) id 1uPpoT-00GvJO-US for pgsql-bugs@arkaria.postgresql.org; Thu, 12 Jun 2025 21:50:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uPpoQ-00GJv6-D5 for pgsql-bugs@arkaria.postgresql.org; Thu, 12 Jun 2025 21:50:19 +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.94.2) (envelope-from ) id 1uPpoQ-00GJuy-4K for pgsql-bugs@lists.postgresql.org; Thu, 12 Jun 2025 21:50:18 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uPpoO-001iAQ-0j for pgsql-bugs@lists.postgresql.org; Thu, 12 Jun 2025 21:50:18 +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 55CLoCZM1268646; Thu, 12 Jun 2025 17:50:12 -0400 From: Tom Lane To: Richard Guo cc: exclusion@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18953: Planner fails to build plan for complex query with LATERAL references In-reply-to: References: <18953-1c9883a9d4afeb30@postgresql.org> Comments: In-reply-to Richard Guo message dated "Wed, 11 Jun 2025 17:33:54 +0900" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1268644.1749765012.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 12 Jun 2025 17:50:12 -0400 Message-ID: <1268645.1749765012@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Richard Guo writes: > Thanks for the report. Here's a simplified repro. > ... > In this query, the join between t3 and s is placed into a separate > join sub-problem due to the from_collapse_limit. This join is deemed > not legal by join_is_legal(), as have_dangerous_phv() thinks the PHV > could pose a hazard as described in that function's comment. As a > result, no join could be built for this sub-problem. Bleah. > No idea how to fix this though. Any thoughts? My thought is that have_dangerous_phv() was never more than a quick-n-dirty kludge, and what we really ought to do is remove it. That means cleaning up the technical debt mentioned in 85e5e222b: In principle we could allow such a PlaceHolderVar to be evaluated at t= he lower join node using values passed down from the upper relation along= with values from the join's own outer relation. However, nodeNestloop.c on= ly supports simple Vars not arbitrary expressions as nestloop parameters. createplan.c is also a few bricks shy of being able to handle such cas= es; it misplaces the PlaceHolderVar parameters in the plan tree, which is = why the visible symptoms of this bug are "plan should not reference subpla= n's variable" and "failed to assign all NestLoopParams to plan nodes" plan= ner errors. Adding the necessary complexity to make this work doesn't seem like it would be repaid in significantly better plans, because in cases where = such a PHV exists, there is probably a corresponding join order constraint = that would allow a good plan to be found without using the star-schema exce= ption. Furthermore, adding complexity to nodeNestloop.c would create a run-ti= me penalty even for plans where this whole consideration is irrelevant. So let's just reject such paths instead. I think that the argument about executor complexity might be a red herring: if we can get the PHV to be evaluated in the tlist of the nestloop's outer relation, then the reference to it will still just be an outer Var in the NestLoopParam structure. I'm still poking at what we'd have to do to the planner to get that to happen, but my initial impression is that it might not be very complicated after all. regards, tom lane