From d8a16e8c640db8781729912089af366e16687d08 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Thu, 11 Dec 2025 15:08:00 +0800
Subject: [PATCH v1] tablecmds: Open pg_class only when an update is required

relation_mark_replica_identity() previously opened pg_class with
RowExclusiveLock unconditionally, even in cases where relreplident
did not change. This incurred unnecessary relation opens, lock
acquisition, and syscache lookups.

Refactor the code to defer opening pg_class until we know that an
update to relreplident is required. This reduces overhead on the
common path where no catalog modification is needed, without changing
any visible behavior.

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

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 1c9ef53be20..c2f34ceaa96 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -18393,7 +18393,6 @@ relation_mark_replica_identity(Relation rel, char ri_type, Oid indexOid,
 							   bool is_internal)
 {
 	Relation	pg_index;
-	Relation	pg_class;
 	HeapTuple	pg_class_tuple;
 	HeapTuple	pg_index_tuple;
 	Form_pg_class pg_class_form;
@@ -18403,7 +18402,7 @@ relation_mark_replica_identity(Relation rel, char ri_type, Oid indexOid,
 	/*
 	 * Check whether relreplident has changed, and update it if so.
 	 */
-	pg_class = table_open(RelationRelationId, RowExclusiveLock);
+
 	pg_class_tuple = SearchSysCacheCopy1(RELOID,
 										 ObjectIdGetDatum(RelationGetRelid(rel)));
 	if (!HeapTupleIsValid(pg_class_tuple))
@@ -18412,10 +18411,12 @@ relation_mark_replica_identity(Relation rel, char ri_type, Oid indexOid,
 	pg_class_form = (Form_pg_class) GETSTRUCT(pg_class_tuple);
 	if (pg_class_form->relreplident != ri_type)
 	{
+		Relation	pg_class = table_open(RelationRelationId, RowExclusiveLock);
+
 		pg_class_form->relreplident = ri_type;
 		CatalogTupleUpdate(pg_class, &pg_class_tuple->t_self, pg_class_tuple);
+		table_close(pg_class, RowExclusiveLock);
 	}
-	table_close(pg_class, RowExclusiveLock);
 	heap_freetuple(pg_class_tuple);
 
 	/*
-- 
2.39.5 (Apple Git-154)

