intermediateLocking — Optimistic vs Pessimistic
How do deadlocks occur and how does PostgreSQL handle them?
Deadlock: Transaction A holds Lock 1 and waits for Lock 2. Transaction B holds Lock 2 and waits for Lock 1. Neither can proceed — circular dependency. PostgreSQL detection: periodically checks the wait graph for cycles (deadlock_timeout = 1s by default). When detected: PostgreSQL aborts one of the transactions (the one that's cheaper to restart) with ERROR: deadlock detected (code 40P01). Preventi
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response