PostgreSQL Lock Modes
PostgreSQL uses 8 table-level lock modes and 4 row-level lock modes. Table locks are recorded in the lock table in shared memory, and row locks in the row's own header (xmax in the tuple header). A plain SELECT reads a snapshot through MVCC, so it takes no row locks and does not wait for writes or block them.
Key specs
| Table lock mode | Statements that take it | Conflicting modes | Intention lock equivalent |
|---|---|---|---|
ACCESS SHARE | SELECT | ACCESS EXCLUSIVE | Close to IS |
ROW SHARE | SELECT ... FOR UPDATE/SHARE | EXCLUSIVE, ACCESS EXCLUSIVE | IS |
ROW EXCLUSIVE | INSERT, UPDATE, DELETE, MERGE | SHARE and above | IX |
SHARE UPDATE EXCLUSIVE | VACUUM, ANALYZE, CREATE INDEX CONCURRENTLY | Itself and above | — |
SHARE | CREATE INDEX | ROW EXCLUSIVE and other writes | S |
SHARE ROW EXCLUSIVE | CREATE TRIGGER, some ALTER TABLE | Itself and writes | SIX |
EXCLUSIVE | REFRESH MATERIALIZED VIEW CONCURRENTLY | Everything except ACCESS SHARE | — |
ACCESS EXCLUSIVE | DROP, TRUNCATE, most ALTER TABLE, VACUUM FULL | Everything | X |
| Row lock mode | Statements that take it | What it blocks |
|---|---|---|
FOR KEY SHARE | Foreign key check | Key changes, deletes |
FOR SHARE | SELECT ... FOR SHARE | All changes |
FOR NO KEY UPDATE | UPDATE of non-key columns | Other writes, FOR SHARE |
FOR UPDATE | SELECT ... FOR UPDATE, DELETE, UPDATE of key columns | All row locks |
How it works
Where row locks are recorded. When a row is locked, the locking transaction's number and mode bits are written to that row's xmax. When several transactions hold shared locks together, a separate number called a MultiXact is written. The lock table in memory does not grow as the row count grows, so there is no escalation that turns row locks into a table lock. Instead, taking a row lock requires modifying that page, so WAL is generated.
How waiting works. When a transaction meets a row locked by another transaction, it waits on the lock held on that transaction's number itself. So in pg_locks, it appears as waiting for a transactionid lock rather than a row.
DDL queueing. While an ALTER TABLE waits for ACCESS EXCLUSIVE, a SELECT that arrives later also queues behind it. When DDL waits because of one long query, the whole table looks stalled, so set a short lock_timeout before DDL.
Deadlock. If a lock wait exceeds deadlock_timeout (default 1 second), a deadlock check runs, and if one is found, one transaction is ended with a deadlock detected error.
Configuration and checks
-- Who is blocking whom
SELECT pid, pg_blocking_pids(pid) AS blocked_by, wait_event_type, state, left(query, 60)
FROM pg_stat_activity WHERE cardinality(pg_blocking_pids(pid)) > 0;
SELECT locktype, relation::regclass, mode, granted, pid FROM pg_locks WHERE NOT granted;
SET lock_timeout = '3s';
ALTER TABLE orders ADD COLUMN note text;
SELECT * FROM orders WHERE id = 1 FOR UPDATE NOWAIT;
SELECT * FROM jobs WHERE status = 'ready' LIMIT 10 FOR UPDATE SKIP LOCKED;
Comparison with other DBMSs
| PostgreSQL | InnoDB | Oracle | SQL Server | |
|---|---|---|---|---|
| Where row locks are recorded | Row header | Per-page bitmap (lock system memory) | ITL in the block | Lock manager memory |
| Escalation | None | None | None | Yes (about 5,000 locks) |
| Range locks | None (Serializable uses SSI) | Gap locks, next-key locks | None | Key-range locks (Serializable) |
Plain SELECT | No lock | No lock | No lock | Shared lock (none if RCSI is on) |
In the articles
- 7.2 Lock Basics
- 7.3 The Lock Family: S, U, X and Intention Locks
- 7.4 Lock Escalation: When Row Locks Become a Table Lock
- 7.6 Locking Reads: FOR UPDATE, Optimistic and Pessimistic Locking
NOTE The lock mode each statement takes may differ by version.
ALTER TABLEin particular differs by subcommand.