1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-07 19:06:32 +03:00

Implement sharable row-level locks, and use them for foreign key references

to eliminate unnecessary deadlocks.  This commit adds SELECT ... FOR SHARE
paralleling SELECT ... FOR UPDATE.  The implementation uses a new SLRU
data structure (managed much like pg_subtrans) to represent multiple-
transaction-ID sets.  When more than one transaction is holding a shared
lock on a particular row, we create a MultiXactId representing that set
of transactions and store its ID in the row's XMAX.  This scheme allows
an effectively unlimited number of row locks, just as we did before,
while not costing any extra overhead except when a shared lock actually
has to be shared.   Still TODO: use the regular lock manager to control
the grant order when multiple backends are waiting for a row lock.

Alvaro Herrera and Tom Lane.
This commit is contained in:
Tom Lane
2005-04-28 21:47:18 +00:00
parent d902e7d63b
commit bedb78d386
55 changed files with 2802 additions and 439 deletions

View File

@@ -16,7 +16,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.26 2005/04/06 16:34:06 tgl Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.27 2005/04/28 21:47:14 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -276,11 +276,22 @@ pull_up_subqueries(Query *parse, Node *jtnode, bool below_outer_join)
parse->rtable = list_concat(parse->rtable, subquery->rtable);
/*
* Pull up any FOR UPDATE markers, too. (OffsetVarNodes
* Pull up any FOR UPDATE/SHARE markers, too. (OffsetVarNodes
* already adjusted the marker values, so just list_concat the
* list.)
*
* Executor can't handle multiple FOR UPDATE/SHARE flags, so
* complain if they are valid but different
*/
if (parse->rowMarks && subquery->rowMarks &&
parse->forUpdate != subquery->forUpdate)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot use both FOR UPDATE and FOR SHARE in one query")));
parse->rowMarks = list_concat(parse->rowMarks, subquery->rowMarks);
if (subquery->rowMarks)
parse->forUpdate = subquery->forUpdate;
/*
* We also have to fix the relid sets of any parent

View File

@@ -15,7 +15,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/prep/preptlist.c,v 1.74 2005/04/06 16:34:06 tgl Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/prep/preptlist.c,v 1.75 2005/04/28 21:47:14 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -102,7 +102,7 @@ preprocess_targetlist(Query *parse, List *tlist)
}
/*
* Add TID targets for rels selected FOR UPDATE. The executor
* Add TID targets for rels selected FOR UPDATE/SHARE. The executor
* uses the TID to know which rows to lock, much as for UPDATE or
* DELETE.
*/
@@ -111,22 +111,22 @@ preprocess_targetlist(Query *parse, List *tlist)
ListCell *l;
/*
* We've got trouble if the FOR UPDATE appears inside
* We've got trouble if the FOR 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.
*/
CheckSelectForUpdate(parse);
CheckSelectLocking(parse, parse->forUpdate);
/*
* Currently the executor only supports FOR UPDATE at top
* Currently the executor only supports FOR UPDATE/SHARE at top
* level
*/
if (PlannerQueryLevel > 1)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("SELECT FOR UPDATE is not allowed in subqueries")));
errmsg("SELECT FOR UPDATE/SHARE is not allowed in subqueries")));
foreach(l, parse->rowMarks)
{