Skip to content

World's first open-source enterprise world model.

13.6 KB
Raw Blame

0048 · Provenance references stay inside the knowledge base

  • Status: Implemented (#832, migration 0070)
  • Written: 2026-09-20 (conventions in the README)
  • Related: 0009's "NULL means undecided" is why several edges below are nullable and therefore cannot lean on MATCH FULL; 0002 owns the derivation model whose premise edges are covered here. Mechanism choice resolved by PR #832; discussion tracked in issue #842.

The exporter writes urn:utopia:kb:A:fact:{id} and asserts the id belongs to knowledge base A. Nothing in the schema made that true — a foreign key proves the row exists, not which base it lives in. A cross-KB supersedes would mint a local IRI naming a foreign fact; a cross-KB type_id would resolve to nothing and the statement would silently lose its class. This record decides where the same-KB invariant is enforced, and with what.

The invariant

Every reference an export can resolve must join rows that live in the same knowledge base. One invariant, stated once; the question is only which mechanism proves it per edge, and at which transaction boundary.

Why the existing foreign keys are insufficient

FOREIGN KEY (ref) REFERENCES t (id) checks one column against one key. kb_id is not part of the check, so (kb A).fact → (kb B).entity is a perfectly satisfied foreign key. The failure is silent in both directions it matters: the exporter mints an IRI that names a row in another base (a complete-looking file pointing at nothing), or a vocabulary reference resolves to a foreign row the vocabulary lookup cannot see (a slice of semantics dropped without an error).

The mechanism, per edge

Thirty-nine reference edges are protected. The split is decided by a single question: does the referencing row carry the kb authority itself?

Source Edge → target kb authority Mechanism
chunks document_id → documents own kb_id composite FK
facts subject_id, object_id → entities; predicate_id → relation_types own kb_id composite FK
facts supersedes, from_statement_id → facts own kb_id composite FK, DEFERRABLE INITIALLY DEFERRED
derived_facts subject_id, object_id → entities; predicate_id → relation_types; rule_id → rules; attribute_rule_id → attribute_rules own kb_id composite FK
entities type_id → entity_types own kb_id composite FK
entity_type_disjoint a_id, b_id → entity_types own kb_id composite FK ×2
relation_types inverse_of, sub_property_of → relation_types own kb_id composite FK, deferred, ON DELETE SET NULL (col)
rules predicate_id → relation_types own kb_id composite FK
attribute_rules subject_type_id, conclude_type_id → entity_types; conclude_predicate_id → relation_types own kb_id composite FK
time_mentions fact_id → facts; chunk_id → chunks own kb_id composite FK
type_bindings type_id → entity_types own kb_id composite FK
phrase_bindings subject_type_id, object_type_id → entity_types; relation_type_id → relation_types own kb_id composite FK
fact_evidence chunk_id → chunks; document_id → documents owning fact's kb trigger
fact_qualifiers qualifier_type_id → relation_types; entity_id → entities owning fact's kb trigger
fact_derivations premise_fact_id → facts; premise_derived_id → derived_facts derived fact's kb trigger
entity_type_parents parent_id → entity_types child's kb trigger
relation_type_domains, relation_type_ranges entity_type_id → entity_types relation's kb trigger (shared function)
relation_type_qualifiers qualifier_type_id → relation_types relation's kb trigger
attribute_rule_conditions predicate_id → relation_types owning rule's kb trigger
typed_fact_sources statement_id → facts owning fact's kb trigger
statement_qualifiers entity_id → entities owning fact's kb trigger

26 edges are declared as FOREIGN KEY (kb_id, ref) REFERENCES t (kb_id, id), replacing the single-column FK that was already there — one kernel-level lookup now proves existence and same-base, where before it proved existence and a second check would have had to prove the rest. Eight UNIQUE (kb_id, id) constraints on the referenced tables back those keys (documents, chunks, entities, entity_types, relation_types, facts, rules, attribute_rules).

13 edges cannot be expressed that way: their kb authority is a parent row's kb_id, and the link row has no kb_id column at all. A composite key cannot name fact.kb_id from a fact_evidence row. These keep row-level BEFORE triggers — nine functions, ten triggers — that read the parent's base and compare.

kb_id is immutable on every owned table (one shared function, twelve BEFORE UPDATE OF kb_id triggers). Composite keys only guard the rows being pointed at; the trigger-covered edges derive their authority from a parent row, and a parent moving base would silently un-anchor every child pointing at it. Making kb reassignment impossible is what lets a point-in-time check stay correct.

Four same-table self-references (facts.supersedes, facts.from_statement_id, relation_types.inverse_of, relation_types.sub_property_of) are DEFERRABLE INITIALLY DEFERRED. A row-level check — immediate FK or BEFORE trigger alike — cannot see a target row inserted later in the same transaction, and forward references are a normal restore shape (a multi-row INSERT or COPY batch lists the referring row before its target). Deferred evaluation at commit sees the whole batch; a sequential write whose target never arrives is rejected at COMMIT. One boundary is honest: "the edge is valid when the transaction ends", which is exactly what a restore needs and no more than what an in-order write already had to satisfy.

COPY, restore, and search_path

pg_restore loads data with COPY and creates constraints after, so any shape that survives the migration survives a restore — and the deferred self-references are what let a same-transaction or intra-statement forward chain restore without ordering games. Two sharper edges were designed for explicitly:

  • pg_restore --disable-triggers and any session_replication_role = replica load suppress user triggers wholesale. Declarative foreign keys are internal constraint triggers and are not suppressed — so the 26 declarative edges hold even in a replica-mode load, which is a second reason to prefer them wherever the schema can say them. The 13 trigger-covered edges admit the gap and are backstopped by the export-side provenance_integrity check and the §0 audit query, which doubles as a post-load audit.
  • The migration pins search_path = pg_catalog in every function body and qualifies every identifier public.*, because restore empties the session search_path and a hostile first schema must not redirect name resolution. migration_0070_runs_under_any_search_path installs the whole migration under a normal, an empty, and a decoy-first search_path.
  • The coverage itself is guarded by a catalog-derived regression in the same test file: it enumerates every column-level reference inside the ledger surface from pg_catalog and fails when an edge resolves to no declared composite-FK, owner-derived-trigger, or explicit exclusion — so a reference column added later cannot silently slip past the invariant. The same guard also reads the exact preflight scan the export runs (export_provenance_integrity.sql) and fails when a protected edge has no scan branch — or a scan branch no longer names a protected edge — so schema protection and export preflight cannot drift apart.

The precondition scan

§0 counts existing cross-KB rows on all 39 edges and aborts the whole migration with the offending edge names and row counts if any exist. Installing an invariant over a ledger that already violates it would be signing off on bad data; whoever will not repair the ledger must not get a green migration. This is also the operational requirement: the scan (or its query, which is safe to run read-only) should be executed against real deployments before rollout, so an upgrade does not stop halfway on a base nobody had checked. The scan is the audit; the migration failing closed is the enforcement.

Measured cost

Same host (macOS, Darwin 25.4.0), same Postgres 16 (pgvector/pgvector:pg16), same toolchain (rustc 1.98.1), two fresh databases migrated by the respective build, alternating runs.

bench_100k populate phase (UTOPIA_BENCH_DOCS=1000, UTOPIA_BENCH_HUB_FACTS=1000, ~1000 documents / ~3000 chunks / ~3000 facts written through the real store functions), three runs each, alternating patched/base:

run base patched
1 9.18s 9.35s
2 10.29s 8.71s
3 9.77s 11.11s
median 9.77s 9.35s

The medians differ by −4% with fully overlapping ranges — no measurable write-path overhead on the populate path, which exercises the composite-FK edges (chunks→documents, facts→entities). A focused measurement covers the trigger-covered edges the populate phase never touches: 4000-row bulk INSERTs on a scratch fixture, warm, median of three — fact_evidence 37.1ms → 63.7ms, typed_fact_sources 34.3ms → 58.1ms, i.e. roughly +6–7µs per row per triggered edge (the two SELECT lookups the trigger performs). On a path that writes thousands of provenance rows a minute this is noise; it is recorded because a permanent write tax should carry a number, not an adjective.

Alternatives considered

  • Uniform triggers everywhere (the first cut of the migration). One mechanism for all 39 edges is simpler to describe and needs no new unique indexes, but it pays PL/pgSQL calls where a kernel-level RI check does the same work, and — the deciding point — user triggers go silent under session_replication_role = replica while declarative constraints do not. The replica-mode hole is real enough that uniformity was not worth it.
  • Fully declarative: add kb_id to the link tables. fact_evidence, fact_qualifiers, fact_derivations, entity_type_parents, relation_type_*, attribute_rule_conditions, typed_fact_sources, statement_qualifiers could grow a kb_id column and take composite keys too. But that column is a denormalization of the parent's kb_id, and keeping it equal is a second invariant — which would itself need a trigger to enforce. Trading a check for a column plus the same check is the worst of both.
  • Application-layer checks. The exporter already filters cross-KB references defensively; that is a backstop for readers, not an invariant for writers. The schema is the only layer every write path — present and future — passes through.
  • MATCH FULL composite keys were considered for nullable edges and rejected: MATCH SIMPLE (skip the check when the reference is NULL) preserves the existing nullable-edge semantics exactly; nothing here makes a NULL reference meaningful.

Revisions

  • 2026-09-23 (#874): the original rationale said replica-mode loading silenced user triggers while declarative foreign-key enforcement stayed active. That was wrong — PostgreSQL foreign keys are enforced by constraint triggers, and session_replication_role = replica suppresses those checks as well, so replica mode does not distinguish the two mechanisms. The hybrid stands for the narrower reason recorded above: composite foreign keys are the native, smaller mechanism where the referencing row carries its own kb_id, while owner-derived rows cannot express that authority declaratively without a denormalized kb_id and a second invariant keeping it equal. The same correction is why the export-side scan covers all 39 edges rather than only the trigger-covered ones. Migration 0070's own header still carries the earlier wording — an applied SQLx migration is checksum-addressed and stays byte-stable, so the corrected operational rule lives here rather than in an edit to that file: replica-mode loading can bypass user triggers and FK constraint-trigger checks alike, and export preflight is the post-load backstop.

  • 2026-09-26 (#901): the family was pinned to migration 0070's table list, so every KB-owned table created afterwards escaped it — the catalog guard's scratch database stopped at 0070 and could not see post-0070 tables at all. Migration 0091 extends the same mechanisms to the post-0070 tables: composite same-KB foreign keys on implication_rules, phrase_readings, errata_runs, errata_actions, attribute_rule_versions, derived_facts.attribute_rule_version_id; an owner-derived trigger on implied_fact_sources (whose KB authority is the parent fact's kb_id); UNIQUE (kb_id, id) support on implication_rules, attribute_rule_versions, errata_runs; and keep_their_kb immutability on the five new KB-owned tables (name_vectors already carried it). name_vectors' two pre-existing composite edges and attribute_rules.join_predicate_id were also missing from the registered surface and export preflight — registry and scan now cover them. The catalog guard's scratch database now migrates through the current chain end, so future tables cannot become structurally invisible to the guard again.

Open questions

  • Whether the eight supporting UNIQUE (kb_id, id) indexes should be partial indexes over live rows instead; measured cost does not currently justify the extra subtlety.