Lesson 4.2

Locking and deadlocks

A query that is doing nothing for thirty seconds is usually waiting on a lock, not working.

48mIntermediate8.2k students

Overview

Blocked, not slow

A query that is doing nothing for thirty seconds is usually waiting on a lock, not working. The lock views tell you which session holds what and who is waiting, which turns a mystery into a specific culprit.

Deadlocks happen when two transactions acquire the same locks in opposite orders. The database detects the cycle and aborts one, and the durable fix is to always acquire locks in a consistent order.

Schema changes take heavy locks. An ALTER that rewrites a table blocks reads and writes for the duration, which on a large table means an outage.

In this lesson you will:

  • Know which statements take which locks
  • Diagnose a blocked query
  • Prevent deadlocks with consistent ordering

Resources

Previous Lesson
Next Lesson
Locking and deadlocks — PostgreSQL for Application Developers — Vertex