Report
With streaming = parallel, REFRESH PUBLICATION WITH (copy_data = false) committing while a parallel apply worker applies a streamed transaction makes it skip the earlier changes to the new table but apply later ones (torn transaction; serial apply applies all)
- Severity
- 45
- Blamed commit
- 216a784829c2c5f03ab0c43e009126cbb819e9b2
On 5 branches
REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
A parallel apply worker decides per change whether to apply it, by
looking up the target table's pg_subscription_rel state each time the
table is not yet READY. A table that is not in the subscription yet
(UNKNOWN) is skipped silently; a READY table is applied. If
ALTER SUBSCRIPTION ... REFRESH PUBLICATION WITH (copy_data = false)
commits while a parallel apply worker is in the middle of a streamed
remote transaction, the new table goes straight from UNKNOWN to READY
mid-transaction. The worker then drops the transaction's earlier
changes to that table and applies its later ones, so one remote
transaction is applied only partly.
With streaming = on or off the same remote transaction is applied
as a whole at commit time, after the REFRESH, so all of its changes to
the new table are applied. The result therefore depends on the
streaming mode, and with parallel the subscriber silently diverges
from the publisher in a way that does not correspond to any
transaction boundary.
Reproduction
Publisher (wal_level = logical) and subscriber, both master.
Publisher:
ALTER SYSTEM SET debug_logical_replication_streaming = immediate;
SELECT pg_reload_conf();
CREATE TABLE t1 (a int PRIMARY KEY);
CREATE TABLE t2 (a int PRIMARY KEY);
CREATE PUBLICATION pub FOR TABLE t1;
Subscriber:
CREATE TABLE t1 (a int PRIMARY KEY);
CREATE TABLE t2 (a int PRIMARY KEY);
CREATE SUBSCRIPTION s1 CONNECTION '<publisher>'
PUBLICATION pub WITH (streaming = parallel);
-- wait until t1 is in state 'r' in pg_subscription_rel
Publisher:
ALTER PUBLICATION pub ADD TABLE t2;
Publisher, session A (keep the transaction open):
BEGIN;
INSERT INTO t2 VALUES (1);
INSERT INTO t1 VALUES (1);
Publisher, session B (any short committing transaction, so the WAL is flushed and the walsender streams session A's changes at once):
CREATE TEMP TABLE nudge (a int);
Wait about a second; the subscriber now has a parallel apply worker that has received INSERT t2 (1) (skipped, t2 is not in the subscription) and INSERT t1 (1).
Subscriber:
ALTER SUBSCRIPTION s1 REFRESH PUBLICATION WITH (copy_data = false);
Publisher, session A:
INSERT INTO t2 VALUES (2);
INSERT INTO t1 VALUES (2);
COMMIT;
After replication catches up:
-- publisher
SELECT array_agg(a ORDER BY a) FROM t2; -- {1,2}
-- subscriber
SELECT array_agg(a ORDER BY a) FROM t2; -- {2}
Expected: {1,2} (or neither row); both rows were written by one
remote transaction that committed after the REFRESH. Repeating with
streaming = on or streaming = off gives {1,2} on the subscriber.
Observed with streaming = parallel: {2}, every time (3/3).
debug_logical_replication_streaming = immediate only makes a small transaction stream; a transaction larger than logical_decoding_work_mem behaves the same.
Analysis
should_apply_changes_for_rel() in
src/backend/replication/logical/worker.c, case
WORKERTYPE_PARALLEL_APPLY, raises an error for states other than READY
and UNKNOWN, and otherwise returns rel->state == SUBREL_STATE_READY.
logicalrep_rel_open() in relation.c re-reads the state with
GetSubscriptionRelState() on every call while the entry is not READY,
and the parallel apply worker processes invalidations between stream
chunks (for example when it takes the stream lock), so it sees the
REFRESH commit in the middle of the remote transaction.
The comment above should_apply_changes_for_rel() anticipates REFRESH PUBLICATION adding a table while a streamed transaction is applied, and the error branch covers the copy_data = true case (state 'i'), where the worker stops and the transaction is re-applied. With copy_data = false the new row is inserted directly as READY (AlterSubscription_refresh()), which the parallel branch accepts, so there is no error and no retry: changes seen as UNKNOWN before the commit are dropped and the ones after it are applied. The leader (pa_can_start()) only checks the states before handing the transaction over, so it cannot catch a state change in the middle.
The serial paths do not have this problem in practice because the whole remote transaction (including a spooled streamed one) is applied at its commit.
Blame: 216a784829c2 (Perform apply of large transactions by parallel workers), which introduced the parallel apply branch of should_apply_changes_for_rel() with this UNKNOWN/READY handling.
Notes
Tested on master b7822f9992216f9aa85422337c15f1e7a8d6db72.
The same tearing would apply to UPDATE and DELETE changes to the new table in the streamed transaction.
Branches
Reproduced on master. The blamed commit is in REL_16_STABLE and every later branch; the release branches were not tested.