diff --git a/src/backend/distributed/utils/resource_lock.c b/src/backend/distributed/utils/resource_lock.c index 13e88a16e..4cfe19b4b 100644 --- a/src/backend/distributed/utils/resource_lock.c +++ b/src/backend/distributed/utils/resource_lock.c @@ -707,13 +707,10 @@ SerializeNonCommutativeWrites(List *shardIntervalList, LOCKMODE lockMode) } List *replicatedShardList = NIL; - if (AnyTableReplicated(shardIntervalList, &replicatedShardList)) - { - if (ClusterHasKnownMetadataWorkers() && !IsFirstWorkerNode()) - { - LockShardListResourcesOnFirstWorker(lockMode, replicatedShardList); - } + bool anyTableReplicated = AnyTableReplicated(shardIntervalList, &replicatedShardList); + if (anyTableReplicated) + { ShardInterval *firstShardInterval = (ShardInterval *) linitial(replicatedShardList); if (ReferenceTableShardId(firstShardInterval->shardId)) @@ -728,7 +725,10 @@ SerializeNonCommutativeWrites(List *shardIntervalList, LOCKMODE lockMode) LockReferencedReferenceShardResources(firstShardInterval->shardId, lockMode); } } - + if (anyTableReplicated && ClusterHasKnownMetadataWorkers() && !IsFirstWorkerNode()) + { + LockShardListResourcesOnFirstWorker(lockMode, replicatedShardList); + } LockShardListResources(shardIntervalList, lockMode); } diff --git a/src/test/regress/expected/issue_7477.out b/src/test/regress/expected/issue_7477.out index 4a23da3d8..224d85c6e 100644 --- a/src/test/regress/expected/issue_7477.out +++ b/src/test/regress/expected/issue_7477.out @@ -45,16 +45,16 @@ SELECT * FROM master_run_on_worker( true); node_name | node_port | success | result --------------------------------------------------------------------- - localhost | 57638 | f | ERROR: deadlock detected + localhost | 57638 | t | UPDATE 1 + localhost | 57637 | t | UPDATE 1 + localhost | 57637 | t | UPDATE 1 + localhost | 57638 | t | UPDATE 1 + localhost | 57637 | t | UPDATE 1 + localhost | 57638 | t | UPDATE 1 + localhost | 57637 | t | UPDATE 1 + localhost | 57638 | t | UPDATE 1 + localhost | 57638 | t | UPDATE 1 localhost | 57637 | t | UPDATE 1 - localhost | 57637 | f | ERROR: deadlock detected - localhost | 57638 | f | ERROR: deadlock detected - localhost | 57637 | f | ERROR: canceling the transaction since it was involved in a distributed deadlock - localhost | 57638 | f | ERROR: deadlock detected - localhost | 57637 | f | ERROR: deadlock detected - localhost | 57638 | f | ERROR: deadlock detected - localhost | 57638 | f | ERROR: canceling the transaction since it was involved in a distributed deadlock - localhost | 57637 | f | ERROR: deadlock detected (10 rows) --- cleanup diff --git a/src/test/regress/sql/issue_7477.sql b/src/test/regress/sql/issue_7477.sql index 4c5aa80d5..b9c1578e9 100644 --- a/src/test/regress/sql/issue_7477.sql +++ b/src/test/regress/sql/issue_7477.sql @@ -1,5 +1,5 @@ ---- Test for updating a table that has a foreign key reference to another reference table. +--- Test for updating a table that has a foreign key reference to another reference table. --- Issue #7477: Distributed deadlock after issuing a simple UPDATE statement --- https://github.com/citusdata/citus/issues/7477 @@ -15,7 +15,7 @@ CREATE TABLE table2 ( SELECT create_reference_table('table2'); INSERT INTO table2 VALUES (1, 'test'); ---- Runs the update command in parallel on workers. +--- Runs the update command in parallel on workers. --- Due to bug #7477, before the fix, the result is non-deterministic --- and have several rows of the form: --- localhost | 57638 | f | ERROR: deadlock detected @@ -23,7 +23,7 @@ INSERT INTO table2 VALUES (1, 'test'); --- localhost | 57637 | f | ERROR: canceling the transaction since it was involved in a distributed deadlock SELECT * FROM master_run_on_worker( - ARRAY['localhost', 'localhost','localhost', 'localhost','localhost', + ARRAY['localhost', 'localhost','localhost', 'localhost','localhost', 'localhost','localhost', 'localhost','localhost', 'localhost']::text[], ARRAY[57638, 57637, 57637, 57638, 57637, 57638, 57637, 57638, 57638, 57637]::int[], ARRAY['UPDATE table2 SET info = ''test_update'' WHERE id = 1',