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

From: Nathan Bossart <nathandbossart(at)gmail(dot)com>
To: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
Cc: David Rowley <dgrowleyml(at)gmail(dot)com>, 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 15:25:21
Message-ID: awb94dzu25l-bMKN@nathan
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Nov 25, 2026 at 04:01:30PM +0200, Heikki Linnakangas wrote:
> On 25/11/2026 00:10, David Rowley wrote:
>> 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.

Yeah, during my work on pg_upgrade my benchmarks regularly showed that
schema restore took most of the time, especially once you've used --jobs
and --swap. So, +1 for optimizing it.

>> 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.
>>
>> [...]
>
> 35% sounds nice..
>
> I'm hesitant to add special cases like this for pg_upgrade, though. Could
> you could optimize validateForeignKeyConstraint() for the case of an empty
> table, whether it's part of pg_upgrade or not? That would also help plain
> pg_dump/restore.

I think that horse is out of the barn with commit 158408fef8, which taught
pg_upgrade to allow nonexistent large objects under the assumption that
they'll show up later. IMHO we shouldn't be too worried about people
misusing binary upgrade mode; it's clearly documented as not recommended or
supported for use outside pg_upgrade, and there's probably all sorts of
other things you can break with it today. That being said, if we can
improve matters outside of binary upgrade mode, that would clearly be a
superior approach.

> Playing with that schema, I noticed a lot of time is also spent in
> ChooseConstraintName(). I recall seeing a separate thread about that,
> though.

Are you thinking of this one [0]?

[0] https://postgr.es/m/179493113608.1129989.9179084531222922608%40gmail.com

--
nathan

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2026-11-25 15:30:29 Re: OOM kill in xmlelement() with large arrays
Previous Message Aidar Imamov 2026-11-25 15:24:42 [PATCH] Use the table AM relation_size callback in the SQL size functions