lock.7
来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 161 行
7
161 行
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "LOCK" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMELOCK \- lock a table.SH SYNOPSIS.sp.nfLOCK [ TABLE ] \fIname\fR [, ...] [ IN \fIlockmode\fR MODE ] [ NOWAIT ]where \fIlockmode\fR is one of: ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE | SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE.sp.fi.SH "DESCRIPTION".PP\fBLOCK TABLE\fR obtains a table-level lock, waitingif necessary for any conflicting locks to be released. IfNOWAIT is specified, \fBLOCKTABLE\fR does not wait to acquire the desired lock: if itcannot be acquired immediately, the command is aborted and anerror is emitted. Once obtained, the lock is held for theremainder of the current transaction. (There is no \fBUNLOCKTABLE\fR command; locks are always released at transactionend.).PPWhen acquiring locks automatically for commands that referencetables, PostgreSQL always uses the leastrestrictive lock mode possible. \fBLOCK TABLE\fRprovides for cases when you might need more restrictive locking.For example, suppose an application runs a transaction at theRead Committed isolation level and needs to ensure that data in atable remains stable for the duration of the transaction. Toachieve this you could obtain SHARE lock mode over thetable before querying. This will prevent concurrent data changesand ensure subsequent reads of the table see a stable view ofcommitted data, because SHARE lock mode conflicts withthe ROW EXCLUSIVE lock acquired by writers, and your\fBLOCK TABLE \fIname\fB IN SHARE MODE\fRstatement will wait until any concurrent holders of ROWEXCLUSIVE mode locks commit or roll back. Thus, once youobtain the lock, there are no uncommitted writes outstanding;furthermore none can begin until you release the lock..PPTo achieve a similar effect when running a transaction at the Serializableisolation level, you have to execute the \fBLOCK TABLE\fR statementbefore executing any \fBSELECT\fR or data modification statement.A serializable transaction's view of data will be frozen when its first\fBSELECT\fR or data modification statement begins. A \fBLOCKTABLE\fR later in the transaction will still prevent concurrent writes\(em but it won't ensure that what the transaction reads corresponds tothe latest committed values..PPIf a transaction of this sort is going to change the data in thetable, then it should use SHARE ROW EXCLUSIVE lock modeinstead of SHARE mode. This ensures that only onetransaction of this type runs at a time. Without this, a deadlockis possible: two transactions might both acquire SHAREmode, and then be unable to also acquire ROW EXCLUSIVEmode to actually perform their updates. (Note that a transaction'sown locks never conflict, so a transaction can acquire ROWEXCLUSIVE mode when it holds SHARE mode \(em but notif anyone else holds SHARE mode.) To avoid deadlocks,make sure all transactions acquire locks on the same objects in thesame order, and if multiple lock modes are involved for a singleobject, then transactions should always acquire the mostrestrictive mode first..PPMore information about the lock modes and locking strategies can befound in in the documentation..SH "PARAMETERS".TP\fB\fIname\fB\fRThe name (optionally schema-qualified) of an existing table tolock.The command LOCK TABLE a, b; is equivalent toLOCK TABLE a; LOCK TABLE b;. The tables are lockedone-by-one in the order specified in the \fBLOCKTABLE\fR command..TP\fB\fIlockmode\fB\fRThe lock mode specifies which locks this lock conflicts with.Lock modes are described in in the documentation.If no lock mode is specified, then ACCESSEXCLUSIVE, the most restrictive mode, is used..TP\fBNOWAIT\fRSpecifies that \fBLOCK TABLE\fR should not wait forany conflicting locks to be released: if the specified lock(s)cannot be acquired immediately without waiting, the transactionis aborted..SH "NOTES".PPLOCK TABLE ... IN ACCESS SHARE MODE requires SELECTprivileges on the target table. All other forms of \fBLOCK\fRrequire UPDATE and/or DELETE privileges..PP\fBLOCK TABLE\fR is useful only inside a transactionblock (\fBBEGIN\fR/\fBCOMMIT\fR pair), since the lockis dropped as soon as the transaction ends. A \fBLOCKTABLE\fR command appearing outside any transaction block forms aself-contained transaction, so the lock will be dropped as soon asit is obtained..PP\fBLOCK TABLE\fR only deals with table-level locks, and sothe mode names involving ROW are all misnomers. Thesemode names should generally be read as indicating the intention ofthe user to acquire row-level locks within the locked table. Also,ROW EXCLUSIVE mode is a sharable table lock. Keep inmind that all the lock modes have identical semantics so far as\fBLOCK TABLE\fR is concerned, differing only in the rulesabout which modes conflict with which. For information on how toacquire an actual row-level lock, see in the documentationand the FOR UPDATE/FOR SHARE Clause [\fBselect\fR(7)] in the \fBSELECT\fRreference documentation..SH "EXAMPLES".PPObtain a SHARE lock on a primary key table when going to performinserts into a foreign key table:.sp.nfBEGIN WORK;LOCK TABLE films IN SHARE MODE;SELECT id FROM films WHERE name = 'Star Wars: Episode I - The Phantom Menace';-- Do ROLLBACK if record was not returnedINSERT INTO films_user_comments VALUES (_id_, 'GREAT! I was waiting for it for so long!');COMMIT WORK;.sp.fi.PPTake a SHARE ROW EXCLUSIVE lock on a primary key table when going to performa delete operation:.sp.nfBEGIN WORK;LOCK TABLE films IN SHARE ROW EXCLUSIVE MODE;DELETE FROM films_user_comments WHERE id IN (SELECT id FROM films WHERE rating < 5);DELETE FROM films WHERE rating < 5;COMMIT WORK;.sp.fi.SH "COMPATIBILITY".PPThere is no \fBLOCK TABLE\fR in the SQL standard,which instead uses \fBSET TRANSACTION\fR to specifyconcurrency levels on transactions. PostgreSQL supports that too;see SET TRANSACTION [\fBset_transaction\fR(7)] for details..PPExcept for ACCESS SHARE, ACCESS EXCLUSIVE,and SHARE UPDATE EXCLUSIVE lock modes, thePostgreSQL lock modes and the\fBLOCK TABLE\fR syntax are compatible with thosepresent in Oracle.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?