- Home
- Skills
- DevOps & Cloud
- Database Deadlock Analyzer for MySQL and PostgreSQL
Database Deadlock Analyzer for MySQL and PostgreSQL
Give it a MySQL or PostgreSQL deadlock log and get the rows involved, the pattern behind the deadlock and the code change that stops it.
$1
What it does
- Turns a cryptic deadlock log into a table of what each transaction ran, held and waited for
- Shows the actual rows and keys involved instead of hex dumps
- Groups repeated deadlocks so you fix the pattern that happens most
- Names the cause in plain words, such as rows updated in opposite order or check-then-insert
- Gives the code change for each cause, with examples to copy
- Reminds you to retry the rolled-back transaction and shows how
Who it's for
Backend developers seeing deadlock errors in production
Find which code paths collide and how to change them, from the log you already have.
Try asking: “We get 'Deadlock found when trying to get lock' every day. Here is SHOW ENGINE INNODB STATUS.”
DBAs and SREs on call for database incidents
Group a day of deadlocks from the error log and hand developers the pattern and the fix.
Try asking: “Summarize the deadlocks in this MySQL error log and tell me which one to fix first.”
Teams running database migrations
See why a migration deadlocked with live traffic and how to run it safely.
Try asking: “Our ALTER TABLE deadlocked in production. Here is the PostgreSQL log.”
Database Deadlock Analyzer for MySQL and PostgreSQL
Example session with this skill installed
Our checkout sometimes fails with "Deadlock found when trying to get lock; try restarting transaction". Here is SHOW ENGINE INNODB STATUS from right after one.
- Read your context and instructions
- Compiled the database deadlock analyzer
Deadlock report
1 deadlock, 1 pattern.
1. Same rows locked in opposite order (MySQL/InnoDB, 1×)
| Transaction (1) | Transaction (2) — rolled back | |
|---|---|---|
| Statement | UPDATE stock_levels SET qty = qty - 2 WHERE product_id = 1002 | UPDATE stock_levels SET qty = qty - 1 WHERE product_id = 1001 |
| Holds | X record lock on tokomaju.stock_levels, index PRIMARY (1001) | X record lock on tokomaju.stock_levels, index PRIMARY (1002) |
| Waits for | X record lock on tokomaju.stock_levels, index PRIMARY (1002) | X record lock on tokomaju.stock_levels, index PRIMARY (1001) |
Why: The transactions update the same rows in a different order: each holds one row the other needs.
Fix: Lock rows in one order everywhere: sort the keys before updating, or lock them all up front with SELECT ... WHERE id IN (...) ORDER BY id FOR UPDATE.
- Both transactions run the same statement for different keys: sort the keys before the loop.
Connects securely to your tools. The creator never sees your data.
About this skill
A deadlock log says which transactions collided but not why. MySQL's InnoDB output is hex dumps of index records and lock modes like "lock_mode X locks gap before rec insert intention waiting"; PostgreSQL's is two process ids and a transaction number. This skill reads them, decodes the rows and keys involved, groups repeated deadlocks, and names the pattern behind each, because every pattern has a different fix.
Reads
- The LATEST DETECTED DEADLOCK section of SHOW ENGINE INNODB STATUS (MySQL 5.7 and 8).
- MySQL error logs written with innodb_print_all_deadlocks, with every deadlock.
- PostgreSQL logs with "deadlock detected", and application exceptions that quote them.
Recognizes
- Rows updated in opposite order (including the same code path called with items in different order).
- Check-then-insert races on gap locks, and concurrent inserts of the same unique key.
- Foreign key checks: a child insert locking the parent row, then the parent being updated.
- A secondary index and the primary key locked in opposite order.
- AUTO-INC table locks, and PostgreSQL migrations taking table locks while the app runs.
- Hints: transactions locking many rows (a missing index), tables without a primary key, long-open transactions.
For each pattern you get the cause in plain words, the evidence, and the code change (sorting keys, INSERT ... ON DUPLICATE KEY UPDATE or ON CONFLICT, locking the parent first, FOR NO KEY UPDATE, lock_timeout for migrations), plus a retry wrapper. It only reads the log files you give it; it never connects to a database.
InnoDB logs only the current statement of each transaction, so earlier statements that took the first lock must be confirmed in the code. MySQL 5.7 doesn't print the locks held by transaction (1); they are inferred. Column names aren't in the log, and dates and decimals in keys stay as hex. PostgreSQL csvlog and jsonlog formats aren't parsed. It explains deadlocks; it doesn't diagnose plain lock-wait timeouts.
Python 3.8+ script using only the standard library, run by the AI agent in a terminal: scripts/deadlocks.py FILE... (or - for stdin). Input: SHOW ENGINE INNODB STATUS output, MySQL 5.7/8 error logs (prefixes and source-file suffixes are stripped), PostgreSQL stderr logs with any log_line_prefix, or application logs quoting the PostgreSQL error (JDBC, psycopg). Output: Markdown report grouped by pattern, or JSON with --json. Exit code 1 = deadlocks found, 2 = none found. Decodes InnoDB record fields (signed and unsigned integers, ASCII strings, supremum) and identifies records by space, page and heap number. Never connects to a database and writes nothing. references/fixes.md has the code patterns per cause. Works with any agent that can run Python, such as Claude Code, Codex or Cursor.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Money-back guarantee
Doesn't do what its page promises? You get your money back.
Trust & safety
Security scan
Security scan passed today
- Software
Python 3.8+
Permissions
File Scopes
Runs offline. Reads only the log files named on the command line; never connects to a database and writes nothing.
What's inside
Frequently Asked Questions
New in DevOps & Cloud
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for DevOps & Cloud.