Skip to content

history.db never shrinks — a database with one row still occupies 87.8 MB after 90-day cleanup #3393

Open

Description

@HetCreep

cleanup_old() deletes rows past the retention window on every insert, but nothing ever reclaims the pages. Retention therefore bounds the row count, not the file size, and the database stays permanently at its high-water mark.

Provenance: AI-assisted source audit, human-directed, at v0.44.2 (700bdde). The numbers below were measured on this machine.

Measured

Populated a history db to 500k rows, aged every row past the 90-day window, then ran rtk so cleanup_old() fired:

rows in commands table : 1
file size              : 87.8 MB
freelist_count         : 22480
page_count             : 22486        (99.9% of pages are free)
auto_vacuum            : 0

The space is never handed back to the filesystem. It is reused by future inserts, so this is not unbounded growth — but a user who had one heavy month keeps that footprint indefinitely.

There is a measurable cost too: rtk gain on that empty-but-88 MB file takes 92 ms, versus 50 ms on a freshly created one.

Source

  • src/core/tracking.rs:454-465 — cleanup_old() issues DELETE, and that is the whole reclamation story.
  • Grepping src/ for VACUUM, auto_vacuum, and ANALYZE returns nothing. No PRAGMA auto_vacuum is set at connection time (tracking.rs:271-274 sets journal_mode=WAL and busy_timeout), and auto_vacuum cannot be enabled on an existing database without a full VACUUM anyway.

Related, smaller

rtk gain --history and --failures scan more than they need on a large table:

  • tracking.rs:595-618 get_summary_filtered selects four columns for every row and sums them in a Rust for loop instead of using SELECT SUM(), COUNT(). Measured on the same database: 887.5 ms vs 92.1 ms at 500k rows (9.6×); 177.9 ms vs 18.7 ms at 100k.
  • tracking.rs:598,656,684 use WHERE (?1 IS NULL OR project_path = ?1 OR project_path GLOB ?2). EXPLAIN QUERY PLAN reports SCAN commands plus temp b-trees for GROUP BY and ORDER BY; the same predicate written without the parameter-dependent OR reports SEARCH commands USING INDEX idx_project_path_timestamp.

End to end, rtk gain goes 50 ms (empty) → 101 ms (10k) → 228 ms (100k) → 920 ms (500k rows). At roughly 1.1k commands/day, 100k rows is about what a 90-day window holds for a heavy user.

Possible directions (untested — no programmer has reviewed these)

  • Run VACUUM occasionally — e.g. when freelist_count exceeds some fraction of page_count, checked at cleanup time rather than on every insert.
  • Or set PRAGMA auto_vacuum = INCREMENTAL for newly created databases and call incremental_vacuum after cleanup. Existing databases would need a one-time VACUUM to switch.
  • Independently: fold the summary aggregation into SQL, and split the project-scope filter into two prepared statements so the index can be used.

For what it is worth, the streaming path in rtk discover is a good counter-example in the same codebase — 837 MB across 1109 files at a flat ~20 MiB.

Activity

  1. added
    bug
    help wanted
    on Aug 4, 2026
  2. pszymkowiak commented on Aug 4, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟡 Priority medium
    🎯 Confidence 90%

    Summary

    history.db never reclaims disk space after row deletion (no VACUUM/auto_vacuum), keeping it at its high-water-mark size and slowing queries; recommend periodic VACUUM or incremental auto_vacuum, plus SQL-side SUM/COUNT aggregation and an index-friendly project-scope filter.

    📁 Relevant files
    • src/core/tracking.rs

    Triaged automatically by wshm · This is an automated analysis, not a human review.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:performancebughelp wantedpriority:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions