DROP SUBSCRIPTION silently orphans the subscription's prepared transactions =========================================================================== AI-generated bug report from the AI Annex Report ID: 01a1cbef-c7a1-7a44-94c8-6c7b7544b408 Filed: 2026-11-11 18:47:31 UTC Severity: 42/100 Blamed commit: a8fd13cab0ba815e9925dc9676e6309f699b5f72 DROP SUBSCRIPTION silently leaves behind prepared transactions that its logical replication worker created. When two_phase is enabled, the subscriber prepares the publisher's transaction under a generated identifier, pg_gid__. The apply worker is what later commits or rolls that transaction back. DROP SUBSCRIPTION stops that worker and, in the normal case, drops the publisher replication slot. It never looks for those prepared transactions. The command succeeds with no error and no warning. The GID stays in pg_prepared_xacts. It keeps the locks it acquired and holds back the transaction-ID horizon until somebody runs COMMIT PREPARED or ROLLBACK PREPARED on the subscriber by hand. A later COMMIT PREPARED or ROLLBACK PREPARED on the publisher is not applied, so the change stays invisible on the subscriber. The same GID is used when the prepare was spilled and when streaming is parallel, so those prepared transactions are left behind by the same path. This is not how ALTER SUBSCRIPTION ... SET (two_phase = false) behaves. Since 1462aad2e4474ab61174f8ab00992cd3d6d57c7b that command calls LookupGXactBySubid and errors with 'cannot disable "two_phase" when prepared transactions exist', and the ALTER SUBSCRIPTION documentation names the GID form and pg_prepared_xacts. DropSubscription has no such check, and the DROP SUBSCRIPTION documentation does not mention the case. On REL_15_STABLE through REL_17_STABLE, two_phase cannot be changed after the subscription is created, so DROP is the command that discards the worker while a prepare is still open. The behavior comes from a8fd13cab0ba815e9925dc9676e6309f699b5f72 ("Add support for prepared transactions to built-in logical replication.", 2021-07-14). It is present on master and on REL_15_STABLE through REL_19_STABLE. REL_14_STABLE has no built-in two_phase subscriptions. A prepared transaction left this way holds its row locks and an ExclusiveLock on its transaction id, and it keeps txid_snapshot_xmin from advancing. Vacuum and anything else that waits on that horizon stall until the GID is resolved. Nothing in the server points at the orphan after the subscription is gone. The same leftover showed up as a 021_twophase failure: https://www.postgresql.org/message-id/CALDaNm329QaZ+bwU--bW6GjbNSZ8-38cDE8QWofafub7NV67oA@mail.gmail.com Dropping one of two subscriptions before its COMMIT PREPARED had been applied left a prepared transaction on the subscriber. The thread fixed the test by waiting for catchup. It did not change DROP SUBSCRIPTION, and the documentation still does not describe the leftover as intended. ## Reproduction On the publisher, wal_level is logical and one table is published. On the subscriber, max_prepared_transactions is greater than zero. ```sql -- publisher CREATE TABLE tab (a int PRIMARY KEY); CREATE PUBLICATION pub FOR TABLE tab; -- subscriber CREATE TABLE tab (a int PRIMARY KEY); CREATE SUBSCRIPTION sub CONNECTION '...' PUBLICATION pub WITH (two_phase = on, copy_data = false); ``` Wait until pg_subscription.subtwophasestate is 'e'. Then, on the publisher: ```sql BEGIN; INSERT INTO tab VALUES (1); PREPARE TRANSACTION 'publisher_gid'; ``` Wait until the subscriber shows one row in pg_prepared_xacts, with a gid of the form pg_gid__. Then, on the subscriber: ```sql DROP SUBSCRIPTION sub; ``` Expected: the drop fails while that prepared transaction exists. The subscription, its replication slot, and the prepared transaction all remain, and a later ROLLBACK PREPARED or COMMIT PREPARED on the publisher is still applied. ALTER SUBSCRIPTION ... SET (two_phase = false) already fails in this state on branches that allow the option to change. Actual, on current master: DROP SUBSCRIPTION succeeds. The only notice is that the replication slot was dropped. pg_prepared_xacts still contains pg_gid__. The inserted row is not visible. The prepared transaction still holds RowExclusiveLock on tab and ExclusiveLock on its transaction id, and txid_snapshot_xmin stays at that xid. COMMIT PREPARED 'publisher_gid' on the publisher succeeds there and does not change the subscriber: the GID remains and the row stays invisible. The slot is already gone, so no apply worker is left to finish it. ## Diagnosis DropSubscription deletes the catalog row, stops the apply and tablesync workers, removes replication origins, and then drops the publisher slot outside the transaction. Nothing in that sequence calls LookupGXactBySubid. The worker that would match a later COMMIT PREPARED or ROLLBACK PREPARED to pg_gid__ has been stopped, and the slot that would deliver that record is gone. apply_handle_prepare_internal builds that GID for an ordinary PREPARE, for STREAM PREPARE of a spilled transaction, and for the parallel apply worker. All three land in the same pg_prepared_xacts entry, so all three are orphaned. Leaving the GID is not required for the drop. The catalog delete is transactional. The slot drop is not, and it happens only after the workers are stopped. A check in between can still abort the drop and leave the slot in place. After the error, the launcher starts the worker again, and the publisher's commit or rollback is applied. ## Fix After the workers have been stopped, and before origins are removed or the slot is dropped, error if LookupGXactBySubid returns true: ``` cannot drop subscription "%s" when prepared transactions exist HINT: Resolve these transactions and try again. ``` That matches the ALTER SUBSCRIPTION failure on master, REL_19_STABLE, and REL_18_STABLE, where LookupGXactBySubid already exists. REL_15_STABLE, REL_16_STABLE, and REL_17_STABLE do not have that function. TwoPhaseState is private to twophase.c, so the patch adds LookupGXactBySubid there and calls it from DropSubscription. No WAL format change and no catalog version bump. Resolve the prepared transaction on the publisher and let apply finish, or commit or roll it back on the subscriber, then retry the drop. The DROP SUBSCRIPTION notes should say so, including the pg_gid_%u_%u form and pg_prepared_xacts. ## Severity 42. The nearest anchor is 40-59: unlikely, medium impact, with a workaround. Dropping a two_phase subscription while one of its transactions is still prepared is uncommon. Once it happens the failure is silent, locks and the xid horizon are held, and the publisher's commit or rollback never arrives. The workaround is to notice the pg_prepared_xacts row and COMMIT PREPARED or ROLLBACK PREPARED. Two points above the bottom of that band because nothing in the drop tells the user the GID was left behind.