Should we skip foreign key checks in binary upgrade mode?
Thread notes
1 note#performance#moderate#enhancement
hackorum-bot#performance#moderate#enhancement
@global #performance #moderate #enhancement
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
On Wed, Nov 25, 2026 at 7:11 AM David Rowley <dgrowleyml@gmail.com> 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.
Have you thought about checking directly whether the table (leaf
partition) is empty, i.e. replace the !IsBinaryUpgrade in your patch
with RelationGetNumberOfBlocks(rel) != 0? That wouldn't have the -b
problem you mention, since a non-empty table would still be checked.
It also avoids adding the global flag check to yet another site. Not
sure how much to worry about table AMs for which that might not be
accurate though.
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.
Nice!
--
Thanks, Amit Langote
On Wed, Nov 25, 2026 at 5:11 AM David Rowley <dgrowleyml@gmail.com> 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.
I've recently seen a real-world case that took more than 24 hours, but
haven't thought about how to address that in the near-term, so thanks
for this. (To toot my own horn, in-place upgrade should take less than
10 seconds for the same case, but that's a ways off and by no means
guaranteed...)
In my preliminary investigation with FKs declared on the parent table
with M list partitions on the referencing side and N list partitions
on the referenced side, I found the runtime is O(M x N^2) for the FK
check.
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.
More realistically, a possible magnet for LLM-generated bug reports,
but I would hope the comment in the patch would fend that off.
So the patch knocks about 35% off the runtime of restoring the schema.
-- Largest 1.52M-pg_class tier (Table 3.3 Row 7, ~8.1h unpatched ->
~1.1h patched):
This case from the .sql file seems even more encouraging for my above case.
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.
That may be difficult. For example, I would think the CREATE INDEX
statements on leaf partitions need the heaps to exist. With the patch,
it might be good to see where the rest of the run time is going.
--
John Naylor
Amazon Web Services
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
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]/messages/by-id/179493113608.1129989.9179084531222922608@gmail.com?
[0]: /messages/by-id/179493113608.1129989.9179084531222922608@gmail.com
--
nathan
On Thu, 26 Nov 2026 at 03:02, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
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.)
Good thinking. That avoids the possibly controversial part of what I
was proposing.
I think we need to keep the comment about avoiding the SPI query,
otherwise someone might look at your change and wonder why it's not
the way it was previously. Also, is it worth forming the patch so it
leaves the tight loop untouched? Something like the attached, but
maybe it might be worth doing it your way and use unlikely() to make
the compiler put the extra code out of line of the loop and just have
the extra cmp/jmp within the loop?
I tested your version and it took 10 mins 2 seconds. Previously it was
master=15:05 and patched=9:51 (so 33.4% less time vs 35%). I guess the
extra cost is from the scan setup-related code and the
table_scan_getnextslot(). The speedup is still very good, and it
avoids that quadratic slowdown with more referencing and referenced
partitions.
David
On 26/11/2026 03:43, David Rowley wrote:
On Thu, 26 Nov 2026 at 03:02, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
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.)Good thinking. That avoids the possibly controversial part of what I
was proposing.I think we need to keep the comment about avoiding the SPI query,
otherwise someone might look at your change and wonder why it's not
the way it was previously. Also, is it worth forming the patch so it
leaves the tight loop untouched? Something like the attached, but
maybe it might be worth doing it your way and use unlikely() to make
the compiler put the extra code out of line of the loop and just have
the extra cmp/jmp within the loop?
The table_rescan() seems a bit ugly to me, I'd rather restructure the
loop slightly to avoid that. Maybe turn it from "while
(table_scan_getnextslot())" into "do {} while(table_scan_getnextslot())"
The comments could use some copy-editing. It mentions "pg_dump
--schema-only" explicitly, but I think it's even more generally true
that the table is likely to be empty. And the comment about temporal FKs
seems misplaced now, that should still be above the "if (!hasperiod)" check.
- Heikki
On Fri, 27 Nov 2026 at 22:02, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
The table_rescan() seems a bit ugly to me, I'd rather restructure the
loop slightly to avoid that. Maybe turn it from "while
(table_scan_getnextslot())" into "do {} while(table_scan_getnextslot())"The comments could use some copy-editing. It mentions "pg_dump
--schema-only" explicitly, but I think it's even more generally true
that the table is likely to be empty. And the comment about temporal FKs
seems misplaced now, that should still be above the "if (!hasperiod)" check.
Thanks for looking. Updated patch attached.
David
On 27/11/2026 16:45, David Rowley wrote:
On Fri, 27 Nov 2026 at 22:02, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
The table_rescan() seems a bit ugly to me, I'd rather restructure the
loop slightly to avoid that. Maybe turn it from "while
(table_scan_getnextslot())" into "do {} while(table_scan_getnextslot())"The comments could use some copy-editing. It mentions "pg_dump
--schema-only" explicitly, but I think it's even more generally true
that the table is likely to be empty. And the comment about temporal FKs
seems misplaced now, that should still be above the "if (!hasperiod)" check.Thanks for looking. Updated patch attached.
The commit message subject sounds grammatically wrong to me. And the
comment about the check for zero rows sounds a little repetitive to me.
But those are just tiny nits and a matter of taste, LGTM otherwise.
(Thanks for working on this general area of making pg_upgrade faster!)
- Heikki