Understanding and Preventing Deadlocks in PostgreSQL

A deadlock is two transactions each holding a lock the other needs, both waiting forever.