[Bug]: History compaction reads every event's payload on each pass (1.1 GB scanned to delete 60 events) #21290
Note
Claude Opus 5.5 in Claude Code, writing on behalf of @josephv123. Measured on his machines on 2026-10-18.
Before submitting#
- I searched existing issues and did not find a duplicate. Related but different: #20203 (a compaction DELETE triggering a main-thread WAL checkpoint; none happened in this run), #19780 and #21154 (what compaction keeps or deletes), #19435 (which added this compaction).
- I included enough detail to reproduce or investigate the problem.
Area#
apps/server
Steps to reproduce#
- Use a database with a few months of history (here
statev2.sqliteis 2.06 GB andorchestration_eventsholds about 188k rows and 1.12 GB). - Start the server and wait a minute for
compactEventStore(apps/server/src/orchestration-v2/ProjectionMaintenance.ts) to run. It runs again every 6 hours. - Look at its
sqlitespans inserver.trace.ndjson, or run its discovery query (pagingORDER BY sequence DESC LIMIT 50) against a copy of the database.
Expected behavior#
Finding the few events that can be deleted costs roughly in proportion to what changed, or at least doesn't read every stored payload.
Actual behavior#
Each pass reads essentially the whole events table, payloads included.
The discovery query selects event.application_event_version for every row. In the table's column order (PRAGMA table_info(orchestration_events)) it comes after payload_json and metadata_json. So SQLite has to walk every row's payload, including all the turn-item.updated rows that compaction never deletes.
Measured on nightly 0.1.1-nightly.20261018.3315 (acerlinux, Ryzen 7 4700U, ext4 on NVMe). The compaction ran at 11:54, one minute after startup:
| Discovery | 3,752 page queries, 17.36 s wall, 16.32 s of SQL. Per statement: p50 3.6 ms, max 33 ms. |
| Deletes | 20 statements, 60 events, all from the first ~2 s of pages. The remaining ~15 s deleted nothing. |
| Connection | Every statement ran on the main-thread writer (db.connection = "writer"), yielding between pages. |
| Logged result | Compacted orchestration history { deletedEventCount: 60, deletedReceiptCount: 0, reclaimableBytes: 28672 } |
Re-running the same paging on a read-only copy of that database (node:sqlite, SQLite 3.54.0):
| Query | Bytes read per pass | Time warm / cold |
|---|---|---|
| Current discovery query | 1,125 MB | 6.0 s / 9.8 s |
Same paging, only columns stored before payload_json | 466 MB | 0.45 s / 6.8 s |
Earlier runs in the same log deleted 16–444 events per pass. So nearly all of each pass is re-reading history that was already compacted. On the 17–22 GB databases reported in #19780 and #21154, the same scan would read roughly ten times as much per pass. That's inferred from the scaling, not measured.
Impact#
Minor bug or occasional failure
Version or commit#
0.1.1-nightly.20261018.3315; the query is unchanged on origin/main (e2cc34f1e4)
Environment#
Ubuntu 26.04, t3 serve systemd user service, auto_vacuum = 0 (database created before incremental auto vacuum was the default)
Workaround#
None needed for correctness. t3 doctor shrinks the file but doesn't change the per-pass scan.
Possible fixes, smallest first:
- A covering index such as
(sequence, application_event_version, aggregate_kind, stream_id, event_type), so discovery only touches the payload of rows whoseentity_idit actually needs. - Remember the highest sequence each pass has examined, and only revisit entities that received new events since then. Older events only become obsolete when a newer event arrives for the same entity.
juliusmarminge added labelsenhancement
juliusmarminge added labelsvia-triage
juliusmarminge closed this
Sign in to join this conversation.
Note
Grok responding on behalf of Julius.
Closing this as a performance request rather than a bug. Reading the whole event history on every pass is deliberate:
compactEventStorepages every event newest first, and the code notes that filtering before the limit could still scan the entire history. The payload cost you measured comes fromapplication_event_versionbeing added withALTER TABLE, so it is stored afterpayload_json. Nothing is deleted or kept incorrectly. Compaction blocking the server is tracked in #20203, and the open #20877 moves discovery reads off the main connection.Your measurements are useful. Please bring the covering index and incremental discovery ideas to Ideas: https://github.com/pingdotgg/t3code/discussions/new?category=ideas