mirror of
https://github.com/postgres/postgres.git
synced 2025-11-09 06:21:09 +03:00
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
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
#include "miscadmin.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/lmgr.h"
|
||||
#include "storage/predicate.h"
|
||||
#include "utils/inval.h"
|
||||
#include "utils/tqual.h"
|
||||
|
||||
@@ -174,6 +175,14 @@ top:
|
||||
|
||||
if (checkUnique != UNIQUE_CHECK_EXISTING)
|
||||
{
|
||||
/*
|
||||
* The only conflict predicate locking cares about for indexes is when
|
||||
* an index tuple insert conflicts with an existing lock. Since the
|
||||
* actual location of the insert is hard to predict because of the
|
||||
* random search used to prevent O(N^2) performance when there are many
|
||||
* duplicate entries, we can just use the "first valid" page.
|
||||
*/
|
||||
CheckForSerializableConflictIn(rel, NULL, buf);
|
||||
/* do the insertion */
|
||||
_bt_findinsertloc(rel, &buf, &offset, natts, itup_scankey, itup, heapRel);
|
||||
_bt_insertonpg(rel, buf, stack, itup, offset, false);
|
||||
@@ -696,6 +705,9 @@ _bt_insertonpg(Relation rel,
|
||||
/* split the buffer into left and right halves */
|
||||
rbuf = _bt_split(rel, buf, firstright,
|
||||
newitemoff, itemsz, itup, newitemonleft);
|
||||
PredicateLockPageSplit(rel,
|
||||
BufferGetBlockNumber(buf),
|
||||
BufferGetBlockNumber(rbuf));
|
||||
|
||||
/*----------
|
||||
* By here,
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "storage/freespace.h"
|
||||
#include "storage/indexfsm.h"
|
||||
#include "storage/lmgr.h"
|
||||
#include "storage/predicate.h"
|
||||
#include "utils/inval.h"
|
||||
#include "utils/snapmgr.h"
|
||||
|
||||
@@ -1183,6 +1184,12 @@ _bt_pagedel(Relation rel, Buffer buf, BTStack stack)
|
||||
rightsib, opaque->btpo_prev, target,
|
||||
RelationGetRelationName(rel));
|
||||
|
||||
/*
|
||||
* Any insert which would have gone on the target block will now go to the
|
||||
* right sibling block.
|
||||
*/
|
||||
PredicateLockPageCombine(rel, target, rightsib);
|
||||
|
||||
/*
|
||||
* Next find and write-lock the current parent of the target page. This is
|
||||
* essentially the same as the corresponding step of splitting.
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "storage/indexfsm.h"
|
||||
#include "storage/ipc.h"
|
||||
#include "storage/lmgr.h"
|
||||
#include "storage/predicate.h"
|
||||
#include "storage/smgr.h"
|
||||
#include "utils/memutils.h"
|
||||
|
||||
@@ -822,6 +823,7 @@ restart:
|
||||
if (_bt_page_recyclable(page))
|
||||
{
|
||||
/* Okay to recycle this page */
|
||||
Assert(!PageIsPredicateLocked(rel, blkno));
|
||||
RecordFreeIndexPage(rel, blkno);
|
||||
vstate->totFreePages++;
|
||||
stats->pages_deleted++;
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "miscadmin.h"
|
||||
#include "pgstat.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/predicate.h"
|
||||
#include "utils/lsyscache.h"
|
||||
#include "utils/rel.h"
|
||||
|
||||
@@ -63,7 +64,10 @@ _bt_search(Relation rel, int keysz, ScanKey scankey, bool nextkey,
|
||||
|
||||
/* If index is empty and access = BT_READ, no root page is created. */
|
||||
if (!BufferIsValid(*bufP))
|
||||
{
|
||||
PredicateLockRelation(rel); /* Nothing finer to lock exists. */
|
||||
return (BTStack) NULL;
|
||||
}
|
||||
|
||||
/* Loop iterates once per level descended in the tree */
|
||||
for (;;)
|
||||
@@ -88,7 +92,11 @@ _bt_search(Relation rel, int keysz, ScanKey scankey, bool nextkey,
|
||||
page = BufferGetPage(*bufP);
|
||||
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
|
||||
if (P_ISLEAF(opaque))
|
||||
{
|
||||
if (access == BT_READ)
|
||||
PredicateLockPage(rel, BufferGetBlockNumber(*bufP));
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find the appropriate item on the internal page, and get the child
|
||||
@@ -1142,6 +1150,7 @@ _bt_steppage(IndexScanDesc scan, ScanDirection dir)
|
||||
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
|
||||
if (!P_IGNORE(opaque))
|
||||
{
|
||||
PredicateLockPage(rel, blkno);
|
||||
/* see if there are any matches on this page */
|
||||
/* note that this will clear moreRight if we can stop */
|
||||
if (_bt_readpage(scan, dir, P_FIRSTDATAKEY(opaque)))
|
||||
@@ -1189,6 +1198,7 @@ _bt_steppage(IndexScanDesc scan, ScanDirection dir)
|
||||
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
|
||||
if (!P_IGNORE(opaque))
|
||||
{
|
||||
PredicateLockPage(rel, BufferGetBlockNumber(so->currPos.buf));
|
||||
/* see if there are any matches on this page */
|
||||
/* note that this will clear moreLeft if we can stop */
|
||||
if (_bt_readpage(scan, dir, PageGetMaxOffsetNumber(page)))
|
||||
@@ -1352,6 +1362,7 @@ _bt_get_endpoint(Relation rel, uint32 level, bool rightmost)
|
||||
if (!BufferIsValid(buf))
|
||||
{
|
||||
/* empty index... */
|
||||
PredicateLockRelation(rel); /* Nothing finer to lock exists. */
|
||||
return InvalidBuffer;
|
||||
}
|
||||
|
||||
@@ -1431,10 +1442,12 @@ _bt_endpoint(IndexScanDesc scan, ScanDirection dir)
|
||||
if (!BufferIsValid(buf))
|
||||
{
|
||||
/* empty index... */
|
||||
PredicateLockRelation(rel); /* Nothing finer to lock exists. */
|
||||
so->currPos.buf = InvalidBuffer;
|
||||
return false;
|
||||
}
|
||||
|
||||
PredicateLockPage(rel, BufferGetBlockNumber(buf));
|
||||
page = BufferGetPage(buf);
|
||||
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
|
||||
Assert(P_ISLEAF(opaque));
|
||||
|
||||
Reference in New Issue
Block a user