mirror of
https://github.com/postgres/postgres.git
synced 2025-06-13 07:41:39 +03:00
Improve concurrency of foreign key locking
This patch introduces two additional lock modes for tuples: "SELECT FOR KEY SHARE" and "SELECT FOR NO KEY UPDATE". These don't block each other, in contrast with already existing "SELECT FOR SHARE" and "SELECT FOR UPDATE". UPDATE commands that do not modify the values stored in the columns that are part of the key of the tuple now grab a SELECT FOR NO KEY UPDATE lock on the tuple, allowing them to proceed concurrently with tuple locks of the FOR KEY SHARE variety. Foreign key triggers now use FOR KEY SHARE instead of FOR SHARE; this means the concurrency improvement applies to them, which is the whole point of this patch. The added tuple lock semantics require some rejiggering of the multixact module, so that the locking level that each transaction is holding can be stored alongside its Xid. Also, multixacts now need to persist across server restarts and crashes, because they can now represent not only tuple locks, but also tuple updates. This means we need more careful tracking of lifetime of pg_multixact SLRU files; since they now persist longer, we require more infrastructure to figure out when they can be removed. pg_upgrade also needs to be careful to copy pg_multixact files over from the old server to the new, or at least part of multixact.c state, depending on the versions of the old and new servers. Tuple time qualification rules (HeapTupleSatisfies routines) need to be careful not to consider tuples with the "is multi" infomask bit set as being only locked; they might need to look up MultiXact values (i.e. possibly do pg_multixact I/O) to find out the Xid that updated a tuple, whereas they previously were assured to only use information readily available from the tuple header. This is considered acceptable, because the extra I/O would involve cases that would previously cause some commands to block waiting for concurrent transactions to finish. Another important change is the fact that locking tuples that have previously been updated causes the future versions to be marked as locked, too; this is essential for correctness of foreign key checks. This causes additional WAL-logging, also (there was previously a single WAL record for a locked tuple; now there are as many as updated copies of the tuple there exist.) With all this in place, contention related to tuples being checked by foreign key rules should be much reduced. As a bonus, the old behavior that a subtransaction grabbing a stronger tuple lock than the parent (sub)transaction held on a given tuple and later aborting caused the weaker lock to be lost, has been fixed. Many new spec files were added for isolation tester framework, to ensure overall behavior is sane. There's probably room for several more tests. There were several reviewers of this patch; in particular, Noah Misch and Andres Freund spent considerable time in it. Original idea for the patch came from Simon Riggs, after a problem report by Joel Jacobson. Most code is from me, with contributions from Marti Raudsepp, Alexander Shulgin, Noah Misch and Andres Freund. This patch was discussed in several pgsql-hackers threads; the most important start at the following message-ids: AANLkTimo9XVcEzfiBR-ut3KVNDkjm2Vxh+t8kAmWjPuv@mail.gmail.com 1290721684-sup-3951@alvh.no-ip.org 1294953201-sup-2099@alvh.no-ip.org 1320343602-sup-2290@alvh.no-ip.org 1339690386-sup-8927@alvh.no-ip.org 4FE5FF020200002500048A3D@gw.wicourts.gov 4FEAB90A0200002500048B7D@gw.wicourts.gov
This commit is contained in:
31
src/backend/utils/cache/relcache.c
vendored
31
src/backend/utils/cache/relcache.c
vendored
@ -30,10 +30,11 @@
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "access/htup_details.h"
|
||||
#include "access/multixact.h"
|
||||
#include "access/reloptions.h"
|
||||
#include "access/sysattr.h"
|
||||
#include "access/transam.h"
|
||||
#include "access/htup_details.h"
|
||||
#include "access/xact.h"
|
||||
#include "catalog/catalog.h"
|
||||
#include "catalog/index.h"
|
||||
@ -2725,7 +2726,8 @@ RelationBuildLocalRelation(const char *relname,
|
||||
* the XIDs that will be put into the new relation contents.
|
||||
*/
|
||||
void
|
||||
RelationSetNewRelfilenode(Relation relation, TransactionId freezeXid)
|
||||
RelationSetNewRelfilenode(Relation relation, TransactionId freezeXid,
|
||||
MultiXactId minmulti)
|
||||
{
|
||||
Oid newrelfilenode;
|
||||
RelFileNodeBackend newrnode;
|
||||
@ -2738,6 +2740,7 @@ RelationSetNewRelfilenode(Relation relation, TransactionId freezeXid)
|
||||
relation->rd_rel->relkind == RELKIND_SEQUENCE) ?
|
||||
freezeXid == InvalidTransactionId :
|
||||
TransactionIdIsNormal(freezeXid));
|
||||
Assert(TransactionIdIsNormal(freezeXid) == MultiXactIdIsValid(minmulti));
|
||||
|
||||
/* Allocate a new relfilenode */
|
||||
newrelfilenode = GetNewRelFileNode(relation->rd_rel->reltablespace, NULL,
|
||||
@ -2793,6 +2796,7 @@ RelationSetNewRelfilenode(Relation relation, TransactionId freezeXid)
|
||||
classform->relallvisible = 0;
|
||||
}
|
||||
classform->relfrozenxid = freezeXid;
|
||||
classform->relminmxid = minmulti;
|
||||
|
||||
simple_heap_update(pg_class, &tuple->t_self, tuple);
|
||||
CatalogUpdateIndexes(pg_class, tuple);
|
||||
@ -3764,6 +3768,9 @@ RelationGetIndexPredicate(Relation relation)
|
||||
* simple index keys, but attributes used in expressions and partial-index
|
||||
* predicates.)
|
||||
*
|
||||
* If "keyAttrs" is true, only attributes that can be referenced by foreign
|
||||
* keys are considered.
|
||||
*
|
||||
* Attribute numbers are offset by FirstLowInvalidHeapAttributeNumber so that
|
||||
* we can include system attributes (e.g., OID) in the bitmap representation.
|
||||
*
|
||||
@ -3775,16 +3782,17 @@ RelationGetIndexPredicate(Relation relation)
|
||||
* be bms_free'd when not needed anymore.
|
||||
*/
|
||||
Bitmapset *
|
||||
RelationGetIndexAttrBitmap(Relation relation)
|
||||
RelationGetIndexAttrBitmap(Relation relation, bool keyAttrs)
|
||||
{
|
||||
Bitmapset *indexattrs;
|
||||
Bitmapset *uindexattrs;
|
||||
List *indexoidlist;
|
||||
ListCell *l;
|
||||
MemoryContext oldcxt;
|
||||
|
||||
/* Quick exit if we already computed the result. */
|
||||
if (relation->rd_indexattr != NULL)
|
||||
return bms_copy(relation->rd_indexattr);
|
||||
return bms_copy(keyAttrs ? relation->rd_keyattr : relation->rd_indexattr);
|
||||
|
||||
/* Fast path if definitely no indexes */
|
||||
if (!RelationGetForm(relation)->relhasindex)
|
||||
@ -3810,26 +3818,38 @@ RelationGetIndexAttrBitmap(Relation relation)
|
||||
* won't be returned at all by RelationGetIndexList.
|
||||
*/
|
||||
indexattrs = NULL;
|
||||
uindexattrs = NULL;
|
||||
foreach(l, indexoidlist)
|
||||
{
|
||||
Oid indexOid = lfirst_oid(l);
|
||||
Relation indexDesc;
|
||||
IndexInfo *indexInfo;
|
||||
int i;
|
||||
bool isKey;
|
||||
|
||||
indexDesc = index_open(indexOid, AccessShareLock);
|
||||
|
||||
/* Extract index key information from the index's pg_index row */
|
||||
indexInfo = BuildIndexInfo(indexDesc);
|
||||
|
||||
/* Can this index be referenced by a foreign key? */
|
||||
isKey = indexInfo->ii_Unique &&
|
||||
indexInfo->ii_Expressions == NIL &&
|
||||
indexInfo->ii_Predicate == NIL;
|
||||
|
||||
/* Collect simple attribute references */
|
||||
for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
|
||||
{
|
||||
int attrnum = indexInfo->ii_KeyAttrNumbers[i];
|
||||
|
||||
if (attrnum != 0)
|
||||
{
|
||||
indexattrs = bms_add_member(indexattrs,
|
||||
attrnum - FirstLowInvalidHeapAttributeNumber);
|
||||
if (isKey)
|
||||
uindexattrs = bms_add_member(uindexattrs,
|
||||
attrnum - FirstLowInvalidHeapAttributeNumber);
|
||||
}
|
||||
}
|
||||
|
||||
/* Collect all attributes used in expressions, too */
|
||||
@ -3846,10 +3866,11 @@ RelationGetIndexAttrBitmap(Relation relation)
|
||||
/* Now save a copy of the bitmap in the relcache entry. */
|
||||
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
|
||||
relation->rd_indexattr = bms_copy(indexattrs);
|
||||
relation->rd_keyattr = bms_copy(uindexattrs);
|
||||
MemoryContextSwitchTo(oldcxt);
|
||||
|
||||
/* We return our original working copy for caller to play with */
|
||||
return indexattrs;
|
||||
return keyAttrs ? uindexattrs : indexattrs;
|
||||
}
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user