On Wed, Nov 25, 2026 at 12:23 PM shveta malik <[email protected]> wrote: > > On Wed, Nov 25, 2026 at 11:48 AM shveta malik <[email protected]> wrote: > > > > Thanks for the patch Nisha. I have just started the reveiw, and > > noticed thatthe patch moves the drop_sub_conflict_log_table() call in > > DropSubscription() from 'after' the prepared-transaction check to > > 'before' it. Won't this reintroduce the hang that check was > > specifically added to prevent? As I understand it, if a prepared > > transaction holds a lock on the conflict log table, the drop now takes > > AccessExclusiveLock on the table and blocks before we ever reach the > > check. Isn't it? > > > > Let me clarify the situation. It depends on whether the apply worker > is running, because the drop now also happens before the worker is > stopped: > > With the worker still running (enabled), the DROP hangs on the > conflict log table lock, but it later clears on its own once the > prepared transaction is resolved. > > With the subscription disabled before the DROP-SUB (or the worker > otherwise gone), the DROP hangs and does not clear by itself. There's > no worker to apply a resolution from the publisher, and trying to > re-enable the sub in the meantime just blocks on the same subscription > lock DROP holds, so no new worker can start. > > Earlier the check raised the error before we actually dropped the > table, which seems better to me than blocking on the lock. > > Shall we move the drop_sub_conflict_log_table() call to just after the > prepared-transaction check? IIUC it would still be before the > non-transactional cleanup (the replication slot drop on the > publisher), so it should also satisfy the reason given in the commit > message for moving it up. >
Agree, the v2 patch moves drop_sub_conflict_log_table() to just after the prepared-transaction check. One clarification on the non-transactional part in commit message. The slot drop is at the very end of DropSubscription(), so the CLT drop was already before it even at its original place. The step that matters is the tablesync origin cleanup, which ran before the CLT drop. replorigin_drop_by_name() runs inside the transaction and its pg_replication_origin delete is transactional, but it also clears the origin's progress in shared memory, which is not undone on rollback. While further assessing the risk of keeping the drop at its original place, I found two problems: 1. Cache lookup failure. DropSubscription() deletes the pg_subscription row early, and the CommandCounterIncrement() in replorigin_drop_by_name() makes that delete visible. If the CLT drop then fails with RESTRICT, reportDependentObjects() looks up the subscription name for the error message and fails: postgres=# DROP SUBSCRIPTION sub; ERROR: cache lookup failed for subscription 16392 2. Lost tablesync origin progress. After the failed DROP, a table still in initial sync keeps its origin but with no progress. When its tablesync worker restarts in FINISHEDCOPY state, it streams from the slot's confirmed_flush, which can be behind what it had already applied, so it applies those changes again. With a primary key this becomes a repeating insert_exists conflict error, and the table never finishes syncing. Neither showed up before, because the CLT was always dropped with CASCADE and this step could not fail. With RESTRICT it fails whenever other objects depend on the CLT. Placing the drop right after the prepared-transaction check avoids both problems. Attached file has steps to reproduce both case when the drop_sub_conflict_log_table() is at original possition at head. -- Thanks, Nisha
------------------------------------- case-1: cache lookup failure ------------------------------------- Publisher (9933): CREATE TABLE t (a int PRIMARY KEY); CREATE PUBLICATION pub FOR TABLE t; Subscriber, session A. Keep it open: CREATE TABLE t (a int PRIMARY KEY); BEGIN; LOCK TABLE t IN SHARE MODE; Subscriber, session B: CREATE SUBSCRIPTION sub CONNECTION 'port=9933 dbname=postgres' PUBLICATION pub WITH (conflict_log_destination = 'table'); SELECT roname FROM pg_replication_origin; -- after a second or two: pg_<subid> and pg_<subid>_<relid> SELECT subconflictlogrelid::regclass AS clt FROM pg_subscription WHERE subname = 'sub' \gset CREATE VIEW v AS SELECT relid FROM :clt; DROP SUBSCRIPTION sub; -- ERROR: cache lookup failed for subscription NNNNN ------------------------------------- case-2: duplicate apply ------------------------------------- Publisher (9933): CREATE TABLE t (a int PRIMARY KEY); INSERT INTO t VALUES (1); CREATE PUBLICATION pub FOR TABLE t; Subscriber (9935), session A. Keep it open: ALTER SYSTEM SET wal_receiver_status_interval = 0; SELECT pg_reload_conf(); CREATE TABLE t (a int PRIMARY KEY); CREATE FUNCTION wait_lock() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.a = 1 THEN PERFORM pg_advisory_lock(1); PERFORM pg_advisory_unlock(1); END IF; -- holds COPY IF NEW.a = 3 THEN PERFORM pg_advisory_lock(2); PERFORM pg_advisory_unlock(2); END IF; -- holds catchup RETURN NEW; END $$; CREATE TRIGGER t_wait BEFORE INSERT ON t FOR EACH ROW EXECUTE FUNCTION wait_lock(); ALTER TABLE t ENABLE ALWAYS TRIGGER t_wait; SELECT pg_advisory_lock(1), pg_advisory_lock(2); 1. Start the sync and pause it in COPY Subscriber, session B: CREATE SUBSCRIPTION sub CONNECTION 'port=9933 dbname=postgres' PUBLICATION pub WITH (conflict_log_destination = 'table'); SELECT srsubstate FROM pg_subscription_rel; -- d (paused in COPY on a=1) 2. Make changes for the catchup phase Publisher, as two separate transactions: INSERT INTO t VALUES (2); INSERT INTO t VALUES (3); 3. Release COPY Session A: SELECT pg_advisory_unlock(1); The worker finishes COPY and sets state f. In catchup it applies and commits a=2, then pauses on a=3 waiting for lock 2. 4. Check the state Subscriber, session B: SELECT srsubstate FROM pg_subscription_rel; -- f SELECT * FROM t; -- 1, 2 SELECT roname, pg_replication_origin_progress(roname, false) FROM pg_replication_origin WHERE roname LIKE 'pg\_%\_%'; -- tablesync origin: LSN after a=2 (call it P) Publisher: SELECT slot_name, confirmed_flush_lsn FROM pg_replication_slots WHERE slot_name LIKE 'pg\_%sync%'; You need confirmed_flush_lsn < P to continue. If it's already at or past P, the walsender has forced feedback, and the restart would be harmless. In that case run it again. 5. Fail the DROP Subscriber, session B: SELECT subconflictlogrelid::regclass AS clt FROM pg_subscription WHERE subname = 'sub' \gset CREATE VIEW v AS SELECT relid FROM :clt; ALTER SUBSCRIPTION sub DISABLE; DROP SUBSCRIPTION sub; -- ERROR (cache lookup failed ... on this build) SELECT roname, pg_replication_origin_progress(roname, false) FROM pg_replication_origin WHERE roname LIKE 'pg\_%\_%'; -- tablesync origin: NULL 6. Restart and watch it apply a=2 again Session A: SELECT pg_advisory_unlock(2); Session B: ALTER SUBSCRIPTION sub ENABLE; Expected in sub.log, repeating every few seconds: ERROR: conflict detected on relation "public.t": conflict=insert_exists DETAIL: Key already exists ... Key (a)=(2) ... SELECT srsubstate FROM pg_subscription_rel; stays at f, so the sync never finishes.
