Avoid scribbling on original parsetree during DECLARE CURSOR. This
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 28 Nov 2004 22:16:49 +0000 (22:16 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 28 Nov 2004 22:16:49 +0000 (22:16 +0000)
prevents problems when the DECLARE is in a portal and is executed
repeatedly, as is possible in v3 protocol.  Per analysis by Oliver
Jowett, though I didn't use his patch exactly.

src/backend/commands/portalcmds.c

index 5829879b7a2d577877341b61cab88e18babcc827..be5ca1105902619419d8b92f7e401661d5f425a4 100644 (file)
@@ -61,13 +61,22 @@ PerformCursorOpen(DeclareCursorStmt *stmt)
        if (!(stmt->options & CURSOR_OPT_HOLD))
                RequireTransactionChain((void *) stmt, "DECLARE CURSOR");
 
+       /*
+        * Because the planner is not cool about not scribbling on its input,
+        * we make a preliminary copy of the source querytree.  This prevents
+        * problems in the case that the DECLARE CURSOR is in a portal and is
+        * executed repeatedly.  XXX the planner really shouldn't modify its
+        * input ... FIXME someday.
+        */
+       query = copyObject(stmt->query);
+
        /*
         * The query has been through parse analysis, but not rewriting or
         * planning as yet.  Note that the grammar ensured we have a SELECT
         * query, so we are not expecting rule rewriting to do anything
         * strange.
         */
-       rewritten = QueryRewrite((Query *) stmt->query);
+       rewritten = QueryRewrite(query);
        if (length(rewritten) != 1 || !IsA(lfirst(rewritten), Query))
                elog(ERROR, "unexpected rewrite result");
        query = (Query *) lfirst(rewritten);