Re: BUG #18284: Filter in left lateral join not respected - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #18284: Filter in left lateral join not respected
Date
Msg-id 229608.1704994239@sss.pgh.pa.us
Whole thread Raw
In response to Re: BUG #18284: Filter in left lateral join not respected  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: BUG #18284: Filter in left lateral join not respected  (Richard Guo <guofenglinux@gmail.com>)
List pgsql-bugs
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

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
             {

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #18284: Filter in left lateral join not respected
Next
From: Tom Lane
Date:
Subject: Re: BUG #17798: Incorrect memory access occurs when using BEFORE ROW UPDATE trigger