postgresql lock introduction
For official documentation, please refer to Postgresql.
Table level
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.
ACCESS SHARE
Conflicts with theACCESS EXCLUSIVE
lock mode only.
TheSELECT
command 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 theEXCLUSIVE
andACCESS EXCLUSIVE
lock modes.
TheSELECT FOR UPDATE
andSELECT FOR SHARE
commands acquire a lock of this mode on the target table(s) (in addition toACCESS SHARE
locks on any other tables that are referenced but not selectedFOR UPDATE
/FOR SHARE
).ROW EXCLUSIVE
Conflicts with theSHARE
,SHARE ROW EXCLUSIVE
,EXCLUSIVE
, andACCESS EXCLUSIVE
lock modes.
The commandsUPDATE
,DELETE
, andINSERT
acquire this lock mode on the target table (in addition toACCESS SHARE
locks 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 theSHARE UPDATE EXCLUSIVE
,SHARE
,SHARE ROW EXCLUSIVE
,EXCLUSIVE
, andACCESS EXCLUSIVE
lock modes.
This mode protects a table against concurrent schema changes andVACUUM
runs.
Acquired byVACUUM
(without FULL),ANALYZE
,CREATE INDEX CONCURRENTLY
, andALTER TABLE VALIDATE
and otherALTER TABLE
variants (for full details see ALTER TABLE).SHARE
Conflicts with theROW EXCLUSIVE
,SHARE UPDATE EXCLUSIVE
,SHARE ROW EXCLUSIVE
,EXCLUSIVE
, andACCESS EXCLUSIVE
lock modes.
This mode protects a table against concurrent data changes.
Acquired byCREATE INDEX
(withoutCONCURRENTLY
).SHARE ROW EXCLUSIVE
Conflicts with theROW EXCLUSIVE
,SHARE UPDATE EXCLUSIVE
,SHARE
,SHARE ROW EXCLUSIVE
,EXCLUSIVE
, andACCESS EXCLUSIVE
lock modes.
This mode protects a table against concurrent data changes, and is self-exclusive so that only one session can hold it at a time.
This lock mode is not automatically acquired by any PostgreSQL command.EXCLUSIVE
Conflicts with theROW SHARE
,ROW EXCLUSIVE
,SHARE UPDATE EXCLUSIVE
,SHARE
,SHARE ROW EXCLUSIVE
,EXCLUSIVE
, andACCESS EXCLUSIVE
lock modes.
This mode allows only concurrentACCESS SHARE
locks, i.e., only reads from the table can proceed in parallel with a transaction holding this lock mode.
Acquired byREFRESH MATERIALIZED VIEW CONCURRENTLY
.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 theDROP TABLE
,TRUNCATE
,REINDEX
,CLUSTER
, andVACUUM FULL
commands.
Many forms ofALTER TABLE
also acquire a lock at this level (see ALTER TABLE).
This is also the default lock mode forLOCK TABLE
statements that do not specify a mode explicitly.
Row level
PostgreSQL doesn’t remember any information about modified rows in memory, so there is no limit on the number of rows locked at one time. However, locking a row might cause a disk write, e.g., SELECT FOR UPDATE modifies selected rows to mark them locked, and so will result in disk writes.
FOR UPDATE
This prevents them from being locked, modified or deleted by other transactions until the current transaction ends.
That is, other transactions that attemptUPDATE
,DELETE
,SELECT FOR UPDATE
,SELECT FOR NO KEY UPDATE
,SELECT FOR SHARE
orSELECT FOR KEY SHARE
of these rows will be blocked until the current transaction ends;
conversely,SELECT FOR UPDATE
will wait for a concurrent transaction that has run any of those commands on the same row, and will then lock and return the updated row (or no row, if the row was deleted).FOR NO KEY UPDATE
Behaves similarly to FOR UPDATE, except that the lock acquired is weaker: this lock will not blockSELECT FOR KEY SHARE
commands that attempt to acquire a lock on the same rows.
This lock mode is also acquired by anyUPDATE
that does not acquire aFOR UPDATE
lock.FOR SHARE
Behaves similarly toFOR NO KEY UPDATE
, except that it acquires a shared lock rather than exclusive lock on each retrieved row.
A shared lock blocks other transactions from performingUPDATE
,DELETE
,SELECT FOR UPDATE
orSELECT FOR NO KEY UPDATE
on these rows, but it does not prevent them from performingSELECT FOR SHARE
orSELECT FOR KEY SHARE
.FOR KEY SHARE
Behaves similarly toFOR SHARE
, except that the lock is weaker:SELECT FOR UPDATE
is blocked, but notSELECT FOR NO KEY UPDATE
.
A key-shared lock blocks other transactions from performingDELETE
or anyUPDATE
that changes the key values, but not otherUPDATE
, and neither does it preventSELECT FOR NO KEY UPDATE
,SELECT FOR SHARE
, orSELECT FOR KEY SHARE
.
Page level
In addition to table and row locks, page-level share/exclusive locks are used to control read/write access to table pages in the shared buffer pool.
These locks are released immediately after a row is fetched or updated. Application developers normally need not be concerned with page-level locks, but they are mentioned here for completeness.