From c1f46d8cc37cd39e125a3cd1dab10b4023bda913 Mon Sep 17 00:00:00 2001
From: Manuel Reyes Bravo <manuelreyesbravo@gmail.com>
Date: Mon, 16 Nov 2026 19:41:27 -0300
Subject: [PATCH v1] Use an index scan in RemoveSubscriptionRel()

heap_drop_with_catalog() calls RemoveSubscriptionRel() for every
relation it drops, including TOAST tables and sequences, and the
function scanned all of pg_subscription_rel each time, even though the
primary key index on (srrelid, srsubid) serves a lookup by relid.  On a
subscriber with many rows in pg_subscription_rel, every DROP TABLE and
every ON COMMIT DROP temporary table paid for that scan.  With 100,000
rows, dropping 1,000 tables with a text column and an identity primary
key took 66 billion instructions instead of 2.3 billion.

Use the index whenever relid is given.  DROP SUBSCRIPTION passes only a
subscription OID, which this index cannot serve, so it keeps scanning
the heap.  When several subscriptions have the relation in a state
other than ready, the error now names the one with the lowest OID
instead of the first one found in the heap.

Discussion:
---
 src/backend/catalog/pg_subscription.c | 28 ++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/src/backend/catalog/pg_subscription.c b/src/backend/catalog/pg_subscription.c
index 60c15b2..50005ce 100644
--- a/src/backend/catalog/pg_subscription.c
+++ b/src/backend/catalog/pg_subscription.c
@@ -520,34 +520,40 @@ void
 RemoveSubscriptionRel(Oid subid, Oid relid)
 {
 	Relation	rel;
-	TableScanDesc scan;
+	SysScanDesc scan;
 	ScanKeyData skey[2];
 	HeapTuple	tup;
 	int			nkeys = 0;
 
 	rel = table_open(SubscriptionRelRelationId, RowExclusiveLock);
 
-	if (OidIsValid(subid))
+	/* Put srrelid first: index scan keys must be in index column order. */
+	if (OidIsValid(relid))
 	{
 		ScanKeyInit(&skey[nkeys++],
-					Anum_pg_subscription_rel_srsubid,
+					Anum_pg_subscription_rel_srrelid,
 					BTEqualStrategyNumber,
 					F_OIDEQ,
-					ObjectIdGetDatum(subid));
+					ObjectIdGetDatum(relid));
 	}
 
-	if (OidIsValid(relid))
+	if (OidIsValid(subid))
 	{
 		ScanKeyInit(&skey[nkeys++],
-					Anum_pg_subscription_rel_srrelid,
+					Anum_pg_subscription_rel_srsubid,
 					BTEqualStrategyNumber,
 					F_OIDEQ,
-					ObjectIdGetDatum(relid));
+					ObjectIdGetDatum(subid));
 	}
 
-	/* Do the search and delete what we found. */
-	scan = table_beginscan_catalog(rel, nkeys, skey);
-	while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
+	/*
+	 * Do the search and delete what we found.  Use the (srrelid, srsubid)
+	 * index when relid is given; otherwise (DROP SUBSCRIPTION) there is no
+	 * suitable index, so fall back to a heap scan.
+	 */
+	scan = systable_beginscan(rel, SubscriptionRelSrrelidSrsubidIndexId,
+							  OidIsValid(relid), NULL, nkeys, skey);
+	while (HeapTupleIsValid(tup = systable_getnext(scan)))
 	{
 		Form_pg_subscription_rel subrel;
 
@@ -584,7 +590,7 @@ RemoveSubscriptionRel(Oid subid, Oid relid)
 
 		CatalogTupleDelete(rel, &tup->t_self);
 	}
-	table_endscan(scan);
+	systable_endscan(scan);
 
 	table_close(rel, RowExclusiveLock);
 }
-- 
2.56.1

