|
8 | 8 | *
|
9 | 9 | *
|
10 | 10 | * 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 $ |
12 | 12 | *
|
13 | 13 | * NOTES
|
14 | 14 | * this is the "main" module of the postgres backend and
|
@@ -730,31 +730,49 @@ List *
|
730 | 730 | pg_plan_queries(List *querytrees, int cursorOptions, ParamListInfo boundParams,
|
731 | 731 | bool needSnapshot)
|
732 | 732 | {
|
733 |
| - List *stmt_list = NIL; |
734 |
| - ListCell *query_list; |
| 733 | + List * volatile stmt_list = NIL; |
| 734 | + Snapshot saveActiveSnapshot = ActiveSnapshot; |
735 | 735 |
|
736 |
| - foreach(query_list, querytrees) |
| 736 | + /* PG_TRY to ensure previous ActiveSnapshot is restored on error */ |
| 737 | + PG_TRY(); |
737 | 738 | {
|
738 |
| - Query *query = (Query *) lfirst(query_list); |
739 |
| - Node *stmt; |
| 739 | + Snapshot mySnapshot = NULL; |
| 740 | + ListCell *query_list; |
740 | 741 |
|
741 |
| - if (query->commandType == CMD_UTILITY) |
| 742 | + foreach(query_list, querytrees) |
742 | 743 | {
|
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 |
749 | 753 | {
|
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); |
752 | 761 | }
|
753 |
| - stmt = (Node *) pg_plan_query(query, cursorOptions, boundParams); |
| 762 | + |
| 763 | + stmt_list = lappend(stmt_list, stmt); |
754 | 764 | }
|
755 | 765 |
|
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(); |
757 | 773 | }
|
| 774 | + PG_END_TRY(); |
| 775 | + ActiveSnapshot = saveActiveSnapshot; |
758 | 776 |
|
759 | 777 | return stmt_list;
|
760 | 778 | }
|
|
0 commit comments