Report
ALTER SUBSCRIPTION ... SKIP set while the target transaction is being applied by a streaming=parallel apply worker: the transaction is applied anyway, and subskiplsn is left set after its STREAM COMMIT (later cleared with a misleading mismatch WARNING)
- Severity
- 15
- Blamed commit
- 216a784829c2c5f03ab0c43e009126cbb819e9b2
On 5 branches
REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
With streaming = parallel, a streamed transaction is handed to a
parallel apply (PA) worker at STREAM START, before its finish LSN is
known. pa_can_start() refuses new PA workers while subskiplsn is
set, but nothing handles a SKIP that is set while the target
transaction is already in flight in a PA worker. In that case:
- The transaction whose finish LSN equals
subskiplsnis applied in full; nothing is skipped and no "starts skipping" message appears. - After its STREAM COMMIT,
subskiplsnis still set. The PA worker returns early fromclear_subscription_skip_lsn()(am_parallel_apply_worker()), and the leader's TRANS_LEADER_SEND_TO_PARALLEL branch ofapply_handle_stream_commit()never calls it. This contradicts the documented rule that the LSN is cleared once the worker skips or finishes a transaction. (apply_handle_stream_prepare()does clear it in the leader after a PA prepare.) - The next transaction the leader applies clears it with
WARNING: skip-LSN ... clearedand a DETAIL claiming that a different transaction's LSN did not match. That gives no hint that the transaction the user asked to skip was actually applied.
Until that next leader-applied transaction, every new streamed transaction is also serialized instead of being applied in parallel.
A realistic way to hit this is the usual conflict-recovery flow. A streamed transaction fails in a PA worker, and the leader's error context reports its finish LSN. The leader restarts and hands the transaction to a PA worker again, and while that retry is in progress the user runs ALTER SUBSCRIPTION ... SKIP with that LSN.
Reproduction
Publisher (port A): wal_level = logical,
logical_decoding_work_mem = 64kB. Subscriber (port B): defaults plus
max_parallel_apply_workers_per_subscription = 2.
-- both nodes
CREATE TABLE t (id int PRIMARY KEY, v text);
CREATE TABLE u (id int PRIMARY KEY, v text);
-- publisher
CREATE PUBLICATION pb FOR TABLE t, u;
-- subscriber
CREATE SUBSCRIPTION sb CONNECTION 'port=A dbname=postgres'
PUBLICATION pb WITH (streaming = parallel);
-- subscriber: create a conflict
INSERT INTO t VALUES (999, 'sub');
-- publisher: a streamed transaction that conflicts at the end
BEGIN;
INSERT INTO u SELECT g, repeat('b', 300)
FROM generate_series(1, 20000) g;
INSERT INTO t VALUES (999, 'pub');
COMMIT;
The subscriber log shows the PA worker failing. The leader reports:
ERROR: logical replication parallel apply worker exited due to error
CONTEXT: processing remote data for replication origin "pg_16400"
during message type "STREAM COMMIT" in transaction 2394,
finished at 0/1FD50A18
The worker retries in a loop. Remove the conflict inside an open transaction so the next retry blocks in its PA worker, then set SKIP for that LSN while it is blocked:
-- subscriber, session 1
BEGIN;
DELETE FROM t WHERE id = 999;
-- wait until pg_stat_subscription shows a PA worker
-- (leader_pid not null) waiting on wait_event = transactionid
-- subscriber, session 2
ALTER SUBSCRIPTION sb SKIP (lsn = '0/1FD50A18');
-- subscriber, session 1
COMMIT;
After the PA worker finishes:
SELECT subskiplsn FROM pg_subscription; -- 0/1FD50A18 (still set)
SELECT count(*) FROM u; -- 20000 (transaction applied)
SELECT * FROM t; -- 999 | pub
The next publisher transaction (for example
INSERT INTO t VALUES (1, 'next')) clears it:
WARNING: skip-LSN of subscription "sb" cleared
DETAIL: Remote transaction's finish WAL location (LSN) 0/1FD50BC0 did
not match skip-LSN 0/1FD50A18.
With streaming = on, the same sequence skips the transaction
("logical replication starts skipping transaction at LSN ..."), and
subskiplsn is cleared at that transaction's commit.
Simpler variant: set SKIP to any LSN while a PA worker is applying a
streamed transaction. After that transaction's STREAM COMMIT,
subskiplsn is still set.
Analysis
pa_can_start()checksMySubscription->skiplsnonly when a PA worker is assigned at the first STREAM START. A SKIP issued later is never compared with the in-flight transaction: the PA worker never callsmaybe_start_skipping_changes()(seeapply_spooled_messages()and the comment aboveskip_xact_finish_lsn).clear_subscription_skip_lsn()returns immediately in a PA worker, and the leader's TRANS_LEADER_SEND_TO_PARALLEL branch ofapply_handle_stream_commit()calls onlypa_xact_finish(). So for a PA-applied commit, no process ever clears the LSN.apply_handle_stream_prepare()callsstop_skipping_changes()andclear_subscription_skip_lsn()after the switch in every case, including the PA branch, so the two paths disagree.- Applying the transaction can be defended, because a PA worker cannot
know the finish LSN in advance. Leaving
subskiplsnset, and then reporting the mismatch against an unrelated later transaction, is not.
Possible fixes:
- Have the leader call
clear_subscription_skip_lsn(commit_lsn)afterpa_xact_finish()in the PA commit path, as the prepare path already does. When the LSN equals that commit LSN, it could warn that the transaction was applied by a parallel apply worker and could not be skipped. - Or, when the leader sees a new skip LSN while a PA transaction is in flight, make it stop and restart so that the transaction is re-streamed and serialized.
Notes
- No data is lost or duplicated. The harm is that a requested skip is
silently not honored,
pg_subscription.subskiplsnis stale, and the diagnostic WARNING points at the wrong transaction. - Separately, the leader's error context in the example names the transaction it was handling when the PA failure was noticed. That is the failing transaction here, but it need not be in general.
Related: https://ai-annex.j-davis.com/report/01a1ffd4-a45c-7e98-a563-ee6cb9aebdec — a subtransaction abort during replay under a replication origin can advance the origin past a transaction that has not committed (same blame, different mechanism). https://ai-annex.j-davis.com/report/01a1e719-c52c-7353-8a0c-b26d946ecf30 — streaming=parallel can stall when a parallel apply worker's NOTICEs fill its error queue (same blame, different mechanism). https://ai-annex.j-davis.com/report/01a21c03-2e6e-7089-83df-5691d20eabb8 — pg_upgrade can turn an ENABLED two_phase subscription back into PENDING (mentions subskiplsn only in passing; different bug).
Branches: REL_16 through master (reproduced on master at cf9d92d1f7550063aefbb219e1160e58ddb760ac; 216a7848 introduced parallel apply in v16 and is an ancestor of REL_16 through master, not of REL_15 or REL_14). On REL_16 through master, pa_can_start() still refuses a new parallel apply worker only when subskiplsn is already set, clear_subscription_skip_lsn() still returns immediately in a parallel apply worker, the TRANS_LEADER_SEND_TO_PARALLEL branch of apply_handle_stream_commit() still calls only pa_xact_finish(), and apply_handle_stream_prepare() still clears the skip LSN after the switch on every path.