1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-15 19:21:59 +03:00

Implement SKIP LOCKED for row-level locks

This clause changes the behavior of SELECT locking clauses in the
presence of locked rows: instead of causing a process to block waiting
for the locks held by other processes (or raise an error, with NOWAIT),
SKIP LOCKED makes the new reader skip over such rows.  While this is not
appropriate behavior for general purposes, there are some cases in which
it is useful, such as queue-like tables.

Catalog version bumped because this patch changes the representation of
stored rules.

Reviewed by Craig Ringer (based on a previous attempt at an
implementation by Simon Riggs, who also provided input on the syntax
used in the current patch), David Rowley, and Álvaro Herrera.

Author: Thomas Munro
This commit is contained in:
Alvaro Herrera
2014-10-07 17:23:34 -03:00
parent c421efd213
commit df630b0dd5
38 changed files with 907 additions and 121 deletions

View File

@ -63,7 +63,8 @@ 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,
LockClauseStrength strength, bool noWait, bool pushedDown);
LockClauseStrength strength, LockWaitPolicy waitPolicy,
bool pushedDown);
static List *matchLocks(CmdType event, RuleLock *rulelocks,
int varno, Query *parsetree);
static Query *fireRIRrules(Query *parsetree, List *activeRIRs,
@ -1482,7 +1483,7 @@ ApplyRetrieveRule(Query *parsetree,
*/
if (rc != NULL)
markQueryForLocking(rule_action, (Node *) rule_action->jointree,
rc->strength, rc->noWait, true);
rc->strength, rc->waitPolicy, true);
return parsetree;
}
@ -1500,7 +1501,8 @@ ApplyRetrieveRule(Query *parsetree,
*/
static void
markQueryForLocking(Query *qry, Node *jtnode,
LockClauseStrength strength, bool noWait, bool pushedDown)
LockClauseStrength strength, LockWaitPolicy waitPolicy,
bool pushedDown)
{
if (jtnode == NULL)
return;
@ -1511,15 +1513,15 @@ markQueryForLocking(Query *qry, Node *jtnode,
if (rte->rtekind == RTE_RELATION)
{
applyLockingClause(qry, rti, strength, noWait, pushedDown);
applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
}
else if (rte->rtekind == RTE_SUBQUERY)
{
applyLockingClause(qry, rti, strength, noWait, pushedDown);
applyLockingClause(qry, rti, strength, waitPolicy, pushedDown);
/* FOR UPDATE/SHARE of subquery is propagated to subquery's rels */
markQueryForLocking(rte->subquery, (Node *) rte->subquery->jointree,
strength, noWait, true);
strength, waitPolicy, true);
}
/* other RTE types are unaffected by FOR UPDATE */
}
@ -1529,14 +1531,14 @@ markQueryForLocking(Query *qry, Node *jtnode,
ListCell *l;
foreach(l, f->fromlist)
markQueryForLocking(qry, lfirst(l), strength, noWait, pushedDown);
markQueryForLocking(qry, lfirst(l), strength, waitPolicy, pushedDown);
}
else if (IsA(jtnode, JoinExpr))
{
JoinExpr *j = (JoinExpr *) jtnode;
markQueryForLocking(qry, j->larg, strength, noWait, pushedDown);
markQueryForLocking(qry, j->rarg, strength, noWait, pushedDown);
markQueryForLocking(qry, j->larg, strength, waitPolicy, pushedDown);
markQueryForLocking(qry, j->rarg, strength, waitPolicy, pushedDown);
}
else
elog(ERROR, "unrecognized node type: %d",