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
