| The PostgreSQL 9.0 Reference Manual - Volume 1A - SQL Language Reference
by The PostgreSQL Global Development Group Paperback (6"x9"), 454 pages ISBN 9781906966041 RRP £14.95 ($19.95) Sales of this book support the PostgreSQL project! Get a printed copy>>> |
11.3.1 Table-Level Locks
The list below shows the available lock modes and the contexts in
which they are used automatically by
PostgreSQL. You can also acquire any
of these locks explicitly with the command LOCK.
Remember that all of these lock modes are table-level locks,
even if the name contains the word
“row”; the names of the lock modes are historical.
To some extent the names reflect the typical usage of each lock
mode--but the semantics are all the same. The only real difference
between one lock mode and another is the set of lock modes with
which each conflicts (see Table 11-2).
Two transactions cannot hold locks of conflicting
modes on the same table at the same time. (However, a transaction
never conflicts with itself. For example, it might acquire
ACCESS EXCLUSIVE lock and later acquire
ACCESS SHARE lock on the same table.) Non-conflicting
lock modes can be held concurrently by many transactions. Notice in
particular that some lock modes are self-conflicting (for example,
an ACCESS EXCLUSIVE lock cannot be held by more than one
transaction at a time) while others are not self-conflicting (for example,
an ACCESS SHARE lock can be held by multiple transactions).
Table-level lock modes:
ACCESS SHARE-
Conflicts with the
ACCESS EXCLUSIVElock mode only. TheSELECTcommand acquires a lock of this mode on referenced tables. In general, any query that only reads a table and does not modify it will acquire this lock mode. ROW SHARE-
Conflicts with the
EXCLUSIVEandACCESS EXCLUSIVElock modes. TheSELECT FOR UPDATEandSELECT FOR SHAREcommands acquire a lock of this mode on the target table(s) (in addition toACCESS SHARElocks on any other tables that are referenced but not selectedFOR UPDATE/FOR SHARE). ROW EXCLUSIVE-
Conflicts with the
SHARE,SHARE ROW EXCLUSIVE,EXCLUSIVE, andACCESS EXCLUSIVElock modes. The commandsUPDATE,DELETE, andINSERTacquire this lock mode on the target table (in addition toACCESS SHARElocks on any other referenced tables). In general, this lock mode will be acquired by any command that modifies data in a table. SHARE UPDATE EXCLUSIVE-
Conflicts with the
SHARE UPDATE EXCLUSIVE,SHARE,SHARE ROW EXCLUSIVE,EXCLUSIVE, andACCESS EXCLUSIVElock modes. This mode protects a table against concurrent schema changes andVACUUMruns. Acquired byVACUUM(withoutFULL),ANALYZE, andCREATE INDEX CONCURRENTLY. SHARE-
Conflicts with the
ROW EXCLUSIVE,SHARE UPDATE EXCLUSIVE,SHARE ROW EXCLUSIVE,EXCLUSIVE, andACCESS EXCLUSIVElock modes. This mode protects a table against concurrent data changes. Acquired byCREATE INDEX(withoutCONCURRENTLY). SHARE ROW EXCLUSIVE-
Conflicts with the
ROW EXCLUSIVE,SHARE UPDATE EXCLUSIVE,SHARE,SHARE ROW EXCLUSIVE,EXCLUSIVE, andACCESS EXCLUSIVElock modes. This lock mode is not automatically acquired by any PostgreSQL command. EXCLUSIVE-
Conflicts with the
ROW SHARE,ROW EXCLUSIVE,SHARE UPDATE EXCLUSIVE,SHARE,SHARE ROW EXCLUSIVE,EXCLUSIVE, andACCESS EXCLUSIVElock modes. This mode allows only concurrentACCESS SHARElocks, i.e., only reads from the table can proceed in parallel with a transaction holding this lock mode. This lock mode is not automatically acquired on user tables by any PostgreSQL command. However it is acquired on certain system catalogs in some operations. ACCESS EXCLUSIVE-
Conflicts with locks of all modes (
ACCESS SHARE,ROW SHARE,ROW EXCLUSIVE,SHARE UPDATE EXCLUSIVE,SHARE,SHARE ROW EXCLUSIVE,EXCLUSIVE, andACCESS EXCLUSIVE). This mode guarantees that the holder is the only transaction accessing the table in any way. Acquired by theALTER TABLE,DROP TABLE,TRUNCATE,REINDEX,CLUSTER, andVACUUM FULLcommands. This is also the default lock mode forLOCK TABLEstatements that do not specify a mode explicitly.
Tip: Only an
ACCESS EXCLUSIVElock blocks aSELECT(withoutFOR UPDATE/SHARE) statement.
Once acquired, a lock is normally held till end of transaction. But if a
lock is acquired after establishing a savepoint, the lock is released
immediately if the savepoint is rolled back to. This is consistent with
the principle that ROLLBACK cancels all effects of the
commands since the savepoint. The same holds for locks acquired within a
PL/pgSQL exception block: an error escape from the block
releases locks acquired within it.
| requested lock mode | current lock mode
| |||||||
| access share | row share | row excl. | share update excl. | share | share row excl. | excl. | access excl.
| |
| access share | X
| |||||||
| row share | X | X
| ||||||
| row excl. | X | X | X | X
| ||||
| share update excl. | X | X | X | X | X
| |||
| share | X | X | X | X | X
| |||
| share row excl. | X | X | X | X | X | X
| ||
| excl. | X | X | X | X | X | X | X
| |
| access excl. | X | X | X | X | X | X | X | X |
| ISBN 9781906966041 | The PostgreSQL 9.0 Reference Manual - Volume 1A - SQL Language Reference | See the print edition |