BUG #19961: Deadlock detector misses a cycle between a READ ONLY DEFERRABLE safe-snapshot wait and a table lock

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: james(dot)moore(at)g5labs(dot)ai
Subject: BUG #19961: Deadlock detector misses a cycle between a READ ONLY DEFERRABLE safe-snapshot wait and a table lock
Date: 2026-11-30 17:33:49
Message-ID: 19961-2cb575b292e9575d@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19961
Logged by: James Moore
Email address: james(dot)moore(at)g5labs(dot)ai
PostgreSQL version: 19.0
Operating system: Debian 13 (official postgres image), x86_64
Description:

A SERIALIZABLE READ ONLY DEFERRABLE transaction waiting for a safe
snapshot can form a cycle with a transaction waiting for a table lock. The
deadlock detector never reports it: both sessions wait until a timeout or
a cancel. No 40P01 is raised.

Reproduced on 17.11, 18.6 and 19.0. Two sessions, W and D, in this order:

setup: CREATE TABLE doc (id int);

W: BEGIN ISOLATION LEVEL SERIALIZABLE;
W: SELECT 1;
D: BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE;
D: 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,
-- i.e. for W to finish

Both directions of the cycle are visible:

pg_blocking_pids(W) = {D} (wait event Lock/relation)
pg_safe_snapshot_blocking_pids(D) = {W} (wait event IPC/SafeSnapshot)

With statement_timeout = 6s on D, D fails with 57014 after 6.5 s and W then
gets its lock. Without a timeout both wait indefinitely.

Expected: the cycle is detected and one transaction gets 40P01, or the
documentation says that a DEFERRABLE wait can deadlock with a lock waiter
without detection.

Cause, as far as we can tell: GetSafeSnapshot() waits on a condition
variable rather than a heavyweight lock, and the deadlock detector only
follows heavyweight-lock edges, so it never sees the D -> W edge. The docs
and README-SSI already say a DEFERRABLE transaction may wait a long time for
a safe snapshot; we found no mention of a deadlock.

The cycle needs D to take its table lock before its first snapshot, as in
the steps above: an explicit LOCK TABLE at the start of a READ ONLY
DEFERRABLE transaction, followed by concurrent DDL or LOCK TABLE on the
same table from a serializable writer.

Found by model-checking PostgreSQL's locking and SSI behaviour against a
formal model.

Browse pgsql-bugs by date

  From Date Subject
Next Message James Moore 2026-11-30 17:37:07 Re: BUG #19362: Extremely log processing of jsonb_path_exists_opr
Previous Message PG Bug reporting form 2026-11-30 17:33:20 BUG #19960: Phrase search matches when it should not: phrase positions wrap at 16383