Multi-session concurrency
Overview
Concurrency tests require independent connections and a controlled schedule. A single connection cannot prove isolation between sessions. Test intermediate observations, conflict errors and the final committed state.
First example
SELECT id,balance,version FROM pt_account ORDER BY id;Observed SQLite result (rows in JSON):
[ [ 1, 100, 0 ], [ 2, 200, 0 ]]How it works
In the host lab, two connections share a temporary WAL-mode database. A reader keeps its snapshot while a writer commits; after the reader ends its transaction it sees the newer version. A second writer with zero timeout encounters a busy error while the first writer holds the lock. PostgreSQL has different locking and isolation behavior; test there independently. The SQL examples below test invariants only, not concurrency.
Practical example
BEGIN;UPDATE pt_account SET balance=balance-10 WHERE id=1;UPDATE pt_account SET balance=balance+10 WHERE id=2;SELECT SUM(balance) FROM pt_account;ROLLBACK;SELECT SUM(balance) FROM pt_account;Observed SQLite result (rows in JSON):
[ [ 300 ]]Practice and mistakes
Practice: run host-lab.py and change the schedule so the reader starts after the commit. Hint 1: explicitly begin and read to establish the snapshot. Hint 2: use separate connections and verify both observations. Mistake: relying on arbitrary sleeps to coordinate tests.
PostgreSQL 17 example — NOT RUN
SHOW transaction_isolation;Expected result, not executed: "Session setting only; not a concurrency test."