diff --git a/src/backend/distributed/executor/adaptive_executor.c b/src/backend/distributed/executor/adaptive_executor.c index 7708693bc..a92dd01a0 100644 --- a/src/backend/distributed/executor/adaptive_executor.c +++ b/src/backend/distributed/executor/adaptive_executor.c @@ -1565,11 +1565,11 @@ LockPartitionsForDistributedPlan(DistributedPlan *distributedPlan) Oid parentRelationId = PartitionParentOid(targetRelationId); /* - * We lock the parent relation after locking relations to prevent - * distributed deadlock. - * Postgres doesn't take AccessShareLock on the parent table when the - * child quals are already cached and a drop/create partition can - * result in a distributed deadlock with multi-shard update. + * Postgres only takes the lock on parent when the session accesses the + * partition for the first time. So it should be okay to get this lock from + * PG perspective. Even though we diverge from PG behavior for concurrent + * modifications on partitions vs CREATE/DROP partitions, we consider this as + * a reasonable trade-off to avoid distributed deadlocks. */ LockRelationOid(parentRelationId, AccessShareLock); } diff --git a/src/test/regress/expected/isolation_partition.out b/src/test/regress/expected/isolation_partition.out index 7fdf2278c..747ee0b21 100644 --- a/src/test/regress/expected/isolation_partition.out +++ b/src/test/regress/expected/isolation_partition.out @@ -5,9 +5,11 @@ step s1-begin: BEGIN; step s1-update: + set citus.force_max_query_parallelization to 'on'; update test_9_1 set destination = 1; step s2-drop: + set citus.force_max_query_parallelization to 'on'; drop table test_6_1; step s1-commit: @@ -20,9 +22,11 @@ step s2-begin: BEGIN; step s2-drop: + set citus.force_max_query_parallelization to 'on'; drop table test_6_1; step s1-update: + set citus.force_max_query_parallelization to 'on'; update test_9_1 set destination = 1; step s2-commit: @@ -35,9 +39,11 @@ step s1-begin: BEGIN; step s1-update: + set citus.force_max_query_parallelization to 'on'; update test_9_1 set destination = 1; step s2-create: + set citus.force_max_query_parallelization to 'on'; CREATE TABLE test_10_1 PARTITION OF test FOR VALUES FROM ('10', '2021-07-16') TO ('11', '2021-07-17'); step s1-commit: @@ -50,9 +56,115 @@ step s2-begin: BEGIN; step s2-create: + set citus.force_max_query_parallelization to 'on'; CREATE TABLE test_10_1 PARTITION OF test FOR VALUES FROM ('10', '2021-07-16') TO ('11', '2021-07-17'); step s1-update: + set citus.force_max_query_parallelization to 'on'; + update test_9_1 set destination = 1; + +step s2-commit: + COMMIT; + +step s1-update: <... completed> + +starting permutation: s1-begin s1-update s2-truncate s1-commit +step s1-begin: + BEGIN; + +step s1-update: + set citus.force_max_query_parallelization to 'on'; + update test_9_1 set destination = 1; + +step s2-truncate: + set citus.force_max_query_parallelization to 'on'; + TRUNCATE TABLE test_6_1; + +step s1-commit: + COMMIT; + + +starting permutation: s2-begin s2-truncate s1-update s2-commit +step s2-begin: + BEGIN; + +step s2-truncate: + set citus.force_max_query_parallelization to 'on'; + TRUNCATE TABLE test_6_1; + +step s1-update: + set citus.force_max_query_parallelization to 'on'; + update test_9_1 set destination = 1; + +step s2-commit: + COMMIT; + + +starting permutation: s1-begin s1-update s2-detach s1-commit +step s1-begin: + BEGIN; + +step s1-update: + set citus.force_max_query_parallelization to 'on'; + update test_9_1 set destination = 1; + +step s2-detach: + ALTER TABLE test DETACH PARTITION test_6_1; + +step s1-commit: + COMMIT; + +step s2-detach: <... completed> + +starting permutation: s2-begin s2-detach s1-update s2-commit +step s2-begin: + BEGIN; + +step s2-detach: + ALTER TABLE test DETACH PARTITION test_6_1; + +step s1-update: + set citus.force_max_query_parallelization to 'on'; + update test_9_1 set destination = 1; + +step s2-commit: + COMMIT; + +step s1-update: <... completed> + +starting permutation: s1-begin s1-update s2-alter-table s1-commit +step s1-begin: + BEGIN; + +step s1-update: + set citus.force_max_query_parallelization to 'on'; + update test_9_1 set destination = 1; + +step s2-alter-table: + set citus.force_max_query_parallelization to 'on'; + SELECT alter_table_set_access_method('test_6_1','columnar'); + +step s1-commit: + COMMIT; + +step s2-alter-table: <... completed> +alter_table_set_access_method + + + +starting permutation: s2-begin s2-alter-table s1-update s2-commit +step s2-begin: + BEGIN; + +step s2-alter-table: + set citus.force_max_query_parallelization to 'on'; + SELECT alter_table_set_access_method('test_6_1','columnar'); + +alter_table_set_access_method + + +step s1-update: + set citus.force_max_query_parallelization to 'on'; update test_9_1 set destination = 1; step s2-commit: diff --git a/src/test/regress/spec/isolation_partition.spec b/src/test/regress/spec/isolation_partition.spec index ceb849b47..d999e2b5d 100644 --- a/src/test/regress/spec/isolation_partition.spec +++ b/src/test/regress/spec/isolation_partition.spec @@ -9,6 +9,7 @@ setup SELECT create_distributed_table('test', 'name'); + DROP TABLE IF EXISTS test_6_1; CREATE TABLE test_6_1 PARTITION OF test FOR VALUES FROM ('6', '2021-07-12') TO ('6', '2021-07-13'); CREATE TABLE test_7_1 PARTITION OF test FOR VALUES FROM ('7', '2021-07-13') TO ('8', '2021-07-14'); CREATE TABLE test_8_1 PARTITION OF test FOR VALUES FROM ('8', '2021-07-14') TO ('9', '2021-07-15'); @@ -29,6 +30,7 @@ step "s1-begin" { } step "s1-update" { + set citus.force_max_query_parallelization to 'on'; update test_9_1 set destination = 1; } @@ -43,12 +45,27 @@ step "s2-begin" { } step "s2-drop" { + set citus.force_max_query_parallelization to 'on'; drop table test_6_1; } step "s2-create" { + set citus.force_max_query_parallelization to 'on'; CREATE TABLE test_10_1 PARTITION OF test FOR VALUES FROM ('10', '2021-07-16') TO ('11', '2021-07-17'); +} +step "s2-detach" { + ALTER TABLE test DETACH PARTITION test_6_1; +} + +step "s2-truncate" { + set citus.force_max_query_parallelization to 'on'; + TRUNCATE TABLE test_6_1; +} + +step "s2-alter-table" { + set citus.force_max_query_parallelization to 'on'; + SELECT alter_table_set_access_method('test_6_1','columnar'); } step "s2-commit" { @@ -59,3 +76,9 @@ permutation "s1-begin" "s1-update" "s2-drop" "s1-commit" permutation "s2-begin" "s2-drop" "s1-update" "s2-commit" permutation "s1-begin" "s1-update" "s2-create" "s1-commit" permutation "s2-begin" "s2-create" "s1-update" "s2-commit" +permutation "s1-begin" "s1-update" "s2-truncate" "s1-commit" +permutation "s2-begin" "s2-truncate" "s1-update" "s2-commit" +permutation "s1-begin" "s1-update" "s2-detach" "s1-commit" +permutation "s2-begin" "s2-detach" "s1-update" "s2-commit" +permutation "s1-begin" "s1-update" "s2-alter-table" "s1-commit" +permutation "s2-begin" "s2-alter-table" "s1-update" "s2-commit" \ No newline at end of file