From 4fe42dfbc3bafa0ea615239d716a6b37d67da253 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 9 Jul 2001 22:18:34 +0000 Subject: [PATCH] Add SHARE UPDATE EXCLUSIVE lock mode, coming soon to a VACUUM near you. Name chosen per pghackers discussion around 6/22/01. --- doc/src/sgml/mvcc.sgml | 34 ++++++++++++--- doc/src/sgml/ref/lock.sgml | 79 +++++++++++++++++++++++------------ src/backend/parser/gram.y | 22 +++++----- src/backend/storage/lmgr/lmgr.c | 44 ++++++++++++------- src/backend/storage/lmgr/lock.c | 3 +- src/include/storage/lmgr.h | 15 +++---- src/include/storage/lock.h | 6 +-- src/interfaces/ecpg/preproc/preproc.y | 20 ++++----- 8 files changed, 143 insertions(+), 80 deletions(-) diff --git a/doc/src/sgml/mvcc.sgml b/doc/src/sgml/mvcc.sgml index 2eaf00803b..6d7013a46a 100644 --- a/doc/src/sgml/mvcc.sgml +++ b/doc/src/sgml/mvcc.sgml @@ -1,5 +1,5 @@ @@ -443,6 +443,25 @@ ERROR: Can't serialize access due to concurrent update + ShareUpdateExclusiveLock + + + + Acquired by VACUUM (without ) + and LOCK TABLE table + for + statements. + + + + Conflicts with ShareUpdateExclusiveLock, ShareLock, + ShareRowExclusiveLock, ExclusiveLock and AccessExclusiveLock modes. + + + + + + ShareLock @@ -454,7 +473,8 @@ ERROR: Can't serialize access due to concurrent update - Conflicts with RowExclusiveLock, ShareRowExclusiveLock, + Conflicts with RowExclusiveLock, ShareUpdateExclusiveLock, + ShareRowExclusiveLock, ExclusiveLock and AccessExclusiveLock modes. @@ -471,7 +491,8 @@ ERROR: Can't serialize access due to concurrent update - Conflicts with RowExclusiveLock, ShareLock, ShareRowExclusiveLock, + Conflicts with RowExclusiveLock, ShareUpdateExclusiveLock, + ShareLock, ShareRowExclusiveLock, ExclusiveLock and AccessExclusiveLock modes. @@ -488,7 +509,8 @@ ERROR: Can't serialize access due to concurrent update - Conflicts with RowShareLock, RowExclusiveLock, ShareLock, + Conflicts with RowShareLock, RowExclusiveLock, + ShareUpdateExclusiveLock, ShareLock, ShareRowExclusiveLock, ExclusiveLock and AccessExclusiveLock modes. @@ -503,13 +525,13 @@ ERROR: Can't serialize access due to concurrent update Acquired by ALTER TABLE, DROP TABLE, - VACUUM and LOCK TABLE + VACUUM FULL and LOCK TABLE statements. Conflicts with all modes (AccessShareLock, RowShareLock, - RowExclusiveLock, ShareLock, + RowExclusiveLock, ShareUpdateExclusiveLock, ShareLock, ShareRowExclusiveLock, ExclusiveLock and AccessExclusiveLock). diff --git a/doc/src/sgml/ref/lock.sgml b/doc/src/sgml/ref/lock.sgml index bffbb7c23d..d576890acb 100644 --- a/doc/src/sgml/ref/lock.sgml +++ b/doc/src/sgml/ref/lock.sgml @@ -1,5 +1,5 @@ @@ -20,12 +20,16 @@ Postgres documentation - 1999-07-20 + 2001-07-09 LOCK [ TABLE ] name -LOCK [ TABLE ] name IN [ ROW | ACCESS ] { SHARE | EXCLUSIVE } MODE -LOCK [ TABLE ] name IN SHARE ROW EXCLUSIVE MODE +LOCK [ TABLE ] name IN lockmode MODE + +where lockmode is one of: + + ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE | + SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE @@ -33,7 +37,6 @@ LOCK [ TABLE ] name IN SHARE ROW EX 1999-06-09 - Inputs @@ -62,7 +65,7 @@ LOCK [ TABLE ] name IN SHARE ROW EX This is the least restrictive lock mode. It conflicts only with ACCESS EXCLUSIVE mode. It is used to protect a table from being modified by concurrent ALTER TABLE, - DROP TABLE and VACUUM + DROP TABLE and VACUUM FULL commands. @@ -103,6 +106,25 @@ LOCK [ TABLE ] name IN SHARE ROW EX + SHARE UPDATE EXCLUSIVE MODE + + + + Automatically acquired by VACUUM (without + ). + + + + + Conflicts with SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, + EXCLUSIVE and + ACCESS EXCLUSIVE modes. This mode protects a table against + concurrent schema changes and VACUUMs. + + + + + SHARE MODE @@ -113,9 +135,10 @@ LOCK [ TABLE ] name IN SHARE ROW EX - Conflicts with ROW EXCLUSIVE, SHARE ROW EXCLUSIVE, EXCLUSIVE and + Conflicts with ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, + SHARE ROW EXCLUSIVE, EXCLUSIVE and ACCESS EXCLUSIVE modes. This mode protects a table against - concurrent updates. + concurrent data updates. @@ -125,14 +148,14 @@ LOCK [ TABLE ] name IN SHARE ROW EX - This is like EXCLUSIVE MODE, but allows SHARE ROW locks + This is like EXCLUSIVE MODE, but allows ROW SHARE locks by others. - Conflicts with ROW EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, - EXCLUSIVE and ACCESS EXCLUSIVE modes. + Conflicts with ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE, + SHARE ROW EXCLUSIVE, EXCLUSIVE and ACCESS EXCLUSIVE modes. @@ -149,7 +172,8 @@ LOCK [ TABLE ] name IN SHARE ROW EX - Conflicts with ROW SHARE, ROW EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, + Conflicts with ROW SHARE, ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, + SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE and ACCESS EXCLUSIVE modes. @@ -161,9 +185,10 @@ LOCK [ TABLE ] name IN SHARE ROW EX Automatically acquired by ALTER TABLE, - DROP TABLE, VACUUM statements. - This is the most restrictive lock mode which conflicts with all other - lock modes and protects a locked table from any concurrent operations. + DROP TABLE, VACUUM FULL + statements. + This is the most restrictive lock mode which + protects a locked table from any concurrent operations. @@ -174,6 +199,10 @@ LOCK [ TABLE ] name IN SHARE ROW EX lock mode option). + + + Conflicts with all lock modes. + @@ -241,7 +270,11 @@ ERROR name: Table does not exist. EXCLUSIVE - Exclusive lock that prevents other locks from being granted. + An exclusive lock prevents other locks of the same type from being + granted. (Note: ROW EXCLUSIVE mode does not follow this naming + convention perfectly, since it is shared at the level of the table; + it is exclusive only with respect to specific rows that are being + updated.) @@ -250,7 +283,8 @@ ERROR name: Table does not exist. SHARE - Allows others to share lock. Prevents EXCLUSIVE locks. + A shared lock allows others to also hold the same type of lock, + but prevents the corresponding EXCLUSIVE lock from being granted. @@ -273,13 +307,6 @@ ERROR name: Table does not exist. - - - - If EXCLUSIVE or SHARE are not specified, EXCLUSIVE is assumed. - Locks exist for the duration of the transaction. - - @@ -370,8 +397,8 @@ ERROR name: Table does not exist. - Except for ACCESS SHARE/EXCLUSIVE lock modes, all other - Postgres lock modes and the + Except for ACCESS SHARE, ACCESS EXCLUSIVE, and SHARE UPDATE EXCLUSIVE lock + modes, the Postgres lock modes and the LOCK TABLE syntax are compatible with those present in Oracle. diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index cf518f3cad..7e6f782984 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -11,7 +11,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.233 2001/06/30 22:03:25 petere Exp $ + * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.234 2001/07/09 22:18:33 tgl Exp $ * * HISTORY * AUTHOR DATE MAJOR EVENT @@ -153,7 +153,7 @@ static void doNegateFloat(Value *v); %type createdb_opt_list, createdb_opt_item %type opt_lock, lock_type -%type opt_lmode, opt_force +%type opt_force %type user_createdb_clause, user_createuser_clause %type user_passwd_clause @@ -3277,18 +3277,18 @@ LockStmt: LOCK_P opt_table relation_name opt_lock } ; -opt_lock: IN lock_type MODE { $$ = $2; } +opt_lock: IN lock_type MODE { $$ = $2; } | /*EMPTY*/ { $$ = AccessExclusiveLock; } ; -lock_type: SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; } - | ROW opt_lmode { $$ = ($2? RowShareLock: RowExclusiveLock); } - | ACCESS opt_lmode { $$ = ($2? AccessShareLock: AccessExclusiveLock); } - | opt_lmode { $$ = ($1? ShareLock: ExclusiveLock); } - ; - -opt_lmode: SHARE { $$ = TRUE; } - | EXCLUSIVE { $$ = FALSE; } +lock_type: ACCESS SHARE { $$ = AccessShareLock; } + | ROW SHARE { $$ = RowShareLock; } + | ROW EXCLUSIVE { $$ = RowExclusiveLock; } + | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; } + | SHARE { $$ = ShareLock; } + | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; } + | EXCLUSIVE { $$ = ExclusiveLock; } + | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; } ; diff --git a/src/backend/storage/lmgr/lmgr.c b/src/backend/storage/lmgr/lmgr.c index fa3812a87e..10861348e8 100644 --- a/src/backend/storage/lmgr/lmgr.c +++ b/src/backend/storage/lmgr/lmgr.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lmgr.c,v 1.48 2001/06/22 00:04:59 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lmgr.c,v 1.49 2001/07/09 22:18:33 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -33,25 +33,35 @@ static LOCKMASK LockConflicts[] = { (1 << ExclusiveLock) | (1 << AccessExclusiveLock), /* RowExclusiveLock */ - (1 << ExclusiveLock) | (1 << ShareRowExclusiveLock) | (1 << ShareLock) | - (1 << AccessExclusiveLock), + (1 << ShareLock) | (1 << ShareRowExclusiveLock) | + (1 << ExclusiveLock) | (1 << AccessExclusiveLock), + + /* ShareUpdateExclusiveLock */ + (1 << ShareUpdateExclusiveLock) | + (1 << ShareLock) | (1 << ShareRowExclusiveLock) | + (1 << ExclusiveLock) | (1 << AccessExclusiveLock), /* ShareLock */ - (1 << ExclusiveLock) | (1 << ShareRowExclusiveLock) | - (1 << RowExclusiveLock) | (1 << AccessExclusiveLock), + (1 << RowExclusiveLock) | (1 << ShareUpdateExclusiveLock) | + (1 << ShareRowExclusiveLock) | + (1 << ExclusiveLock) | (1 << AccessExclusiveLock), /* ShareRowExclusiveLock */ - (1 << ExclusiveLock) | (1 << ShareRowExclusiveLock) | - (1 << ShareLock) | (1 << RowExclusiveLock) | (1 << AccessExclusiveLock), + (1 << RowExclusiveLock) | (1 << ShareUpdateExclusiveLock) | + (1 << ShareLock) | (1 << ShareRowExclusiveLock) | + (1 << ExclusiveLock) | (1 << AccessExclusiveLock), /* ExclusiveLock */ - (1 << ExclusiveLock) | (1 << ShareRowExclusiveLock) | (1 << ShareLock) | - (1 << RowExclusiveLock) | (1 << RowShareLock) | (1 << AccessExclusiveLock), + (1 << RowShareLock) | + (1 << RowExclusiveLock) | (1 << ShareUpdateExclusiveLock) | + (1 << ShareLock) | (1 << ShareRowExclusiveLock) | + (1 << ExclusiveLock) | (1 << AccessExclusiveLock), /* AccessExclusiveLock */ - (1 << ExclusiveLock) | (1 << ShareRowExclusiveLock) | (1 << ShareLock) | - (1 << RowExclusiveLock) | (1 << RowShareLock) | - (1 << AccessExclusiveLock) | (1 << AccessShareLock) + (1 << AccessShareLock) | (1 << RowShareLock) | + (1 << RowExclusiveLock) | (1 << ShareUpdateExclusiveLock) | + (1 << ShareLock) | (1 << ShareRowExclusiveLock) | + (1 << ExclusiveLock) | (1 << AccessExclusiveLock) }; @@ -63,14 +73,16 @@ static int LockPrios[] = { 2, /* RowExclusiveLock */ 3, - /* ShareLock */ + /* ShareUpdateExclusiveLock */ 4, - /* ShareRowExclusiveLock */ + /* ShareLock */ 5, - /* ExclusiveLock */ + /* ShareRowExclusiveLock */ 6, + /* ExclusiveLock */ + 7, /* AccessExclusiveLock */ - 7 + 8 }; LOCKMETHOD LockTableId = (LOCKMETHOD) NULL; diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c index 3eb0104827..cff407a4a8 100644 --- a/src/backend/storage/lmgr/lock.c +++ b/src/backend/storage/lmgr/lock.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.90 2001/06/27 23:31:39 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.91 2001/07/09 22:18:33 tgl Exp $ * * NOTES * Outside modules can create a lock table and acquire/release @@ -58,6 +58,7 @@ static char *lock_mode_names[] = "AccessShareLock", "RowShareLock", "RowExclusiveLock", + "ShareUpdateExclusiveLock", "ShareLock", "ShareRowExclusiveLock", "ExclusiveLock", diff --git a/src/include/storage/lmgr.h b/src/include/storage/lmgr.h index c279ef63f2..1fb490e6d6 100644 --- a/src/include/storage/lmgr.h +++ b/src/include/storage/lmgr.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: lmgr.h,v 1.31 2001/06/22 00:04:59 tgl Exp $ + * $Id: lmgr.h,v 1.32 2001/07/09 22:18:34 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -25,13 +25,14 @@ #define AccessShareLock 1 /* SELECT */ #define RowShareLock 2 /* SELECT FOR UPDATE */ #define RowExclusiveLock 3 /* INSERT, UPDATE, DELETE */ -#define ShareLock 4 /* CREATE INDEX */ -#define ShareRowExclusiveLock 5 /* like EXCLUSIVE MODE, allows - * SHARE ROW */ -#define ExclusiveLock 6 /* blocks ROW SHARE/SELECT...FOR +#define ShareUpdateExclusiveLock 4 /* VACUUM (non-FULL) */ +#define ShareLock 5 /* CREATE INDEX */ +#define ShareRowExclusiveLock 6 /* like EXCLUSIVE MODE, but allows + * ROW SHARE */ +#define ExclusiveLock 7 /* blocks ROW SHARE/SELECT...FOR * UPDATE */ -#define AccessExclusiveLock 7 /* ALTER TABLE, DROP TABLE, - * VACUUM, and unqualified LOCK +#define AccessExclusiveLock 8 /* ALTER TABLE, DROP TABLE, + * VACUUM FULL, and unqualified LOCK * TABLE */ /* diff --git a/src/include/storage/lock.h b/src/include/storage/lock.h index 30a13649e4..1010e6760b 100644 --- a/src/include/storage/lock.h +++ b/src/include/storage/lock.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: lock.h,v 1.50 2001/06/27 23:31:39 tgl Exp $ + * $Id: lock.h,v 1.51 2001/07/09 22:18:34 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -49,7 +49,7 @@ typedef int LOCKMODE; typedef int LOCKMETHOD; /* MAX_LOCKMODES cannot be larger than the # of bits in LOCKMASK */ -#define MAX_LOCKMODES 8 +#define MAX_LOCKMODES 10 /* * MAX_LOCK_METHODS corresponds to the number of spin locks allocated in @@ -91,7 +91,7 @@ typedef int LOCKMETHOD; * * prio -- each lockmode has a priority, so, for example, waiting * writers can be given priority over readers (to avoid - * starvation). + * starvation). XXX this field is not actually used at present! * * masterlock -- synchronizes access to the table */ diff --git a/src/interfaces/ecpg/preproc/preproc.y b/src/interfaces/ecpg/preproc/preproc.y index 2228c125d8..e0626025d8 100644 --- a/src/interfaces/ecpg/preproc/preproc.y +++ b/src/interfaces/ecpg/preproc/preproc.y @@ -326,7 +326,7 @@ make_name(void) %type createdb_opt_list opt_encoding OptInherit Geometric %type DropdbStmt ClusterStmt grantee RevokeStmt Bit bit %type GrantStmt privileges operation_commalist operation PosAllConst -%type opt_cursor opt_lmode ConstraintsSetStmt comment_tg AllConst +%type opt_cursor ConstraintsSetStmt comment_tg AllConst %type case_expr when_clause_list case_default case_arg when_clause %type select_clause opt_select_limit select_limit_value ConstraintTimeSpec %type select_offset_value ReindexStmt join_type opt_boolean @@ -2440,15 +2440,15 @@ opt_lock: IN lock_type MODE { $$ = cat_str(3, make_str("in"), $2, ma | /*EMPTY*/ { $$ = EMPTY;} ; -lock_type: SHARE ROW EXCLUSIVE { $$ = make_str("share row exclusive"); } - | ROW opt_lmode { $$ = cat2_str(make_str("row"), $2);} - | ACCESS opt_lmode { $$ = cat2_str(make_str("access"), $2);} - | opt_lmode { $$ = $1; } - ; - -opt_lmode: SHARE { $$ = make_str("share"); } - | EXCLUSIVE { $$ = make_str("exclusive"); } - ; +lock_type: ACCESS SHARE { $$ = make_str("access share"); } + | ROW SHARE { $$ = make_str("access share"); } + | ROW EXCLUSIVE { $$ = make_str("row exclusive"); } + | SHARE UPDATE EXCLUSIVE { $$ = make_str("share update exclusive"); } + | SHARE { $$ = make_str("share"); } + | SHARE ROW EXCLUSIVE { $$ = make_str("share row exclusive"); } + | EXCLUSIVE { $$ = make_str("exclusive"); } + | ACCESS EXCLUSIVE { $$ = make_str("access exclusive"); } + ; /***************************************************************************** * -- 2.11.0