From f551c8a899775d1d09aa3c6beef8c6f784b187df Mon Sep 17 00:00:00 2001 From: Ajin Cherian Date: Thu, 26 Nov 2026 13:19:54 +1100 Subject: [PATCH v1 1/2] Report the leaf partition in apply worker errors When an UPDATE or DELETE is routed to a leaf partition on the subscriber, apply_handle_tuple_routing() calls check_relation_updatable() on the partition's map entry. That entry's updatable and idxisreplident fields are computed from the leaf partition, but the error messages use remoterel's name, which logicalrep_partition_open() copies from the partitioned table the change was published for. As a result, the errors name the partitioned table, not the partition that failed the check. This can be wrong as well as unhelpful. REPLICA IDENTITY is not inherited by partitions, so a partitioned table can have a REPLICA IDENTITY index while its partitions have none. An UPDATE routed to such a partition then reports that the partitioned table "has neither REPLICA IDENTITY index nor PRIMARY KEY". The "publisher did not send replica identity column" message has the same problem: the replica identity index it refers to belongs to the leaf partition. Report the local relation instead. For tables that are not reached through tuple routing, the local and remote names are the same, so those messages do not change. The error context still names the replication target relation, so for a partition both names appear. --- src/backend/replication/logical/worker.c | 13 ++++++--- src/test/subscription/t/013_partition.pl | 34 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c index 6d522573f90..56fe1d5d104 100644 --- a/src/backend/replication/logical/worker.c +++ b/src/backend/replication/logical/worker.c @@ -2816,14 +2816,20 @@ check_relation_updatable(LogicalRepRelMapEntry *rel) if (rel->updatable) return; - /* Use the entry, so this matches what updatable was decided from. */ + /* + * Report the local relation, since that is what updatable was decided + * from. For a leaf partition reached through tuple routing, remoterel + * holds the name of the partitioned table the change was published for, + * which is not the relation that failed the check. + */ if (rel->idxisreplident) { ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("publisher did not send replica identity column " "expected by the logical replication target relation \"%s.%s\"", - rel->remoterel.nspname, rel->remoterel.relname))); + get_namespace_name(RelationGetNamespace(rel->localrel)), + RelationGetRelationName(rel->localrel)))); } ereport(ERROR, @@ -2832,7 +2838,8 @@ check_relation_updatable(LogicalRepRelMapEntry *rel) "neither REPLICA IDENTITY index nor PRIMARY " "KEY and published relation does not have " "REPLICA IDENTITY FULL", - rel->remoterel.nspname, rel->remoterel.relname))); + get_namespace_name(RelationGetNamespace(rel->localrel)), + RelationGetRelationName(rel->localrel)))); } /* diff --git a/src/test/subscription/t/013_partition.pl b/src/test/subscription/t/013_partition.pl index 21c3a9e261b..efe06ca0cdc 100644 --- a/src/test/subscription/t/013_partition.pl +++ b/src/test/subscription/t/013_partition.pl @@ -897,6 +897,40 @@ $result = $node_subscriber2->safe_psql('postgres', "SELECT a, b, c FROM tab5_1 ORDER BY 1"); is($result, qq(4||1), 'updates of tab5 replicated correctly'); +# Test that when the leaf partition is not updatable, the error names the +# leaf partition, even though the partitioned target table has a REPLICA +# IDENTITY index. REPLICA IDENTITY is not inherited by partitions. +$node_subscriber2->safe_psql( + 'postgres', q{ + ALTER TABLE tab5 REPLICA IDENTITY USING INDEX tab5_a_idx; + ALTER TABLE tab5_1 REPLICA IDENTITY DEFAULT;}); + +$log_location = -s $node_subscriber2->logfile; + +$node_publisher->safe_psql('postgres', "UPDATE tab5 SET a = 5 WHERE a = 4"); + +$node_subscriber2->wait_for_log( + qr/ERROR: ( [A-Z0-9]+:)? logical replication target relation "[^"]+" has neither REPLICA IDENTITY index nor PRIMARY KEY/, + $log_location); + +$logfile = slurp_file($node_subscriber2->logfile(), $log_location); +like( + $logfile, + qr/logical replication target relation "public.tab5_1" has neither REPLICA IDENTITY index nor PRIMARY KEY/, + 'error names the leaf partition that has no replica identity'); + +# Fixing the leaf partition lets replication continue. +$node_subscriber2->safe_psql('postgres', + "ALTER TABLE tab5_1 REPLICA IDENTITY USING INDEX tab5_1_a_idx"); + +$node_publisher->wait_for_catchup('sub2'); + +$result = $node_subscriber2->safe_psql('postgres', + "SELECT a, b, c FROM tab5_1 ORDER BY 1"); +is($result, qq(5||1), + 'update of tab5 replicated after setting replica identity on the leaf partition' +); + # Validate we didn't neglect to cleanup any resources on either subscriber. foreach my $node ($node_subscriber1, $node_subscriber2) { -- 2.47.3