Re: Should we skip foreign key checks in binary upgrade mode?

From: Amit Langote <amitlangote09(at)gmail(dot)com>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Should we skip foreign key checks in binary upgrade mode?
Date: 2026-11-25 01:20:29
Message-ID: CA+HiwqE_q1z_X30ZNh8-Cxw=0GAPMGkwD8PKPQXpUcft719F5A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Nov 25, 2026 at 7:11 AM David Rowley <dgrowleyml(at)gmail(dot)com> wrote:
> I've been investigating some internally reported pg_upgrade slowness.
> The schema I was given (attached) creates a few highly partitioned
> tables and foreign keys that reference some partitioned tables.
>
> A big part of pg_upgrade slowness is the schema restore on the target
> instance. The problem from the SPI call for the foreign key check
> planning and executing the query to the referenced partitioned table.
> There's quite a bit of planner and executor startup/shutdown work when
> there's lots of partitions.
>
> What I'm wondering now is if we should just skip the foreign key
> checks when IsBinaryUpgrade == true. We expect all tables to be empty
> in that case, so I'm not sure there's any point in doing the checks.
> One problem might be that, in theory, someone could run postgres
> directly without using pg_ctl and pass the -b flag to run in binary
> upgrade mode and create a valid foreign key with violations.
>
> Another thing I considered was if we were to modify pg_dump to put the
> foreign key constraints inside the CREATE TABLE statements. In that
> case, we don't do the validation because the table is empty. The
> problem there is dependencies. The referenced table must exist, and
> the unique/PK index must exist too. There's also a problem with
> circular FKs where tables reference each other. i.e. probably a
> nightmare and a constant source of bug reports.

Have you thought about checking directly whether the table (leaf
partition) is empty, i.e. replace the !IsBinaryUpgrade in your patch
with RelationGetNumberOfBlocks(rel) != 0? That wouldn't have the -b
problem you mention, since a non-empty table would still be checked.
It also avoids adding the global flag check to yet another site. Not
sure how much to worry about table AMs for which that might not be
accurate though.

> The test case I'm working with is attached. Running pg_upgrade on master with:
>
> pg_upgrade -d pgdata -D pgdata_new -b ~/pg/bin -B ~/pg/bin | ts
> '%Y-%m-%dT%H:%M:%S%z'
>
> master:
>
> 2026-11-24T18:20:21+1300 Restoring global objects in the new cluster
> ok
> 2026-11-24T18:35:26+1300 Restoring database schemas in the new cluster
> ok (15:05)
>
> Skip FK validation patch:
>
> 2026-11-24T18:42:24+1300 Restoring global objects in the new cluster
> ok
> 2026-11-24T18:52:15+1300 Restoring database schemas in the new cluster
> ok (9:51)
>
> So the patch knocks about 35% off the runtime of restoring the schema.

Nice!

--
Thanks, Amit Langote

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Neil Chen 2026-11-25 01:34:06 Re: [PATCH] psql: Complete GRANTED BY in GRANT and REVOKE
Previous Message Lukas Fittl 2026-11-25 01:13:35 Re: Parallel GIN, BRIN builds fail to maintain ii_BrokenHotChain