Report
Transient "replication origin ... is already active for PID" race during tablesync worker restart disables the subscription when disable_on_error = true
- Severity
- 30
- Blamed commit
- 705e20f8550c0e8e47c0b6b20b5f5ffd6ffd9e33
On 6 branches
REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
When a tablesync worker exits, logicalrep_worker_onexit() (a before_shmem_exit callback) frees its LogicalRepWorker slot in logicalrep_worker_detach() first. After that it deletes the stream fileset (the spool files), releases locks and wakes the launcher. The worker's replication origin is released only later, by the on_shmem_exit callback ReplicationOriginExitCleanup(). Once the slot is free, the apply worker may start a replacement tablesync worker for the same relation. If that worker reaches replorigin_session_setup() in LogicalRepSyncTableStart() (the FINISHEDCOPY restart path) before the old process has finished exiting, it fails with
ERROR: replication origin with ID N is already active for PID <old>
This call is inside start_table_sync()'s PG_TRY block. With disable_on_error = true, the PG_CATCH branch calls DisableSubscriptionAndExit(), so a purely transient internal race permanently disables the subscription. Without disable_on_error the worker just retries and the error is harmless.
The leader apply worker has the same window (seen often: 11 and 25 errors in 25 forced restarts each, with streaming = on and streaming = parallel). Its replorigin_session_setup() call in run_apply_worker() is outside start_apply()'s PG_TRY, so it only logs and retries; data converged in every run.
Reproduction
Publisher: wal_level = logical, debug_logical_replication_streaming = immediate.
Subscriber: wal_retrieve_retry_interval = 50ms.
- Publisher:
CREATE TABLE c(id int PRIMARY KEY, v int), 1000 rows,CREATE PUBLICATION pc FOR TABLE c. - Subscriber: the same table plus an
ENABLE ALWAYSBEFORE INSERT trigger that, only in the tablesync worker, sleeps whileNEW.id >=a value in a control table (used to hold the tablesync worker in catchup, state 'f').CREATE SUBSCRIPTION s ... WITH (disable_on_error = true, streaming = on). - Loop: on the publisher open about 30 transactions that each insert 200 rows into
c(streamed, so the tablesync worker spools them), then insert the next gate row; advance the gate so the tablesync worker spools and blocks; lower the tablesync worker's CPU priority under CPU load;pg_terminate_backend()it; commit or roll back the open transactions. - After some restarts (2 hits in about 35 kills) the subscriber log shows the "already active for PID" ERROR from the tablesync worker, followed by the subscription being disabled (
pg_subscription.subenabled = false).ALTER SUBSCRIPTION ... ENABLErecovers and the data converge.
The CPU-priority change, the load and the spool files only widen the window between slot release and origin release; any delay there (a loaded host, many or large spool files to unlink) has the same effect.
Observed subscriber log (second hit; PIDs replaced):
tablesync worker [A] FATAL: terminating logical replication worker due to administrator command
tablesync worker [B] ERROR: replication origin with ID 2 is already active for PID A (175 ms later)
tablesync worker [B] LOG: subscription "s" has been disabled because of an error
apply worker LOG: logical replication worker for subscription "s" will stop because the subscription was disabled
Analysis
The slot is released before the origin, so "no worker for this relation" in shared memory does not imply that the origin is free. The tablesync worker connects to the publisher before the origin setup, which narrows the window but does not close it.
Possible fixes: release the session origin in logicalrep_worker_onexit() before logicalrep_worker_detach(); or have replorigin_session_setup() wait (on origin_cv) for the previous owner when the owner is a no-longer-attached worker of the same subscription; or at least keep this error out of the disable_on_error path. logicalrep_worker_onexit() also reads MyLogicalRepWorker->stream_fileset after the slot has been freed (and possibly reused, as logicalrep_worker_launch() resets it); that may be worth reordering at the same time.
Notes
Data checks (publisher vs subscriber count, distinct count and hash sum) matched after every run, including after re-enabling. No duplicated or lost rows and no wrong origin LSN were observed. No leftover spool files. The parallel apply "already active in another process" variant was not observed.
Branches
Reproduced on master (026b3b282a4a18801d22b85509dbf6868f48168e, cassert, -O0); probabilistic. The blamed commit 705e20f8550c0e8e47c0b6b20b5f5ffd6ffd9e33 (disable_on_error) is in REL_15_STABLE and later; back branches were not tested.
Related
- 01a22d2f-bff3-7841-9c19-10a509eedd5c: another transient error that disable_on_error turns into a disabled subscription.
- 01a200e4-245f-762b-9680-b45de74089eb: replorigin_session_setup() does not wait for a concurrent origin owner change (origin drop).