Sub topic 3
Table-Level Lock Modes and Which Ones Conflict
Assume you know basic SQL, transactions, primary keys, foreign keys, and how to use two concurrent PostgreSQL sessions. This chapter focuses on table-level locks. Row-level locks are related but follow different rules.
What a table-level lock protects
PostgreSQL automatically takes a table-level lock when a statement uses a table. The lock mode describes which other operations may run at the same time. A table-level lock does not necessarily prevent access to every row. For example, a normal SELECT takes an ACCESS SHARE lock, which is compatible with most operations.
Locks normally remain until the transaction ends. A lock acquired inside BEGIN is released by COMMIT or ROLLBACK, not when the statement finishes.
BEGIN;
SELECT * FROM accounts;
-- The SELECT holds an ACCESS SHARE lock.
COMMIT;
-- The table-level lock is released here.PostgreSQL uses multiversion concurrency control (MVCC), so ordinary readers usually do not block writers. However, readers still conflict with operations that must change the table's structure, such as many forms of ALTER TABLE.
The main table-level lock modes
PostgreSQL has eight table-level lock modes. You usually do not choose the mode directly. PostgreSQL selects one based on the statement, although you can request one with LOCK.
| Lock mode | Common source | Conflicts with |
|---|---|---|
| `ACCESS SHARE` | `SELECT` | `ACCESS EXCLUSIVE` |
| `ROW SHARE` | `SELECT ... FOR UPDATE` | `EXCLUSIVE`, `ACCESS EXCLUSIVE` |
| `ROW EXCLUSIVE` | `INSERT`, `UPDATE`, `DELETE` | `SHARE`, `SHARE ROW EXCLUSIVE`, `EXCLUSIVE`, `ACCESS EXCLUSIVE` |
| `SHARE UPDATE EXCLUSIVE` | `VACUUM`, `ANALYZE`, some concurrent index operations | `SHARE UPDATE EXCLUSIVE`, `SHARE`, `SHARE ROW EXCLUSIVE`, `EXCLUSIVE`, `ACCESS EXCLUSIVE` |
| `SHARE` | `CREATE INDEX` | `ROW EXCLUSIVE`, `SHARE UPDATE EXCLUSIVE`, `SHARE ROW EXCLUSIVE`, `EXCLUSIVE`, `ACCESS EXCLUSIVE` |
| `SHARE ROW EXCLUSIVE` | Some restrictive maintenance operations | `ROW EXCLUSIVE`, `SHARE UPDATE EXCLUSIVE`, `SHARE`, `SHARE ROW EXCLUSIVE`, `EXCLUSIVE`, `ACCESS EXCLUSIVE` |
| `EXCLUSIVE` | Rare explicit or internal operations | `ROW SHARE`, `ROW EXCLUSIVE`, `SHARE UPDATE EXCLUSIVE`, `SHARE`, `SHARE ROW EXCLUSIVE`, `EXCLUSIVE`, `ACCESS EXCLUSIVE` |
| `ACCESS EXCLUSIVE` | Most table rewrites and many `ALTER TABLE` commands | Every table-level mode |
A conflict means that the second transaction must wait until the first transaction releases its lock. Compatible locks can coexist.
Reading a conflict in practice
An UPDATE takes a ROW EXCLUSIVE table lock. A normal SELECT takes ACCESS SHARE, and those modes are compatible. Both statements can proceed.
A typical ALTER TABLE takes ACCESS EXCLUSIVE. That mode conflicts with even ACCESS SHARE, so a new SELECT waits while an earlier transaction holds a conflicting lock.
-- Session A
BEGIN;
SELECT * FROM accounts;
-- The transaction stays open.
-- Session B
ALTER TABLE accounts ADD COLUMN note text;
-- Usually waits for Session A because ALTER TABLE requests
-- ACCESS EXCLUSIVE and the SELECT holds ACCESS SHARE.
-- Session A
COMMIT;
-- Session B can now continue.The exact lock mode can vary by command and PostgreSQL version. Check the command's documentation before assuming that all schema changes have the same behavior.
Explicitly requesting a table lock
Use LOCK when an operation needs a known table-level guarantee. The transaction must already be active.
BEGIN;
LOCK TABLE accounts IN SHARE MODE;
-- Perform work that requires writers to stay out.
COMMIT;SHARE permits other readers but conflicts with ROW EXCLUSIVE, which is used by INSERT, UPDATE, and DELETE. It is therefore stronger than the lock taken by a normal read.
Do not use a stronger lock than necessary. Strong locks increase waiting and can create longer blocking chains. Keep the transaction short, especially when it holds a lock that conflicts with ordinary application traffic.
Lock modes and row locks are separate
ROW EXCLUSIVE does not mean that PostgreSQL locks every row touched by an UPDATE at the table-lock level. The statement also takes row-level locks on affected rows. Two updates to different rows can often run concurrently because their row locks do not conflict.
The table-level mode still matters because it determines whether the statements can begin alongside operations such as index creation or schema changes.
Exercise
Exercise
Session A has an open transaction after a normal SELECT. Session B runs UPDATE on the same table. Session C runs ALTER TABLE. Which session is likely to wait, and why?
Show answer
Session B can usually proceed because ROW EXCLUSIVE and ACCESS SHARE are compatible. Session C requests ACCESS EXCLUSIVE, which conflicts with Session A's ACCESS SHARE lock, so it waits until Session A ends its transaction.