Skip to content

Find the stored outputs an input's range touches by index - #3240

Open
Sinity wants to merge 1 commit into
masterfrom
pib16-output-ranges
Open

Sinity wants to merge 1 commit into
masterfrom
pib16-output-ranges

Conversation

@Sinity

@Sinity Sinity commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Admission now finds the stored outputs an input's range touches through an index over the range each live output covers. It no longer pages every output membership of the derivation and reads each output's payload. The lookup reads the ended outputs that overlap and one row per key that has an open output, whatever the derivation has stored.

What was wrong

The range an output covers was recorded only in its payload. For every admission batch that carried an input with an affected range, the writer walked all memberships of the derivation in pages of 256, archived ones included, and read the payload of every live one. On the deployed store state.intervals has 976,228 memberships, 89,917 of them live: about 3,800 pages per affected batch. On 2026-11-26, in 240 one-second samples of the writer, seven had a transaction older than 5 s and 39 of the 114 in-transaction samples were in the paged membership read.

What changed

  • A derivation declares what its outputs cover, once: Keyed::OUTPUT_RANGES names, per output type, the payload fields for the start, the end, an open flag and a fence. AffectedRange carries only the output type and the range the input asks about. The five declarations are the ones every input repeated before (state.intervals, work.episodes, activity.days, activity.sessions, analysis.products). key_match had no user left after unit runs got their own derivation and is removed with its key paging.
  • derivation.output_ranges holds one row per key and live output of a declared type: start, and end (the fence when the payload has no end; null when there is neither or the output is declared open). The batch of keys that makes an output a member writes the row from the staged payload; an output stored earlier that joins another key keeps the range stored with it. archive_at deletes the output's rows in the transaction that archives it. Times are read with core.payload_time, so an unreadable time is absent instead of an error, and an output with no readable start has no row.
  • The lookup (sinex_store::output_ranges::affected_keys) joins the ranges of one admission where they share an instant, then runs two statements. Ended outputs come from a GiST index on (derivation, event_type, [start, end]), with the stored bounds compared afterwards, inclusive on both sides as before. Open outputs come from a B-tree on (derivation, event_type, key, started_at), read one key at a time for the key's earliest open output.
  • Upgrade step output_ranges creates the table, fills it from the live outputs of the five derivations and builds the indexes.
  • A replaced reading's source comes from its replacement's live row. archive_at counted a replaced reading under the source it read from core.events, one lookup per reading, because the live row already named the replacement. The replacement is a reading of the same record, so its row gives the source. On the deployed store that lookup decompressed about 27,000 rows per reading for ten old state.intervals outputs; 495,517 of that derivation's archived outputs were archived this way.
  • cargo xtask bench output-ranges runs the two lookup statements over generated ranges at 10,000, 100,000 and 1,000,000 stored outputs and records what each read.

Decisions

  • A side table of live ranges, not columns on derivation.outputs. Memberships outlive their outputs: nine in ten of state.intervals' are archived. Keeping those out of an index over membership rows needs a liveness flag written at every archive, and every membership of a derivation that declares nothing would carry the columns. A table of live rows is deleted from at archive and holds only what is asked about.
  • Not core.live_intervals. It keeps no fence, has no row for an instant and no key, and work.episode and activity.day are not interval kinds with these fields.
  • The key is in the row, and open outputs are indexed by key. 63,764 of the 89,917 live state.intervals outputs have neither an end nor a fence, in 5 keys. An index of open outputs by start would return all of them to every lookup and need a membership probe for each; by key it reads one row per key.
  • Bounds in either order never raise. The range index is built and asked with the lesser and greater bound, and the stored bounds decide. An output or a question that ends before it starts compares as the payload test compared it.

The upgrade on the deployed store

Schema 52 to 53 (the number is the next free one at landing). With sinexd stopped, the step reads the events of the five derivations by source and type and writes one row per key and live output. Measured read-only on the deployed store on 2026-11-26: the fill's read for state.intervals took 33.5 s (1,090,652 events read, 89,919 rows returned, 386,246 blocks read from disk); the other four derivations hold 131 live outputs between them. Expect about a minute, and up to a few minutes when the disk is contended; three index builds over about 90,000 rows follow. unit.intervals declares no range and gets no rows.

Verified

  • admission/tests/dirty_ranges.rs: the boundary, missing-end, open, archived, wrong-kind and other-derivation cases of the former test with the same expected keys, the outputs now admitted through a batch of keys so the rows are the ones admission writes; fences, unreadable times and reversed bounds; rows written, copied to a second key, kept by a repeat and deleted by an archive; and the plan of both statements reading the same rows at 4,096 and 65,536 stored outputs.

  • sinex-schema: the step fills the rows of live outputs per key on compressed chunks, and leaves the upgraded catalog equal to a fresh one.

  • cargo xtask bench output-ranges (agentctl job 4480, generated ranges of one derivation with 256 keys, half of them with open outputs, on this machine's disk):

    stored outputs ended outputs read blocks ms open outputs read (128 keys) blocks ms
    10,000 2 12 0.29 128 297 1.2
    100,000 2 12 0.28 128 451 1.5
    1,000,000 2 13 0.46 128 580 2.2

    The rows read are the same at the three sizes; the blocks follow the depth of the index. The traversal this replaces read every membership and every live output's payload: about 3,800 pages and 90,000 payloads for state.intervals on the deployed store. That figure is from the deployed store, not from this bench, which has no case for the removed code.

  • The store, schema and daemon tests the change can affect, locally on the rebased tree (agentctl job 4467, after compile job 4466); the hosted check and test jobs on this head.

Not verified here: the step on a restored copy of the deployed database, and the writer's transaction ages on the deployed daemon. Both need the deploy.

Task: sxnext-pib.16

🤖 Generated with Claude Code

https://claude.ai/code/session_01UuFixsXwismFLxbfWcuXHU

Admission found the keys whose stored outputs an input's range covers by
paging every output membership of the derivation, archived ones included,
and reading each live output's payload, because the range an output covers
was recorded only there. One admission cost a pass over the derivation's
whole history: about 3,800 pages for state.intervals on the deployed store.

A derivation now declares what its outputs cover, once, and the store keeps
that range for every live output of a declared type in
derivation.output_ranges: written by the batch that makes the output a
member of a key, deleted when it is archived. The lookup reads a range
index over ended outputs and an index of open outputs one key at a time.
An upgrade step fills the table from the stored live outputs.

A replaced reading is counted under its source from its replacement's live
row instead of a lookup of its own on the events. A bench case records what
the lookup reads at three sizes of stored outputs.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UuFixsXwismFLxbfWcuXHU
@Sinity

Sinity commented Nov 26, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T23:28:07.960013Z c6ab0ad Manual request
🔒 Security Review ✅ Completed 2026-11-26T23:31:31.781411Z c6ab0ad PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: c6ab0ada24

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant