Two Shoppers, One Item Left. What Does Postgres Do?
Two shoppers try to buy the last mug. Run two Postgres sessions side by side to see how one conditional update prevents an oversale, even when a checkout rolls back.

There’s one mug left in the Black Friday sale. Two shoppers click Buy. Both checkouts read a stock count of one. By the time the warehouse opens, the database shows zero mugs on the shelf and two orders to pack.
You can make that happen on a laptop. You can also fix it, leave one checkout halfway through, and watch the other one wait. I ran the examples below in two local psql sessions on PostgreSQL 19.0 at its default isolation level, read committed. The terminal output comes from those runs.
Open a terminal for each shopper
Use a fresh local database so you can reset the stock between checkouts. With the Layerbase CLI, run this in terminal A. If you need the CLI first, install it with pnpm add -g layerbase.
lbase create last-mug --engine postgresql --start
lbase connect last-mugIn terminal B, run lbase connect last-mug as well. Each window now has its own database session. In A, create the two tables and put one mug on the shelf.
CREATE TABLE inventory (
sku text PRIMARY KEY,
stock integer NOT NULL CHECK (stock >= 0)
);
CREATE TABLE orders (
buyer text PRIMARY KEY,
sku text NOT NULL REFERENCES inventory
);
INSERT INTO inventory VALUES ('mug', 1);Sell one mug twice
Run these two lines in A, then in B. Leave both transactions open.
BEGIN;
SELECT stock FROM inventory WHERE sku = 'mug';Both terminals print the same answer.
stock
-------
1
(1 row)Suppose the checkout code subtracts one from the value it just read and sends the result back to Postgres. Both shoppers’ apps calculate zero. Run this in A, then repeat it in B with 'B' as the buyer.
UPDATE inventory SET stock = 0 WHERE sku = 'mug';
INSERT INTO orders VALUES ('A', 'mug');
COMMIT;Count the orders alongside the remaining stock.
SELECT stock, (SELECT count(*) FROM orders WHERE sku = 'mug') AS orders
FROM inventory WHERE sku = 'mug'; stock | orders
-------+--------
0 | 2
(1 row)The stock constraint passed both times. Zero is a perfectly legal value, and the second checkout asked Postgres to write zero to a row that already held zero. The mistake was treating an earlier read as permission to place an order.
Make the update claim the mug
Reset the example in A. Use this same reset between the next two runs, after both transactions have ended.
TRUNCATE orders;
UPDATE inventory SET stock = 1 WHERE sku = 'mug';This time, ask Postgres to decrement the current stock only while there’s a mug left. In terminal A, run the following and leave the transaction open.
BEGIN;
UPDATE inventory SET stock = stock - 1
WHERE sku = 'mug' AND stock > 0
RETURNING stock; stock
-------
0
(1 row)
UPDATE 1That zero is good news for A. One row came back, and zero is the stock after A claimed the last mug. In A, run INSERT INTO orders VALUES ('A', 'mug');. Leave the transaction open so the order and the stock change still share the same fate.
In terminal B, run the same BEGIN; and conditional UPDATE ... RETURNING stock; shown above. B’s prompt stays busy. It has reached the inventory row and is waiting for A. Leave B alone and switch back to A.
First run: A commits
In A, type COMMIT;. Here’s what the two windows printed.
| Terminal A | Terminal B |
|---|---|
COMMIT; returns COMMIT | The waiting update finishes and prints the result below. |
stock
-------
(0 rows)
UPDATE 0B got zero rows. At Read Committed, Postgres checks the updated row against B’s WHERE clause after A commits. The stock is now zero, so B has nothing to claim. The checkout should report sold out. In B, run ROLLBACK; to end its transaction, then SELECT buyer, sku FROM orders;.
buyer | sku
-------+-----
A | mug
(1 row)Second run: A rolls back
In A, run the reset, then repeat A’s BEGIN, conditional update, and order insert. In B, repeat BEGIN and the same conditional update. Once B is waiting again, change just one line in terminal A.
| Terminal A | Terminal B |
|---|---|
ROLLBACK; returns ROLLBACK | The waiting update finishes and prints the result below. |
stock
-------
0
(1 row)
UPDATE 1Rolling back put A’s mug back on the shelf and discarded A’s order. B could now decrement the stock itself. In B, finish the order and check who gets the mug.
INSERT INTO orders VALUES ('B', 'mug');
COMMIT;
SELECT buyer, sku FROM orders;INSERT 0 1
COMMIT
buyer | sku
-------+-----
B | mug
(1 row)What to put in your checkout handler
Check the number of rows returned by the conditional update. With one row, insert the order using the same connection and transaction, then commit. With zero rows, end the transaction and tell the shopper the item sold out. Testing whether the returned stock value is truthy would turn away the customer who successfully claimed the last mug.
Keep the transaction short. We held A open so you could see the wait. In a real checkout, commit the reservation before calling a payment provider and decide when an unpaid reservation should expire. The concurrency behavior above is specific to Read Committed. PostgreSQL’s transaction isolation guide explains how the other levels handle competing updates.
When you’re finished, type \q in both windows and run lbase stop last-mug from your shell. The database stays on disk if you want to try the race again.
Keep reading
- Why Is Everyone Suddenly Into DuckDB?MariaDB ships a DuckDB engine, MySQL has one in the works, and AWS bought DuckLabs. One join across Postgres and MariaDB shows the appeal, and on Layerbase it runs without a connection string.

- Boxd vs Layerbase Agent VMs: Can I close my laptop yet?Where should your coding agent run after you close your laptop? Compare a flat monthly workspace with a machine you can fork mid-task.
- Chroma Cloud goes read-only December 7: export and move to pgvector or QdrantChroma Cloud stops accepting writes on December 7, 2026 and stops serving reads on December 22. Here are tested scripts to export your vectors and load them into pgvector or Qdrant, and how to tell whether you need to re-embed.
- Cloud Hypervisor vs Firecracker: what we measured for databases and Next.jsWe tested Cloud Hypervisor and Firecracker side by side on lab hardware for two workload types, databases and Next.js apps, with every microVM restored from a snapshot. Here is what each one cost in wake time, tail latency, CPU and memory, and the two gotchas that bit us.
