LOCK TABLE name
[, ...] IN lockmode
MODE [ NOWAIT ]
is one of:
obtains a table-level lock, waiting if necessary for any conflicting locks to be released. If NOWAIT
is specified, LOCK TABLE
does not wait to acquire the desired lock: if it cannot be acquired immediately, the command is aborted and an error is emitted. Once obtained, the lock is held for the remainder of the current transaction. (There is no UNLOCK TABLE
command; locks are always released at transaction end.)
When acquiring locks automatically for commands that reference tables, Advanced Server
always uses the least restrictive lock mode possible. LOCK TABLE
provides for cases when you might need more restrictive locking. For example, suppose an application runs a transaction at the isolation level read committed and needs to ensure that data in a table remains stable for the duration of the transaction. To achieve this you could obtain SHARE
lock mode over the table before querying. This will prevent concurrent data changes and ensure subsequent reads of the table see a stable view of committed data, because SHARE
lock mode conflicts with the ROW EXCLUSIVE
lock acquired by writers, and your LOCK TABLE
name IN SHARE MODE
statement will wait until any concurrent holders of ROW EXCLUSIVE
mode locks commit or roll back. Thus, once you obtain the lock, there are no uncommitted writes outstanding; furthermore none can begin until you release the lock.
The command LOCK TABLE a, b
; is equivalent to LOCK TABLE a
; LOCK TABLE b
. The tables are locked one-by-one in the order specified in the LOCK TABLE
Specifies that LOCK TABLE
should not wait for any conflicting locks to be released: if the specified lock cannot be immediately acquired without waiting, the transaction is aborted.
is useful only inside a transaction block since the lock is dropped as soon as the transaction ends. A LOCK TABLE
command appearing outside any transaction block forms a self-contained transaction, so the lock will be dropped as soon as it is obtained.
only deals with table-level locks, and so the mode names involving ROW
are all misnomers. These mode names should generally be read as indicating the intention of the user to acquire row-level locks within the locked table. Also, ROW EXCLUSIVE
mode is a sharable table lock. Keep in mind that all the lock modes have identical semantics so far as LOCK TABLE
is concerned, differing only in the rules about which modes conflict with which.