pg_anyfk
pg_anyfk · Trigger-enforced foreign keys for individual array elements
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 102427 | pg_anyfk | pg_anyfk | 0.2.0 | FEAT | Apache-2.0 | Rust |
PostgreSQL · 15, 16, 17, 18
Usage
Sources:
pg_anyfk 0.2.0 enforces references from individual array elements to a parent key using triggers. It is intended for existing array-based schemas; a junction table remains an ordinary alternative.
Core Workflow
Upstream supports PostgreSQL 15–18. A superuser installs this untrusted, non-relocatable extension; no preload or restart is required.
The caller must own both tables. The array element type must match the parent column type, and the parent needs a single-column unique index. A GIN index avoids scanning all child rows for every parent deletion.
Validation and Deletion
anyfk.add validates existing data by default. Inspect exceptions before adoption:
The options include validate, is_deferrable, conname, and on_delete. The default restrict rejects deleting referenced parent rows. Select scrub when deletion should remove that element from child arrays; the deleting role then needs UPDATE on the child table. scrub does not permit updating referenced parent keys.
is_deferrable => true defers checking to transaction completion. Remove a registered constraint with anyfk.drop_constraint, using its name from anyfk.constraints.
Boundaries
Null arrays, empty arrays, null elements and duplicate elements are accepted. Parent truncation is rejected while nonempty references remain, but truncating parent and child together is supported. Row-key locks protect checks against concurrent parent changes.
These are triggers, not native foreign-key entries in pg_constraint; disabling triggers disables enforcement. The extension tracks table identities and uses event triggers for renames and dependent-object cleanup.