From: Manu <manuelreyesbravo@gmail.com>
Subject: [PATCH v1 2/3] Use GetNamespaceRelations() for schema publications

GetSchemaPublicationRelations() scans the whole of pg_class once per
published schema.  It runs on the publisher for CREATE PUBLICATION ...
FOR TABLES IN SCHEMA, ALTER PUBLICATION ... ADD/DROP TABLES IN SCHEMA,
SET (...) and RENAME, DROP PUBLICATION, and for every call of
pg_get_publication_tables() without a target relation, which CREATE
SUBSCRIPTION and ALTER SUBSCRIPTION ... REFRESH PUBLICATION issue.  With
many published schemas that is one full scan of pg_class per schema. Get
the tables and partitioned tables of the schema from
GetNamespaceRelations() instead.

---
diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c
index f11ea88..7447849 100644
--- a/src/backend/catalog/pg_publication.c
+++ b/src/backend/catalog/pg_publication.c
@@ -1228,33 +1228,35 @@ GetSchemaPublications(Oid schemaid)
 List *
 GetSchemaPublicationRelations(Oid schemaid, PublicationPartOpt pub_partopt)
 {
-	Relation	classRel;
-	ScanKeyData key[1];
-	TableScanDesc scan;
-	HeapTuple	tuple;
+	static const char pubkinds[] = {RELKIND_RELATION, RELKIND_PARTITIONED_TABLE, '\0'};
+	List	   *schemarels;
+	ListCell   *lc;
 	List	   *result = NIL;
 
 	Assert(OidIsValid(schemaid));
 
-	classRel = table_open(RelationRelationId, AccessShareLock);
-
-	ScanKeyInit(&key[0],
-				Anum_pg_class_relnamespace,
-				BTEqualStrategyNumber, F_OIDEQ,
-				ObjectIdGetDatum(schemaid));
+	/* get all the tables present in the specified schema */
+	schemarels = GetNamespaceRelations(schemaid, pubkinds);
 
-	/* get all the relations present in the specified schema */
-	scan = table_beginscan_catalog(classRel, 1, key);
-	while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
+	foreach(lc, schemarels)
 	{
-		Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple);
-		Oid			relid = relForm->oid;
+		Oid			relid = lfirst_oid(lc);
+		HeapTuple	tuple;
+		Form_pg_class relForm;
 		char		relkind;
+		bool		publishable;
 
-		if (!is_publishable_class(relid, relForm))
+		tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
+		if (!HeapTupleIsValid(tuple))
+			continue;
+		relForm = (Form_pg_class) GETSTRUCT(tuple);
+		publishable = is_publishable_class(relid, relForm);
+		relkind = relForm->relkind;
+		ReleaseSysCache(tuple);
+
+		if (!publishable)
 			continue;
 
-		relkind = get_rel_relkind(relid);
 		if (relkind == RELKIND_RELATION)
 			result = lappend_oid(result, relid);
 		else if (relkind == RELKIND_PARTITIONED_TABLE)
@@ -1268,13 +1270,12 @@ GetSchemaPublicationRelations(Oid schemaid, PublicationPartOpt pub_partopt)
 			 */
 			partitionrels = GetPubPartitionOptionRelations(partitionrels,
 														   pub_partopt,
-														   relForm->oid);
+														   relid);
 			result = list_concat_unique_oid(result, partitionrels);
 		}
 	}
 
-	table_endscan(scan);
-	table_close(classRel, AccessShareLock);
+	list_free(schemarels);
 	return result;
 }
 
