diff --git a/src/backend/optimizer/plan/subselect.c b/src/backend/optimizer/plan/subselect.c index cd6b32a77f07c05e59f8a8c3dffe7e530c6b185f..78f6b6eec9b41b2a3bd234079338d479ebcfc865 100644 --- a/src/backend/optimizer/plan/subselect.c +++ b/src/backend/optimizer/plan/subselect.c @@ -9,7 +9,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.148 2009/04/05 19:59:40 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.150.2.2 2010/01/18 18:17:52 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1369,6 +1369,17 @@ convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink, if (has_correlation_in_funcexpr_rte(subselect->rtable)) return NULL; + /* + * 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; + /* * If deeply correlated, don't bother. */