Optimistic vs Pessimistic Locking
The problem: edits across requests
A user opens an edit form (reads version 1), takes 2 minutes, then saves. Meanwhile someone else saved a change. A plain UPDATE … SET title = … overwrites their change with no warning (a lost update across HTTP requests).
You can't hold a database lock for 2 minutes while someone types — so we need another approach.
Pessimistic locking
"Assume conflicts will happen — lock first."
SELECT … FOR UPDATEinside a short transaction- ✅ Guarantees no conflict
- ❌ Others wait; locks can't span user think-time; risk of deadlocks
- Best for: short, high-contention operations (balances, stock, seat booking)
Optimistic locking ⭐
"Assume conflicts are rare — detect them when saving."
- Add a
version INT NOT NULL DEFAULT 1column (or useupdated_at) - When reading, the app remembers the version
- When saving:
UPDATE expenses
SET title = 'New', version = version + 1
WHERE id = 5 AND version = 3; -- the version I read
- 0 rows updated → someone else changed it → show "This expense was changed by someone else. Reload?"
- ✅ No locks held, scales well, works across HTTP requests and offline sync
- ❌ The app must handle conflicts (retry / merge / ask the user)
- Best for: forms, documents, settings, mobile sync — low-contention data
Comparison
| Pessimistic | Optimistic | |
|---|---|---|
| When it checks | before changing (lock) | at save time (version) |
| Waiting | yes | no |
| Works across requests | ❌ | ✅ |
| Conflict handling | prevents | detects, app reacts |
Automating the version bump
A BEFORE UPDATE trigger can increment version and set updated_at automatically, so no update can forget it.
Example
-- Add a version column for optimistic locking
ALTER TABLE expenses ADD COLUMN version INT NOT NULL DEFAULT 1;
-- Tab 1 and Tab 2 both open expense #2 → both see version 1
SELECT id, title, amount, version FROM expenses WHERE id = 2;
-- 2 | Dinner at Thamel | 1800.00 | 1
-- Tab 1 saves first: matches version 1 → succeeds, version becomes 2
UPDATE expenses
SET amount = 2000, version = version + 1
WHERE id = 2 AND version = 1
RETURNING id, amount, version; -- 1 row: 2000.00, version 2 ✅
-- Tab 2 saves its stale edit: still thinks version = 1 → 0 rows updated
UPDATE expenses
SET title = 'Birthday dinner', version = version + 1
WHERE id = 2 AND version = 1
RETURNING id; -- 0 rows ⚠️ conflict detected
-- App: "This expense was changed elsewhere. Reload to see the latest version."
-- A function that reports conflicts clearly (callable from the app/Supabase RPC)
CREATE FUNCTION update_expense_title(p_id BIGINT, p_title TEXT, p_version INT)
RETURNS TEXT LANGUAGE plpgsql AS $$
BEGIN
UPDATE expenses SET title = p_title, version = version + 1
WHERE id = p_id AND version = p_version;
IF NOT FOUND THEN
RETURN 'conflict';
END IF;
RETURN 'saved';
END $$;
SELECT update_expense_title(2, 'Birthday dinner', 1); -- conflict
SELECT update_expense_title(2, 'Birthday dinner', 2); -- saved
SELECT title, amount, version FROM expenses WHERE id = 2;
-- Birthday dinner | 2000.00 | 3 (both changes kept, nothing lost)