From 706d7cb4b3ac7f30f131c64f859cabe00773b07d Mon Sep 17 00:00:00 2001
From: Zhijie Hou <houzj.fnst@fujitsu.com>
Date: Tue, 31 Mar 2026 19:35:44 +0800
Subject: [PATCH v1] refactor the function

---
 src/backend/catalog/pg_publication.c | 79 +++++++++-------------------
 1 file changed, 26 insertions(+), 53 deletions(-)

diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c
index 14ae03fc0ff..a036cd8ff33 100644
--- a/src/backend/catalog/pg_publication.c
+++ b/src/backend/catalog/pg_publication.c
@@ -1312,75 +1312,48 @@ static bool
 is_table_publishable_in_publication(Oid relid, Publication *pub)
 {
 	bool		relispartition;
+	List	   *ancestors = NIL;
+	Oid			topmost = InvalidOid;
 
 	/*
 	 * For non-pubviaroot publications, a partitioned table is never the
 	 * effective published OID; only its leaf partitions can be.
 	 */
-	if (!pub->pubviaroot && get_rel_relkind(relid) == RELKIND_PARTITIONED_TABLE)
+	if (!pub->pubviaroot &&
+		get_rel_relkind(relid) == RELKIND_PARTITIONED_TABLE)
 		return false;
 
 	relispartition = get_rel_relispartition(relid);
 
-	if (pub->alltables)
-	{
-		Oid			target_relid = relid;
-
-		if (pub->pubviaroot)
-		{
-			/*
-			 * ALL TABLES with pubviaroot includes only regular tables or
-			 * top-most partitioned tables -- never child partitions.
-			 */
-			if (relispartition)
-				return false;
-		}
-		else if (relispartition)
-		{
-			List	   *ancestors = get_partition_ancestors(relid);
+	/*
+	 * ALL TABLES with pubviaroot includes only regular tables or
+	 * top-most partitioned tables -- never child partitions.
+	 */
+	if (pub->alltables && pub->pubviaroot && relispartition)
+		return false;
 
-			/*
-			 * Only the top-most ancestor can appear in the EXCEPT clause.
-			 * Therefore, for a partition, exclusion must be evaluated at the
-			 * top-most ancestor.
-			 */
-			target_relid = llast_oid(ancestors);
-			list_free(ancestors);
-		}
+	if (relispartition)
+		ancestors = get_partition_ancestors(relid);
 
-		/*
-		 * The table is published unless it appears in the EXCEPT clause. ALL
-		 * TABLES publications store only EXCEPT'ed tables in
-		 * pg_publication_rel, so checking existence is sufficient.
-		 */
+	/*
+	 * The table is published unless it appears in the EXCEPT clause. ALL
+	 * TABLES publications store only EXCEPT'ed tables in
+	 * pg_publication_rel, so checking existence is sufficient.
+	 */
+	if (pub->alltables)
 		return !SearchSysCacheExists2(PUBLICATIONRELMAP,
-									  ObjectIdGetDatum(target_relid),
+									  ObjectIdGetDatum(ancestors
+													   ? llast_oid(ancestors) : relid),
 									  ObjectIdGetDatum(pub->oid));
-	}
 
 	/*
-	 * Non-alltables
+	 * If pubviaroot is true, the ancestor is published instead of the
+	 * partition, so exclude it. Otherwise, the ancestor covers the partition,
+	 * so include it.
 	 */
-
-	if (relispartition)
-	{
-		List	   *ancestors = get_partition_ancestors(relid);
-		Oid			topmost = GetTopMostAncestorInPublication(pub->oid, ancestors, NULL);
-
-		list_free(ancestors);
-
-		if (OidIsValid(topmost))
-		{
-			/*
-			 * If pubviaroot is true, the ancestor is published instead of the
-			 * partition, so exclude it. Otherwise, the ancestor covers the
-			 * partition, so include it.
-			 */
-			return !pub->pubviaroot;
-		}
-
-		/* Ancestor not published; fall through to check the partition itself */
-	}
+	if (relispartition &&
+		OidIsValid(GetTopMostAncestorInPublication(pub->oid, ancestors, NULL)))
+		return !pub->pubviaroot;
 
 	/*
 	 * Check whether the table is explicitly published via pg_publication_rel
-- 
2.53.0.windows.2

