Re: Should we skip foreign key checks in binary upgrade mode?

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
Cc: 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-26 01:43:50
Message-ID: CAApHDvoo7V4WqtDot-amFY7B_0gy_iMBn9ESCQG-in_Eb9BEvA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, 26 Nov 2026 at 03:02, Heikki Linnakangas <hlinnaka(at)iki(dot)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

Attachment Content-Type Size
zero_row_foreign_key_precheck_before_spi.patch text/plain 2.3 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Xuneng Zhou 2026-11-26 01:54:09 Re: pg_rewind: Fix buffer size passed when building WAL file name
Previous Message Xuneng Zhou 2026-11-26 01:39:33 Re: Bitwise OR instead of logical one in test_aio.c