Re: Do we need an index on pg_constraint.confrelid?

From: Manu <manuelreyesbravo(at)gmail(dot)com>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: Álvaro Herrera <alvherre(at)kurilemu(dot)de>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Do we need an index on pg_constraint.confrelid?
Date: 2026-11-04 23:46:49
Message-ID: 179383600943.2830195.14527584186888213513@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi David,

On Sat, Oct 24, 2026 at 11:21 PM David Rowley
<dgrowleyml(at)gmail(dot)com> wrote:
> I'm only interested in discussion here, as there are
> obviously trade-offs to adding an index.

On your schema at full scale, pg_upgrade takes 1121 s on master and
about two minutes with the index, from v10 or from your patch alike:
both use it in the same three lookups. The price is on writes, since
every constraint gets an index entry, under zero if it is not a
foreign key: creating 1,000 tables with 100 NOT NULL columns each
takes 4.5% more instructions and 5.3% more WAL.

v10, attached, is just the plain index Tom suggested in [1].
heap_truncate_find_FKs() keeps its seqscan: v9-0002 probed the index
once per relation, and that made a commit with 1,000 ON COMMIT DELETE
ROWS tables 14-24% slower on a fresh database. The stats.sql check
now reads this backend's own counter inside a transaction, so a
parallel test can't hide a revert. catversion is not bumped.
Details and scripts attached.

[1] https://www.postgresql.org/message-id/1298334.1790896057%40sss.pgh.pa.us

Regards,
Manu

Attachment Content-Type Size
v10-0001-Index-pg_constraint.confrelid-to-avoid-seqscans-.patch text/x-patch 11.6 KB
measurements.txt text/plain 6.3 KB
david-repro.sh.txt text/plain 5.2 KB
ops-counts.sh.txt text/plain 7.8 KB
fk-lookup-counts.sh.txt text/plain 12.6 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Smith 2026-11-04 23:47:23 Re: BUG? Unpublished generated columns are reported "missing" by the subscription
Previous Message Ayoub Kazar 2026-11-04 23:18:19 Re: file_fdw: Fix ANALYZE crashes with DEFAULT markers