From ef094e7eba68c0d97ba1f0b46a61db376f4e8a56 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Thu, 11 Dec 2025 17:03:37 +0800
Subject: [PATCH v1] tablecmds: Cascade REPLICA IDENTITY changes to leaf
 partitions

ALTER TABLE ... REPLICA IDENTITY currently updates only the
partitioned table itself, even though partitioned tables have no
storage and their replica identity is never used by logical
replication.  The replica identity that matters for replication is the
setting on each leaf partition, but users must alter those tables
individually today.

Extend ATExecReplicaIdentity() so that when invoked on a partitioned
table, the requested replica identity is applied automatically to all
leaf partitions.  This makes replica identity configuration consistent
with how publications automatically include leaf partitions, and avoids
silent misconfigurations where only some partitions can safely
replicate UPDATE or DELETE operations.

Author: Chao Li <lic@highgo.com>
---
 src/backend/commands/tablecmds.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 1c9ef53be20..ba4f3331870 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -18482,6 +18482,25 @@ ATExecReplicaIdentity(Relation rel, ReplicaIdentityStmt *stmt, LOCKMODE lockmode
 	Oid			indexOid;
 	Relation	indexRel;
 	int			key;
+	List	   *partRelIds = NIL;
+
+	if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
+	{
+		/* Also apply to partitions, if any */
+		partRelIds = GetPubPartitionOptionRelations(partRelIds, PUBLICATION_PART_LEAF,
+													RelationGetRelid(rel));
+		if (list_length(partRelIds) > 0)
+		{
+			foreach_oid(partRelOid, partRelIds)
+			{
+				Relation	partRel;
+
+				partRel = relation_open(partRelOid, lockmode);
+				ATExecReplicaIdentity(partRel, stmt, lockmode);
+				relation_close(partRel, lockmode);
+			}
+		}
+	}
 
 	if (stmt->identity_type == REPLICA_IDENTITY_DEFAULT)
 	{
-- 
2.39.5 (Apple Git-154)

