diff options
| author | Robert Haas | 2013-07-02 13:47:01 +0000 |
|---|---|---|
| committer | Robert Haas | 2013-07-02 13:47:01 +0000 |
| commit | 568d4138c646cd7cd8a837ac244ef2caf27c6bb8 (patch) | |
| tree | 82022e9bd58a217976f94fea942f24b0c40278c0 /src/backend/rewrite | |
| parent | 384f933046dc9e9a2b416f5f7b3be30b93587c63 (diff) | |
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/rewrite')
| -rw-r--r-- | src/backend/rewrite/rewriteDefine.c | 6 | ||||
| -rw-r--r-- | src/backend/rewrite/rewriteHandler.c | 4 | ||||
| -rw-r--r-- | src/backend/rewrite/rewriteRemove.c | 2 | ||||
| -rw-r--r-- | src/backend/rewrite/rewriteSupport.c | 2 |
4 files changed, 9 insertions, 5 deletions
diff --git a/src/backend/rewrite/rewriteDefine.c b/src/backend/rewrite/rewriteDefine.c index fb576219627..3157aba330d 100644 --- a/src/backend/rewrite/rewriteDefine.c +++ b/src/backend/rewrite/rewriteDefine.c @@ -38,6 +38,7 @@ #include "utils/inval.h" #include "utils/lsyscache.h" #include "utils/rel.h" +#include "utils/snapmgr.h" #include "utils/syscache.h" #include "utils/tqual.h" @@ -418,14 +419,17 @@ DefineQueryRewrite(char *rulename, event_relation->rd_rel->relkind != RELKIND_MATVIEW) { HeapScanDesc scanDesc; + Snapshot snapshot; - scanDesc = heap_beginscan(event_relation, SnapshotNow, 0, NULL); + snapshot = RegisterSnapshot(GetLatestSnapshot()); + scanDesc = heap_beginscan(event_relation, snapshot, 0, NULL); if (heap_getnext(scanDesc, ForwardScanDirection) != NULL) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not convert table \"%s\" to a view because it is not empty", RelationGetRelationName(event_relation)))); heap_endscan(scanDesc); + UnregisterSnapshot(snapshot); if (event_relation->rd_rel->relhastriggers) ereport(ERROR, diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c index a467588e50e..d4b97081950 100644 --- a/src/backend/rewrite/rewriteHandler.c +++ b/src/backend/rewrite/rewriteHandler.c @@ -2092,8 +2092,8 @@ relation_is_updatable(Oid reloid, bool include_triggers) /* * If the relation doesn't exist, return zero rather than throwing an * error. This is helpful since scanning an information_schema view under - * MVCC rules can result in referencing rels that were just deleted - * according to a SnapshotNow probe. + * MVCC rules can result in referencing rels that have actually been + * deleted already. */ if (rel == NULL) return 0; diff --git a/src/backend/rewrite/rewriteRemove.c b/src/backend/rewrite/rewriteRemove.c index 75fc7765ae6..51e27cf4e2e 100644 --- a/src/backend/rewrite/rewriteRemove.c +++ b/src/backend/rewrite/rewriteRemove.c @@ -58,7 +58,7 @@ RemoveRewriteRuleById(Oid ruleOid) ObjectIdGetDatum(ruleOid)); rcscan = systable_beginscan(RewriteRelation, RewriteOidIndexId, true, - SnapshotNow, 1, skey); + NULL, 1, skey); tuple = systable_getnext(rcscan); diff --git a/src/backend/rewrite/rewriteSupport.c b/src/backend/rewrite/rewriteSupport.c index f481c531ac7..a68734257cd 100644 --- a/src/backend/rewrite/rewriteSupport.c +++ b/src/backend/rewrite/rewriteSupport.c @@ -143,7 +143,7 @@ get_rewrite_oid_without_relid(const char *rulename, CStringGetDatum(rulename)); RewriteRelation = heap_open(RewriteRelationId, AccessShareLock); - scanDesc = heap_beginscan(RewriteRelation, SnapshotNow, 1, &scanKeyData); + scanDesc = heap_beginscan_catalog(RewriteRelation, 1, &scanKeyData); htup = heap_getnext(scanDesc, ForwardScanDirection); if (!HeapTupleIsValid(htup)) |
