Ensure dummy paths have correct required_outer if rel is parameterized.
authorTom Lane
Thu, 14 Mar 2019 16:16:09 +0000 (12:16 -0400)
committerTom Lane
Thu, 14 Mar 2019 16:16:09 +0000 (12:16 -0400)
The assertions added by commits 34ea1ab7f et al found another problem:
set_dummy_rel_pathlist and mark_dummy_rel were failing to label
the dummy paths they create with the correct outer_relids, in case
the relation is necessarily parameterized due to having lateral
references in its tlist.  It's likely that this has no user-visible
consequences in production builds, at the moment; but still an assertion
failure is a bad thing, so back-patch the fix.

Per bug #15694 from Roman Zharkov (via Alexander Lakhin)
and an independent report by Tushar Ahuja.

Discussion: https://postgr.es/m/15694-74f2ca97e7044f7f@postgresql.org
Discussion: https://postgr.es/m/7d72ab20-c725-3ce2-f99d-4e64dd8a0de6@enterprisedb.com

src/backend/optimizer/path/allpaths.c
src/backend/optimizer/path/joinrels.c
src/test/regress/expected/join.out
src/test/regress/sql/join.sql

index 2091f715d3266003ccea91df6123e2256fee93fc..eef58b5dce8aa68b24d4a6a0229a465355e5f61c 100644 (file)
@@ -2022,7 +2022,8 @@ set_dummy_rel_pathlist(RelOptInfo *rel)
    rel->partial_pathlist = NIL;
 
    /* Set up the dummy path */
-   add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL, NULL,
+   add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL,
+                                             rel->lateral_relids,
                                              0, false, NIL, -1));
 
    /*
index 50fc0c500d03699ac32c3adb8e38c0fc46d6b37f..d3c25766b5bfdbc0f76a4492e32f0dcdf83ea934 100644 (file)
@@ -1260,7 +1260,8 @@ mark_dummy_rel(RelOptInfo *rel)
    rel->partial_pathlist = NIL;
 
    /* Set up the dummy path */
-   add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL, NULL,
+   add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL,
+                                             rel->lateral_relids,
                                              0, false, NIL, -1));
 
    /* Set or update cheapest_total_path and related fields */
index dc6262be43a70153ae10b5f69ed8b4cee0121597..d63cf442a1b07138c925656d1bd1df0dd6c18179 100644 (file)
@@ -5325,6 +5325,35 @@ select * from
          Output: 3
 (11 rows)
 
+-- check dummy rels with lateral references (bug #15694)
+explain (verbose, costs off)
+select * from int8_tbl i8 left join lateral
+  (select *, i8.q2 from int4_tbl where false) ss on true;
+              QUERY PLAN              
+--------------------------------------
+ Nested Loop Left Join
+   Output: i8.q1, i8.q2, f1, (i8.q2)
+   ->  Seq Scan on public.int8_tbl i8
+         Output: i8.q1, i8.q2
+   ->  Result
+         Output: f1, i8.q2
+         One-Time Filter: false
+(7 rows)
+
+explain (verbose, costs off)
+select * from int8_tbl i8 left join lateral
+  (select *, i8.q2 from int4_tbl i1, int4_tbl i2 where false) ss on true;
+               QUERY PLAN                
+-----------------------------------------
+ Nested Loop Left Join
+   Output: i8.q1, i8.q2, f1, f1, (i8.q2)
+   ->  Seq Scan on public.int8_tbl i8
+         Output: i8.q1, i8.q2
+   ->  Result
+         Output: f1, f1, i8.q2
+         One-Time Filter: false
+(7 rows)
+
 -- check handling of nested appendrels inside LATERAL
 select * from
   ((select 2 as v) union all (select 3 as v)) as q1
index d3ba2a1c339b5d9e6f39c2113a91ce25f94d727e..19eb782cb0f74a8fbff8fc50a6f0342b39c41fff 100644 (file)
@@ -1744,6 +1744,14 @@ select * from
     select * from (select 3 as z offset 0) z where z.z = x.x
   ) zz on zz.z = y.y;
 
+-- check dummy rels with lateral references (bug #15694)
+explain (verbose, costs off)
+select * from int8_tbl i8 left join lateral
+  (select *, i8.q2 from int4_tbl where false) ss on true;
+explain (verbose, costs off)
+select * from int8_tbl i8 left join lateral
+  (select *, i8.q2 from int4_tbl i1, int4_tbl i2 where false) ss on true;
+
 -- check handling of nested appendrels inside LATERAL
 select * from
   ((select 2 as v) union all (select 3 as v)) as q1