Database Deadlock Diagnosis

Detect, interpret, and eliminate database deadlocks in PostgreSQL and MySQL production systems.

Overview

Overview

A deadlock happens when two or more transactions each hold a lock the other needs, so neither can proceed. The database detects the cycle, aborts one transaction (the 'victim') and lets the other finish. The victim gets an error and the application must retry it.

The picture below shows the smallest case: two transactions, two rows, and the detector that breaks the tie.

Key concepts

  • Deadlock
  • deadlock_timeout
  • Lock Acquisition Order
  • NOWAIT
  • SKIP LOCKED

Interview questions

  • How do you read a PostgreSQL deadlock log entry to identify the root cause?
  • What is the difference between SELECT FOR UPDATE, NOWAIT, and SKIP LOCKED, and when should you use each?
  • What is the canonical application-code fix for deadlocks caused by row lock ordering?

Sign up free to read the full topic

More Troubleshooting topics

Start free on Cariara · Coding problems · System design studio