From 2d92aac347bc6d3640459fec349fc7bd46d4e7a3 Mon Sep 17 00:00:00 2001 From: eaydingol Date: Mon, 4 Mar 2024 11:19:47 +0000 Subject: [PATCH] First acquire locks on the modified tables, then the reference ones --- src/backend/distributed/utils/resource_lock.c | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/backend/distributed/utils/resource_lock.c b/src/backend/distributed/utils/resource_lock.c index 4cfe19b4b..52a8006d4 100644 --- a/src/backend/distributed/utils/resource_lock.c +++ b/src/backend/distributed/utils/resource_lock.c @@ -709,6 +709,21 @@ SerializeNonCommutativeWrites(List *shardIntervalList, LOCKMODE lockMode) List *replicatedShardList = NIL; bool anyTableReplicated = AnyTableReplicated(shardIntervalList, &replicatedShardList); + /* + * Acquire locks on the modified table. + * If the table is replicated, the locks are first acquired on the first worker node then locally. + */ + if (anyTableReplicated && ClusterHasKnownMetadataWorkers() && !IsFirstWorkerNode()) + { + LockShardListResourcesOnFirstWorker(lockMode, replicatedShardList); + } + LockShardListResources(shardIntervalList, lockMode); + + /* + * Next, acquire locks on the reference tables if there is any. + * Note that LockReferencedReferenceShardResources() first acquires locks on the first worker, + * then locally. + */ if (anyTableReplicated) { ShardInterval *firstShardInterval = @@ -725,11 +740,6 @@ SerializeNonCommutativeWrites(List *shardIntervalList, LOCKMODE lockMode) LockReferencedReferenceShardResources(firstShardInterval->shardId, lockMode); } } - if (anyTableReplicated && ClusterHasKnownMetadataWorkers() && !IsFirstWorkerNode()) - { - LockShardListResourcesOnFirstWorker(lockMode, replicatedShardList); - } - LockShardListResources(shardIntervalList, lockMode); }