OSDN Git Service

Fix oversight in join removal patch: we have to delete the removed relation
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 23 May 2010 16:34:38 +0000 (16:34 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 23 May 2010 16:34:38 +0000 (16:34 +0000)
from SpecialJoinInfo relid sets as well.  Per example from Vaclav Novotny.

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

index da6482b..a06e647 100644 (file)
@@ -16,7 +16,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/plan/analyzejoins.c,v 1.1 2010/03/28 22:59:32 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/plan/analyzejoins.c,v 1.2 2010/05/23 16:34:38 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -344,6 +344,24 @@ remove_rel_from_query(PlannerInfo *root, int relid)
        }
 
        /*
+        * Likewise remove references from SpecialJoinInfo data structures.
+        *
+        * This is relevant in case the outer join we're deleting is nested
+        * inside other outer joins: the upper joins' relid sets have to be
+        * adjusted.  The RHS of the target outer join will be made empty here,
+        * but that's OK since caller will delete that SpecialJoinInfo entirely.
+        */
+       foreach(l, root->join_info_list)
+       {
+               SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
+
+               sjinfo->min_lefthand = bms_del_member(sjinfo->min_lefthand, relid);
+               sjinfo->min_righthand = bms_del_member(sjinfo->min_righthand, relid);
+               sjinfo->syn_lefthand = bms_del_member(sjinfo->syn_lefthand, relid);
+               sjinfo->syn_righthand = bms_del_member(sjinfo->syn_righthand, relid);
+       }
+
+       /*
         * Likewise remove references from PlaceHolderVar data structures.
         *
         * Here we have a special case: if a PHV's eval_at set is just the target
index 5fd7d79..6dfc710 100644 (file)
@@ -2525,6 +2525,20 @@ explain (costs off)
  Seq Scan on a
 (1 row)
 
+-- check optimization of outer join within another special join
+explain (costs off)
+select id from a where id in (
+       select b.id from b left join c on b.id = c.id
+);
+         QUERY PLAN         
+----------------------------
+ Hash Semi Join
+   Hash Cond: (a.id = b.id)
+   ->  Seq Scan on a
+   ->  Hash
+         ->  Seq Scan on b
+(5 rows)
+
 rollback;
 create temp table parent (k int primary key, pd int);
 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "parent_pkey" for table "parent"
index d627973..8657636 100644 (file)
@@ -588,6 +588,12 @@ explain (costs off)
   SELECT a.* FROM a LEFT JOIN (b left join c on b.c_id = c.id)
   ON (a.b_id = b.id);
 
+-- check optimization of outer join within another special join
+explain (costs off)
+select id from a where id in (
+       select b.id from b left join c on b.id = c.id
+);
+
 rollback;
 
 create temp table parent (k int primary key, pd int);