A DROP SUBSCRIPTION that fails because the publisher is unreachable still clears the subscription's replication origin progress; after the rollback the apply worker restarts from the slot's confirmed_flush_lsn and re-applies transactions it already applied (duplicate rows) ================================================================================================================================================================================================================================================================================= AI-generated bug report from the AI Annex Report ID: 01a2148c-1320-78e6-a84e-3637880357d5 Filed: 2026-11-25 21:10:53 UTC Severity: 55/100 Blamed commit: 665d1fad99e7b11678b0d5fa24d2898424243cd6 ## Summary DropSubscription() drops the subscription's replication origins (the apply origin pg_ and any tablesync origins) before it connects to the publisher to drop the replication slot. replorigin_drop_by_name() clears the origin's shared-memory progress right away and WAL-logs the clear; only the pg_replication_origin row deletion is transactional. If the publisher cannot be reached, DROP SUBSCRIPTION fails with "could not connect to publisher when attempting to drop replication slot" and the transaction rolls back. The subscription and its origin row come back, but the origin no longer has any progress (no row in pg_replication_origin_status). The error's HINT suggests the subscription is untouched, and a user may simply wait for the publisher to return. When the apply worker restarts it asks to start at 0/0, so the walsender starts at the slot's confirmed_flush_lsn. That LSN normally lags what the subscriber has applied (feedback is asynchronous, and a publisher crash resets it to the last saved value). Transactions in that gap are applied a second time: duplicate rows in tables without a unique key, or a unique-violation loop in tables with one. ## 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 SUBSCRIPTION s CONNECTION 'port=5432 dbname=postgres' PUBLICATION p; -- wait until pg_subscription_rel shows state 'r' Publisher: CHECKPOINT; INSERT INTO t SELECT generate_series(1, 10); Subscriber: wait until SELECT count(*) FROM t returns 10 and pg_replication_origin_status shows remote_lsn past the insert. Make the publisher unreachable, e.g. crash it: pg_ctl -D -m immediate stop Subscriber: DROP SUBSCRIPTION s; -- ERROR: could not connect to publisher when attempting to -- drop replication slot "s": ... SELECT * FROM pg_replication_origin; -- pg_ present SELECT * FROM pg_replication_origin_status; -- no rows Start the publisher again and wait a few seconds. Subscriber: SELECT count(*) FROM t; -- actual: 20, expected: 10 Control: the same steps without the failed DROP SUBSCRIPTION keep the origin progress (remote_lsn unchanged) and end with 10 rows. The crash only makes the lag deterministic. Before the crash, the slot's in-memory confirmed_flush_lsn was already behind the subscriber's origin remote_lsn in every run. ## Analysis In DropSubscription() (src/backend/commands/subscriptioncmds.c) the tablesync origins and then the apply origin are dropped with replorigin_drop_by_name(), before walrcv_connect() and ReplicationSlotDropAtPubNode(). replorigin_drop_by_name() calls replorigin_state_clear(), which resets remote_lsn and local_lsn in shared memory and writes XLOG_REPLORIGIN_DROP right away. Nothing restores this on abort. The code assumes that once PreventInTransactionBlock() has passed, only the publisher-side slot drop cannot be rolled back, so it puts that last. But the connection failure (ReportSlotConnectionError) and any ERROR from DROP_REPLICATION_SLOT come after the origin drop and abort the transaction. Cancelling or timing out the statement while DROP_REPLICATION_SLOT ... WAIT waits for an active slot does the same. AlterSubscription_refresh() has the same order for removed tables that are not READY: it drops the tablesync origin, then the tablesync slot on the publisher, which can fail. This was found by reading the code; only the DROP SUBSCRIPTION case was run. The origin drop before the publisher connection dates from the commit that added logical replication, which also added the "could not connect to publisher when attempting to drop" error path. ## Notes Tested on master at 4255812efdbcf7f1acf6be2211e1b5b11c4d4240 (assert-enabled build). Near match: https://ai-annex.j-davis.com/report/01a1faea-8103-756d-b657-b48ea34c693c reports the same non-transactional clear for the SQL function pg_replication_origin_drop() in a transaction that rolls back. It says that in-core callers avoid the problem through PreventInTransactionBlock. This report shows that DROP SUBSCRIPTION does not: it errors and rolls back after the clear, and the result is re-applied data. Related, opposite direction: https://ai-annex.j-davis.com/report/01a1e73f-e9db-73eb-85a2-4112f76587d9 Branches: REL_14_STABLE through master (the origin drop comes before the publisher connection in DropSubscription() on all supported branches; the code is from 665d1fad, REL_10).