1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-02 09:02:37 +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:
Alvaro Herrera
2013-01-23 12:04:59 -03:00
parent f925c79b9f
commit 0ac5ad5134
106 changed files with 6019 additions and 1483 deletions

View File

@ -55,7 +55,7 @@ static void rewriteValuesRTE(RangeTblEntry *rte, Relation target_relation,
static void rewriteTargetListUD(Query *parsetree, RangeTblEntry *target_rte,
Relation target_relation);
static void markQueryForLocking(Query *qry, Node *jtnode,
bool forUpdate, bool noWait, bool pushedDown);
LockClauseStrength strength, bool noWait, bool pushedDown);
static List *matchLocks(CmdType event, RuleLock *rulelocks,
int varno, Query *parsetree);
static Query *fireRIRrules(Query *parsetree, List *activeRIRs,
@ -68,7 +68,7 @@ static Query *fireRIRrules(Query *parsetree, List *activeRIRs,
* These locks will ensure that the relation schemas don't change under us
* while we are rewriting and planning the query.
*
* forUpdatePushedDown indicates that a pushed-down FOR UPDATE/SHARE applies
* forUpdatePushedDown indicates that a pushed-down FOR [KEY] UPDATE/SHARE applies
* to the current subquery, requiring all rels to be opened with RowShareLock.
* This should always be false at the start of the recursion.
*
@ -130,7 +130,7 @@ AcquireRewriteLocks(Query *parsetree, bool forUpdatePushedDown)
*
* If the relation is the query's result relation, then we
* need RowExclusiveLock. Otherwise, check to see if the
* relation is accessed FOR UPDATE/SHARE or not. We can't
* relation is accessed FOR [KEY] UPDATE/SHARE or not. We can't
* just grab AccessShareLock because then the executor would
* be trying to upgrade the lock, leading to possible
* deadlocks.
@ -1357,7 +1357,7 @@ ApplyRetrieveRule(Query *parsetree,
}
/*
* If FOR UPDATE/SHARE of view, be sure we get right initial lock on the
* If FOR [KEY] UPDATE/SHARE of view, be sure we get right initial lock on the
* relations it references.
*/
rc = get_parse_rowmark(parsetree, rt_index);
@ -1405,8 +1405,8 @@ ApplyRetrieveRule(Query *parsetree,
rte->modifiedCols = NULL;
/*
* If FOR UPDATE/SHARE of view, mark all the contained tables as implicit
* FOR UPDATE/SHARE, the same as the parser would have done if the view's
* If FOR [KEY] UPDATE/SHARE of view, mark all the contained tables as implicit
* FOR [KEY] UPDATE/SHARE, the same as the parser would have done if the view's
* subquery had been written out explicitly.
*
* Note: we don't consider forUpdatePushedDown here; such marks will be
@ -1414,13 +1414,13 @@ ApplyRetrieveRule(Query *parsetree,
*/
if (rc != NULL)
markQueryForLocking(rule_action, (Node *) rule_action->jointree,
rc->forUpdate, rc->noWait, true);
rc->strength, rc->noWait, true);
return parsetree;
}
/*
* Recursively mark all relations used by a view as FOR UPDATE/SHARE.
* Recursively mark all relations used by a view as FOR [KEY] UPDATE/SHARE.
*
* This may generate an invalid query, eg if some sub-query uses an
* aggregate. We leave it to the planner to detect that.
@ -1432,7 +1432,7 @@ ApplyRetrieveRule(Query *parsetree,
*/
static void
markQueryForLocking(Query *qry, Node *jtnode,
bool forUpdate, bool noWait, bool pushedDown)
LockClauseStrength strength, bool noWait, bool pushedDown)
{
if (jtnode == NULL)
return;
@ -1446,16 +1446,16 @@ markQueryForLocking(Query *qry, Node *jtnode,
/* ignore foreign tables */
if (rte->relkind != RELKIND_FOREIGN_TABLE)
{
applyLockingClause(qry, rti, forUpdate, noWait, pushedDown);
applyLockingClause(qry, rti, strength, noWait, pushedDown);
rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
}
}
else if (rte->rtekind == RTE_SUBQUERY)
{
applyLockingClause(qry, rti, forUpdate, noWait, pushedDown);
/* FOR UPDATE/SHARE of subquery is propagated to subquery's rels */
applyLockingClause(qry, rti, strength, noWait, pushedDown);
/* FOR [KEY] UPDATE/SHARE of subquery is propagated to subquery's rels */
markQueryForLocking(rte->subquery, (Node *) rte->subquery->jointree,
forUpdate, noWait, true);
strength, noWait, true);
}
/* other RTE types are unaffected by FOR UPDATE */
}
@ -1465,14 +1465,14 @@ markQueryForLocking(Query *qry, Node *jtnode,
ListCell *l;
foreach(l, f->fromlist)
markQueryForLocking(qry, lfirst(l), forUpdate, noWait, pushedDown);
markQueryForLocking(qry, lfirst(l), strength, noWait, pushedDown);
}
else if (IsA(jtnode, JoinExpr))
{
JoinExpr *j = (JoinExpr *) jtnode;
markQueryForLocking(qry, j->larg, forUpdate, noWait, pushedDown);
markQueryForLocking(qry, j->rarg, forUpdate, noWait, pushedDown);
markQueryForLocking(qry, j->larg, strength, noWait, pushedDown);
markQueryForLocking(qry, j->rarg, strength, noWait, pushedDown);
}
else
elog(ERROR, "unrecognized node type: %d",