Interactive engineering lab

Row lock order: two batch updates, a deadlock and deadlock_timeout

Row lock order: two batch updates, a deadlock and deadlock_timeout. Replay row locks, waits and deadlock detection, then sort the keys.

By Sandeep Belgavi · Published 2026-11-09

Synthetic fixtures computed locally. No model, database or production service is contacted.

Experiment workspace

Decision trace

    The question this experiment answers

    Dinky runs the nightly stock reconciliation for Belgavi's Book Store while the returns service updates the same order lines. When two or more transactions lock overlapping rows in different orders, who waits, when does PostgreSQL notice the cycle, which transaction is aborted with 40P01, and what does sorting the row ids change? Edit the transactions, the deadlock_timeout, the pause between locks and the retry policy, and replay every lock decision.

    Change one assumption at a time

    Start from a preset and change one control. In opposite_order set Sort row ids to Yes and the deadlock disappears; T2 only waits. Lower deadlock_timeout to 200 and the abort comes 800 ms sooner. Turn on Retry and the aborted transaction runs again and commits. In timer_before_cycle raise deadlock_timeout to 1000 and the other transaction is aborted instead. Transaction lines look like T1 0 1 2: a name, a start time in milliseconds, then row ids in the order they are locked.

    Expected outcomes

    opposite_order: T1 starts waiting at 300 ms, its check at 1,300 ms finds the cycle and T1 is aborted; T2 commits at 1,300 ms. opposite_order_200ms: the abort comes at 500 ms. sorted_keys: no deadlock; T2 waits 150 ms and both commit by 600 ms. retry_after_abort: T1 is aborted once, retries at 1,350 ms and commits at 1,650 ms. three_way_cycle: T1 is aborted at 1,200 ms; T2 and T3 commit. overlap_without_cycle: T1 waits 150 ms, no abort. timer_before_cycle: T1's check at 500 ms finds no cycle yet; T2 closes the cycle and is aborted at 850 ms. four_batches_unsorted: 4 aborts and 12,120 ms of waiting before all commit at 4,970 ms. four_batches_sorted: no aborts, 1,340 ms of waiting, all committed by 900 ms.

    Read the table and the trace

    The metrics count committed transactions, deadlock aborts, transactions that failed without a retry, total waiting time, when the last transaction finished, deadlock checks that found no cycle, the most transactions waiting at once and any left waiting forever. The table lists each transaction with its lock order, outcome, attempts and waiting time. The trace walks through every lock, wait, check, abort, grant and commit with its time in milliseconds.

    Reset and export

    Reset inputs restores the selected preset and recomputes. Export JSON downloads the inputs, every transaction, every trace step, the metrics and the stated limits.

    Limits

    A rule model in whole milliseconds with one lock mode, FIFO queues and one deadlock check per wait, as PostgreSQL documents for deadlock_timeout; no soft-deadlock queue rearrangement, no lock upgrades and no other lock types. The two-session opposite-order and sorted cases were measured on PostgreSQL 18.4 in the companion probe; the larger presets are model results. PostgreSQL does not promise which transaction it aborts.

    Views are my own and do not represent any employer, past or present.