Re: BUG #18953: Planner fails to build plan for complex query with LATERAL references

Alexander Lakhin <exclusion@gmail.com>

From: Alexander Lakhin <exclusion@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-bugs@lists.postgresql.org, Richard Guo <guofenglinux@gmail.com>
Date: 2025-06-24T07:00:00Z
Lists: pgsql-bugs
Hello Tom and Richard,

23.06.2025 21:24, Tom Lane wrote:
> Anyway, with one eye on the possibility that we'll need to back-patch
> this in some form, I went for the localized fix of ensuring that
> we use a copy of the PHV that's been through the normal processing.
> I do want to look into restructuring the handling of these PHVs
> to make this less messy, but that's not a job to undertake for v18
> (much less if we have to back-patch).

I've managed to discover one more anomaly introduced by a16ef313f, that is
not fixed by fix-bug-18953-some-more:
CREATE TABLE t(i int PRIMARY KEY);

MERGE INTO t
USING
   (SELECT 1 AS j FROM generate_series(1, 1))
   RIGHT JOIN (SELECT 1) ON true
   LEFT JOIN (SELECT 1 FROM (SELECT 1 FROM generate_series(1, 1))) ON false
ON i = j
WHEN NOT MATCHED THEN INSERT VALUES (1);
ERROR:  XX000: wrong phnullingrels (b) (expected (b 4)) for PlaceHolderVar 1
LOCATION:  search_indexed_tlist_for_phv, setrefs.c:2958

Thank you for spending your time on this!

Best regards,
Alexander

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Fix some new issues with planning of PlaceHolderVars.

  2. Remove planner's have_dangerous_phv() join-order restriction.