From 5da9da71c44f27ba48fdad08ef263bf70e43e689 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Mon, 12 May 2008 20:02:02 +0000 Subject: Improve snapshot manager by keeping explicit track of snapshots. There are two ways to track a snapshot: there's the "registered" list, which is used for arbitrary long-lived snapshots; and there's the "active stack", which is used for the snapshot that is considered "active" at any time. This also allows users of snapshots to stop worrying about snapshot memory allocation and freeing, and about using PG_TRY blocks around ActiveSnapshot assignment. This is all done automatically now. As a consequence, this allows us to reset MyProc->xmin when there are no more snapshots registered in the current backend, reducing the impact that long-running transactions have on VACUUM. --- src/backend/tcop/postgres.c | 56 ++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 34 deletions(-) (limited to 'src/backend/tcop/postgres.c') diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index d9599851c59..d212eb9449e 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.551 2008/05/12 00:00:50 alvherre Exp $ + * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.552 2008/05/12 20:02:01 alvherre Exp $ * * NOTES * this is the "main" module of the postgres backend and @@ -732,49 +732,37 @@ List * pg_plan_queries(List *querytrees, int cursorOptions, ParamListInfo boundParams, bool needSnapshot) { - List * volatile stmt_list = NIL; - Snapshot saveActiveSnapshot = ActiveSnapshot; + List *stmt_list = NIL; + ListCell *query_list; + bool snapshot_set = false; - /* PG_TRY to ensure previous ActiveSnapshot is restored on error */ - PG_TRY(); + foreach(query_list, querytrees) { - Snapshot mySnapshot = NULL; - ListCell *query_list; + Query *query = (Query *) lfirst(query_list); + Node *stmt; - foreach(query_list, querytrees) + if (query->commandType == CMD_UTILITY) { - Query *query = (Query *) lfirst(query_list); - Node *stmt; - - if (query->commandType == CMD_UTILITY) - { - /* Utility commands have no plans. */ - stmt = query->utilityStmt; - } - else + /* Utility commands have no plans. */ + stmt = query->utilityStmt; + } + else + { + if (needSnapshot && !snapshot_set) { - if (needSnapshot && mySnapshot == NULL) - { - mySnapshot = CopySnapshot(GetTransactionSnapshot()); - ActiveSnapshot = mySnapshot; - } - stmt = (Node *) pg_plan_query(query, cursorOptions, - boundParams); + PushActiveSnapshot(GetTransactionSnapshot()); + snapshot_set = true; } - stmt_list = lappend(stmt_list, stmt); + stmt = (Node *) pg_plan_query(query, cursorOptions, + boundParams); } - if (mySnapshot) - FreeSnapshot(mySnapshot); + stmt_list = lappend(stmt_list, stmt); } - PG_CATCH(); - { - ActiveSnapshot = saveActiveSnapshot; - PG_RE_THROW(); - } - PG_END_TRY(); - ActiveSnapshot = saveActiveSnapshot; + + if (snapshot_set) + PopActiveSnapshot(); return stmt_list; } -- cgit v1.2.3