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/commands/indexcmds.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'src/backend/commands/indexcmds.c') diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 37c5c416f52..05eb55e8699 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.175 2008/05/12 00:00:47 alvherre Exp $ + * $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.176 2008/05/12 20:01:59 alvherre Exp $ * *------------------------------------------------------------------------- */ @@ -484,6 +484,7 @@ DefineIndex(RangeVar *heapRelation, */ LockRelationIdForSession(&heaprelid, ShareUpdateExclusiveLock); + PopActiveSnapshot(); CommitTransactionCommand(); StartTransactionCommand(); @@ -542,7 +543,7 @@ DefineIndex(RangeVar *heapRelation, indexRelation = index_open(indexRelationId, RowExclusiveLock); /* Set ActiveSnapshot since functions in the indexes may need it */ - ActiveSnapshot = CopySnapshot(GetTransactionSnapshot()); + PushActiveSnapshot(GetTransactionSnapshot()); /* We have to re-build the IndexInfo struct, since it was lost in commit */ indexInfo = BuildIndexInfo(indexRelation); @@ -581,6 +582,9 @@ DefineIndex(RangeVar *heapRelation, heap_close(pg_index, RowExclusiveLock); + /* we can do away with our snapshot */ + PopActiveSnapshot(); + /* * Commit this transaction to make the indisready update visible. */ @@ -616,8 +620,8 @@ DefineIndex(RangeVar *heapRelation, * We also set ActiveSnapshot to this snap, since functions in indexes may * need a snapshot. */ - snapshot = CopySnapshot(GetTransactionSnapshot()); - ActiveSnapshot = snapshot; + snapshot = RegisterSnapshot(GetTransactionSnapshot()); + PushActiveSnapshot(snapshot); /* * Scan the index and the heap, insert any missing index entries. @@ -645,7 +649,7 @@ DefineIndex(RangeVar *heapRelation, * Also, GetCurrentVirtualXIDs never reports our own vxid, so we need not * check for that. */ - old_snapshots = GetCurrentVirtualXIDs(ActiveSnapshot->xmax, false, + old_snapshots = GetCurrentVirtualXIDs(snapshot->xmax, false, PROC_IS_AUTOVACUUM | PROC_IN_VACUUM); while (VirtualTransactionIdIsValid(*old_snapshots)) @@ -686,6 +690,12 @@ DefineIndex(RangeVar *heapRelation, */ CacheInvalidateRelcacheByRelid(heaprelid.relId); + /* we can now do away with our active snapshot */ + PopActiveSnapshot(); + + /* And we can remove the validating snapshot too */ + UnregisterSnapshot(snapshot); + /* * Last thing to do is release the session-level lock on the parent table. */ @@ -1453,6 +1463,7 @@ ReindexDatabase(const char *databaseName, bool do_system, bool do_user) heap_close(relationRelation, AccessShareLock); /* Now reindex each rel in a separate transaction */ + PopActiveSnapshot(); CommitTransactionCommand(); foreach(l, relids) { @@ -1460,11 +1471,12 @@ ReindexDatabase(const char *databaseName, bool do_system, bool do_user) StartTransactionCommand(); /* functions in indexes may want a snapshot set */ - ActiveSnapshot = CopySnapshot(GetTransactionSnapshot()); + PushActiveSnapshot(GetTransactionSnapshot()); if (reindex_relation(relid, true)) ereport(NOTICE, (errmsg("table \"%s\" was reindexed", get_rel_name(relid)))); + PopActiveSnapshot(); CommitTransactionCommand(); } StartTransactionCommand(); -- cgit v1.2.3