liveBook · Manning

Locking

As explained in PostgreSQL Mistakes and How to Avoid Them

A database lock is a concurrency control mechanism that permits or prevents access to a database object by other transactions.

Mutual exclusion coordinates concurrent database access through locks. PostgreSQL obtains locks at table or row level; conflicting lock types either permit or prevent another transaction from using the same object. section 6.5.1

 Client A               | Client B
------------------------+-----------------------
 BEGIN;                 |
 SELECT * FROM mytable; |
 (...)                  |
                        | ALTER TABLE mytable
                        | ADD description text;
                        | *hangs*

A realistic PostgreSQL failure: Client A leaves a transaction open after reading, while Client B’s ALTER TABLE waits for the required lock. section 6.5.1

The tempting mistake is to assume that a completed SELECT leaves no operational consequence.

  • An open transaction retains an ACCESS SHARE table lock, even though that lock does not block ordinary reads or writes. section 6.5.1
  • ALTER TABLE requires ACCESS EXCLUSIVE, which conflicts with the lingering read lock and can wait indefinitely. section 6.5.1

How PostgreSQL Mistakes and How to Avoid Them covers it

How deep it goes
Explains lock levels, conflicts, and a blocking sequence, but stops short of catalog-based lock diagnosis or exhaustive lock-mode reference.
Examples
Uses an idle SELECT transaction, ALTER TABLE, and a later SELECT to demonstrate cascading lock blockage.
What it assumes
Assumes transaction and ACID terminology while connecting lock behavior to MVCC visibility.
What you can do after
Diagnose why ordinary-looking reads block schema changes and why later queries queue behind the blocked operation.
How it connects
Connects locks with transactions, MVCC, ACID isolation, DDL, and table or row access.

Also explained in

Compare how 18 books explain locking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of PostgreSQL Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.