Deadlock Log Analysis: Reconstructing Lock Order with a Large Language Model
Deadlock logs are hard to read because events are interleaved in time, wait relationships stay implicit, and business context is missing. This article explains a three-step workflow that uses a large language model to structure threads and locks, reconstruct the wait cycle, and point to opposing lock orders—while leaving the final conclusion to code and database verification.
1. Why deadlock logs feel chaotic
Deadlock logs are rarely unreadable because they contain too little information. The problem is that the way the information is organized does not match how people reason about it. The friction usually falls into three categories.
1. Threads and locks are interleaved on one timeline
Logs are appended in time order. Thread A holds lock 1 and waits for lock 2, while thread B holds lock 2 and waits for lock 1, and the two kinds of events appear mixed together. A reader has to mentally regroup the timeline into one line per thread before the cycle becomes visible. That cognitive load is high.
2. Wait relationships are implicit
“Who is waiting for whom” and “who holds what” appear as natural-language descriptions. The reader has to build the graph. Once there are dozens of threads and lock objects span multiple transactions, a hand-built graph is almost certain to be wrong.
3. Business context is missing
Logs usually record lock and wait events, not the business action behind them. Even when two locks are clearly taken in opposite order, it is still unclear which transaction, SQL statement, or business branch each one belongs to, so the root cause cannot be pinned down.
The conclusion is straightforward: deadlock analysis is not mainly about reading text. It is about reconstructing who holds what, what they are waiting for, and where the cycle is. Once that order is restored, the cause tends to surface on its own.
2. Three steps to reconstruct lock order
Step 1: Build a thread–lock table before drawing conclusions
Structure the log entry by entry: one row per thread, with lock objects in their own columns. Useful fields include:
| Thread | Locks held | Lock waited for | Wait time | Transaction / statement |
|---|---|---|---|---|
| T-101 | L1 | L2 | 3.2s | tx_88 / UPDATE A |
| T-102 | L2 | L1 | 3.2s | tx_91 / UPDATE B |
The instruction given to the model should constrain the output shape and explicitly forbid early attribution:
Split the following deadlock log into a table by thread. For each thread, list the locks it holds, the lock it is waiting for, the wait duration, and the related transaction or statement. Only structure the log. Do not judge the cause.
This step turns unstructured text into an intermediate artifact that can be checked. Once the table exists, a person only needs to spot-check a few rows to judge whether the extraction is reliable.
Step 2: Reconstruct the wait cycle and mark the crossing
Connect threads by “who waits for whom” into a directed graph and look for a cycle:
T-101 --waits for--> L2 (held by T-102)T-102 --waits for--> L1 (held by T-101)=> cycle: T-101 -> T-102 -> T-101
Every lock on the cycle should be labeled with the corresponding business operation, and the output should state which two paths take locks in opposite order. A prompt can be written as:
Using the thread table, reconstruct the wait cycle. Label each lock on the cycle and the operation it corresponds to. Point out which two paths take locks in opposite order, and list the evidence line numbers.
Requiring evidence line numbers is an effective way to limit hallucination: every conclusion must be traceable back to the original log.
Step 3: Confirm against code and the database
The model’s output is a candidate cycle, not a final conclusion. Three checks are required:
- Code: Locate the lock statements on both paths and confirm that the order really is reversed, rather than an extraction error.
- Database: Confirm the lock types (row lock, gap lock, table lock) and whether the expected indexes were used. A missing index can greatly widen the locked range.
- Transaction boundaries: Confirm whether both locks are held inside the same transaction. A long transaction enlarges the conflict window.
A simplified example of the wrong order:
// Path 1tx1.lock(A); tx1.lock(B);// Path 2tx2.lock(B); tx2.lock(A); // opposite order; can deadlock
The fix is to use one global lock order, or to sort resource IDs and lock them in that sequence:
ids = sort([A, B]); for id in ids: lock(id);
3. From a one-off investigation to lasting control
- One lock order: Make “lock resources in ascending ID order” a coding rule so cycles are removed at the source.
- Lock-wait timeouts and limited retries: Make a deadlock a recoverable error instead of a permanently blocked thread.
- Shorter transactions: Move non-database work out of the transaction so locks are held for less time.
- Better observability: Collect deadlock events through logging and monitoring alerts, and keep the lock object, transaction ID, and statement fingerprint so the next analysis has evidence.
- Index hygiene: Avoid range scans that widen the locked range.
4. Where the method stops
A large language model is good at structuring logs and labeling candidate cycles. It is not a substitute for judging business meaning. A reliable flow remains: structure the log → reconstruct the wait cycle → confirm in code and the database → fix and verify with regression. Keep the model in the role of organizing and suggesting, and leave the final call to a person. That is how the work gets faster without becoming less accurate.
