Remove leftover code in deconstruct_distribute_oj_quals(). master
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 7 Feb 2023 16:56:43 +0000 (11:56 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 7 Feb 2023 16:56:43 +0000 (11:56 -0500)
The initial "put back OJ relids" adjustment of ojscope was
incorrect and unnecessary; it seems to be a leftover from
when I (tgl) was trying to get this function to work at all.

Richard Guo

Discussion: https://postgr.es/m/CAMbWs4-L2C47ZGZPabBAi5oDZsKmsbvhYcGCy5o=gCjsaG_ZQA@mail.gmail.com

src/backend/optimizer/plan/initsplan.c
src/test/regress/expected/join.out
src/test/regress/sql/join.sql

index 2d9aa85e591a740ae5b0341f65f2f5ebd5ee7fd2..904f710d5931a71649c84711b60d8d3ab72b32b2 100644 (file)
@@ -1903,13 +1903,6 @@ deconstruct_distribute_oj_quals(PlannerInfo *root,
        int         save_last_rinfo_serial;
        ListCell   *lc;
 
-       /*
-        * Put any OJ relids that were removed from min_righthand back into
-        * ojscope, else distribute_qual_to_rels will complain.
-        */
-       ojscope = bms_join(ojscope, bms_intersect(sjinfo->commute_below,
-                                                 sjinfo->syn_righthand));
-
        /* Identify the outer joins this one commutes with */
        joins_above = sjinfo->commute_above_r;
        joins_below = bms_intersect(sjinfo->commute_below,
index 07f5aad5ea4b190bcaecaab42691aa1e6b46f1f4..eea8978fad458e6996461b3c03a6c91df5197918 100644 (file)
@@ -4982,6 +4982,32 @@ select id from a where id in (
          ->  Seq Scan on b
 (5 rows)
 
+-- check optimization with oddly-nested outer joins
+explain (costs off)
+select a1.id from
+  (a a1 left join a a2 on true)
+  left join
+  (a a3 left join a a4 on a3.id = a4.id)
+  on a2.id = a3.id;
+          QUERY PLAN          
+------------------------------
+ Nested Loop Left Join
+   ->  Seq Scan on a a1
+   ->  Materialize
+         ->  Seq Scan on a a2
+(4 rows)
+
+explain (costs off)
+select a1.id from
+  (a a1 left join a a2 on a1.id = a2.id)
+  left join
+  (a a3 left join a a4 on a3.id = a4.id)
+  on a2.id = a3.id;
+    QUERY PLAN    
+------------------
+ Seq Scan on a a1
+(1 row)
+
 -- check that join removal works for a left join when joining a subquery
 -- that is guaranteed to be unique by its GROUP BY clause
 explain (costs off)
index 7157b5ccccac57c43d2b2d3a752f363641e112c9..9d20b88d71d4a88eac39c8dfe710aeb99b5ab23b 100644 (file)
@@ -1770,6 +1770,21 @@ select id from a where id in (
    select b.id from b left join c on b.id = c.id
 );
 
+-- check optimization with oddly-nested outer joins
+explain (costs off)
+select a1.id from
+  (a a1 left join a a2 on true)
+  left join
+  (a a3 left join a a4 on a3.id = a4.id)
+  on a2.id = a3.id;
+
+explain (costs off)
+select a1.id from
+  (a a1 left join a a2 on a1.id = a2.id)
+  left join
+  (a a3 left join a a4 on a3.id = a4.id)
+  on a2.id = a3.id;
+
 -- check that join removal works for a left join when joining a subquery
 -- that is guaranteed to be unique by its GROUP BY clause
 explain (costs off)