| From: | Dipak Dudhabhate <dipak(dot)dudhabhate(at)enterprisedb(dot)com> |
|---|---|
| To: | pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Subject: | Undetected deadlock in logical decoding of composite-type columns with synchronous replication |
| Date: | 2026-12-01 11:39:26 |
| Message-ID: | CABMcV8oa8yT=rTzDdpG2+Ti8fD=jmbEZ1NdVmDhLSaiN69Wj1w@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
I ran into an undetected deadlock between a committing backend and the
walsender decoding its transaction, when the transaction writes a row
containing a composite type whose relation it holds AccessExclusiveLock
on, and synchronous replication is waiting for that walsender.
Repro (two clusters, publisher and subscriber, no extensions):
-- publisher: wal_level = logical
CREATE PUBLICATION p FOR ALL TABLES;
-- subscriber
CREATE TYPE ct AS (a int, b text);
CREATE TABLE t (id int PRIMARY KEY, v ct);
CREATE SUBSCRIPTION sub1
CONNECTION 'port=5491 dbname=postgres application_name=sub1'
PUBLICATION p WITH (streaming = off);
-- publisher
ALTER SYSTEM SET synchronous_standby_names = 'sub1';
SELECT pg_reload_conf();
BEGIN;
CREATE TYPE ct AS (a int, b text);
CREATE TABLE t (id int PRIMARY KEY, v ct);
INSERT INTO t VALUES (1, ROW(1, 'x'));
COMMIT; -- never returns
On the publisher:
backend_type | wait_event | relation | mode | granted
---------------+------------+----------+---------------------+--------
client backend | SyncRep | ct | AccessExclusiveLock | t
walsender | relation | ct | AccessShareLock | f
LOG: process NNN still waiting for AccessShareLock on relation NNNNN
of database NNNNN after 1000.000 ms
The subscriber receives nothing, so it never confirms, and nothing
times out: the subscriber's wal_receiver_timeout eventually drops the
connection, but the stuck walsender keeps the slot active, so every
reconnect fails with 'replication slot "sub1" is active for PID NNN'.
The same thing happens without the type being created in the same
transaction, e.g. an existing table used as a row type that the
committing transaction ALTERs:
BEGIN;
ALTER TABLE rt ALTER COLUMN b SET DEFAULT 'y';
INSERT INTO t2 VALUES (1, ROW(1, 'x')::rt); -- t2.v is of type rt
COMMIT;
and, as a stall rather than a deadlock, when a different, still-open
transaction holds AccessExclusiveLock on the type's relation: the
walsender then blocks every synchronous commit behind it until that
transaction ends.
Streaming of in-progress transactions is not involved (the repro uses
streaming = off, and pg_stat_replication_slots shows stream_txns = 0).
Analysis:
CommitTransaction() flushes the commit record and waits in
SyncRepWaitForLSN() before releasing its locks. The walsender decodes
the transaction from that commit record, and pgoutput formats the
composite column with record_out() -> lookup_rowtype_tupdesc() ->
load_typcache_tupdesc(), which does
rel = relation_open(typentry->typrelid, AccessShareLock);
That waits behind the committing transaction's AccessExclusiveLock,
while the committing transaction waits for the walsender: the backend
sleeping in SyncRepWaitForLSN() can only be released by a walsender
calling SyncRepReleaseWaiters() when the standby acknowledges the commit
LSN, and the blocked walsender neither sends the transaction nor reads
the standby's replies. The deadlock detector only sees the walsender's
lock wait; the other edge is a latch wait in SyncRepWaitForLSN(), so no
cycle is found.
This is similar to the documented caveat in "Synchronous Replication
Support for Logical Decoding" about exclusively locking [user] catalog
tables, but here the table is an ordinary user table, and the lock can
be taken implicitly by CREATE TYPE / CREATE TABLE / ALTER TABLE.
load_typcache_tupdesc() has taken this lock since 2004 (375369acd1c),
so as far as I can tell all supported branches are affected. I
reproduced it on master and REL_19_STABLE.
Proposed fix:
While decoding (HistoricSnapshotActive()), open the type's relation
without a lock, using RelationIdGetRelation()/RelationClose(). Decoding
reads the catalogs through the historic snapshot, which concurrent DDL
cannot change, and ReorderBufferProcessTXN() already opens the decoded
relations the same way, without locking them. All other callers still
take AccessShareLock. With the attached patch the repro above commits
immediately.
The attached patch also adds a TAP test in src/test/subscription.
For background: we first hit this in an extension that waits for its
peers to acknowledge DDL in the middle of a transaction, which closes
the same cycle without synchronous replication.
Thoughts? In particular, I'd be glad to hear whether skipping the lock
under a historic snapshot is acceptable here, or whether it would be
better handled elsewhere.
Regards,
Dipak Dudhabhate
EDB
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Don-t-lock-a-composite-type-s-relation-in-typcach.patch | text/x-patch | 7.3 KB |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | vignesh C | 2026-12-01 11:03:53 | Re: Preserve conflict log table during upgrade |