1616 *
1717 *
1818 * IDENTIFICATION
19- * $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.14 2003/11/29 19:51:51 pgsql Exp $
19+ * $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.15 2004/01/10 00:30:21 tgl Exp $
2020 *
2121 *-------------------------------------------------------------------------
2222 */
@@ -168,10 +168,11 @@ pull_up_subqueries(Query *parse, Node *jtnode, bool below_outer_join)
168168List * rt ;
169169
170170/*
171- * First make a modifiable copy of the subquery. This avoids
172- * problems if the same subquery is referenced from multiple
173- * jointree items (which can't happen normally, but might after
174- * rule rewriting).
171+ * Need a modifiable copy of the subquery to hack on. Even if
172+ * we didn't sometimes choose not to pull up below, we must do
173+ * this to avoid problems if the same subquery is referenced from
174+ * multiple jointree items (which can't happen normally, but might
175+ * after rule rewriting).
175176 */
176177subquery = copyObject (subquery );
177178
@@ -196,6 +197,33 @@ pull_up_subqueries(Query *parse, Node *jtnode, bool below_outer_join)
196197pull_up_subqueries (subquery , (Node * )subquery -> jointree ,
197198 false);
198199
200+ /*
201+ * Now we must recheck whether the subquery is still simple
202+ * enough to pull up. If not, abandon processing it.
203+ *
204+ * We don't really need to recheck all the conditions involved,
205+ * but it's easier just to keep this "if" looking the same as
206+ * the one above.
207+ */
208+ if (is_simple_subquery (subquery )&&
209+ (!below_outer_join || has_nullable_targetlist (subquery ))&&
210+ !contain_whole_tuple_var ((Node * )parse ,varno ,0 ))
211+ {
212+ /* good to go */
213+ }
214+ else
215+ {
216+ /*
217+ * Give up, return unmodified RangeTblRef.
218+ *
219+ * Note: The work we just did will be redone when the
220+ * subquery gets planned on its own. Perhaps we could avoid
221+ * that by storing the modified subquery back into the
222+ * rangetable, but I'm not gonna risk it now.
223+ */
224+ return jtnode ;
225+ }
226+
199227/*
200228 * Adjust level-0 varnos in subquery so that we can append its
201229 * rangetable to upper query's.