PostgreSQL 19 skips its fast FK check for partitioned parents. Here is how to get it back.

PostgreSQL 19 sped up foreign-key checks. The catch, documented in the fast-path commit, is that the new path is skipped when the referenced table is partitioned. I wanted a way to get that speed back on a star-schema fact load, and to know what it costs to keep it.
I ran this on PostgreSQL 18.7 and 19.0, built from the official tarballs, on a single 8 vCPU, 15 GiB KVM box. The fact file is a 5 million row CSV with valid keys into date, store, product, and customer dimensions. The customer dim has 2 million keys. Unless I say otherwise, times are medians of 3, and the FK under test is the customer key only.
The gap on 19.0
A COPY of those 5 million rows, with the fact FK pointing at an 8-partition hash-partitioned customer dim, took 73.8 seconds (range 73.0-76.0). The same COPY against a plain, non-partitioned customer dim took 14.6 seconds (14.1-14.6). That is about 5x, on the same box, the same build, and the same file.
The 19.0 release notes call this out as "Improve performance of foreign key constraint checks." The commit that added the fast path is more specific: it probes the referenced table's unique index and bypasses SPI, and it does not apply when the referenced table is partitioned or when the constraint is temporal. The 73.8 vs 14.6 second split is what that looks like on a fact load.
Things that did not close the gap
Declaring the FK NOT VALID before the load did not help: 73.7 seconds (72.6-74.7). New rows are still checked, which is what the documentation says. NOT VALID skips the scan of rows that are already in the table. It is not a cheaper path for the COPY itself.
Sorting the input file by customer_key took 2.0 seconds and brought the partitioned COPY down to 67.4 seconds (66.7-67.9). Real, but not enough.
A shadow key table got the fast path back
I built a narrow, non-partitioned table of the customer keys, filled it from the partitioned dim, and pointed the fact FK at that table instead.
CREATE TABLE customer_keys ( customer_key bigint PRIMARY KEY ); INSERT INTO customer_keys SELECT customer_key FROM dim_customer_p; CREATE TABLE fact_sales ( sale_id bigint, date_key int, store_key int, product_key int, customer_key bigint, qty int, amount numeric(12,2), CONSTRAINT fk_cust FOREIGN KEY (customer_key) REFERENCES customer_keys ); Filling 2 million keys took 2.5 seconds. The 5 million row COPY then took 13.6 seconds (13.4-14.9), in the same band as the plain parent. The fact table is now protected against the shadow, not against the dim itself. That is the trade-off. Integrity holds only while the two stay in lockstep. The next two sections are about that lockstep, under load and under concurrent dim changes.
The benefit grows with the batch, and it is not a 19-only trick
On 19.0, with the 8-partition parent versus the shadow, the same pattern appeared at every size I ran:
| Rows | Partitioned parent | Shadow keys | Ratio |
|---|---|---|---|
| 10,000 | 0.24 s | 0.14 s | 1.7x |
| 100,000 | 1.68 s | 0.43 s | 3.9x |
| 1,000,000 | 15.2 s | 3.06 s | 5.0x |
| 5,000,000 | 73.8 s | 13.6 s | 5.4x |
(n=3 for each cell. 10k, 100k, and 1M are prefixes of the same 5M file.)
I also ran the 5 million row case on 18.7. The partitioned parent took 73.9 seconds (72.2-76.3). The plain parent took 24.6 seconds (24.0-25.0). The shadow took 23.4 seconds (23.1-23.6). So the shadow already helps on 18.7: it avoids per-row routing into a partitioned parent, and it lands next to a plain parent. PostgreSQL 19 then adds the fast path on top, which is why the shadow and the plain parent both drop from the mid-20s into the mid-teens.
That also shows up on a four-FK star (date, store, product, plus customer). On 19.0, pointing only the customer FK at the shadow, and the other three at plain dims, took 37.5 seconds (37.3-39.7). The all-plain four-FK load took 37.8 seconds (37.4-38.7). The same star with the customer FK on the 8-partition dim took 99.4 seconds (99.2-99.7). On 18.7, four-FK shadow and four-FK plain were 75.5 and 76.8 seconds: again, no 19 fast path, and no gap between shadow and plain.
Partition count, range vs hash, and a smaller dim
I expected more partitions to make the slow path worse. On 19.0, 5 million rows, 2 million parent keys:
| Referenced table | Median | Range |
|---|---|---|
| Hash, 1 partition | 24.4 s | 24.2-25.4 |
| Hash, 8 partitions | 73.8 s | 73.0-76.0 |
| Hash, 64 partitions | 73.2 s | 72.8-74.6 |
| Hash, 256 partitions | 75.2 s | 75.2-75.7 |
| Range, 8 partitions | 78.0 s | 74.8-78.8 |
| Plain parent | 14.6 s | 14.1-14.6 |
| Shadow keys | 13.6 s | 13.4-14.9 |
A table with PARTITION BY HASH and a single partition is still partitioned, so the fast path stays off. It was much cheaper than 8 partitions (24.4 vs 73.8 seconds) and still clearly slower than a plain parent (14.6 seconds). From 8 to 256 partitions the time barely moved. Range with 8 parts was in the same band as hash with 8 parts, a few seconds slower.
A smaller parent did not remove the gap either. I generated a second 5 million row file whose customer keys sit in a 200,000-key dim. On 19.0 the 8-partition parent took 68.7 seconds (68.3-71.2), the plain parent 11.7 seconds (11.6-12.1), and the shadow 11.2 seconds (11.1-11.2). Faster than the 2 million key case, and the partitioned parent was still about 6x the shadow.
I did not run a 20 million row load. After a cancelled delete in the first sweep (more on that below) it was not a cheap extra, and the 10k-to-5M sweep already showed the ratio growing with batch size.
If you only need a one-time gate
If the goal is "reject this batch if it has orphans," you do not have to check row by row during COPY.
CREATE TABLE fact_sales ( sale_id bigint, date_key int, store_key int, product_key int, customer_key bigint, qty int, amount numeric(12,2) ); COPY fact_sales FROM '/path/sales.csv' WITH (FORMAT csv); ALTER TABLE fact_sales ADD CONSTRAINT fk_cust FOREIGN KEY (customer_key) REFERENCES dim_customer_p NOT VALID; ALTER TABLE fact_sales VALIDATE CONSTRAINT fk_cust; On 19.0 I loaded the 5 million rows with no FK, added the FK to the partitioned parent as NOT VALID (2.7 milliseconds), and ran VALIDATE CONSTRAINT (1.83 seconds). Total: 3.99 seconds (3.90-4.01), of which 2.14 seconds was the COPY.
A 1,000-row batch of orphan keys was rejected during COPY by the partitioned FK, the shadow FK, and a NOT VALID FK. For the load-then-VALIDATE path those orphans landed, ADD FK NOT VALID succeeded, and VALIDATE then rejected them. If you skip VALIDATE, the orphans stay, and the constraint is present but not validated.
Keeping the shadow in sync: per-row vs statement-level triggers
A trigger on the partitioned dim is what keeps the shadow honest when keys are added, removed, or changed. I first used per-row triggers. They work, and they are simple. An earlier one-off on 19.0 put 100,000 new dim keys in 418 milliseconds with the per-row trigger and 118 milliseconds without it.
The maintenance I actually want, though, is set-based: a bulk dim load or a bulk purge of unused keys should update the shadow in one statement, not one row at a time. PostgreSQL can do that with statement-level triggers and transition tables (REFERENCING NEW TABLE / OLD TABLE). On 19.0 those triggers installed cleanly on the partitioned parent itself. I did not need to attach them to every partition.
One syntax trap, which I hit on the first attempt: transition tables cannot be combined with a column list. AFTER UPDATE OF customer_key ... REFERENCING is rejected (transition tables cannot be specified for triggers with column lists). The update trigger has to be AFTER UPDATE, so it also fires on a non-key change.
This is the SQL I ran, as written:
CREATE OR REPLACE FUNCTION customer_keys_ins_stmt() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN INSERT INTO customer_keys SELECT customer_key FROM new_rows; RETURN NULL; END; $$; CREATE OR REPLACE FUNCTION customer_keys_del_stmt() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN DELETE FROM customer_keys k USING old_rows o WHERE k.customer_key = o.customer_key; RETURN NULL; END; $$; CREATE OR REPLACE FUNCTION customer_keys_upd_stmt() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN -- Only move keys that actually changed. DELETE FROM customer_keys k USING old_rows o WHERE k.customer_key = o.customer_key AND NOT EXISTS ( SELECT 1 FROM new_rows n WHERE n.customer_key = o.customer_key ); INSERT INTO customer_keys SELECT n.customer_key FROM new_rows n WHERE NOT EXISTS ( SELECT 1 FROM old_rows o WHERE o.customer_key = n.customer_key ) ON CONFLICT (customer_key) DO NOTHING; RETURN NULL; END; $$; CREATE TRIGGER trg_ck_ins_stmt AFTER INSERT ON dim_customer_p REFERENCING NEW TABLE AS new_rows FOR EACH STATEMENT EXECUTE FUNCTION customer_keys_ins_stmt(); CREATE TRIGGER trg_ck_del_stmt AFTER DELETE ON dim_customer_p REFERENCING OLD TABLE AS old_rows FOR EACH STATEMENT EXECUTE FUNCTION customer_keys_del_stmt(); CREATE TRIGGER trg_ck_upd_stmt AFTER UPDATE ON dim_customer_p REFERENCING OLD TABLE AS old_rows NEW TABLE AS new_rows FOR EACH STATEMENT EXECUTE FUNCTION customer_keys_upd_stmt(); On 19.0, 100,000 new dim keys took 293 milliseconds with the statement-level triggers (292.3-312.3) and 462 milliseconds with the per-row triggers (431.7-465.0). A delete of 200,000 unused keys, not referenced by any fact table, took 551 milliseconds with the statement-level triggers (545.9-551.7, n=3) and 701 milliseconds with the per-row triggers (n=1, 10-minute cap, it finished). In every cell the dim and the shadow stayed at the same row count, with no keys missing either way.
That last number needs a correction to something I wrote after the first sweep. During that sweep I cancelled a 200,000-key delete with the per-row trigger still on, after nine minutes, and cleaned up by disabling the triggers and rebuilding the shadow. I did not isolate why that statement stalled. In this controlled retest of the same shape, unused keys, no fact table, the per-row delete finished in 701 milliseconds. I am not going to claim that per-row deletes are unusable at 200,000 rows. The retest did not reproduce the stall. Statement-level was still faster, and it is the path I would keep.
Correctness checks on the statement-level path held. After a 100,000-row fact load against the shadow, deleting an in-use dim key (customer_key = 128641) was rejected: the statement trigger tried to delete the key from customer_keys, the fact FK blocked it, and the dim row stayed put. Dim, shadow, and fact still agreed, with zero orphans. Updating ten unused keys (customer_key = customer_key + 100) took 6.3 milliseconds and left no drift.
Because the trigger has to be AFTER UPDATE, the function itself has to ignore non-key changes. The version above only deletes keys that disappeared and only inserts keys that appeared. I tested that on an in-use row: after the same 100,000-row load, UPDATE dim_customer_p SET name = name || '_nk' WHERE customer_key = 128641 succeeded in 3.0 milliseconds, the dim name changed, and the shadow row's ctid and xmin stayed put ((1176,174) / 2206). No drift, no orphans. A naive delete-then-insert of every updated key would have been the wrong behaviour here: it would have tried to remove a shadow key the fact table still referenced.
Concurrent ETL: repeated fact batches while the dim is changing
The earlier concurrent checks were short: two sessions inserting 100,000 distinct dim keys each (648 milliseconds, no drift), and a 20,000-row fact COPY against the shadow at the same time as a 50,000-row dim insert (zero orphans, no drift). A mid-load delete of 200 existing dim keys, with the per-row trigger, deadlocked. The delete failed, the COPY finished, and no orphan landed.
I wanted a closer picture of an incremental job: many small fact batches, in a row, with another session changing the dim at the same time. On 19.0 I ran 3 rounds of 8 COPY batches of 100,000 rows each (24 batches per parent). Statement-level shadow triggers were on. The other session inserted unused dim keys, updated names, updated some of those keys (which can move hash partitions), and deleted a slice of the unused keys. log_lock_waits was on, deadlock_timeout was 1 second.
| Fact parent | Median | p95 | Min | Max | Batches ok | Deadlocks | Orphans | Drift |
|---|---|---|---|---|---|---|---|---|
| Shadow keys | 333 ms | 403 ms | 312 ms | 407 ms | 24/24 | 0 | 0 | 0 |
| 8-partition parent | 1550 ms | 1731 ms | 1479 ms | 1834 ms | 24/24 | 0 | 0 | 0 |
The shadow stayed about 4.7x faster than the partitioned parent under that load. After 2.4 million fact rows, there were zero orphans versus the shadow and versus the dim. No lock waits were logged. The dim worker's unused-key deletes did not deadlock with the fact COPY. That is a different shape from the earlier deadlock, which deleted 200 keys that already lived in the dim while a COPY of valid fact rows was in flight, through per-row triggers. I did not rerun that exact case with statement-level triggers.
Each dim cycle used a fresh, disjoint key range, so later rounds did not collide with leftover keys. Every dim operation succeeded: 6/6 inserts, name updates, key updates, and deletes during the shadow batches, and 18/18 of each during the slower partitioned-parent batches (more wall time, so more cycles). Zero dim errors, zero dim deadlocks.
When I would use which option
If later incremental loads must be checked as they arrive, and the parent has to stay partitioned, I would keep a non-partitioned shadow of the parent keys, pay the 2.5-second backfill once, and keep statement-level transition-table triggers on the partitioned parent. The update function should only move keys that actually changed, so a name-only update of an in-use row does not touch the shadow. I would treat a missing trigger as a correctness bug, not an operational inconvenience. If I ever disable the triggers for a bulk change, I would rebuild the shadow from the dim before turning them back on.
If I only need a one-time gate before the data moves on, I would load without the FK and VALIDATE after. That was the fastest complete check in this test, by a wide margin. I would make VALIDATE a required step in the job, not a follow-up that can be skipped.
I would not rely on NOT VALID to make the load cheap, I would not expect a pre-sorted file to fix a partitioned parent, and I would not expect adding partitions (beyond a single partition) to change this picture much. NOT ENFORCED FKs, which PostgreSQL 18 introduced, are documentation: the database will not check them, and the planner may still assume they hold. They are not a gate.
Application-side checks belong in the same family as the staging orphan query I measured earlier on this box (about 7 seconds for 5 million rows against all four dims). They can be the right choice when you cannot add catalog objects, or when the source of truth is outside Postgres. They are only a gate if they actually run, and they do not give you the catalog-level guarantee that an enforced FK does.
Limitations
Single node, 8 vCPU, 15 GiB RAM, no swap, KVM guest, Debian 13. Official 18.7 and 19.0 tarballs, gcc 14.2.0, -O2, --without-icu. shared_buffers=1GB, autovacuum off, dims prewarmed, CHECKPOINT before each timed statement. Synthetic uniform keys, all valid in the main file. n=3 for every load cell reported above, and for the statement-level insert and delete cells. The per-row 200,000-key delete is n=1.
The 19.0 5 million row partitioned-parent, shadow, NOT VALID, sorted-input, and load-then-VALIDATE numbers come from an earlier script the same day, on the same builds and the same CSV. The batch-size, partition-count, range, 200k-dim, four-FK, 18.7, and short concurrent numbers come from the follow-up sweep. The statement-level trigger timings come from a third script, still on 19.0, the same day, the same cluster layout. The 24-batch concurrent ETL numbers, and the in-use non-key update, come from a rerun of that cell with disjoint dim key ranges and the safer update function. I did not re-run a 20 million row file. I did not re-measure the mid-load delete of 200 existing dim keys with statement-level triggers.
Reproduction
Scripts, raw jsonl, hardware, and tarball SHA-256 sums are in pg-fk-load-bench. The clusters used ports 54187 and 54190, Unix sockets only, and data under /tmp/b1203 on the measurement box. The 5 million row CSV (setseed(0.42)) has SHA-256 1a8c70e8f87329660cb1abc2c0cfb04ae2ee235f329987d70bbb6068dd71909a. The statement-level trigger SQL, as run, is in that repo under as-run/sql_triggers_etl/.
Sources
- PostgreSQL 19.0 release notes, 19 November 2026, "Improve performance of foreign key constraint checks": https://www.postgresql.org/docs/release/19.0/
- Commit message, "Add fast path for foreign key constraint checks" (Amit Langote, 31 March 2026). The fast path is skipped when the referenced table is partitioned: https://www.postgresql.org/message-id/E1w7R6y-002Fk8-1I@gemulon.postgresql.org
-
CREATE TABLEdocumentation forNOT ENFORCED: https://www.postgresql.org/docs/18/sql-createtable.html - Trigger transition tables, including the restriction on column lists: https://www.postgresql.org/docs/current/sql-createtrigger.html


