Report
Logical replication sends an UPDATE/DELETE with no replica identity key columns, and the subscriber stops with "publisher did not send replica identity column", when the publisher drops the primary key or identity index and VACUUM clears relhasindex in place before the change is decoded
- Severity
- 45
- 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
pgoutput builds the Relation message's replica identity key flags with RelationGetIdentityKeyBitmap(), which returns "no key" at once when pg_class.relhasindex is false. relhasindex is changed in place by VACUUM, so the historic snapshot used by logical decoding sees the current value, not the value at the decoded change. If the primary key (or replica identity index) is dropped and the table is vacuumed before an earlier UPDATE or DELETE has been decoded, pgoutput sends that change with no key columns. The subscriber then stops with:
ERROR: publisher did not send replica identity column expected
by the logical replication target relation "public.e"
and retries forever. Without the VACUUM the same change is decoded with the key flag set and applied normally.
Reproduction
Single node, wal_level = logical:
CREATE TABLE r (a int PRIMARY KEY, b int);
INSERT INTO r VALUES (1, 1);
CREATE PUBLICATION pub_r FOR TABLE r;
SELECT pg_create_logical_replication_slot('s', 'pgoutput');
UPDATE r SET b = 2 WHERE a = 1;
ALTER TABLE r DROP CONSTRAINT r_pkey;
-- byte 17 of the Relation message is the flags byte of column a
SELECT get_byte(data, 17)
FROM pg_logical_slot_peek_binary_changes('s', NULL, NULL,
'proto_version', '1', 'publication_names', 'pub_r')
WHERE get_byte(data, 0) = ascii('R');
-- 1 (key), correct
VACUUM r; -- sets relhasindex = false in place
SELECT get_byte(data, 17)
FROM pg_logical_slot_peek_binary_changes('s', NULL, NULL,
'proto_version', '1', 'publication_names', 'pub_r')
WHERE get_byte(data, 0) = ascii('R');
-- 0: the same UPDATE is now sent with no key column
With a subscriber (table e (a int PRIMARY KEY, b int) on both sides, subscription created and synced, then disabled):
-- publisher
UPDATE e SET b = 20 WHERE a = 2;
ALTER TABLE e DROP CONSTRAINT e_pkey;
VACUUM e;
-- subscriber
ALTER SUBSCRIPTION sub_e ENABLE;
Expected: the subscriber applies the UPDATE (row 2|20), as it does when the VACUUM is left out. Actual: the apply worker fails with the error above on every retry and the row stays 2|2.
Analysis
logicalrep_write_attrs() in proto.c calls RelationGetIdentityKeyBitmap() (relcache.c), which starts with:
/* Fast path if definitely no indexes */
if (!RelationGetForm(relation)->relhasindex)
return NULL;
Under a historic snapshot this is not "definitely no indexes". vac_update_relstats() clears relhasindex with an inplace update when the table currently has no index. Inplace updates have no snapshot-specific versions, so the pg_class tuple read through the historic snapshot already carries relhasindex = false while the pg_index row of the dropped primary key is still visible to that snapshot. RelationGetIndexList() would find the index; the fast path skips it. The README.tuplock section on reading inplace-updated columns during decoding only covers relhasindex = true being seen early, which is harmless; relhasindex = false being seen early is not.
The fast path under a historic snapshot dates from the introduction of logical replication, where logicalrep_write_attrs() used RelationGetIndexAttrBitmap(), which has the same relhasindex check. Commit e7eea52b2d61 later moved the call to the new RelationGetIdentityKeyBitmap() and kept the check.
Notes
Tested on master at dc168a83874fbf5cee4afb76a623e1349a539724.
The same happens for REPLICA IDENTITY USING INDEX when that index is dropped, and for DELETE. Autovacuum can clear relhasindex just as well as a manual VACUUM, so a lagging subscriber is enough to hit it.
Branches
The relhasindex fast path dates from 665d1fad99e7 and is on every supported branch (REL_14_STABLE through master; since e7eea52b2d61 in RelationGetIdentityKeyBitmap()). Reproduced on master only; back branches not yet tested.
Related
- 01a11f5c-052d-7791-8780-0c2b056ede90 (dropped REPLICA IDENTITY USING INDEX leaves relreplident = 'i'; different mechanism)
- 01a228f1-5cf3-766a-a3e2-b35b2fcd1f7d (REPLICA IDENTITY FULL updates not applied with subscriber-only columns; different mechanism)