Skip to content

Commit c604ed5

Browse files
committed
PREPARE and EXPLAIN need to copy the source query just like we recently
had to do in DECLARE CURSOR. AFAICS these are all the places affected. PREPARE case per example from Michael Fuhr, EXPLAIN case located by grepping for planner calls ...
1 parent f9a6ba1 commit c604ed5

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/backend/commands/explain.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994-5, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.127 2004/09/30 17:42:42 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.128 2004/12/12 20:17:06 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -75,6 +75,16 @@ ExplainQuery(ExplainStmt *stmt, DestReceiver *dest)
7575
List *rewritten;
7676
ListCell *l;
7777

78+
/*
79+
* Because the planner is not cool about not scribbling on its input,
80+
* we make a preliminary copy of the source querytree. This prevents
81+
* problems in the case that the EXPLAIN is in a portal or plpgsql
82+
* function and is executed repeatedly. (See also the same hack in
83+
* DECLARE CURSOR and PREPARE.) XXX the planner really shouldn't
84+
* modify its input ... FIXME someday.
85+
*/
86+
query = copyObject(query);
87+
7888
/* prepare for projection of tuples */
7989
tstate = begin_tup_output_tupdesc(dest, ExplainResultDesc(stmt));
8090

src/backend/commands/prepare.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Copyright (c) 2002-2004, PostgreSQL Global Development Group
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.34 2004/12/03 21:26:31 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.35 2004/12/12 20:17:06 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -48,6 +48,7 @@ void
4848
PrepareQuery(PrepareStmt *stmt)
4949
{
5050
const char *commandTag;
51+
Query *query;
5152
List *query_list,
5253
*plan_list;
5354

@@ -87,8 +88,18 @@ PrepareQuery(PrepareStmt *stmt)
8788
* the query.
8889
*/
8990

91+
/*
92+
* Because the planner is not cool about not scribbling on its input,
93+
* we make a preliminary copy of the source querytree. This prevents
94+
* problems in the case that the PREPARE is in a portal or plpgsql
95+
* function and is executed repeatedly. (See also the same hack in
96+
* DECLARE CURSOR and EXPLAIN.) XXX the planner really shouldn't
97+
* modify its input ... FIXME someday.
98+
*/
99+
query = copyObject(stmt->query);
100+
90101
/* Rewrite the query. The result could be 0, 1, or many queries. */
91-
query_list = QueryRewrite(stmt->query);
102+
query_list = QueryRewrite(query);
92103

93104
/* Generate plans for queries. Snapshot is already set. */
94105
plan_list = pg_plan_queries(query_list, NULL, false);

0 commit comments

Comments
 (0)