source-index-report.md 4.9 KB

Source projection index: correctness and write-cost verification

Verified on 2026-11-10 using a private PostgreSQL copy of current production source data. The copy used the real existing row-digest triggers and the new projection triggers; readiness functions were not replaced with unconditional success. Production source tables were only read to create the copy. Both temporary schemas and their restricted diagnostic gateways were removed after verification.

The public receipt is source-index-concurrency.json. It omits company identifiers, record identifiers, temporary schema names, and data fingerprints.

Correctness experiments

  • READ COMMITTED and REPEATABLE READ: Writer A changed a lesson and held its projection target lock. Writer B changed the related appointment, skipped the unavailable projection lock, and committed its real source change. Writer A committed last, leaving a confirmed stale projection. Before A committed and after both commits, current ordered source JSON and the complete source version matched the original SQL exactly; all guards remained ready.
  • Concurrent fact insertion: Writer A changed a canonical CommonModel mapping while writer B inserted a fact for the new effective legacy identity. After both committed, the fact remained present in the derived index but was absent from authoritative source output. Original and indexed source JSON/version matched before and after the first commit.
  • Canonical deletion: Removing the canonical lesson made the historical fact visible again. Original and indexed results remained identical.
  • Tenant move: Moving a lesson between two test companies preserved exact source output and versions for both the departure and destination companies. The changes were rolled back.
  • Local regressions: 41 targeted payroll, bulk-source, and projection-index tests passed, including missing/stale/orphaned projections, zero pay snapshots, schema and trigger drift, full reseeding, unknown dates, filtered metadata, and hidden fact rows.

Write-cost correction

The first implementation unnecessarily refreshed fact visibility from lesson writes and searched canonical lessons while computing each fact projection. This added up to 710 ms for an appointment touching five lessons. Fact projection content depends only on the fact and selected appointment; canonical existence is already established independently at every read. Removing that redundant propagation and lookup preserved all exact-read checks while reducing the sampled write overhead. CommonModel changes outside models 54 and 59 now return before JSON serialization or metadata reads.

Each round contains 18 UPDATE observations: nine paired examples with the new trigger enabled and disabled. Original digest triggers remained enabled. Samples used alternating pair order and rolled back their changes. Times are database-call wall-clock milliseconds.

Anonymous example Canonical lesson fanout Before: off / on After: off / on
lesson_1 — 2 / 155 3 / 5
lesson_2 — 1 / 18 2 / 13
lesson_3 — 2 / 172 3 / 3
appointment_1 5 1 / 701 2 / 39
appointment_2 5 3 / 710 4 / 33
appointment_3 4 1 / 585 2 / 34
common_1 (model 54) — 7 / 181 10 / 20
common_2 (model 59) — 2 / 17 3 / 8
common_3 (model 60) — 4 / 2 7 / 3

With the final implementation, lesson updates measured 3–13 ms, appointments touching four or five canonical lessons measured 33–39 ms, and sampled CommonModel updates measured 3–20 ms. These are individual paired samples, not p75/p95 estimates. The results do not establish sustained synchronization throughput, production response latency, or browser/device acceptance.

Safety boundary

The derived index never determines which source rows exist or which facts are suppressed. Every read rebuilds those relationships from current source rows, checks the current dependency signature, and only then reuses a stored projection. Missing or stale entries are recalculated in the reader’s snapshot; obsolete extra entries cannot create payable source rows. Writers use nonblocking per-target advisory locks and may skip projection upkeep.

TRUNCATE and metadata/authority drift cause authoritative fallback. Re-enabling acceleration requires a new generation and a complete reseed, rather than changing a readiness flag. The experiments changed no settlement, payment, or original financial-function definitions.

Reproduction

Run the explicit manual runner only in the authorized environment:

node --import ./tmp/admin-speed-round2-2026-11-10/env.mjs scripts/tests/run-payroll-source-index-concurrency.mjs

The runner accepts no arbitrary SQL from its remote caller, restricts mutation to a generated private speed3_ schema, and removes the schema and temporary gateway in finally blocks.