Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit da6a707

Browse files
committed
Fix pg_plan_queries() to restore the previous setting of ActiveSnapshot
(probably NULL) before exiting. Up to now it's just left the variable as it set it, which means that after we're done processing the current client message, ActiveSnapshot is probably pointing at garbage (because this function is typically run in MessageContext which will get reset). There doesn't seem to have been any code path in which that mattered before 8.3, but now the plancache module might try to use the stale value if the next client message is a Bind for a prepared statement that is in need of replanning. Per report from Alex Hunsaker.
1 parent 0d540b0 commit da6a707

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

src/backend/tcop/postgres.c

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.544 2008/03/10 12:55:13 mha Exp $
11+
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.545 2008/03/12 23:58:27 tgl Exp $
1212
*
1313
* NOTES
1414
* this is the "main" module of the postgres backend and
@@ -730,31 +730,49 @@ List *
730730
pg_plan_queries(List *querytrees, int cursorOptions, ParamListInfo boundParams,
731731
bool needSnapshot)
732732
{
733-
List *stmt_list = NIL;
734-
ListCell *query_list;
733+
List * volatile stmt_list = NIL;
734+
Snapshot saveActiveSnapshot = ActiveSnapshot;
735735

736-
foreach(query_list, querytrees)
736+
/* PG_TRY to ensure previous ActiveSnapshot is restored on error */
737+
PG_TRY();
737738
{
738-
Query *query = (Query *) lfirst(query_list);
739-
Node *stmt;
739+
Snapshot mySnapshot = NULL;
740+
ListCell *query_list;
740741

741-
if (query->commandType == CMD_UTILITY)
742+
foreach(query_list, querytrees)
742743
{
743-
/* Utility commands have no plans. */
744-
stmt = query->utilityStmt;
745-
}
746-
else
747-
{
748-
if (needSnapshot)
744+
Query *query = (Query *) lfirst(query_list);
745+
Node *stmt;
746+
747+
if (query->commandType == CMD_UTILITY)
748+
{
749+
/* Utility commands have no plans. */
750+
stmt = query->utilityStmt;
751+
}
752+
else
749753
{
750-
ActiveSnapshot = CopySnapshot(GetTransactionSnapshot());
751-
needSnapshot = false;
754+
if (needSnapshot && mySnapshot == NULL)
755+
{
756+
mySnapshot = CopySnapshot(GetTransactionSnapshot());
757+
ActiveSnapshot = mySnapshot;
758+
}
759+
stmt = (Node *) pg_plan_query(query, cursorOptions,
760+
boundParams);
752761
}
753-
stmt = (Node *) pg_plan_query(query, cursorOptions, boundParams);
762+
763+
stmt_list = lappend(stmt_list, stmt);
754764
}
755765

756-
stmt_list = lappend(stmt_list, stmt);
766+
if (mySnapshot)
767+
FreeSnapshot(mySnapshot);
768+
}
769+
PG_CATCH();
770+
{
771+
ActiveSnapshot = saveActiveSnapshot;
772+
PG_RE_THROW();
757773
}
774+
PG_END_TRY();
775+
ActiveSnapshot = saveActiveSnapshot;
758776

759777
return stmt_list;
760778
}

0 commit comments

Comments
 (0)