diff options
author | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2011-02-07 23:46:51 +0200 |
---|---|---|
committer | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2011-02-08 00:09:08 +0200 |
commit | dafaa3efb75ce1aae2e6dbefaf6f3a889dea0d21 (patch) | |
tree | 93271101a38832fce7a6864e96fc9de65b0acff4 /src/backend/commands/variable.c | |
parent | c18f51da17d8cf01d62218e0404e18ba246bde54 (diff) | |
download | postgresql-dafaa3efb75ce1aae2e6dbefaf6f3a889dea0d21.tar.gz postgresql-dafaa3efb75ce1aae2e6dbefaf6f3a889dea0d21.zip |
Implement genuine serializable isolation level.
Until now, our Serializable mode has in fact been what's called Snapshot
Isolation, which allows some anomalies that could not occur in any
serialized ordering of the transactions. This patch fixes that using a
method called Serializable Snapshot Isolation, based on research papers by
Michael J. Cahill (see README-SSI for full references). In Serializable
Snapshot Isolation, transactions run like they do in Snapshot Isolation,
but a predicate lock manager observes the reads and writes performed and
aborts transactions if it detects that an anomaly might occur. This method
produces some false positives, ie. it sometimes aborts transactions even
though there is no anomaly.
To track reads we implement predicate locking, see storage/lmgr/predicate.c.
Whenever a tuple is read, a predicate lock is acquired on the tuple. Shared
memory is finite, so when a transaction takes many tuple-level locks on a
page, the locks are promoted to a single page-level lock, and further to a
single relation level lock if necessary. To lock key values with no matching
tuple, a sequential scan always takes a relation-level lock, and an index
scan acquires a page-level lock that covers the search key, whether or not
there are any matching keys at the moment.
A predicate lock doesn't conflict with any regular locks or with another
predicate locks in the normal sense. They're only used by the predicate lock
manager to detect the danger of anomalies. Only serializable transactions
participate in predicate locking, so there should be no extra overhead for
for other transactions.
Predicate locks can't be released at commit, but must be remembered until
all the transactions that overlapped with it have completed. That means that
we need to remember an unbounded amount of predicate locks, so we apply a
lossy but conservative method of tracking locks for committed transactions.
If we run short of shared memory, we overflow to a new "pg_serial" SLRU
pool.
We don't currently allow Serializable transactions in Hot Standby mode.
That would be hard, because even read-only transactions can cause anomalies
that wouldn't otherwise occur.
Serializable isolation mode now means the new fully serializable level.
Repeatable Read gives you the old Snapshot Isolation level that we have
always had.
Kevin Grittner and Dan Ports, reviewed by Jeff Davis, Heikki Linnakangas and
Anssi Kääriäinen
Diffstat (limited to 'src/backend/commands/variable.c')
-rw-r--r-- | src/backend/commands/variable.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/backend/commands/variable.c b/src/backend/commands/variable.c index 139148eec2e..2a61ea3bc7c 100644 --- a/src/backend/commands/variable.c +++ b/src/backend/commands/variable.c @@ -616,6 +616,15 @@ assign_XactIsoLevel(const char *value, bool doit, GucSource source) errmsg("SET TRANSACTION ISOLATION LEVEL must not be called in a subtransaction"))); return NULL; } + /* Can't go to serializable mode while recovery is still active */ + if (RecoveryInProgress() && strcmp(value, "serializable") == 0) + { + ereport(GUC_complaint_elevel(source), + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("cannot use serializable mode in a hot standby"), + errhint("You can use REPEATABLE READ instead."))); + return false; + } } if (strcmp(value, "serializable") == 0) @@ -667,6 +676,35 @@ show_XactIsoLevel(void) } } +/* + * SET TRANSACTION [NOT] DEFERRABLE + */ + +bool +assign_transaction_deferrable(bool newval, bool doit, GucSource source) +{ + /* source == PGC_S_OVERRIDE means do it anyway, eg at xact abort */ + if (source == PGC_S_OVERRIDE) + return true; + + if (IsSubTransaction()) + { + ereport(GUC_complaint_elevel(source), + (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION), + errmsg("SET TRANSACTION [NOT] DEFERRABLE cannot be called within a subtransaction"))); + return false; + } + + if (FirstSnapshotSet) + { + ereport(GUC_complaint_elevel(source), + (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION), + errmsg("SET TRANSACTION [NOT] DEFERRABLE must be called before any query"))); + return false; + } + + return true; +} /* * Random number seed |