Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Process use when you need to work with deadlock detection. This skill provides deadlock detection and resolution with comprehensive guidance and automation. Trigger with phrases like "detect deadlocks", "resolve deadlocks", or "prevent deadlocks".
| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-04 | ✗→✓ | ▲ Improved | 126% | 0% |
| case-03 | ✓→✓ | = Same ✓ | 63% | 0% |
| case-05 | ✓→✓ | = Same ✓ | 92% | 0% |
| case-06 | ✓→✓ | = Same ✓ | 253% | 0% |
| case-08 | ✓→✓ | = Same ✓ | 177% | 0% |
Detect, analyze, and prevent database deadlocks in PostgreSQL, MySQL, and MongoDB by examining lock wait graphs, parsing deadlock log entries, identifying the application code paths that cause lock ordering conflicts, and implementing preventive patterns.
pg_locks, INNODB_LOCK_WAITS)psql or mysql CLI for executing diagnostic querieslog_lock_waits = on and deadlock_timeout = 1s configuredinnodb_print_all_deadlocks = ON for deadlock logging to error logSELECT blocked.pid AS blocked_pid, blocked.query AS blocked_query, blocking.pid AS blocking_pid, blocking.query AS blocking_query FROM pg_stat_activity blocked JOIN pg_locks bl ON bl.pid = blocked.pid JOIN pg_locks bl2 ON bl2.locktype = bl.locktype AND bl2.relation = bl.relation AND bl2.pid != bl.pid JOIN pg_stat_activity blocking ON blocking.pid = bl2.pid WHERE NOT bl.grantedSELECT * FROM information_schema.INNODB_LOCK_WAITSERROR: deadlock detected entries, which include the two conflicting queries and the lock typesSHOW ENGINE INNODB STATUS\G and examine the LATEST DETECTED DEADLOCK sectionBEGIN/COMMIT blocks or ORM transaction decorators). Map the full sequence of operations within each transaction.READ COMMITTED isolation.SELECT ... FOR UPDATE NOWAIT or SKIP LOCKED to fail fast instead of waiting40P01, MySQL error code 1213) and retry the entire transaction up to 3 times with a short random delay.pg_advisory_lock() in PostgreSQL) to serialize access to contended resources at the application level, avoiding database-level lock contention entirely.| Error | Cause | Solution | |-------|-------|---------| | PostgreSQL error 40P01: deadlock detected | Circular lock dependency between transactions | Implement retry logic; fix lock ordering in application code; reduce transaction scope | | MySQL error 1213: Deadlock found when trying to get lock | InnoDB detected circular wait in lock wait graph | Enable innodb_print_all_deadlocks; analyze SHOW ENGINE INNODB STATUS; implement retry logic | | Lock wait timeout (not deadlock) | Transaction holding lock too long, exceeding lock_wait_timeout | Investigate the blocking transaction; increase timeout or implement NOWAIT; optimize the long-running transaction | | Phantom deadlocks in monitoring | Transient lock waits resolved before deadlock detection runs | Increase monitoring frequency; use database deadlock log instead of snapshot queries; set deadlock_timeout lower | | Deadlock frequency increases after schema change | New index or constraint creates additional lock targets | Analyze new lock patterns with EXPLAIN and pg_locks; adjust transaction scope to avoid locking new index entries |
Classic opposite-ordering deadlock in an order processing system: Transaction A processes order 100 (locks order row), then updates inventory for product 50 (waits for inventory lock). Transaction B processes order 200 with product 50 (locks inventory row), then updates order 100 status (waits for order lock). Fix: always lock inventory first, then order, regardless of the business flow.
MySQL gap lock deadlock on a queue table: Two workers concurrently DELETE FROM job_queue WHERE status = 'pending' LIMIT 1. InnoDB gap locks on the index range conflict even though the workers target different rows. Fix: use SELECT ... FOR UPDATE SKIP LOCKED to skip already-locked rows, or add unique job IDs and target specific rows.
Foreign key deadlock between parent and child inserts: Concurrent transactions inserting into order_items (child) acquire shared locks on orders (parent) for FK validation. A third transaction updating orders requires an exclusive lock and deadlocks with the shared FK locks. Fix: explicitly SELECT ... FOR UPDATE on the parent order row before inserting child items.
Other measured skills in the registry, with their headline benchmark lift.