Database Deadlock & Concurrency Race Condition Investigator
Diagnose Postgres/MySQL transaction deadlocks, lock contention, and concurrent update race conditions.
Interactive Prompt Playground
{{DEADLOCK_LOG}}{{QUERIES}}{{DB_ENGINE}}{{ISOLATION_LEVEL}}You are a Principal Database Administrator and Concurrency Specialist. Analyze the following database deadlock log and related application transactions: Deadlock Graph / Error Output: ```sql ERROR: deadlock detected Detail: Process 14201 waits for ShareLock on transaction 889102; blocked by process 14205. Process 14205 waits for ExclusiveLock on tuple (42, 8) of relation "inventory"; blocked by process 14201. Hint: See server log for query details. ``` Concurrent SQL Queries / Application Code: ```sql -- Tx A: UPDATE orders SET status = 'PROCESSING' WHERE id = 101; UPDATE inventory SET stock = stock - 1 WHERE product_id = 505; -- Tx B: UPDATE inventory SET stock = stock + 10 WHERE product_id = 505; UPDATE orders SET updated_at = NOW() WHERE id = 101; ``` Database Engine: PostgreSQL 16 Isolation Level: Read Committed Investigate and deliver: 1. **Deadlock Sequence Diagram / Lock Graph**: - Reconstruct step-by-step how Transaction A and Transaction B acquired and requested competing locks (RowExclusive, ShareLock, Next-Key Lock, etc.). - Identify the exact resource ordering discrepancy. 2. **Root Cause Analysis**: - Why did the transactions access tables or rows in differing orders? - Did foreign key checks or cascade operations introduce implicit locks? 3. **Architectural Solutions**: - Solution 1: Strict consistent locking order in application code. - Solution 2: Optimistic locking (`version` column) or `SELECT ... FOR UPDATE SKIP LOCKED`. - Solution 3: Query restructuring or batching improvements. 4. **Corrected SQL / Code Implementation**: - Provide the refactored transaction logic.
How to Use This Prompt
- Paste your database error log or deadlock graph.
- Provide the two concurrent application routines or SQL transaction blocks.
- Receive deterministic locking orders and concurrency fixes.
Engineering Tips & Best Practices
- Always sort multi-item batch update IDs before executing queries to prevent cyclic deadlocks.
What This Prompt Inspects
Key failure modes, design principles, and quality standards evaluated during execution.
Lock Acquisition Order
Ensures all concurrent transactions acquire row locks in deterministic ascending primary key order.
Lock Types & Scope
Differentiates row-level vs page-level vs index-range locks.
Zero-Downtime Fixes
Suggests non-blocking transaction designs such as SKIP LOCKED or background workers.
SprintKit Workflow Integrations
Complementary interactive tools and workflows across SprintKit to accelerate your engineering process.
Related Prompts
Explore related developer prompts in this workflow domain.
Database Schema & Relational Indexing Strategy
Design production PostgreSQL schemas with composite indexes, foreign key strategies, and partitioning.
Production Exception & Stack Trace Root-Cause Analyzer
Analyze production stack traces, error logs, and surrounding code using hypothesis-driven debugging.
API Performance Regression & N+1 Query Debugger
Identify slow database queries, serialization bottlenecks, and memory churn in sluggish API endpoints.