Report
The deadlock detector misses a cycle between a SERIALIZABLE READ ONLY DEFERRABLE safe-snapshot wait and a heavyweight lock waiter blocked by that transaction, so both sessions wait until a timeout or cancel
- Severity
- 25
- Blamed commit
- dafaa3efb75ce1aae2e6dbefaf6f3a889dea0d21
On 17 branches
REL9_1_STABLE, REL9_2_STABLE, REL9_3_STABLE, REL9_4_STABLE, REL9_5_STABLE, REL9_6_STABLE, REL_10_STABLE, REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Reported on pgsql-bugs as BUG #19961 (against 17, 18 and 19).
The deadlock detector does not see a cycle between a SERIALIZABLE READ ONLY DEFERRABLE transaction waiting for a safe snapshot and a heavyweight lock waiter. GetSafeSnapshot() waits on the process latch (wait event IPC / SafeSnapshot) until the concurrent read/write serializable transactions finish. That wait has no lock-table entry, so deadlock.c has no edge from the deferrable transaction to the transaction it waits for. If that transaction is itself waiting for a lock held by the deferrable transaction, both sessions wait indefinitely and no 40P01 is raised.
CREATE TABLE doc (id int); INSERT INTO doc VALUES (1);
-- W: BEGIN ISOLATION LEVEL SERIALIZABLE; SELECT 1;
-- D: BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE;
-- LOCK TABLE doc IN ACCESS SHARE MODE;
-- W: LOCK TABLE doc IN ACCESS EXCLUSIVE MODE; -- waits for D's lock
-- D: SELECT count(*) FROM doc; -- waits for a safe snapshot (W is r/w)
LOCK TABLE does not acquire a snapshot, so D's LOCK returns at once and D blocks at its first query. With deadlock_timeout = 500ms, seven seconds later W is waiting on Lock/relation with pg_blocking_pids() = {D}, and D is waiting on IPC/SafeSnapshot with pg_blocking_pids() empty. The log has "process ... still waiting for AccessExclusiveLock" and no "deadlock detected". The cycle lasts until something outside the detector breaks it. In the test that was W's statement_timeout: W's LOCK was cancelled, and D's query then returned count = 1. With D declared NOT DEFERRABLE, D's query returns at once and W waits only until D commits.
The documentation says a SERIALIZABLE READ ONLY DEFERRABLE transaction "may block when first acquiring its snapshot" (SET TRANSACTION, and the Serializable section of the MVCC chapter). The Deadlocks section says deadlocks are detected and resolved by aborting one transaction. Neither the docs, README-SSI nor the GetSafeSnapshot() comment mentions that this wait is invisible to the deadlock detector.
Blame is dafaa3efb75ce1aae2e6dbefaf6f3a889dea0d21 ("Implement genuine serializable isolation level.", 2011), which added DEFERRABLE and the GetSafeSnapshot() loop around ProcWaitForSignal(). That is from code reading, because trees of that age do not build with current toolchains. The oldest tree built and run, 8097d189ccc2cd55a8bf189bd21cb196e3cfc385 (2017), reproduces with the same wait events. The wait was a semaphore originally, became a latch in 6753333f55e1d9bcb9da4323556b456583624a07, and got the SafeSnapshot wait event in 6f3bd98ebfc008cbd676da777bb0b2376c4c4bfa. None of these changes made it visible to deadlock.c.
No fix is committed as of master ca492a9197a449bca3953f7e287c9aee6d295c6d. The thread has no patch.
Branches: REL_14 through master (reproduced on the master and REL_19 through REL_14 tips).