Don't try to optimize EXISTS subqueries with empty FROM-lists: we need to
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 8 Dec 2008 00:16:09 +0000 (00:16 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 8 Dec 2008 00:16:09 +0000 (00:16 +0000)
form a join and that case doesn't have anything to join to.  (We could
probably make it work if we didn't pull up the subquery, but it seems to
me that the case isn't worth extra code.)  Per report from Greg Stark.

src/backend/optimizer/plan/subselect.c

index 1bb61c34a58b2b0b60220df5c300ea544fa3db30..7fa38f6a78999d7a5123947e91b271a034ab3337 100644 (file)
@@ -1097,6 +1097,12 @@ convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink,
        if (!simplify_EXISTS_query(subselect))
                return false;
 
+       /*
+        * The subquery must have a nonempty jointree, else we won't have a join.
+        */
+       if (subselect->jointree->fromlist == NIL)
+               return false;
+
        /*
         * Separate out the WHERE clause.  (We could theoretically also remove
         * top-level plain JOIN/ON clauses, but it's probably not worth the
@@ -1180,6 +1186,7 @@ convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink,
        /* Identify all the rels syntactically within the subselect */
        subselect_varnos = get_relids_in_jointree((Node *) subselect->jointree,
                                                                                          true);
+       Assert(!bms_is_empty(subselect_varnos));
        Assert(bms_is_subset(right_varnos, subselect_varnos));
 
        /* Now we can attach the modified subquery rtable to the parent */