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 modeStatements that take itConflicting modesIntention lock equivalent
ACCESS SHARESELECTACCESS EXCLUSIVEClose to IS
ROW SHARESELECT ... FOR UPDATE/SHAREEXCLUSIVE, ACCESS EXCLUSIVEIS
ROW EXCLUSIVEINSERT, UPDATE, DELETE, MERGESHARE and aboveIX
SHARE UPDATE EXCLUSIVEVACUUM, ANALYZE, CREATE INDEX CONCURRENTLYItself and above—
SHARECREATE INDEXROW EXCLUSIVE and other writesS
SHARE ROW EXCLUSIVECREATE TRIGGER, some ALTER TABLEItself and writesSIX
EXCLUSIVEREFRESH MATERIALIZED VIEW CONCURRENTLYEverything except ACCESS SHARE—
ACCESS EXCLUSIVEDROP, TRUNCATE, most ALTER TABLE, VACUUM FULLEverythingX
Row lock modeStatements that take itWhat it blocks
FOR KEY SHAREForeign key checkKey changes, deletes
FOR SHARESELECT ... FOR SHAREAll changes
FOR NO KEY UPDATEUPDATE of non-key columnsOther writes, FOR SHARE
FOR UPDATESELECT ... FOR UPDATE, DELETE, UPDATE of key columnsAll 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

PostgreSQLInnoDBOracleSQL Server
Where row locks are recordedRow headerPer-page bitmap (lock system memory)ITL in the blockLock manager memory
EscalationNoneNoneNoneYes (about 5,000 locks)
Range locksNone (Serializable uses SSI)Gap locks, next-key locksNoneKey-range locks (Serializable)
Plain SELECTNo lockNo lockNo lockShared lock (none if RCSI is on)

In the articles

NOTE The lock mode each statement takes may differ by version. ALTER TABLE in particular differs by subcommand.

Related pages