pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: holger.reise@vitagroup.ag
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #18284: Filter in left lateral join not respected
Date: Thu, 11 Jan 2024 12:30:39 -0500
Message-ID: <229608.1704994239@sss.pgh.pa.us> (raw)
In-Reply-To: <74950.1704988899@sss.pgh.pa.us>
References: <18284-47505a20c23647f8@postgresql.org>
	<74950.1704988899@sss.pgh.pa.us>

I wrote:
> Hmph ... this has been broken for a good long while.  Bisecting
> shows it gave the right answer before
> commit 4be058fe9ec5e630239b656af21fc083371f30ed
> so I'm betting that missed a condition about when it is safe to
> flatten RTE_RESULT RTEs.  Will look, thanks for the report!

Huh.  It looks like the oversight is actually even more ancient than
that, dating clear back to 9e7e29c75 of 2013-08-17.  That commit
recognized that lateral-reference Vars had to be wrapped in
PlaceHolderVars during subquery pullup, but failed to make the same
conclusion for PlaceHolderVars.  Somehow that didn't cause any visible
problems before 4be058fe9, or more likely we just didn't get any
relevant trouble reports.  This seems to be quite a rare situation:
spot testing says that we never reach this code for a PHV with
target_rte->lateral true in any of our regression tests.

The attached seems to be enough to fix it, though of course it needs
a regression test.

			regards, tom lane

Attachments:

  [text/x-diff] fix-PHV-wrap-rules-for-lateral-references.patch (743B, ../229608.1704994239@sss.pgh.pa.us/2-fix-PHV-wrap-rules-for-lateral-references.patch)
  download | inline diff:
diff --git a/src/backend/optimizer/prep/prepjointree.c b/src/backend/optimizer/prep/prepjointree.c
index d0df5374ef..aa83dd3636 100644
--- a/src/backend/optimizer/prep/prepjointree.c
+++ b/src/backend/optimizer/prep/prepjointree.c
@@ -2435,8 +2435,13 @@ pullup_replace_vars_callback(Var *var,
 			else if (newnode && IsA(newnode, PlaceHolderVar) &&
 					 ((PlaceHolderVar *) newnode)->phlevelsup == 0)
 			{
-				/* No need to wrap a PlaceHolderVar with another one, either */
-				wrap = false;
+				/* The same rules apply for a PlaceHolderVar */
+				if (rcon->target_rte->lateral &&
+					!bms_is_subset(((PlaceHolderVar *) newnode)->phrels,
+								   rcon->relids))
+					wrap = true;
+				else
+					wrap = false;
 			}
 			else
 			{

view thread (5+ messages)  latest in thread

Message-ID: <229608.1704994239@sss.pgh.pa.us>
Permalink:  ../229608.1704994239@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/229608.1704994239@sss.pgh.pa.us

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, holger.reise@vitagroup.ag, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #18284: Filter in left lateral join not respected
  In-Reply-To: <229608.1704994239@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox