Repository navigation
Description
Page SQL message pruning by id, matching Ruby after #113
Goal: maintenance plus parity. After solid-objects-ruby#113 (merged as 038af6b, shipped in Ruby v1.1.0), Ruby's message pruner keeps the same rows and stays fast on large tables by using correlated NOT EXISTS guards plus keyset paging. SQL message pruning in solid-objects-js should have that same observable behavior. No public API change.
Status: Spec. Do not start implementation until this issue is approved and labeled ready-to-implement. Merge and release still need Lucas.
Inspected JS main at ef6a15e (2026-12-02).
Problem
JS message retention already uses correlated NOT EXISTS guards. In src/repository.ts messageRetentionPredicate a message is prunable only when it has a completed_at_ms, is past the retention cutoff (including per-actor overrides), and no live row points at it:
- a
ready_messagesrow with thatmessage_id - a
claimed_messagesrow with thatmessage_id - a
dead_lettersrow with thatmessage_idorretried_message_id - an
effectsrow with thatmessage_idandstatus != 'completed' - a
broadcastsrow with thatmessage_idandstatus != 'delivered'
That already matches the six Ruby #113 guards in MessagePruner#prunable. One query-shape difference is called out under Scope: JS combines the two dead-letter probes into one OR.
The gap is paging. Repository#pruneRetention selects ORDER BY id LIMIT ? with pruneBatchSize and, after a delete, runs the same query again with no id > last_id cursor. The loop stops only when a batch deletes zero rows, so a short last page is always followed by one more full candidate query, and every batch re-walks leading rows that are still protected.
Ruby #113 fixed that in MessagePruner#prune_relation: each batch resumes after the last id of the previous page, and a short page ends the loop without another query. A full final page is still followed by one query that finds nothing.
Ruby measurements only, from the #113 PR description, on synthetic SQLite 3.54.1 (1M messages, 1.5M effects, 1.5M broadcasts, 25k old messages blocked by pending effects). Candidate query per 1000-row batch: 365, 271, 282, 227, 228, 225 ms before, with SCAN of effects and broadcasts; 31, 5.8, 4.8, 3.2, 2.9, 2.9 ms after. The first after-batch still walks the 25k blocked rows once; later batches resume after them. Those numbers are not JS measurements, and JS already uses NOT EXISTS, so the JS win is the missing keyset resume, not a rewrite of the guards. This issue does not claim a JS wall-clock result.
Existing coverage in test/retention.test.ts checks that only terminal unreferenced message history is pruned and that per-actor retention is honored. It does not cover batch-boundary paging, cursor resume, or a large mixed fixture.
Scope
SQL Repository#pruneRetention for target: "messages" (reached from runtime.retention.prune({ target: "messages" })):
- Keep correlated
NOT EXISTSguards. The messages pruned and retained must stay the same as today's predicate on the same data. - Page by primary key the way Ruby #113 does:
ORDER BY id, at mostpruneBatchSizeids per batch, each later batch considering only ids after the last id of the previous candidate page. Message ids in JS are random UUIDv4 values stored asTEXT(column type insrc/schema.ts; assigned withrandomUUID()insrc/repository.ts, which iscrypto.randomUUID()insrc/platform/uuid.ts). The cursor is stillid > last_idunder the same collation asORDER BY id, not Ruby's integerlast_id + 1. A row inserted during a prune can sort below the cursor and wait until the next run, where Ruby's autoincrement would catch it in the same run. That is acceptable, because new rows are not past retention. - Keep JS's per-batch transaction and the DELETE that re-applies the retention predicate. Do not drop either just to look like Ruby.
- A short candidate page (fewer than
pruneBatchSizeids) ends pruning without another candidate query. That stop keys off the candidate page length, not the DELETE change count, because the re-applied DELETE can remove fewer rows than the page held. A full final page is followed by one query that finds nothing. This is the batch semantics in Ruby #113's tests (test/integration/message_pruner_test.rbat038af6b, "a short batch ends pruning without another query" and "a full final batch is followed by one query that finds nothing"). previewfor messages must still report the same count thatprunewould delete on a stable dataset. Preview may stay a single count query; it does not need to page.- If the shared
pruneRetentionloop is what changes, instance and process pruning must still delete the same rows as today. This issue does not add product work for those targets. - The two dead-letter guards may stay one
ORor be split into twoNOT EXISTSprobes, as Ruby #113 does (DeadLetteronmessage_id, andDeadLetteron non-nullretried_message_id). Either shape is in scope only if the pruned set is unchanged. Splitting is not an index change; JS already indexesdead_letters.retried_message_id.
Acceptance criteria
- For the same data, message
pruneandpreviewresults match today's behavior: the same messages are deleted, and the same messages are retained. That includes every combination of the six guards, effect statusespending/processing/completed/dead, broadcast statusespending/processing/delivered/dead, a dead letter that was retried (both the original and the retry stay), a message still inside the default retention window, and per-actor retention overrides. Ruby #113's tests intest/integration/message_pruner_test.rbat038af6bare the behavior reference. - Paging deletes every prunable message across batch boundaries when protected rows sit before, between, and after prunable ones, including counts that fall around
pruneBatchSize(Ruby #113 covers 0–7 prunable rows at batch size 3). A second prune then deletes nothing. - Each batch after the first resumes after the last id of the previous candidate page. A short page (fewer than
pruneBatchSizecandidate ids) is the last page and does not run another candidate query. That stop keys off the candidate page length, not the DELETE change count, because the re-applied DELETE can remove fewer rows than the page held. A full final page is followed by exactly one query that finds nothing. - A large-data regression test comparable to Ruby #113's SQLite fixture (300 messages, 10 completed effects and 10 delivered broadcasts per message, plus leading protected rows so a paged run must skip work already passed) asserts that a paged prune deletes the same messages as the current unpaged predicate and that later batches resume after the last id already considered. Ruby's additional SQLite
EXPLAIN QUERY PLANassertion (SEARCHeffectsandbroadcastsbymessage_id, noSCANof those tables) must not be forced by adding indexes. JSsrc/schema.tsatef6a15ehas nomessage_idindex oneffectsorbroadcasts; this issue does not add one. - Existing message retention tests keep passing.
- CI stays green on the current SQL adapter matrix in
.github/workflows/ci.yml: SQLite (qualityandfloor), PostgreSQL 14 and 18, and MySQL 8.0 and 8.4. The other required jobs stay green. CHANGELOG.mdgets a bullet under## Unreleased(no version heading) that tells a user message pruning is fast on large tables because each batch resumes after the last id, the same messages are pruned on every SQL database, and no migration is needed. Write it for the user, asAGENTS.mdrequires.
What not to build
- No index or schema changes, and no migration. The Ruby index migration is a separate spec.
- No public API change:
runtime.retention.preview/prune,RetentionOptions,RetentionResult,pruneBatchSize, and retention settings stay as they are. - No change to Durable Objects pruning in
src/cloudflare/storage.ts. That path uses a different schema and is not this issue. - No release PR, version bump, or publish. Merge and release still need Lucas.
- No edits or comments on other issues.
Activity