88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.100 2001/01/24 19 :42:59 momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.101 2001/01/27 04 :42:32 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -279,7 +279,13 @@ pull_up_subqueries(Query *parse, Node *jtnode)
279279/*
280280 * First, recursively pull up the subquery's subqueries,
281281 * so that this routine's processing is complete for its
282- * jointree and rangetable.
282+ * jointree and rangetable. NB: if the same subquery is
283+ * referenced from multiple jointree items (which can't happen
284+ * normally, but might after rule rewriting), then we will invoke
285+ * this processing multiple times on that subquery. OK because
286+ * nothing will happen after the first time. We do have to be
287+ * careful to copy everything we pull up, however, or risk
288+ * having chunks of structure multiply linked.
283289 */
284290subquery -> jointree = (FromExpr * )
285291pull_up_subqueries (subquery , (Node * )subquery -> jointree );
@@ -288,7 +294,8 @@ pull_up_subqueries(Query *parse, Node *jtnode)
288294 * no adjustments will be needed in the subquery's rtable).
289295 */
290296rtoffset = length (parse -> rtable );
291- parse -> rtable = nconc (parse -> rtable ,subquery -> rtable );
297+ parse -> rtable = nconc (parse -> rtable ,
298+ copyObject (subquery -> rtable ));
292299/*
293300 * Make copies of the subquery's jointree and targetlist
294301 * with varnos adjusted to match the merged rangetable.