r/SQL • • 1d ago

PostgreSQL Foreign keys for array column elements

Postgres foreign keys can't check the elements of an array column, so a
product_ids bigint[] can hold ids that don't exist, and deleting the referenced
row leaves them dangling.

I wrote an extension that enforces it with triggers:

SELECT anyfk.add('orders(product_ids)', 'products(id)');

Feedback welcome.

https://github.com/Vinnu124/pg_anyFK

7 Upvotes

11 comments sorted by

2

u/Straight_Waltz_9530 1d ago

Sounds very heavyweight compared to typical foreign key constraints. An unnest and query on every mutation? Oof. What do benchmarks say?

3

u/Global-Apartment-872 22h ago

On postgres 18 (MAC m1), testing on 10k rows and 5000 inserts of an 5 element array

  1. normal array column + no constraint takes about 15 ms(nothing inside the array is checked)
  2. array + pg_anyfk takes about 1300 ms
  3. normal junction table + native FK take about 1990 ms.

The real alternative to this is the 3rd one. Also the trigger wont go on every mutation,only the ones that touch the array column.Updating a different column is still almost the same.

Obviously it is heavier per check than native FK, and if youre writing hard to a wide array , it will show up.

2

u/Discordbothost 22h ago

I'd also time deleting a product with no orders, with a GIN index on the array and a B-tree on the junction table's product_id. That compares the two reference checks without timing any cleanup.

2

u/Global-Apartment-872 21h ago

Good call ,
10k products, 5000 orders of 5 products each, so 25k relationships either way. GIN on the array column, B-tree on the junction table's product_id. Both designs reference the same 5000 products.
array + pg_anyfk: ~1200 ms
junction + native FKs: ~1900 ms
Deleting 1000 unreferenced products, so no cleanup, just the check:
array + pg_anyfk (GIN): 112 ms
junction + native FK (B-tree): 81 ms

1

u/squadette23 19h ago

normal junction table taking longer than array + extension is extremely counterintuitive.

Basically, a cell in a normal junction table can be considered a single-element array, and it would become faster? Do you understand why?

2

u/TheMagarity 21h ago

Isn't this more of an underlying model issue?

1

u/Global-Apartment-872 21h ago

yea, postgres core tried patching this in too but it hasnt landed yet.

2

u/TheMagarity 21h ago

No I mean your model. If you use a 1:many table instead of an array then you can enforce the fk no problem.

1

u/mirinai_ai 13h ago

On squadette23's question about why the junction table comes out slower: in that design the 5000 orders become 25,000 child rows, each with its own tuple, its own index entries (the primary key plus the B-tree on product_id) and its own foreign key check. The array version writes 5000 rows and runs one check per row. So the benchmark is mostly measuring per-row overhead, and it would help to say how the junction rows were inserted, since one multi-row INSERT or COPY usually narrows the gap compared with 25,000 single-row statements.

Since the README's case for arrays is the read path, a read benchmark might be more convincing than inserts: loading one order with its products, and finding every order that contains a given product (GIN with @> against the B-tree on the junction table).

I'd also test pg_dump and restore. The triggers on the user tables should come across, but if the registered constraints live in an extension table, they're only included in a dump when the extension marks that table with pg_extension_config_dump. A quick check that anyfk.constraints and drop_constraint still work after restoring into a fresh database would settle it.

1

u/Global-Apartment-872 3m ago

Ran the reads:
load one order, product ids only: array 0.086 ms junction 0.090 ms
load one order with the product rows: array 0.196 ms junction 0.353 ms
find all orders containing a product: array 0.109 ms junction 0.091 ms

So it splits. Loading an order with its products is faster with the array, since it's one row lookup instead of walking five.

Also the constraints and triggers do come back after a restore (needed a patch for including the id counter in the dump)

1

u/Thlvg 22h ago

Why?