Re: ATTACH PARTITION cost grows linearly with pg_constraint size (seqscan in CloneFkReferenced), much worse since not-null constraints are in pg_constraint (PG 18)

From: Manu <manuelreyesbravo(at)gmail(dot)com>
To: Álvaro Herrera <alvherre(at)kurilemu(dot)de>
Cc: Bernhard Wonisch <bernhard(dot)wonisch(at)gmx(dot)at>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: ATTACH PARTITION cost grows linearly with pg_constraint size (seqscan in CloneFkReferenced), much worse since not-null constraints are in pg_constraint (PG 18)
Date: 2026-09-29 18:02:09
Message-ID: 179070492900.527107.8843747083878165117@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> If I recall correctly, there are other pg_constraint scans that could
> benefit from this index -- GetParentedForeignKeyRefs() at least; maybe
> others? I couldn't find anything in a quick grep.

Right. v2 attached points three confrelid scans at the index:

- CloneFkReferenced() on ATTACH PARTITION / PARTITION OF
- GetParentedForeignKeyRefs() on DETACH PARTITION
- ATPrepChangePersistence() on ALTER TABLE ... SET UNLOGGED

The third is the "maybe others": grepping Anum_pg_constraint_confrelid
turns up its else branch, which scans confrelid on SET UNLOGGED and
whose own comment already noted it had no usable index -- it does now.
The other two share CloneFkReferenced's shape, so they scan on confrelid
alone and filter contype in the loop.

On a catalog bloated to ~250k rows, each of the three drops from a
pg_constraint seqscan to an index scan (seq_scan delta 1-2 -> 0), and
ATTACH stays at the ~25 ms -> 0.3 ms per partition from before. make
check is clean.

> I mentioned the syscache because I think I wanted to add a syscache on
> top of such index for some reason.

No need for these three -- they all go through systable_beginscan, so a
plain index is enough; I left the syscache out.

> Hmm, I'm not eager to backpatch anything here, I'd rather go with a
> master-only solution.

Works for me -- a catalog change is master-only anyway.

--
Manu

Attachment Content-Type Size
v2-0001-Index-pg_constraint.confrelid-to-avoid-seqscans-i.patch text/x-patch 6.6 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message surya poondla 2026-09-29 18:28:03 Re: pg_xmin_horizon: a system view of everything pinning the xmin horizon
Previous Message Tomas Vondra 2026-09-29 17:57:23 Re: hashjoins vs. Bloom filters (yet again)