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.

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.

If we did decide to do this, then likely there's more that could be
done along these lines. I've not looked in any detail, but in theory
there's probably not much reason to create relation files during
CREATE TABLE when pg_upgrade is going to copy in the old one.

Any opinions on this?

David
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 9786a5af58a..211c7c48e8d 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -11790,10 +11790,17 @@ addFkRecurseReferencing(List **wqueue, Constraint 
*fkconstraint, Relation rel,
                 * specified as NOT ENFORCED, or when requested explicitly by
                 * specifying NOT VALID in an ADD FOREIGN KEY command, and when 
we're
                 * recreating a constraint following a SET DATA TYPE operation 
that
-                * did not impugn its validity.
+                * did not impugn its validity.  We also skip the check when in 
binary
+                * upgrade mode as we expect the referencing (and referenced) 
tables
+                * to be empty, in which case there can be no rows violating the
+                * foreign key.  You might think that it's barely worth 
skipping this
+                * for empty tables, but since the validation work is done via 
SPI an
+                * query, a fairly large amount of effort can be saved, 
especially so
+                * when the referenced table is partitioned, which requires 
much more
+                * planning and executor startup / shutdown effort.
                 */
                if (wqueue && !old_check_ok && !fkconstraint->skip_validation &&
-                       fkconstraint->is_enforced)
+                       fkconstraint->is_enforced && !IsBinaryUpgrade)
                {
                        NewConstraint *newcon;
                        AlteredTableInfo *tab;

Attachment: 01_partitions_and_fks.sql
Description: Binary data

Reply via email to