| From: | David Rowley <dgrowleyml(at)gmail(dot)com> |
|---|---|
| To: | PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Should we skip foreign key checks in binary upgrade mode? |
| Date: | 2026-11-24 22:10:26 |
| Message-ID: | CAApHDvqwNJMaQoWH31D8qow9PozivzOwD17VSgfpEZDmChTsqQ@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
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
| Attachment | Content-Type | Size |
|---|---|---|
| skip_fk_checks_in_binary_upgrade_mode.patch | text/plain | 1.3 KB |
| 01_partitions_and_fks.sql | application/octet-stream | 3.8 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Masahiko Sawada | 2026-11-24 22:24:28 | Re: Fix standby logical slot creation during dynamic logical decoding activation |
| Previous Message | Álvaro Herrera | 2026-11-24 20:39:51 | Re: Avoid unnecessary StringInfo allocation in tablesync COPY buffer |