summaryrefslogtreecommitdiff
path: root/src/backend/access/index/genam.c
diff options
context:
space:
mode:
authorRobert Haas <rhaas@postgresql.org>2013-07-02 09:47:01 -0400
committerRobert Haas <rhaas@postgresql.org>2013-07-02 09:47:01 -0400
commit568d4138c646cd7cd8a837ac244ef2caf27c6bb8 (patch)
tree82022e9bd58a217976f94fea942f24b0c40278c0 /src/backend/access/index/genam.c
parent384f933046dc9e9a2b416f5f7b3be30b93587c63 (diff)
downloadpostgresql-568d4138c646cd7cd8a837ac244ef2caf27c6bb8.tar.gz
Use an MVCC snapshot, rather than SnapshotNow, for catalog scans.
SnapshotNow scans have the undesirable property that, in the face of concurrent updates, the scan can fail to see either the old or the new versions of the row. In many cases, we work around this by requiring DDL operations to hold AccessExclusiveLock on the object being modified; in some cases, the existing locking is inadequate and random failures occur as a result. This commit doesn't change anything related to locking, but will hopefully pave the way to allowing lock strength reductions in the future. The major issue has held us back from making this change in the past is that taking an MVCC snapshot is significantly more expensive than using a static special snapshot such as SnapshotNow. However, testing of various worst-case scenarios reveals that this problem is not severe except under fairly extreme workloads. To mitigate those problems, we avoid retaking the MVCC snapshot for each new scan; instead, we take a new snapshot only when invalidation messages have been processed. The catcache machinery already requires that invalidation messages be sent before releasing the related heavyweight lock; else other backends might rely on locally-cached data rather than scanning the catalog at all. Thus, making snapshot reuse dependent on the same guarantees shouldn't break anything that wasn't already subtly broken. Patch by me. Review by Michael Paquier and Andres Freund.
Diffstat (limited to 'src/backend/access/index/genam.c')
-rw-r--r--src/backend/access/index/genam.c34
1 files changed, 33 insertions, 1 deletions
diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c
index 31a419b841..2bfe78a92a 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -28,6 +28,7 @@
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
+#include "utils/snapmgr.h"
#include "utils/tqual.h"
@@ -231,7 +232,7 @@ BuildIndexValueDescription(Relation indexRelation,
* rel: catalog to scan, already opened and suitably locked
* indexId: OID of index to conditionally use
* indexOK: if false, forces a heap scan (see notes below)
- * snapshot: time qual to use (usually should be SnapshotNow)
+ * snapshot: time qual to use (NULL for a recent catalog snapshot)
* nkeys, key: scan keys
*
* The attribute numbers in the scan key should be set for the heap case.
@@ -266,6 +267,19 @@ systable_beginscan(Relation heapRelation,
sysscan->heap_rel = heapRelation;
sysscan->irel = irel;
+ if (snapshot == NULL)
+ {
+ Oid relid = RelationGetRelid(heapRelation);
+
+ snapshot = RegisterSnapshot(GetCatalogSnapshot(relid));
+ sysscan->snapshot = snapshot;
+ }
+ else
+ {
+ /* Caller is responsible for any snapshot. */
+ sysscan->snapshot = NULL;
+ }
+
if (irel)
{
int i;
@@ -401,6 +415,9 @@ systable_endscan(SysScanDesc sysscan)
else
heap_endscan(sysscan->scan);
+ if (sysscan->snapshot)
+ UnregisterSnapshot(sysscan->snapshot);
+
pfree(sysscan);
}
@@ -444,6 +461,19 @@ systable_beginscan_ordered(Relation heapRelation,
sysscan->heap_rel = heapRelation;
sysscan->irel = indexRelation;
+ if (snapshot == NULL)
+ {
+ Oid relid = RelationGetRelid(heapRelation);
+
+ snapshot = RegisterSnapshot(GetCatalogSnapshot(relid));
+ sysscan->snapshot = snapshot;
+ }
+ else
+ {
+ /* Caller is responsible for any snapshot. */
+ sysscan->snapshot = NULL;
+ }
+
/* Change attribute numbers to be index column numbers. */
for (i = 0; i < nkeys; i++)
{
@@ -494,5 +524,7 @@ systable_endscan_ordered(SysScanDesc sysscan)
{
Assert(sysscan->irel);
index_endscan(sysscan->iscan);
+ if (sysscan->snapshot)
+ UnregisterSnapshot(sysscan->snapshot);
pfree(sysscan);
}