Nov 23, 2026
Two people edited the same record — the last save silently won
Two editors read the same row, both save, and the second save silently erases the first. A version column in the UPDATE's WHERE clause is the fix.
Two people open the same product record. One fixes the price. The other fixes the description. Both hit save. Both get a success message.
The price fix is gone.
Nobody deleted it. Nobody saw an error. The second save simply wrote back the whole row as it looked when the second person loaded it — including the old price — and overwrote the first person's change. This is a lost update, and it is the quietest bug a dashboard can have, because every individual request did exactly what it was asked to do.
Why a transaction does not save you
The instinct is to wrap the save in a transaction. It does not help, because the read and the write never share one.
The edit page runs a SELECT in one short transaction. Minutes later — after a human has read the form, thought, and typed — the save handler runs an UPDATE in a different transaction. To PostgreSQL, two sequential transactions each updated a row. That is not an anomaly. Raising the isolation level changes how overlapping transactions behave, and these two never overlapped.
The gap is the problem, not the query. Any form that reads a row, hands it to a human, and writes it back later has a window where someone else can change the same row.
The fix: make the write check what it read
Add an integer version column to the row, starting at 1, and make every update conditional on the version the writer originally read:
ALTER TABLE products ADD COLUMN version integer NOT NULL DEFAULT 1;
UPDATE products
SET price = $1, version = version + 1
WHERE id = $2 AND version = $3;
The database evaluates the predicate and the increment atomically, so two writers cannot both match the same version. Then check how many rows changed:
- 1 row — the write succeeded, and the version is now one higher.
- 0 rows — someone changed the row since you read it. The write was rejected, not applied.
A count of zero is ambiguous on its own — the row could also have been deleted — so a follow-up SELECT version FROM products WHERE id = $1 tells you which one it was.
In an Express endpoint, that looks like this:
// Express + pg — personal project shape
app.put("/api/products/:id", async (req, res) => {
const { price, version } = req.body; // version: the one the form loaded
const result = await pool.query(
`UPDATE products
SET price = $1, version = version + 1
WHERE id = $2 AND version = $3`,
[price, req.params.id, version],
);
if (result.rowCount === 0) {
return res.status(409).json({
error: "conflict",
message: "This record changed since you opened it. Reload and try again.",
});
}
res.json({ ok: true, version: version + 1 });
});
The check is the feature. If you run the UPDATE and never look at rowCount, you have the same lost update with extra columns.
A conflict is a UX decision, not a retry
When the write is rejected, the honest answer is to tell the user. Show them the record changed since they opened it, let them reload and see the current values, and let them re-apply their edit deliberately. Silently retrying the write with the new version just reproduces the original bug one step later — their edit would overwrite the other person's change instead.
HTTP already has vocabulary for this. Send the version back as an ETag when the client reads the record, and require an If-Match header on the write. A mismatch is a 412 Precondition Failed instead of a silent overwrite. Same mechanism, standard shape.
The other two shapes
Pessimistic locking takes the row lock before reading: SELECT ... FOR UPDATE inside a transaction, and the second writer waits until the first commits. It works when the read and the write live in the same short transaction — a checkout decrementing stock, for example. It cannot span a form a human is staring at for ten minutes. A lock held across HTTP requests is a lock held forever.
A single atomic statement removes the gap entirely when the change does not depend on what the user read:
UPDATE products
SET stock = stock - 1
WHERE id = $1 AND stock > 0;
No read, no stale value, no version column needed. If your write can be expressed as a change relative to the current row instead of a replacement of it, do that first. The version column is for the cases where a human read the row and decided what it should become.
What you give up
A version column only works if every write path uses it. One endpoint that updates the row without the version check makes the check meaningless for every other writer. That includes the migration script, the admin bulk edit, and the cron job that "just fixes the price."
It also costs a retry under real contention. If ten writers hammer the same row, nine of them lose and have to re-read and decide again. On a hot row, a lock is cheaper than nine wasted writes. For the usual dashboard case — two people editing the same record a few times a day — conflicts are rare and the retry cost is nothing.
And do not use updated_at as the version. Two writes in the same millisecond share a timestamp, and clock skew makes the comparison meaningless. An integer that only ever goes up is the whole point.
The version column is ten lines of schema and one predicate in the WHERE clause. The alternative is a support ticket that says "my edit disappeared" with no error in any log, because nothing failed. The database did exactly what it was told. It just was never told what the writer had actually read.