| From: | Heikki Linnakangas <hlinnaka(at)iki(dot)fi> |
|---|---|
| To: | 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 14:01:30 |
| Message-ID: | 295bc60c-2a96-4a2c-a145-bc53a6bba077@iki.fi |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On 25/11/2026 00:10, David Rowley 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.
>
> 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.
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.
For example, you could first do a quick check to see if the table is
completely empty, before starting up the SPI query. Something like the
attached quick prototype. (I first tried calling
table_relation_estimate_size(), but that returns a dummy, constant
estimate rather than 0 for an empty table that hasn't been vacuumed yet.)
Playing with that schema, I noticed a lot of time is also spent in
ChooseConstraintName(). I recall seeing a separate thread about that,
though.
- Heikki
| Attachment | Content-Type | Size |
|---|---|---|
| optimize-validateForeignKeyConstraint-for-empty-table.patch | text/x-patch | 2.1 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Ayush Tiwari | 2026-11-25 14:01:43 | Re: [PATCH] Table sync race with REFRESH PUBLICATION |
| Previous Message | Tatsuya Kawata | 2026-11-25 13:57:14 | Re: HashAggregate loses worker spill stats on rescan |