Understanding Locking and Concurrency in PostgreSQL
summary
Explains that PostgreSQL uses three real lock layers with concrete tradeoffs: spin locks held for nanoseconds as the low level primitive, LW locks that allow read/write modes but have no deadlock detection, and heavyweight locks managed by the lock manager and visible via pg_locks shows heavyweight locks. Points out eight table lock modes, row locks stored on disk so no escalation limit, prepared transactions that survive restarts and appear with pid 0, and practical fixes: pg_stat_activity/pg_locks queries, CONCURRENTLY variants, short transactions and idle_in_transaction_timeout.
AI-written summary · about
chapters (16)
- 0:00Introduction to locking
- 1:31Locking subsystems overview
- 6:29Spin locks in PostgreSQL
- 7:55Lightweight locks explained
- 12:53Heavyweight locks deep dive
- 19:41Row-level locks and their use
- 22:28Lock lifecycle and management
- 25:16Prepared transactions and locks
- 27:48Lock manager internals
- 29:13Deadlock detection and resolution
- 31:04Diagnosing lock issues
- 33:54Handling blocked transactions
- 34:36Dangers of access exclusive locks
- 36:43Concurrent DDL operations
- 38:30Best practices for locking
- 40:13Summary and Q&A
related talks
48mUnderstanding and Reducing Application Contention
42mAnatomy of Table-Level Locks in PostgreSQL
39mAnatomy of Table-Level Locks in PostgreSQL
47mAnatomy of concurrent data access in PostgreSQL
58mConcurrency in Postgres
30mQueues in PostgreSQL
32mMonitoring Waits in PostgreSQL
45mTransactions and Concurrency Control Patterns
