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 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 theEXCLUSIVEandACCESS 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 theSHARE,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 theSHARE UPDATE EXCLUSIVE,SHARE,SHARE ROW EXCLUSIVE,EXCLUSIVE, andACCESS EXCLUSIVElock modes.
This mode protects a table against concurrent schema changes andVACUUMruns.
Acquired byVACUUM(without FULL),ANALYZE,CREATE INDEX CONCURRENTLY, andALTER TABLE VALIDATEand otherALTER TABLEvariants (for full details see ALTER TABLE).SHARE
Conflicts with theROW 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 theROW EXCLUSIVE,SHARE UPDATE EXCLUSIVE,SHARE,SHARE ROW EXCLUSIVE,EXCLUSIVE, andACCESS EXCLUSIVElock 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 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.
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 FULLcommands.
Many forms ofALTER TABLEalso acquire a lock at this level (see ALTER TABLE).
This is also the default lock mode forLOCK TABLEstatements 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 SHAREorSELECT FOR KEY SHAREof these rows will be blocked until the current transaction ends;
conversely,SELECT FOR UPDATEwill 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 SHAREcommands that attempt to acquire a lock on the same rows.
This lock mode is also acquired by anyUPDATEthat does not acquire aFOR UPDATElock.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 UPDATEorSELECT FOR NO KEY UPDATEon these rows, but it does not prevent them from performingSELECT FOR SHAREorSELECT FOR KEY SHARE.FOR KEY SHARE
Behaves similarly toFOR SHARE, except that the lock is weaker:SELECT FOR UPDATEis blocked, but notSELECT FOR NO KEY UPDATE.
A key-shared lock blocks other transactions from performingDELETEor anyUPDATEthat 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.