Report
Logical replication apply of an UPDATE or DELETE whose row is missing on the subscriber fails with "new row for relation ... violates partition constraint" (and disable_on_error disables the subscription) when ATTACH PARTITION into a root with a default partition commits concurrently
- Severity
- 25
- Blamed commit
- f481d2823297e692e9d44c9edfd18e626e768b0c
On 7 branches
REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
When the subscriber's target table is partitioned and has a DEFAULT partition, a replicated UPDATE or DELETE whose row does not exist on the subscriber should be reported as an update_missing / delete_missing conflict (LOG) and skipped. If an ALTER TABLE ... ATTACH PARTITION that carves the row's key range out of the default partition commits while the apply worker is routing the remote "search" tuple, the apply worker instead fails with
ERROR: new row for relation "t_def" violates partition constraint
even for a DELETE. The worker exits; with disable_on_error = true the subscription is disabled. The same UPDATE or DELETE run as SQL through the root in the same race does not error (it affects 0 rows).
Reproduction
Publisher (wal_level = logical):
CREATE TABLE t (a int PRIMARY KEY, b int);
CREATE PUBLICATION p FOR TABLE t;
INSERT INTO t VALUES (1, 0), (150, 0);
Subscriber:
CREATE TABLE t (a int PRIMARY KEY, b int) PARTITION BY RANGE (a);
CREATE TABLE t_lo PARTITION OF t FOR VALUES FROM (0) TO (100);
CREATE TABLE t_def PARTITION OF t DEFAULT;
CREATE SUBSCRIPTION s CONNECTION '<publisher conninfo>'
PUBLICATION p WITH (disable_on_error = true);
-- wait for the initial sync, then make row 150 missing locally
DELETE FROM t WHERE a = 150;
CREATE TABLE t_new (LIKE t);
Subscriber, session 1:
BEGIN;
ALTER TABLE t ATTACH PARTITION t_new FOR VALUES FROM (100) TO (200);
SELECT pg_sleep(4);
COMMIT;
Publisher, while session 1 sleeps:
UPDATE t SET b = 1 WHERE a = 150;
-- or: DELETE FROM t WHERE a = 150;
The apply worker waits for RowExclusiveLock on t_def (held by the ATTACH), and once session 1 commits:
ERROR: new row for relation "t_def" violates partition constraint
DETAIL: Failing row contains (150, 1).
CONTEXT: processing remote data for replication origin "pg_NNNNN"
during message type "UPDATE" for replication target relation
"public.t" in transaction NNN, finished at X/XXXXXXXX
LOG: subscription "s" has been disabled because of an error
For DELETE the DETAIL is "Failing row contains (150, null)".
SELECT subenabled FROM pg_subscription WHERE subname = 's';
-- f
Expected: an update_missing (or delete_missing) conflict is logged and replication continues, as happens without the concurrent ATTACH.
For comparison, on the subscriber with the same session 1 running, plain SQL
DELETE FROM t WHERE a = 150;
UPDATE t SET b = 1 WHERE a = 150;
wait for the ATTACH and then complete with 0 rows and no error; only INSERT raises the partition constraint error in this race, which is the intended behavior for a new row.
Under a loop of ATTACH/DETACH of such a partition while the publisher updates rows missing on the subscriber, the apply worker exited with this error 41 times in 40 seconds.
Analysis
apply_handle_tuple_routing() (worker.c) uses ExecFindPartition() to locate the leaf partition for the remote search tuple of an UPDATE or DELETE, i.e. to find where an existing row would be, not to insert a new one. ExecFindPartition() (execPartition.c) rechecks the partition constraint of a chosen DEFAULT partition, because its constraint may have changed due to a concurrently attached partition, and raises ERRCODE_CHECK_VIOLATION "new row for relation ... violates partition constraint" via ExecPartitionCheck(..., emitError = true).
The partition descriptor the apply worker routes with is built before the ATTACH commits (ATTACH holds only ShareUpdateExclusiveLock on the parent), so the key is routed to the default partition; opening it waits for the ATTACH's AccessExclusiveLock; after the commit the default partition's new constraint excludes the key, and the recheck errors out. That recheck is right for INSERT routing, but for the search tuple of an UPDATE/DELETE it turns what should be a LOG-level update_missing/delete_missing conflict into an ERROR, and the message speaks of a "new row".
The recheck was added to ExecFindPartition() by f481d2823297 ("Check default partitions constraints while descending"); the apply worker had been routing UPDATE/DELETE search tuples through ExecFindPartition() since f1ac27bfda6 (logical replication into partitioned tables), so from f481d2823297 on the apply path is subject to an INSERT-only check.
Notes
- Tested on master 03dd453a67cfb37e440f998eb1a61b643e037591.
- Reproduces with and without conflict_log_destination = 'table', and for a small (non-streamed) remote transaction.
- A retry after the worker restart routes correctly, so without disable_on_error the effect is a worker exit and restart.
- Branches: master (reproduced at 03dd453a67cf); REL_14 through REL_19 likely by code inspection (the default-partition recheck in ExecFindPartition() and the apply worker's use of it for UPDATE/DELETE search tuples are present on all of them).