mirror of
https://github.com/postgres/postgres.git
synced 2025-07-07 00:36:50 +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:
@ -861,11 +861,11 @@ make_outerjoininfo(PlannerInfo *root,
|
||||
Assert(jointype != JOIN_RIGHT);
|
||||
|
||||
/*
|
||||
* Presently the executor cannot support FOR UPDATE/SHARE marking of rels
|
||||
* Presently the executor cannot support FOR [KEY] UPDATE/SHARE marking of rels
|
||||
* appearing on the nullable side of an outer join. (It's somewhat unclear
|
||||
* what that would mean, anyway: what should we mark when a result row is
|
||||
* generated from no element of the nullable relation?) So, complain if
|
||||
* any nullable rel is FOR UPDATE/SHARE.
|
||||
* any nullable rel is FOR [KEY] UPDATE/SHARE.
|
||||
*
|
||||
* You might be wondering why this test isn't made far upstream in the
|
||||
* parser. It's because the parser hasn't got enough info --- consider
|
||||
@ -883,7 +883,7 @@ make_outerjoininfo(PlannerInfo *root,
|
||||
(jointype == JOIN_FULL && bms_is_member(rc->rti, left_rels)))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||
errmsg("SELECT FOR UPDATE/SHARE cannot be applied to the nullable side of an outer join")));
|
||||
errmsg("SELECT FOR UPDATE/SHARE/KEY UPDATE/KEY SHARE cannot be applied to the nullable side of an outer join")));
|
||||
}
|
||||
|
||||
sjinfo->syn_lefthand = left_rels;
|
||||
|
@ -562,7 +562,7 @@ subquery_planner(PlannerGlobal *glob, Query *parse,
|
||||
returningLists = NIL;
|
||||
|
||||
/*
|
||||
* If there was a FOR UPDATE/SHARE clause, the LockRows node will
|
||||
* If there was a FOR [KEY] UPDATE/SHARE clause, the LockRows node will
|
||||
* have dealt with fetching non-locked marked rows, else we need
|
||||
* to have ModifyTable do that.
|
||||
*/
|
||||
@ -954,7 +954,7 @@ inheritance_planner(PlannerInfo *root)
|
||||
root->simple_rel_array = save_rel_array;
|
||||
|
||||
/*
|
||||
* If there was a FOR UPDATE/SHARE clause, the LockRows node will have
|
||||
* If there was a FOR [KEY] UPDATE/SHARE clause, the LockRows node will have
|
||||
* dealt with fetching non-locked marked rows, else we need to have
|
||||
* ModifyTable do that.
|
||||
*/
|
||||
@ -1065,13 +1065,13 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
|
||||
tlist);
|
||||
|
||||
/*
|
||||
* Can't handle FOR UPDATE/SHARE here (parser should have checked
|
||||
* Can't handle FOR [KEY] UPDATE/SHARE here (parser should have checked
|
||||
* already, but let's make sure).
|
||||
*/
|
||||
if (parse->rowMarks)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||
errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
|
||||
errmsg("SELECT FOR UPDATE/SHARE/KEY UPDATE/KEY SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
|
||||
|
||||
/*
|
||||
* Calculate pathkeys that represent result ordering requirements
|
||||
@ -1797,7 +1797,7 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
|
||||
}
|
||||
|
||||
/*
|
||||
* If there is a FOR UPDATE/SHARE clause, add the LockRows node. (Note: we
|
||||
* If there is a FOR [KEY] UPDATE/SHARE clause, add the LockRows node. (Note: we
|
||||
* intentionally test parse->rowMarks not root->rowMarks here. If there
|
||||
* are only non-locking rowmarks, they should be handled by the
|
||||
* ModifyTable node instead.)
|
||||
@ -1983,7 +1983,7 @@ preprocess_rowmarks(PlannerInfo *root)
|
||||
if (parse->rowMarks)
|
||||
{
|
||||
/*
|
||||
* We've got trouble if FOR UPDATE/SHARE appears inside grouping,
|
||||
* We've got trouble if FOR [KEY] UPDATE/SHARE appears inside grouping,
|
||||
* since grouping renders a reference to individual tuple CTIDs
|
||||
* invalid. This is also checked at parse time, but that's
|
||||
* insufficient because of rule substitution, query pullup, etc.
|
||||
@ -1993,7 +1993,7 @@ preprocess_rowmarks(PlannerInfo *root)
|
||||
else
|
||||
{
|
||||
/*
|
||||
* We only need rowmarks for UPDATE, DELETE, or FOR UPDATE/SHARE.
|
||||
* We only need rowmarks for UPDATE, DELETE, or FOR [KEY] UPDATE/SHARE.
|
||||
*/
|
||||
if (parse->commandType != CMD_UPDATE &&
|
||||
parse->commandType != CMD_DELETE)
|
||||
@ -2003,7 +2003,7 @@ preprocess_rowmarks(PlannerInfo *root)
|
||||
/*
|
||||
* We need to have rowmarks for all base relations except the target. We
|
||||
* make a bitmapset of all base rels and then remove the items we don't
|
||||
* need or have FOR UPDATE/SHARE marks for.
|
||||
* need or have FOR [KEY] UPDATE/SHARE marks for.
|
||||
*/
|
||||
rels = get_base_rel_indexes((Node *) parse->jointree);
|
||||
if (parse->resultRelation)
|
||||
@ -2020,7 +2020,7 @@ preprocess_rowmarks(PlannerInfo *root)
|
||||
PlanRowMark *newrc;
|
||||
|
||||
/*
|
||||
* Currently, it is syntactically impossible to have FOR UPDATE
|
||||
* Currently, it is syntactically impossible to have FOR UPDATE et al
|
||||
* applied to an update/delete target rel. If that ever becomes
|
||||
* possible, we should drop the target from the PlanRowMark list.
|
||||
*/
|
||||
@ -2040,10 +2040,21 @@ preprocess_rowmarks(PlannerInfo *root)
|
||||
newrc = makeNode(PlanRowMark);
|
||||
newrc->rti = newrc->prti = rc->rti;
|
||||
newrc->rowmarkId = ++(root->glob->lastRowMarkId);
|
||||
if (rc->forUpdate)
|
||||
newrc->markType = ROW_MARK_EXCLUSIVE;
|
||||
else
|
||||
newrc->markType = ROW_MARK_SHARE;
|
||||
switch (rc->strength)
|
||||
{
|
||||
case LCS_FORUPDATE:
|
||||
newrc->markType = ROW_MARK_EXCLUSIVE;
|
||||
break;
|
||||
case LCS_FORNOKEYUPDATE:
|
||||
newrc->markType = ROW_MARK_NOKEYEXCLUSIVE;
|
||||
break;
|
||||
case LCS_FORSHARE:
|
||||
newrc->markType = ROW_MARK_SHARE;
|
||||
break;
|
||||
case LCS_FORKEYSHARE:
|
||||
newrc->markType = ROW_MARK_KEYSHARE;
|
||||
break;
|
||||
}
|
||||
newrc->noWait = rc->noWait;
|
||||
newrc->isParent = false;
|
||||
|
||||
|
Reference in New Issue
Block a user