Logical replication apply caches the leaf-partition map entry by local OID only, so when two published tables route into the same subscriber leaf (e.g. "top" and its sub-partitioned child "mid"), UPDATEs via the second use the first one's column map and write values into the wrong columns ================================================================================================================================================================================================================================================================================================= AI-generated bug report from the AI Annex Report ID: 01a229ad-e7fc-7aea-85b1-ca377d1cbfe8 Filed: 2026-11-29 23:39:52 UTC Severity: 50/100 Blamed commit: f1ac27bfda6ce8a399d8001843e9aefff5814f9b ## Summary When the subscriber's target of a remote relation is a partitioned table, UPDATE and DELETE are applied through logicalrep_partition_open(), which keeps a cache entry per leaf partition in LogicalRepPartMap. The hash key is only the local leaf partition OID. The entry also holds the remote relation description (remoteid, attribute names) and the leaf-to-remote attribute map derived from the root entry it was first built from. If two different remote relations reach the same local leaf through different local partitioned tables, both use one entry. This happens when the subscriber has a multi-level hierarchy, say top -> mid -> leaf, and the publisher publishes separate tables named "top" and "mid". Once an UPDATE or DELETE for remote "top" has built the leaf entry, a later UPDATE for remote "mid" finds that entry (found && localrelvalid) and reuses it as is. slot_modify_data() then reads the new tuple of "mid" with the attribute map computed for "top". If the two remote tables order their columns differently, values go into the wrong subscriber columns without any error. If the swapped values break a partition constraint, the apply worker fails instead, with "new row for relation ... violates partition constraint". After the worker restarts with an empty cache, the same change applies correctly. ## Reproduction Publisher (wal_level = logical): CREATE TABLE top (k int PRIMARY KEY, x int, y int); CREATE TABLE mid (k int PRIMARY KEY, y int, x int); CREATE PUBLICATION p FOR TABLE top, mid; Subscriber: CREATE TABLE top (k int PRIMARY KEY, x int, y int) PARTITION BY LIST (k); CREATE TABLE mid PARTITION OF top FOR VALUES IN (1, 2) PARTITION BY LIST (k); CREATE TABLE leaf PARTITION OF mid FOR VALUES IN (1, 2); CREATE SUBSCRIPTION s CONNECTION 'port= dbname=postgres' PUBLICATION p; Wait for the initial sync, then on the publisher: INSERT INTO top VALUES (1, 10, 100); INSERT INTO mid (k, x, y) VALUES (2, 20, 200); -- wait for apply UPDATE top SET x = 11 WHERE k = 1; -- builds the leaf entry -- wait for apply UPDATE mid SET x = 21 WHERE k = 2; Publisher: SELECT k, x, y FROM mid; 2 | 21 | 200 Subscriber: SELECT tableoid::regclass, k, x, y FROM top ORDER BY k; leaf | 1 | 11 | 100 leaf | 2 | 200 | 21 -- expected 2 | 21 | 200 The INSERTs replicate correctly (they do not use the partition map). The UPDATE through "mid" swaps x and y. No error or conflict is logged. A variant with the partition key among the swapped columns (sub "mid" partitioned by a column that the swap changes) makes the apply worker fail with "new row for relation "mid" violates partition constraint" during message type "UPDATE" for target "public.mid". After the worker restarts, the change applies correctly because the first relation to use the leaf is now "mid". ## Analysis src/backend/replication/logical/relation.c, logicalrep_partition_open(): - The entry is looked up with hash_search(LogicalRepPartMap, &partOid) and returned immediately when found && entry->localrelvalid. Which root entry the caller passed (root->remoterel.remoteid) is never compared with the cached entry->remoterel.remoteid. - Even when the entry is rebuilt after an invalidation, "if (!entry->remoterel.remoteid)" keeps the remoterel copied from the first root. Only the attrmap is recomputed from the current root, so the entry can mix two remote relations. - logicalrep_partmap_reset_relmap() clears entries only on a RELATION message for the matching remoteid. It does not help when two remoteids map to the same leaf. apply_handle_tuple_routing() in worker.c passes the returned entry to slot_modify_data() (UPDATE) and to conflict reporting. The remote-to-local column mapping used for the new values therefore belongs to the wrong remote relation. The search tuple (remoteslot_part) is built from the correct root entry, so the row is still found. Only the new values are misplaced. The partition map, keyed by partition OID alone, was introduced in f1ac27bfda6 ("Add logical replication support to replicate into partitioned tables"). Later changes to the function (5a97b132545, 75bfe7434d4) kept the key and the early return. ## Notes Branches: master (reproduced at a68bb26f294c7f1280284f753b26ea7fb3f25964); REL_14 through REL_19 likely by code inspection (logicalrep_partition_open() has the same OID-only lookup and early return on every stable branch). Related (same blame, different root cause): https://ai-annex.j-davis.com/report/01a11f5c-0953-7fb0-82d0-c46ef875db47 Tested on master a68bb26f294c7f1280284f753b26ea7fb3f25964 (cassert build), with default replica identity (primary keys). Streaming is not needed.