Two Postgres deadlocks, one rule: lock the region first A developer building Last Thaw, a free browser strategy game with up to 100 players across 408 map regions, fixed two Postgres deadlocks caused by inconsistent row-lock ordering in its region-settlement transactions. The root cause was a training-order path that locked its own region first before locking other regions, conflicting with homecoming-army and loyalty passes that lock regions in ascending id order; the fix makes any order that may draw resources from other regions lock all of the player's regions up front in ascending order. The deadlocks appeared 14 times in one night's match and were only caught by reading the Postgres log, since the worker's retry masked the failures. I make Last Thaw https://lastthaw.com , a free browser strategy game. Up to a hundred players start with five settlers on a real map of Earth, cut into 408 regions, and have 30 real days to build an empire while an ice age advances from both poles. It's in early access and I'm looking for people to test it. Nothing in the game waits for a player to be online. Builds finish, convoys arrive and armies come home on timers, and a worker process settles each of those in its own Postgres transaction. Players and AI tribes send commands through the API at the same time. So at any moment there are several transactions that all want the same handful of rows. Twice in the past month, two of them wanted the same rows in opposite orders. Postgres did what it does: it picked one, killed it and wrote deadlock detected in its log. Both times the fix was a few lines. The lesson was that a lock order is only a rule if every code path follows it, including the ones you wrote before you had the rule. A region's stores wood, stone, food and so on aren't updated every second. The row holds what was there at settledAt , and anything that touches the region first brings it up to date. That read-then-write has to be atomic, so every path goes through one function: export async function settleAndLockRegion tx: Tx, matchId: string, regionId: string, asOf: Date, speed: number, resourceMult = 1 { const rows = await tx .select .from matchRegions .where and eq matchRegions.matchId, matchId , eq matchRegions.regionId, regionId .for "update" ; // row lock, held until the transaction ends const row = rows 0 ; if row throw Object.assign new Error "region not found" , { statusCode: 404 } ; if asOf.getTime <= row.settledAt.getTime return row; // already up to date // ...project production forward to asOf and write it back } SELECT ... FOR UPDATE is the whole concurrency story for a single region. Two transactions that want the same region line up, and the second one sees what the first one wrote. I wrote about the settle-on-read idea in an earlier post in this series https://dev.to/canad1an/a-strategy-game-that-keeps-running-while-nobody-is-online-12ln . The trouble starts when one transaction needs two regions. Training soldiers costs materials. If the region doesn't have enough, the order can be supplied from your other regions, the same way a build can. So the first version of queueTrain did the natural thing: lock the region the order is for, check the stores, and only if they fall short, lock the rest of your regions to see what they can send. Now picture a transport coming home at the same moment. Its transaction locks regions too, and so does the pass that works out loyalty in occupied regions. Those take their regions in ascending id order. The training order took its own region first , wherever that fell in the order, and the others after. training order: locks region 212 ... then wants region 087 homecoming army: locks region 087 ... then wants region 212 Neither can finish. Postgres notices after its deadlock timeout and aborts one of them. This showed up 14 times in one night's match. Nobody lost anything, because the worker retries a failed job and the retry went through. I only knew because I go through the Postgres log every night. If I hadn't been reading that log I'd have shipped it for months. The fix is to decide up front whether the order might need other regions, and if so take all of them in the one order everything else uses: // An order that may be supplied from other regions locks every region of mine up front, // in ascending order like a build. const canWait = opts.allowWait ?? true; const mine = canWait ? await tx .select { regionId: matchRegions.regionId } .from matchRegions .where and eq matchRegions.matchId, m.id , eq matchRegions.ownerPlayerId, p.id , isNull matchRegions.frozenAt .orderBy asc matchRegions.regionId // the same order every other transaction uses : ; const locked = new Map