Copy sub-Query nodes to avoid trouble when same sub-Query is linked to
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 6 Aug 2000 04:13:22 +0000 (04:13 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 6 Aug 2000 04:13:22 +0000 (04:13 +0000)
multiple times in the parsetree (can happen in COALESCE or BETWEEN
contexts, for example).  This is a pretty grotty solution --- it will
do for now, but perhaps we can do better when we redesign querytrees.
What we need is a consistent policy about whether querytrees should be
considered read-only structures or not ...

src/backend/optimizer/plan/subselect.c

index 515db0fbce2d738a0eaab0bec08653c90e295020..1b8e19f0b5c99e4a73c84e7b7e7154ebaa0ef50c 100644 (file)
@@ -134,13 +134,24 @@ make_subplan(SubLink *slink)
 
        PlannerQueryLevel++;            /* we become child */
 
-       /* Check to see if this node was already processed; if so we have
-        * trouble.  Someday should change tree representation so that we can
-        * cope with multiple links to the same subquery, but for now...
+       /*
+        * Check to see if this node was already processed; if so we have
+        * trouble.  We check to see if the linked-to Query appears to have
+        * been planned already, too.
         */
        if (subquery == NULL)
+               elog(ERROR, "make_subplan: invalid expression structure (SubLink already processed?)");
+       if (subquery->base_rel_list != NIL)
                elog(ERROR, "make_subplan: invalid expression structure (subquery already processed?)");
 
+       /*
+        * Copy the source Query node.  This is a quick and dirty kluge to resolve
+        * the fact that the parser can generate trees with multiple links to the
+        * same sub-Query node, but the planner wants to scribble on the Query.
+        * Try to clean this up when we do querytree redesign...
+        */
+       subquery = (Query *) copyObject(subquery);
+
        /*
         * For an EXISTS subplan, tell lower-level planner to expect that only
         * the first tuple will be retrieved.  For ALL and ANY subplans, we