Report
With REPLICA IDENTITY FULL, UPDATE and DELETE are never applied to rows whose subscriber-only column (one the publisher does not have, e.g. added with a DEFAULT) is not NULL; tuples_equal() compares it against NULL and reports update_missing/delete_missing
- Severity
- 55
- Blamed commit
- 665d1fad99e7b11678b0d5fa24d2898424243cd6
On 11 branches
REL_10_STABLE, REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
The documentation says a subscriber table "can also have additional columns not provided by the published table" and that they are filled with their default. When the published table uses REPLICA IDENTITY FULL, the apply worker compares every non-dropped, non-generated column of the local row with the remote old tuple. For a subscriber-only column the remote tuple has no value, so the search slot holds NULL there, and any local row whose value in that column is not NULL never matches. Every UPDATE and DELETE of such rows is skipped and reported as update_missing or delete_missing; the subscriber silently diverges.
INSERTs are replicated, so the table looks healthy until rows are changed on the publisher.
Reproduction
Publisher (wal_level = logical):
CREATE TABLE t (a int, b int);
ALTER TABLE t REPLICA IDENTITY FULL;
INSERT INTO t SELECT g, g FROM generate_series(1, 5) g;
CREATE PUBLICATION p1 FOR TABLE t;
Subscriber:
CREATE TABLE t (a int, b int, c int DEFAULT 7);
CREATE SUBSCRIPTION s1
CONNECTION 'dbname=postgres port=<publisher port>'
PUBLICATION p1;
After the initial sync (subscriber rows are (g, g, 7)), on the publisher:
UPDATE t SET b = b + 100 WHERE a = 1;
DELETE FROM t WHERE a = 2;
Expected: subscriber has (1,101,7), (3,3,7), (4,4,7), (5,5,7).
Actual: subscriber still has (1,1,7) and (2,2,7); the log shows
conflict detected on relation "public.t": conflict=update_missing
conflict detected on relation "public.t": conflict=delete_missing
The result is the same with a usable index on the subscriber (e.g. CREATE INDEX ON t (a) or ON t (a, c)). If c is left NULL (no default) the changes are applied, which shows the comparison is against NULL.
Analysis
slot_store_data() (replication/logical/worker.c) fills only columns that have a remote counterpart (attrmap entry >= 0) and leaves the rest NULL. RelationFindReplTupleSeq() and, for a non-replica-identity index, RelationFindReplTupleByIndex() (executor/execReplication.c) then call tuples_equal(), which skips only dropped and generated columns. The subscriber-only column is compared: the local value is not NULL, the search value is NULL, so the rows are "certainly not equal". The same comparison is used for update_deleted detection (RelationFindDeletedTupleInfoSeq/ByIndex).
The whole-row comparison over all local attributes dates back to the original logical replication commit 665d1fad99e (tuple_equals_slot()); later commits b797def5951 and adedf54e659 excluded dropped and generated columns for the same reason but not columns that the publisher simply does not have.
Notes
- Tested on master c1dd701fb5f866fbab78dcd33b184e2fb7a4782d.
- When the publisher table has a primary key or replica identity index, extra subscriber columns are not compared and changes apply normally; only REPLICA IDENTITY FULL is affected.
- Nothing in the replica identity or column-mapping documentation says that additional subscriber columns are incompatible with REPLICA IDENTITY FULL.
- Branches: master (reproduced at c1dd701fb5f8); REL_14 through REL_19 likely by code inspection (tuples_equal() in execReplication.c on every branch skips only dropped and generated columns).
- Related: https://ai-annex.j-davis.com/report/01a228f0-f7d7-7a19-8c16-f444e1a63dae (index scan key on a subscriber-only stored generated column; different code path and blame).