Lock compatibility
Overview and setup
Compatibility determines whether two transactions can hold particular lock modes on the same resource concurrently. In PostgreSQL, ACCESS SHARE conflicts with ACCESS EXCLUSIVE only; ROW EXCLUSIVE is compatible with itself but conflicts with SHARE. For row locks on one row, FOR KEY SHARE and FOR NO KEY UPDATE are compatible; FOR UPDATE conflicts with all four row lock modes. A lock can conflict with itself across different transactions.
Use a fresh disposable file database initialized with versions/v56/locking-fixture.sql. Enable WAL with PRAGMA journal_mode=WAL; before opening two independent private-cache connections A and B. Use driver autocommit, no implicit transaction, zero busy timeout, and fully fetch each query result. Follow steps in order; the included Python validator automates this. Never use production data.
First two-session example
Run each numbered statement in the indicated connection; do not paste the whole schedule into one session.
1. Session A
BEGIN;2. Session B
BEGIN;3. Session A
SELECT qty,version FROM lock_item WHERE id=1;Observed rows: [[10, 1]]
4. Session B
SELECT qty,version FROM lock_item WHERE id=1;Observed rows: [[10, 1]]
5. Session A
COMMIT;6. Session B
COMMIT;How it works
In SQLite WAL two read transactions coexist. Two BEGIN IMMEDIATE requests cannot both reserve the write transaction. With timeout zero the second returns SQLITE_BUSY rather than waiting; after release it succeeds. These are observed SQLite behaviors, not a PostgreSQL compatibility matrix. In PostgreSQL evaluate locks on the same resource, including locks acquired implicitly by the statement.
Second two-session example
Run each numbered statement in the indicated connection; do not paste the whole schedule into one session.
1. Session A
BEGIN IMMEDIATE;2. Session B
BEGIN IMMEDIATE;Observed error: SQLITE_BUSY
3. Session A
ROLLBACK;4. Session B
BEGIN IMMEDIATE;5. Session B
ROLLBACK;Practice and mistakes
Practice: predict whether ACCESS SHARE plus ROW EXCLUSIVE, and SHARE plus ROW EXCLUSIVE, can coexist on one PostgreSQL table. Hint 1: ordinary reads use ACCESS SHARE. Hint 2: consult the table-level conflict matrix in the official docs. Mistake: mixing row and table modes in one matrix.