An ERROR raised by a ddl_command_end event trigger rolls back CREATE/ALTER/DROP SUBSCRIPTION after the publisher-side slot change was already made: DROP leaves the subscription with its slot gone, ALTER SET (failover) leaves slot and catalog disagreeing, CREATE leaks the slot ==================================================================================================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a2148c-6605-7209-8f86-8c36e8c9001b Filed: 2026-11-25 21:11:15 UTC Severity: 30/100 Blamed commit: 665d1fad99e7b11678b0d5fa24d2898424243cd6 ## Summary CREATE SUBSCRIPTION (create_slot = true), DROP SUBSCRIPTION with a slot, and ALTER SUBSCRIPTION ... SET (failover / two_phase) change a replication slot on the publisher. That change cannot be rolled back, so each command calls PreventInTransactionBlock() and does the remote step last. That only helps if the statement cannot fail after the remote step. But ddl_command_end event triggers fire for subscription commands after the command's work is done, and an ERROR there (the documented way to reject DDL with an event trigger) aborts the transaction after the slot has been created, dropped or altered. Results: - DROP SUBSCRIPTION: the publisher slot is dropped, the subscription stays, and its apply worker loops on "replication slot ... does not exist". - ALTER SUBSCRIPTION ... SET (failover = true): the slot has failover = true while pg_subscription.subfailover stays false. - CREATE SUBSCRIPTION: the slot is created on the publisher and is never used or dropped, so it retains WAL indefinitely. ## Reproduction Two clusters with wal_level = logical; publisher on port 5432, subscriber on port 5433. Publisher: CREATE TABLE t (a int); CREATE PUBLICATION p FOR TABLE t; Subscriber: CREATE TABLE t (a int); CREATE FUNCTION etf() RETURNS event_trigger LANGUAGE plpgsql AS $$BEGIN RAISE EXCEPTION 'reject %', tg_tag; END$$; DROP variant (subscriber): CREATE SUBSCRIPTION s CONNECTION 'port=5432 dbname=postgres' PUBLICATION p; CREATE EVENT TRIGGER et ON ddl_command_end WHEN TAG IN ('DROP SUBSCRIPTION') EXECUTE FUNCTION etf(); DROP SUBSCRIPTION s; -- NOTICE: dropped replication slot "s" on publisher -- ERROR: reject DROP SUBSCRIPTION SELECT subname, subslotname FROM pg_subscription; -- s | s Publisher: SELECT slot_name FROM pg_replication_slots; returns no rows. The subscriber log then repeats ERROR: replication slot "s" does not exist. Expected: the event trigger's ERROR rejects the whole command, and the subscription and its slot are both still there. ALTER variant (subscriber, on a disabled subscription s with a slot, trigger WHEN TAG IN ('ALTER SUBSCRIPTION')): ALTER SUBSCRIPTION s SET (failover = true); -- ERROR: reject ALTER SUBSCRIPTION SELECT subfailover FROM pg_subscription; -- false Publisher: SELECT failover FROM pg_replication_slots WHERE slot_name = 's'; returns true (it was false before). CREATE variant (trigger WHEN TAG IN ('CREATE SUBSCRIPTION')): CREATE SUBSCRIPTION s2 CONNECTION 'port=5432 dbname=postgres' PUBLICATION p; -- NOTICE: created replication slot "s2" on publisher -- ERROR: reject CREATE SUBSCRIPTION The subscriber has no subscription s2, but the publisher keeps slot s2. ## Analysis In src/backend/commands/subscriptioncmds.c, DropSubscription() calls ReplicationSlotDropAtPubNode() last, AlterSubscription() calls walrcv_alter_slot() after CatalogTupleUpdate() ("This has to be at the end because otherwise if there is an error ... we won't be able to rollback"), and CreateSubscription() creates the slot at the end. All of them use PreventInTransactionBlock() so that statement end implies commit. ProcessUtilitySlow() then runs EventTriggerDDLCommandEnd() (and, for DROP, sql_drop triggers) after the command returns and before commit, so a trigger ERROR still aborts the transaction. Subscription commands have supported event triggers since the commit that added logical replication, which also added the non-transactional slot create and drop in these commands. The ALTER ... SET (failover) path was added later (commit 776621a5e4796fa214b6b29a7ca134f6c138572a) with the same ordering. ## Notes Tested on master at 4255812efdbcf7f1acf6be2211e1b5b11c4d4240 (assert-enabled build). Related, same family but a different trigger (stale-tuple update after the slot drop): https://ai-annex.j-davis.com/report/01a213dc-add0-7cae-80ac-b2e6eb97665b Branches: REL_14_STABLE through master for the CREATE and DROP variants (code from 665d1fad, REL_10); the ALTER ... SET (failover) variant needs REL_17_STABLE or later (776621a5).