Allow subquery pullup to wrap a PlaceHolderVar in another one.
authorTom Lane <[email protected]>
Thu, 11 Jan 2024 20:28:13 +0000 (15:28 -0500)
committerTom Lane <[email protected]>
Thu, 11 Jan 2024 20:28:13 +0000 (15:28 -0500)
The code for wrapping subquery output expressions in PlaceHolderVars
believed that if the expression already was a PlaceHolderVar, it was
never necessary to wrap that in another one.  That's wrong if the
expression is underneath an outer join and involves a lateral
reference to outside that scope: failing to add an additional PHV
risks evaluating the expression at the wrong place and hence not
forcing it to null when the outer join should do so.  This is an
oversight in commit 9e7e29c75, which added logic to forcibly wrap
lateral-reference Vars in PlaceHolderVars, but didn't see that the
adjacent case for PlaceHolderVars needed the same treatment.

The test case we have for this doesn't fail before 4be058fe9, but now
that I see the problem I wonder if it is possible to demonstrate
related errors before that.  That's moot though, since all such
branches are out of support.

Per bug #18284 from Holger Reise.  Back-patch to all supported
branches.

Discussion: https://postgr.es/m/18284-47505a20c23647f8@postgresql.org

src/backend/optimizer/prep/prepjointree.c
src/test/regress/expected/join.out
src/test/regress/sql/join.sql

index 999bd3ec56a2665343dca7b375815a10c87a4442..388694e7095093a9f1b9d1fa42943255c758dbae 100644 (file)
@@ -2365,8 +2365,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 if (rcon->wrap_non_vars)
            {
index d7153a6fd9088df59b8fcad090688c3b5a1f49ba..0638c2d3ce7fd4679ab90c86849c0ccc2c108b41 100644 (file)
@@ -5696,6 +5696,32 @@ select * from
          Output: (COALESCE((COALESCE(b.q2, '42'::bigint)), d.q2))
 (24 rows)
 
+-- another case requiring nested PlaceHolderVars
+explain (verbose, costs off)
+select * from
+  (select 0 as val0) as ss0
+  left join (select 1 as val) as ss1 on true
+  left join lateral (select ss1.val as val_filtered where false) as ss2 on true;
+           QUERY PLAN           
+--------------------------------
+ Nested Loop Left Join
+   Output: 0, (1), ((1))
+   ->  Result
+         Output: 1
+   ->  Result
+         Output: (1)
+         One-Time Filter: false
+(7 rows)
+
+select * from
+  (select 0 as val0) as ss0
+  left join (select 1 as val) as ss1 on true
+  left join lateral (select ss1.val as val_filtered where false) as ss2 on true;
+ val0 | val | val_filtered 
+------+-----+--------------
+    0 |   1 |             
+(1 row)
+
 -- case that breaks the old ph_may_need optimization
 explain (verbose, costs off)
 select c.*,a.*,ss1.q1,ss2.q1,ss3.* from
index 92ae4ca79c7804ae581ff8c8baf9d70a214175f2..1795ed823532c651afa04acc3644dd6ddde3532f 100644 (file)
@@ -1948,6 +1948,18 @@ select * from
   ) on c.q2 = ss2.q1,
   lateral (select ss2.y offset 0) ss3;
 
+-- another case requiring nested PlaceHolderVars
+explain (verbose, costs off)
+select * from
+  (select 0 as val0) as ss0
+  left join (select 1 as val) as ss1 on true
+  left join lateral (select ss1.val as val_filtered where false) as ss2 on true;
+
+select * from
+  (select 0 as val0) as ss0
+  left join (select 1 as val) as ss1 on true
+  left join lateral (select ss1.val as val_filtered where false) as ss2 on true;
+
 -- case that breaks the old ph_may_need optimization
 explain (verbose, costs off)
 select c.*,a.*,ss1.q1,ss2.q1,ss3.* from