pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Álvaro Herrera <alvherre@kurilemu.de>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Index pg_constraint.confrelid to avoid seqscans
Date: Thu, 12 Nov 2026 14:15:31 +0000
Message-ID: <E1xRVaN-00000000Ri2-224M@gemulon.postgresql.org> (raw)

Index pg_constraint.confrelid to avoid seqscans

Since commit 14e87ffa5c54 added pg_constraint rows for not-null
constraints, the pg_constraint seqscans in various operations became
slower, sometimes painfully so; pg_upgrade and ATTACH/DETACH PARTITION
have been reported as particularly problematic.  This new index should
speed up those operations.

Truncating tables involved in FK relationships also have a seqscan of
pg_constraint, but that's not modified by this patch because fixing that
case (heap_truncate_find_FKs()) is not as straightforward.

This new index, like the ones on conrelid and contypid, has a lot of
useless entries where the indexed column is just zeroes, both bloating
the index and slowing down operations that needn't be slowed down.
Someday it might be nice to avoid indexing those, but that's a larger
patch that requires more discussion.

Author: Manuel Reyes Bravo <manuelreyesbravo@gmail.com>
Reported-by: Bernhard Wonisch <bernhard.wonisch@gmx.at>
Reported-by: David Rowley <dgrowleyml@gmail.com>
Suggested-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de>
Discussion: https://postgr.es/m/trinity-08d3329b-f6b2-4c11-91d7-2198e50bf4fc-1790681922354@trinity-msg-rest-gmx-...
Discussion: https://postgr.es/m/1298334.1790896057@sss.pgh.pa.us
Discussion: https://postgr.es/m/CAApHDvpxGcb-_ddd+qL+F=Vmv4d3WZNbUCi3WQyj0HcFnC9v1g@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/b0496e30830b7cee6d17485404dd4c51d7aea286

Modified Files
--------------
src/backend/catalog/heap.c          |  5 ++--
src/backend/commands/tablecmds.c    | 46 ++++++++++++++++++++++---------------
src/include/catalog/catversion.h    |  2 +-
src/include/catalog/pg_constraint.h |  1 +
4 files changed, 33 insertions(+), 21 deletions(-)



Message-ID: <E1xRVaN-00000000Ri2-224M@gemulon.postgresql.org>
Permalink:  ../E1xRVaN-00000000Ri2-224M@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xRVaN-00000000Ri2-224M@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: alvherre@kurilemu.de
  Subject: Re: pgsql: Index pg_constraint.confrelid to avoid seqscans
  In-Reply-To: <E1xRVaN-00000000Ri2-224M@gemulon.postgresql.org>
  References: <E1xRVaN-00000000Ri2-224M@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox