Skip to content

Archive a large set of derived outputs in bounded transactions - #3236

Merged
Sinity merged 5 commits into
masterfrom
pib20-bounded-archive
Nov 26, 2026
Merged

Sinity merged 5 commits into
masterfrom
pib20-bounded-archive

Conversation

@Sinity

@Sinity Sinity commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Archiving a large set of derived outputs no longer holds the writer for the whole set. A key that stops producing most of its outputs, and a version switch that recomputes history, now archive in transactions sized by how long the last one held the writer, with live batches admitted between them. The statement that made the old transactions take minutes is replaced by a read that costs about the rows it returns.

What was wrong

On 2026-11-26 three keys with about 390,000 live outputs computed to nothing and their outputs were archived in consecutive writer transactions of 600 to 775 s; the commit position stood still and capture spooled. Two things caused it:

  • One statement did almost all the reading. To mark the keys that read an archived event, admission read each event's type, time and payload with one lookup per ID. On a compressed chunk each lookup scanned the chunk's batches. Measured on the deployed store with EXPLAIN (ANALYZE, BUFFERS) for 256 live state.interval outputs in a compressed chunk: 114.9 s, about 380,000 rows decompressed per ID.
  • Nothing bounded the transaction. A batch of keys archived every output its keys no longer produced, and a switch archived every live output of the derivation, each in the one transaction.

