All Articles
Database & Concurrency·November 2026·5 min read

Preventing Overselling and Deadlocks Under Concurrent Checkouts in PostgreSQL

Why reading stock in application code before updating inventory breaks under simultaneous requests—and how row-level lock ordering keeps checkout race-free.

PostgreSQLTransactionsConcurrencyE-Commerce

Why Naive Read-Then-Write Stock Checks Fail

In a quiet development environment, checking whether product stock is available feels straightforward: query the current stock count, verify that stock >= requestedQty, and decrement the row. Under real traffic, however, two buyers clicking Checkout on the last unit in stock at nearly the same millisecond will both read stock = 1 before either transaction writes stock = 0.

Without database-level coordination, both checkouts succeed, stock drops to -1 (or silently overwrites to 0), and you have promised the same physical item to two paying customers.

Locking Rows Explicitly Inside a Transaction

PostgreSQL's default isolation level (Read Committed) does not prevent two transactions from reading the same row before updating it unless you acquire an explicit row lock with SELECT ... FOR UPDATE, or perform a guarded atomic UPDATE ... WHERE available_qty >= requested_qty.

When checking out a multi-item cart, locking the target inventory variant rows inside a single ACID transaction ensures the second buyer waits cleanly until the first transaction commits or rolls back.

Acquiring row locks in deterministic ascending ID ordersql
BEGIN;

-- Always lock variant rows in ascending ID order across all transactions
SELECT variant_id, on_hand - reserved AS available
FROM inventory_levels
WHERE variant_id = ANY($1::uuid[])
ORDER BY variant_id ASC
FOR UPDATE;

-- Proceed only if every requested variant has sufficient available units
UPDATE inventory_levels
SET reserved = reserved + req.qty
FROM unnest($1::uuid[], $2::int[]) AS req(id, qty)
WHERE inventory_levels.variant_id = req.id;

COMMIT;

The Hidden Trap: AB-BA Deadlocks on Multi-Item Carts

Row locks solve overselling, but they introduce a subtle new failure mode when carts contain multiple items. Imagine Buyer A has [Shirt, Shoes] in their cart while Buyer B has [Shoes, Shirt]. If Transaction A locks Shirt first and waits for Shoes while Transaction B locks Shoes first and waits for Shirt, PostgreSQL detects a cycle and aborts one checkout with a deadlock error.

The fix is canonical lock ordering: sort variant IDs in ascending order before acquiring FOR UPDATE locks, and always lock parent resources in the same sequence (cart -> sorted variants -> order). Once every transaction acquires locks in the exact same order, circular waits become mathematically impossible.

Key Takeaway

Concurrency bugs rarely show up in manual browser clicks. Enforcing canonical lock ordering in SQL and testing simultaneous checkouts with automated integration suites catches race conditions before real users do.

Written by Prethu Raj Debnath

Full-Stack & Backend Developer · Questions or feedback? Reach out anytime.