From d2e55920d341daa2b613ee8f02640a1035ac8a35 Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Thu, 13 May 2021 11:53:29 +1000 Subject: [PATCH v2] Fix deadlock for multiple replicating truncates of same table. The ExecuteTruncate uses AccessExclusiveLock, but the apply_handle_truncate only used RowExclusiveLock. This could lead to a deadlock detected error in scenarios where there are 2 subscribers for a single table which is being truncated. Fix to use the same lock level as ExecuteTruncate. --- src/backend/replication/logical/worker.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c index 1432554..60bf7f7 100644 --- a/src/backend/replication/logical/worker.c +++ b/src/backend/replication/logical/worker.c @@ -1818,6 +1818,7 @@ apply_handle_truncate(StringInfo s) List *relids = NIL; List *relids_logged = NIL; ListCell *lc; + LOCKMODE lockmode = AccessExclusiveLock; if (handle_streamed_transaction(LOGICAL_REP_MSG_TRUNCATE, s)) return; @@ -1831,14 +1832,14 @@ apply_handle_truncate(StringInfo s) LogicalRepRelId relid = lfirst_oid(lc); LogicalRepRelMapEntry *rel; - rel = logicalrep_rel_open(relid, RowExclusiveLock); + rel = logicalrep_rel_open(relid, lockmode); if (!should_apply_changes_for_rel(rel)) { /* * The relation can't become interesting in the middle of the * transaction so it's safe to unlock it. */ - logicalrep_rel_close(rel, RowExclusiveLock); + logicalrep_rel_close(rel, lockmode); continue; } @@ -1856,7 +1857,7 @@ apply_handle_truncate(StringInfo s) { ListCell *child; List *children = find_all_inheritors(rel->localreloid, - RowExclusiveLock, + lockmode, NULL); foreach(child, children) @@ -1876,7 +1877,7 @@ apply_handle_truncate(StringInfo s) */ if (RELATION_IS_OTHER_TEMP(childrel)) { - table_close(childrel, RowExclusiveLock); + table_close(childrel, lockmode); continue; } -- 1.8.3.1