On Mon, Nov 16, 2026 at 9:32 PM Manu <[email protected]> wrote:
>
> Hi,
>
> On a subscriber, every dropped relation scans all of
> pg_subscription_rel: RemoveSubscriptionRel() uses a heap scan although
> the (srrelid, srsubid) primary key serves the lookup by relid.  A table
> with a text column and an identity key pays it three times (the table,
> its TOAST table and its sequence).  The attached patch uses the index
> when relid is given; DROP SUBSCRIPTION keeps the heap scan.
>
> With 100,000 rows in pg_subscription_rel, dropping a schema of 1,000
> such tables takes 2.8 s on master and 0.23 s patched, and a transaction
> with an ON COMMIT DROP temporary table runs 45M instructions instead of
> 2.5M.  With an empty catalog the index costs 0.6% more instructions.
> If several subscriptions have the table in a non-ready state, the error
> now names the lowest OID rather than the first one in heap order.  The
> setup is synthetic; measurements.txt and the script are attached.

Thank you for the patch. +1 for the idea. Here are some comments:

---
+ /* Put srrelid first: index scan keys must be in index column order. */
+ if (OidIsValid(relid))
  {

The reordering is harmless but it's not actually required.
systable_beginscan() maps each scan key to its index column by
attribute number, so the order in the array doesn't matter. I'm fine
with keeping it for readability, but I think the comment should be
reworded (or just dropped).

---
+ /*
+ * 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);

The comments seem too detailed to me. The first half is clear from the
OidIsValid(relid) check, but I think the fallback part is worth
keeping. It's not obvious that DROP SUBSCRIPTION can't use this index
(srsubid is the second column), so it has to fall back to a heap scan.

Regards,

-- 
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com


Reply via email to