diff --git a/src/backend/distributed/executor/multi_router_executor.c b/src/backend/distributed/executor/multi_router_executor.c index 5e5138f6c..9d7e47446 100644 --- a/src/backend/distributed/executor/multi_router_executor.c +++ b/src/backend/distributed/executor/multi_router_executor.c @@ -298,6 +298,11 @@ AcquireExecutorMultiShardLocks(List *taskList) lockMode = ExclusiveLock; } + /* + * If we are dealing with a partition we are also taking locks on parent table + * to prevent deadlocks on concurrent operations on a partition and its parent. + */ + LockParentShardResourceIfPartition(task->anchorShardId, lockMode); LockShardResource(task->anchorShardId, lockMode); /* diff --git a/src/backend/distributed/utils/resource_lock.c b/src/backend/distributed/utils/resource_lock.c index 0a085fd74..9e08d95a1 100644 --- a/src/backend/distributed/utils/resource_lock.c +++ b/src/backend/distributed/utils/resource_lock.c @@ -321,9 +321,9 @@ LockRelationShardResources(List *relationShardList, LOCKMODE lockMode) /* - * LockParentShardResourceIfPartition checks whether given shard belongs to a - * partition. If it is, LockParentShardResourceIfPartition acquires a shard - * resource lock on the colocated shard of the parent table. + * LockParentShardResourceIfPartition checks whether the given shard belongs + * to a partition. If it does, LockParentShardResourceIfPartition acquires a + * shard resource lock on the colocated shard of the parent table. */ void LockParentShardResourceIfPartition(uint64 shardId, LOCKMODE lockMode) diff --git a/src/include/distributed/resource_lock.h b/src/include/distributed/resource_lock.h index 95a64c7be..96debf4dc 100644 --- a/src/include/distributed/resource_lock.h +++ b/src/include/distributed/resource_lock.h @@ -84,4 +84,7 @@ extern void LockRelationShardResources(List *relationShardList, LOCKMODE lockMod extern void LockPartitionsInRelationList(List *relationIdList, LOCKMODE lockmode); extern void LockPartitionRelations(Oid relationId, LOCKMODE lockMode); +/* Lock parent table's colocated shard resource */ +extern void LockParentShardResourceIfPartition(uint64 shardId, LOCKMODE lockMode); + #endif /* RESOURCE_LOCK_H */