The apply worker flips two_phase from PENDING to ENABLED without locking the subscription, so a concurrent ALTER SUBSCRIPTION ... REFRESH PUBLICATION with copy_data passes its two_phase check and adds tables under ENABLED; rows of a prepared transaction on a new table are silently lost ============================================================================================================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a22e51-50fb-75d5-a38e-1f19916226f9 Filed: 2026-11-30 21:16:50 UTC Severity: 50/100 Blamed commit: a8fd13cab0ba815e9925dc9676e6309f699b5f72 ## Summary ALTER SUBSCRIPTION ... REFRESH PUBLICATION with copy_data = true is refused once subtwophasestate is ENABLED, because a table that is synchronized after two_phase is enabled can lose the rows of prepared transactions. The apply worker moves the state from PENDING to ENABLED at startup when AllTablesyncsReady() returns true and then calls UpdateTwoPhaseState(). Neither step takes a lock on the subscription, and AllTablesyncsReady() does not see pg_subscription_rel rows that an in-progress ALTER has inserted but not committed. A REFRESH PUBLICATION that starts while the state is still PENDING therefore passes its check, holds AccessExclusiveLock on the subscription, and inserts new 'i' tables, while the restarted apply worker concurrently decides that all tables are ready, starts streaming with two_phase, and commits ENABLED. Both commit. The result is the state the check is meant to prevent: two_phase ENABLED with a table in initial synchronization. A transaction prepared on the publisher during that synchronization has its rows for the new table dropped, and nothing is logged. The window is the apply worker's restart that follows the last table reaching READY ("will restart so that two_phase can be enabled"), from the worker's InitializeLogRepWorker() until its AllTablesyncsReady() call, which includes connecting to the publisher. ## Reproduction Publisher (wal_level = logical, max_prepared_transactions = 10), subscriber (max_prepared_transactions = 10). pre_auth_delay on the publisher only widens the worker's window (connection setup); ENABLE ALWAYS triggers that wait on advisory locks only park the initial copies. Publisher: CREATE TABLE t1(a int PRIMARY KEY); CREATE TABLE t2(a int PRIMARY KEY); INSERT INTO t1 VALUES (100); INSERT INTO t2 VALUES (100); CREATE PUBLICATION pub FOR TABLE t1; Subscriber: CREATE TABLE t1(a int PRIMARY KEY); CREATE TABLE t2(a int PRIMARY KEY); CREATE FUNCTION public.park() RETURNS trigger LANGUAGE plpgsql AS $$BEGIN PERFORM pg_advisory_xact_lock_shared(tg_argv[0]::int); RETURN NEW; END$$; CREATE TRIGGER park BEFORE INSERT ON t1 FOR EACH ROW EXECUTE FUNCTION public.park('44'); CREATE TRIGGER park BEFORE INSERT ON t2 FOR EACH ROW EXECUTE FUNCTION public.park('43'); ALTER TABLE t1 ENABLE ALWAYS TRIGGER park; ALTER TABLE t2 ENABLE ALWAYS TRIGGER park; -- session A: SELECT pg_advisory_lock(44); (parks t1's copy) CREATE SUBSCRIPTION s1 CONNECTION '...' PUBLICATION pub WITH (two_phase = true); -- subtwophasestate = 'p', t1 = 'd' Publisher: ALTER PUBLICATION pub ADD TABLE t2; ALTER SYSTEM SET pre_auth_delay = '3s'; SELECT pg_reload_conf(); Subscriber: -- session B: SELECT pg_advisory_lock(43); (will park t2's copy) -- session A: SELECT pg_advisory_unlock(44); -- t1 becomes READY; the apply worker logs "will restart so that -- two_phase can be enabled". As soon as the new apply worker -- appears in pg_stat_subscription (state still 'p'): ALTER SUBSCRIPTION s1 REFRESH PUBLICATION; -- succeeds Publisher: ALTER SYSTEM RESET pre_auth_delay; SELECT pg_reload_conf(); Subscriber: SELECT subtwophasestate FROM pg_subscription; -- 'e' SELECT srrelid::regclass, srsubstate FROM pg_subscription_rel; -- t1 'r', t2 'd' Publisher: BEGIN; INSERT INTO t1 VALUES (1); INSERT INTO t2 VALUES (1); PREPARE TRANSACTION 'g1'; Subscriber: the transaction appears in pg_prepared_xacts. Session B: SELECT pg_advisory_unlock(43); wait for t2 to become 'r'. Publisher: COMMIT PREPARED 'g1'; INSERT INTO t2 VALUES (2); Expected: either REFRESH PUBLICATION fails with "ALTER SUBSCRIPTION ... REFRESH PUBLICATION with copy_data is not allowed when two_phase is enabled", or the worker sees the new table and leaves two_phase PENDING; in both cases subscriber t2 ends as {1,2,100}. Actual: REFRESH succeeds, the subscription is ENABLED with t2 in initial sync, and subscriber t2 is {2,100} while publisher t2 is {1,2,100}. No error or warning in either log. Running the REFRESH a few seconds later (after the flip) gives the documented error. ## Analysis run_apply_worker() in src/backend/replication/logical/worker.c: if (MySubscription->twophasestate == LOGICALREP_TWOPHASE_STATE_PENDING && AllTablesyncsReady()) { options.proto.logical.twophase = true; walrcv_startstreaming(...); ... UpdateTwoPhaseState(MySubscription->oid, LOGICALREP_TWOPHASE_STATE_ENABLED); UpdateTwoPhaseState() in src/backend/replication/logical/tablesync.c fetches and updates the pg_subscription tuple without any LockSharedObject() on the subscription, and AllTablesyncsReady() reads pg_subscription_rel under a fresh snapshot without one either. The worker's AccessShareLock taken in InitializeLogRepWorker() is released when that startup transaction commits, so it does not cover this later decision. AlterSubscription() (REFRESH PUBLICATION branch, src/backend/commands/subscriptioncmds.c) checks sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED and then, under AccessExclusiveLock, connects to the publisher and inserts the new pg_subscription_rel rows in AlterSubscription_refresh(). Because REFRESH PUBLICATION does not update the pg_subscription tuple, the worker's UpdateTwoPhaseState() does not conflict with it, and both transactions commit. Re-reading the subscription after taking the lock in AlterSubscription() would not close this, because the worker can flip the state after the ALTER has taken the lock. The PENDING-to-ENABLED switch, UpdateTwoPhaseState() and the REFRESH PUBLICATION check were all introduced by a8fd13cab0ba ("Add support for prepared transactions to built-in logical replication"). ## Notes Tested on master ca492a9197a449bca3953f7e287c9aee6d295c6d. With ALTER SUBSCRIPTION ... ADD PUBLICATION (which also updates the pg_subscription tuple) in the same window, the ALTER instead fails with "tuple concurrently updated" because the worker updated the tuple without a lock after the ALTER had fetched it; that outcome does not lose data. The missing lock also shows up without REFRESH PUBLICATION: if an ALTER SUBSCRIPTION ... SET (synchronous_commit = local) is open in a transaction during the same restart window, the apply worker's UpdateTwoPhaseState() waits for it and then fails with "tuple concurrently updated", after the slot has already been switched to two_phase; the next worker start completes the switch. Related, different mechanisms reaching the same "table synchronized while the slot decodes prepared transactions" loss: 01a1e730-bd18-7e6e-99e7-eb097fed124c (crash right after enabling), 01a21c03-2e6e-7089-83df-5691d20eabb8 (pg_upgrade), 01a21d15-5144-7448-b166-7774be616142 (pg_createsubscriber). Related: 01a21312-8c24-71b9-905d-29ace66a2063 (ALTER SUBSCRIPTION fails with "tuple concurrently updated" against the apply worker's unlocked catalog updates). Branches: REL_15 through master likely (the PENDING-to-ENABLED switch and the REFRESH check are unchanged since a8fd13cab0ba); reproduced on master.