What changed

  • The payload read (mark_selected_dirty). The chain to archive comes from the live tables with each member's event type (archive::live_chain, the query the lifecycle already used, now shared with admission). Only the types some derivation reads have their payload read, once each, in ID order, 4,096 per statement, bounded by the group's inclusive IDs and its types. The same 256 rows as above: 0.47 ms, 2,000 rows decompressed. 4,096 IDs: 3.5 ms, 6,000 rows decompressed, 1.46 rows read per row returned.
  • A batch of keys archives a bounded number of outputs. DerivedScope::Keys carries replaces_at_most (the runner passes 4,096). A batch that would archive more is refused whole with Error::ArchiveFirst, which names the outputs. The runner archives them in DerivedScope::Archive batches, each its own writer job: 256 outputs in the first, then a size that doubles while a transaction holds the writer for under 125 ms and halves above 500 ms (writer::next_bulk_size, the rule the lifecycle driver had, now used by both). It then submits the batch again. The archive batch keeps the key dirty with the mark its computation saw, so the completing batch clears it and a stop in between computes the key again.
  • A version switch selects first and archives after. The selecting transaction registers the version, records the producer_switch operation with its position and no result, and schedules the keys held by the operation. The runner then lists the live outputs committed up to that position and archives them through the same archive batches (reason version_change, the operation's ID). DerivedScope::Switched ends the operation, releases the keys and resets a per-event derivation's position. A runner finishes unfinished switches when it starts, before its first pass. A keep_history switch is complete in the selecting transaction, as before.

Decisions

  • Archive before the new outputs, not after. The alternative admits the new outputs first and archives the old ones in parts afterwards. Readers would then see a key's new outputs beside the ones they replace until the last part. Archiving first leaves a dirty key with fewer outputs, which is the state a key computed for the first time is already in.
  • Admission names what to archive; the runner does not work it out. The runner could find the outputs a computation no longer offers by comparing record keys before submitting. That is an extra read of every membership of the batch's keys on every batch, and a second copy of what classification decides. The refusal costs one staged batch, and only when the bound is passed. A batch the database refuses for invalid data is still refused before anything of its keys is archived, so a refused key keeps its previous outputs.
  • The switch commits its decision before the archive. Archiving first would run the selection's input check after minutes of archiving; a refusal there would leave the previous version to recompute everything archived.
  • Not the lifecycle's source archive for a switch. It would record the rows as an archive operation without the version_change reason or the switch's operation ID, and a lifecycle operation's end has no place for the position reset.

Not changed: a per-event batch and a replay sequence still archive what they replace in their own transaction, and a key's new outputs still commit in one transaction.

Verified

  • a_large_replacement_archives_in_transactions_with_live_admission_between (sinexd): six outputs replaced with a bound of two are archived in three transactions, a live batch commits between the first and the second, and the key is dirty in between. With the previous code the archive is one position, so the assertion that the live commit lies between the first and last archive position fails.
  • a_switch_stopped_before_its_archive_ends_at_the_next_start (sinexd): after the selecting transaction alone the previous outputs are live, the operation has no result and the held keys are not computed; the next runner start archives them with version_change, ends the operation and the new version computes.
  • The existing switch, replay, lifecycle and admission tests cover the shared chain read and the changed payload read.
  • The hosted check and test jobs on this head.

Not verified here: the acceptance row that asks for a bench run reporting writer hold time while a 100,000-output key is archived. The bench has no case that replaces a large key; the figures above are plans read on the deployed store.

Task: sxnext-pib.20

🤖 Generated with Claude Code

https://claude.ai/code/session_01UuFixsXwismFLxbfWcuXHU

@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-26T21:31:32.209899Z 2a062b2 Manual request
🔒 Security Review ✅ Completed 2026-11-26T21:21:22.868664Z b33b638 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.

@Sinity

Sinity commented Nov 26, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2a062b2aab

ℹ️ 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".

Comment on lines +1154 to +1157
let scheduled = self.select(name, comparison, actor).await?;
// The version is selected. Other keyed work need not wait for the
// archive of the previous outputs, which holds this switch's keys.
self.finish_switches(Some(name)).await?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Prevent per-event passes from racing an unfinished switch

When a switch runs through the API while the daemon's background runner is active, select() releases the derivation lock before finish_switches() archives the outputs and resets progress. The background runner's per_event_pass() takes that same lock but never checks unfinished switches or held keys, so it can start processing new inputs from the old saved position during the archive. If its output batch commits after Switched resets the position, refusals::position() advances the position past the historical inputs again, leaving their archived outputs permanently uncomputed. Keep the switched derivation blocked until the switch finishes, including any pass already queued for its lock.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a3de2ed. A saved position only moves forward, so a batch computed from the old position and committed after the reset would have moved it past the history. The transaction that ends the switch is now submitted while finish_switches holds the derivations lock. A per-event pass holds that lock from reading its position to its last commit, so none is in flight when the position is reset, and the next pass reads the reset position. Passes that run while the outputs are being archived are left alone: the position is not reset yet, they read only new inputs, and their outputs are committed after the selection, outside what the switch archives. The lock is also where the switch is checked again, so two runners that both find it unfinished do not both end it.

@Sinity
Sinity force-pushed the pib20-bounded-archive branch 2 times, most recently from a8cdf88 to 8f7b09e Compare November 26, 2026 22:51
Sinity and others added 5 commits November 27, 2026 00:08
Replacing a key that no longer produced most of its outputs, and a version
switch that recomputes history, archived every previous output in one
writer transaction. On the deployed store three keys with about 390,000
outputs held the writer in transactions of 600 to 775 s while capture
spooled. Most of that time was one statement: each archived event was read
by its own lookup, which on a compressed chunk decompressed the whole
chunk's batches of that kind per event.

The payload read now takes the archived chain's event types from the live
tables, reads only the types a derivation reads, once each, in ID order in
groups bounded by their IDs and types.

A batch of keys states how many previous outputs it may archive. Admission
refuses a batch that would archive more and names them; the runner archives
them in transactions of their own, sized by how long the last one held the
writer, and submits the batch again. A version switch selects in one
transaction and archives the previous outputs the same way, holding its
keys and a per-event position until a final transaction ends it; a runner
finishes an unfinished switch when it starts.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UuFixsXwismFLxbfWcuXHU
The outputs a batch of keys archives were selected by a row in the keyed
live table only, and an archive batch accepted only such rows. An output
identified by its record location has its row in the other live table and
would have stayed live when its key stopped producing it. Both now use the
liveness the chain read uses.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UuFixsXwismFLxbfWcuXHU
A batch of keys with no bound serialized the field as null, which changed
the scope other producers of such batches write.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UuFixsXwismFLxbfWcuXHU
The transaction that ends a switch resets a per-event derivation's
position. A pass that read the old position before that and committed a
batch after it would move the position past the history again, since a
saved position only moves forward. A pass holds the derivations lock from
reading its position to its last commit, so the switch now ends under that
lock, after checking that another runner has not ended it already.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UuFixsXwismFLxbfWcuXHU
The test landed on master while this branch was open and builds a batch
scope without the new field.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UuFixsXwismFLxbfWcuXHU
@Sinity
Sinity force-pushed the pib20-bounded-archive branch from 8f7b09e to 32fb1b7 Compare November 26, 2026 23:08
@Sinity
Sinity merged commit 0621ceb into master Nov 26, 2026
2 checks passed
@Sinity
Sinity deleted the pib20-bounded-archive branch November 26, 2026 23:22
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