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 1uRZBV-00DTar-ER for pgsql-bugs@arkaria.postgresql.org; Tue, 17 Jun 2025 16:29:17 +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 1uRZBT-00BCXy-GS for pgsql-bugs@arkaria.postgresql.org; Tue, 17 Jun 2025 16:29:16 +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 1uRZBT-00BCXq-7y for pgsql-bugs@lists.postgresql.org; Tue, 17 Jun 2025 16:29:15 +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 1uRZBR-002Zh0-24 for pgsql-bugs@lists.postgresql.org; Tue, 17 Jun 2025 16:29:15 +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 55HGTBFx835660; Tue, 17 Jun 2025 12:29:11 -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> <1268645.1749765012@sss.pgh.pa.us> <1499514.1749848324@sss.pgh.pa.us> <527134.1750087495@sss.pgh.pa.us> Comments: In-reply-to Richard Guo message dated "Tue, 17 Jun 2025 11:20:20 +0900" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <835658.1750177751.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Tue, 17 Jun 2025 12:29:11 -0400 Message-ID: <835659.1750177751@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Richard Guo writes: > On Tue, Jun 17, 2025 at 12:24 AM Tom Lane wrote: >> In the only test case I have that exercises this >> logic, both paths are marked with the required_outer anyway, so it >> doesn't seem to matter. If we can find a case where that's not so, >> then probably we should do it the other way. > I tried to find such a case but didn't succeed; though I suspect > that's simply because I haven't tried hard enough. Conceptually, I'm > thinking of a query where 1) A laterally references both B and C while > B does not reference any other relation, and 2) A has a PHV parameter > whose ph_eval_at includes both B and C. In such a query, the B/A join > path is parameterized by C, while its left path B is not parameterized > at all. In order to add the PHV to B's tlist, we'll need to consider > the join path's required-outer rels not just those of the left path. After thinking about this for awhile, I'm not seeing how that could happen. A PHV with ph_eval_at exceeding its syntactic scope could only be created via something like B left join lateral (select coalesce(B.x, ...) as Q from D) C that is we need a non-strict targetlist expression that references something outside the sub-select proper. In this case all paths created for C will have required_outer mentioning B, because there's no way to compute C's reltarget without an outer reference to B. This might be embedded in ... join A on A.y = C.Q which gives rise to the situation you describe --- but there's no possibility of the planner trying to do this in the order C join (B join A) because it will think that all paths for C require B on the outside. It will only consider B join (C join A) and the path for C will show B as required_outer. So I'm inclined to leave that code as I had it. It's notationally a bit simpler and it doesn't require assuming that we can ignore the path's required_outer marking at this stage. If I'm wrong, someone will eventually find a counterexample and we can fix it then; the changes won't be large. regards, tom lane