]> granicus.if.org Git - postgresql/commitdiff
Fix an oversight in convert_EXISTS_sublink_to_join: we can't convert an
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 18 Jan 2010 18:17:52 +0000 (18:17 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 18 Jan 2010 18:17:52 +0000 (18:17 +0000)
EXISTS that contains a WITH clause.  This would usually lead to a
"could not find CTE" error later in planning, because the WITH wouldn't
get processed at all.  Noted while playing with an example from Ken Marshall.

src/backend/optimizer/plan/subselect.c

index 5a320c652f8ba0a09699cb31a96ecca4c492ed2a..f7b94a98ad1d13d21c116017b371f8168e6abbf1 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.150.2.1 2009/07/06 02:16:14 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.150.2.2 2010/01/18 18:17:52 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1119,6 +1119,17 @@ convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink,
 
        Assert(sublink->subLinkType == EXISTS_SUBLINK);
 
+       /*
+        * Can't flatten if it contains WITH.  (We could arrange to pull up the
+        * WITH into the parent query's cteList, but that risks changing the
+        * semantics, since a WITH ought to be executed once per associated query
+        * call.)  Note that convert_ANY_sublink_to_join doesn't have to reject
+        * this case, since it just produces a subquery RTE that doesn't have to
+        * get flattened into the parent query.
+        */
+       if (subselect->cteList)
+               return NULL;
+
        /*
         * Copy the subquery so we can modify it safely (see comments in
         * make_subplan).