diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index 3da77cd..0938bf5 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -2902,6 +2902,7 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel) * table, so do it before the non-transactional cleanup of origins and the * slot below. */ + INJECTION_POINT("subscription-conflict-log-drop-before-table-lock", NULL); drop_sub_conflict_log_table(subid, subname, subconflictlogrelid, stmt->behavior, true); diff --git a/src/test/modules/injection_points/Makefile b/src/test/modules/injection_points/Makefile index a7a16d5..3cfee6f 100644 --- a/src/test/modules/injection_points/Makefile +++ b/src/test/modules/injection_points/Makefile @@ -37,7 +37,8 @@ ISOLATION = basic \ $(WAIT_CLEANUP_TST) \ heap_lock_update \ on_conflict_probe_window \ - multixact-aborted-updater + multixact-aborted-updater \ + subscription_conflict_log_owner_drop # some isolation tests require wal_level=replica ISOLATION_OPTS = --temp-config $(top_srcdir)/src/test/modules/injection_points/extra.conf diff --git a/src/test/modules/injection_points/expected/subscription_conflict_log_owner_drop.out b/src/test/modules/injection_points/expected/subscription_conflict_log_owner_drop.out new file mode 100644 index 0000000..b7ff1cb --- /dev/null +++ b/src/test/modules/injection_points/expected/subscription_conflict_log_owner_drop.out @@ -0,0 +1,20 @@ +Parsed test spec with 3 sessions + +starting permutation: s1_deadlock_timeout s1_quiet_notices s1_drop_subscription s2_deadlock_timeout s2_alter_owner s3_wakeup +injection_points_attach +----------------------- + +(1 row) + +step s1_deadlock_timeout: SET deadlock_timeout = '100ms'; +step s1_quiet_notices: SET client_min_messages = warning; +step s1_drop_subscription: DROP SUBSCRIPTION regress_clt_drop_sub; +step s2_deadlock_timeout: SET deadlock_timeout = '100ms'; +step s2_alter_owner: ALTER SUBSCRIPTION regress_clt_drop_sub OWNER TO regress_clt_drop_new_owner; +step s3_wakeup: SELECT injection_points_wakeup('subscription-conflict-log-drop-before-table-lock'); +injection_points_wakeup +----------------------- + +(1 row) + +step s1_drop_subscription: <... completed> diff --git a/src/test/modules/injection_points/meson.build b/src/test/modules/injection_points/meson.build index cf9cb21..9561e0f 100644 --- a/src/test/modules/injection_points/meson.build +++ b/src/test/modules/injection_points/meson.build @@ -45,6 +45,7 @@ injection_points_isolation = [ 'heap_lock_update', 'on_conflict_probe_window', 'multixact-aborted-updater', + 'subscription_conflict_log_owner_drop', ] # wait_cleanup terminates another backend, whose FATAL message can be lost diff --git a/src/test/modules/injection_points/specs/subscription_conflict_log_owner_drop.spec b/src/test/modules/injection_points/specs/subscription_conflict_log_owner_drop.spec new file mode 100644 index 0000000..075c441 --- /dev/null +++ b/src/test/modules/injection_points/specs/subscription_conflict_log_owner_drop.spec @@ -0,0 +1,54 @@ +# Exercise the conflict-log table and pg_subscription lock order with the two +# DDL commands from the reported deadlock. A third session wakes DROP after +# ALTER has attempted the catalog update. + +setup +{ + CREATE EXTENSION injection_points; + CREATE ROLE regress_clt_drop_new_owner; + DO $$ + BEGIN + EXECUTE format('GRANT CREATE ON DATABASE %I TO regress_clt_drop_new_owner', current_database()); + END; + $$; + + CREATE SUBSCRIPTION regress_clt_drop_sub + CONNECTION 'dbname=unused' + PUBLICATION unused + WITH (connect = false, slot_name = NONE, enabled = false, + conflict_log_destination = 'table'); + + SELECT injection_points_attach('subscription-conflict-log-drop-before-table-lock', 'wait'); +} + +teardown +{ + SELECT injection_points_detach('subscription-conflict-log-drop-before-table-lock'); + DROP SUBSCRIPTION IF EXISTS regress_clt_drop_sub; + DO $$ + BEGIN + EXECUTE format('REVOKE CREATE ON DATABASE %I FROM regress_clt_drop_new_owner', current_database()); + END; + $$; + DROP ROLE regress_clt_drop_new_owner; + DROP EXTENSION injection_points; +} + +session "s1" +step "s1_deadlock_timeout" { SET deadlock_timeout = '100ms'; } +step "s1_quiet_notices" { SET client_min_messages = warning; } +step "s1_drop_subscription" { DROP SUBSCRIPTION regress_clt_drop_sub; } + +session "s2" +step "s2_deadlock_timeout" { SET deadlock_timeout = '100ms'; } +step "s2_alter_owner" { ALTER SUBSCRIPTION regress_clt_drop_sub OWNER TO regress_clt_drop_new_owner; } + +session "s3" +step "s3_wakeup" { SELECT injection_points_wakeup('subscription-conflict-log-drop-before-table-lock'); } + +# DROP reaches the injection point first. With the original tuple-delete +# order it has already deleted the catalog row; ALTER then locks the table and +# waits for that row. Waking DROP makes it wait for ALTER's table lock and +# exposes the deadlock. With the fix, ALTER updates the row and completes +# before DROP resumes. +permutation "s1_deadlock_timeout" "s1_quiet_notices" "s1_drop_subscription" "s2_deadlock_timeout" "s2_alter_owner" "s3_wakeup"