Use an index scan in RemoveSubscriptionRel()
Use an index scan in RemoveSubscriptionRel()
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. Manu
Re: Use an index scan in RemoveSubscriptionRel()
От:
Masahiko Sawada <sawada.mshk@gmail.com>
Дата:
On Mon, Nov 16, 2026 at 9:32 PM Manu 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
Re: Use an index scan in RemoveSubscriptionRel()
> On Nov 19, 2026, at 07:28, Masahiko Sawada wrote:
>
> On Mon, Nov 16, 2026 at 9:32 PM Manu 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).
The reordering is required.
I created a TAP script, see the attachment. The test passes with the reordering. But if we revert the reordering, the test fails with this error:
```
[15:08:45.242](0.000s) # psql::1: ERROR: btree index keys must be ordered by attribute
[15:08:45.253](0.011s) not ok 3 - refresh removes the subscription relation
[15:08:45.253](0.000s)
[15:08:45.253](0.000s) # Failed test 'refresh removes the subscription relation'
```
Then in _bt_preprocess_keys(), I found this check:
```
/* check input keys are correctly ordered */
if (i < numberOfKeys && inkey->sk_attno < attno)
elog(ERROR, "btree index keys must be ordered by attribute");
```
Then looking at systable_beginscan(), though it converts attribute numbers to index column numbers, it doesn't reorder the keys in the array. Without the reordering, the index column numbers are 2 and 1, which violates the above check in _bt_preprocess_keys().
But I still agree that the “Put srrelid first” comment is unnecessary.
> ---
> + /*
> + * 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.
>
+1
Other than the nitpicks about the code comments, the patch overall looks good to me.
BTW, I put the attached TAP script under src/test/subscription/t/.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Re: Use an index scan in RemoveSubscriptionRel()
On Wed, Nov 18, 2026 at 11:35 PM Chao Li wrote:
>
>
>
> > On Nov 19, 2026, at 07:28, Masahiko Sawada wrote:
> >
> > On Mon, Nov 16, 2026 at 9:32 PM Manu 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).
>
> The reordering is required.
You're right. I misunderstood the code.
> Other than the nitpicks about the code comments, the patch overall looks good to me.
Agreed.
I've attached the updated patch that includes the minor fix. I'm going
to push it, barring any objections.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
Re: Use an index scan in RemoveSubscriptionRel()
От:
Manu <manuelreyesbravo@gmail.com>
Дата:
Hi Sawada-san, On Thu, Nov 19, 2026 at 10:46 AM Masahiko Sawada wrote: > I've attached the updated patch that includes the minor fix. I'm going > to push it, barring any objections. Thanks, v2 looks good to me. On the test: without the reordering, the existing t/001_rep_changes.pl (SET PUBLICATION, line 491) and t/024_add_drop_pub.pl (DROP PUBLICATION, line 62) already fail with "btree index keys must be ordered by attribute", so the current suite covers it, as does Chao's test. The check is a plain elog(ERROR) in nbtpreprocesskeys.c, not an assertion, so it isn't limited to assert-enabled builds. Regards, Manu
Re: Use an index scan in RemoveSubscriptionRel()
От:
Chao Li <li.evan.chao@gmail.com>
Дата:
> On Nov 20, 2026, at 03:17, Manu wrote: > > Hi Sawada-san, > > On Thu, Nov 19, 2026 at 10:46 AM Masahiko Sawada > wrote: >> I've attached the updated patch that includes the minor fix. I'm going >> to push it, barring any objections. > > Thanks, v2 looks good to me. On the test: without the reordering, > the existing t/001_rep_changes.pl (SET PUBLICATION, line 491) and > t/024_add_drop_pub.pl (DROP PUBLICATION, line 62) already fail with > "btree index keys must be ordered by attribute", Sigh! I should have run the existing tests first. I did a manual test and found the issue. To better describe the test procedure, I converted it into a TAP test, which could have been completely avoided if I had run the existing tests first. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: Use an index scan in RemoveSubscriptionRel()
От:
Masahiko Sawada <sawada.mshk@gmail.com>
Дата:
On Thu, Nov 19, 2026 at 11:17 AM Manu wrote: > > Hi Sawada-san, > > On Thu, Nov 19, 2026 at 10:46 AM Masahiko Sawada > wrote: > > I've attached the updated patch that includes the minor fix. I'm going > > to push it, barring any objections. > > Thanks, v2 looks good to me. Thank you for reviewing the patch. Pushed. Regards, -- Masahiko Sawada Amazon Web Services: https://aws.amazon.com