On Thu, 26 Nov 2026 at 03:02, Heikki Linnakangas <[email protected]> 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
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 9786a5af58a..6c1d639fa61 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -14841,15 +14841,6 @@ validateForeignKeyConstraint(char *conname,
        trig.tginitdeferred = false;
        /* we needn't fill in remaining fields */
 
-       /*
-        * See if we can do it with a single LEFT JOIN query.  A false result
-        * indicates we must proceed with the fire-the-trigger method. We can't 
do
-        * a LEFT JOIN for temporal FKs yet, but we can once we support temporal
-        * left joins.
-        */
-       if (!hasperiod && RI_Initial_Check(&trig, rel, pkrel))
-               return;
-
        /*
         * Scan through each tuple, calling RI_FKey_check_ins (insert trigger) 
as
         * if that tuple had just been inserted.  If any of those fail, it 
should
@@ -14864,6 +14855,31 @@ validateForeignKeyConstraint(char *conname,
                                                                          
ALLOCSET_SMALL_SIZES);
        oldcxt = MemoryContextSwitchTo(perTupCxt);
 
+       if (!hasperiod)
+       {
+               /*
+                * For non-temporal FKs, we use a LEFT JOIN SPI query to check 
for
+                * foreign key violations, checking if the referencing table 
contains
+                * rows not in the referenced table.  Because restoring 
--schema-only
+                * pg_dumps creates foreign keys on empty tables, it's worth 
checking
+                * to see if the referencing table has any rows before we go to 
the
+                * trouble of running the SPI query.  If the referenced table is
+                * partitioned, then the SPI query can be expensive to plan and 
go
+                * through executor startup/shutdown, so checking for 0 rows 
can be a
+                * significant shortcut.
+                *
+                * A false result from RI_Initial_Check() indicates we must 
proceed
+                * with the fire-the-trigger method.  We can't do a LEFT JOIN 
for
+                * temporal FKs yet, but we can once we support temporal left 
joins.
+                */
+               if (!table_scan_getnextslot(scan, ForwardScanDirection, slot) ||
+                       RI_Initial_Check(&trig, rel, pkrel))
+                       goto cleanup;
+
+               /* rescan, so we don't miss the first row in the loop below */
+               table_rescan(scan, NULL);
+       }
+
        while (table_scan_getnextslot(scan, ForwardScanDirection, slot))
        {
                LOCAL_FCINFO(fcinfo, 0);
@@ -14895,6 +14911,7 @@ validateForeignKeyConstraint(char *conname,
                MemoryContextReset(perTupCxt);
        }
 
+cleanup:
        MemoryContextSwitchTo(oldcxt);
        MemoryContextDelete(perTupCxt);
        table_endscan(scan);

Reply via email to