GeezerVison is showing github.com — New and incremental core databases do not reclaim deleted pages during their lifecycle · Issue #47591 · anomalyco/opencode
New and incremental core databases do not reclaim deleted pages during their lifecycle #47591DescriptionDescription
Scoped follow-up to #31526: new core databases use auto_vacuum=NONE, and opening an already-INCREMENTAL database or deleting an event aggregate does not reclaim its free pages.
Expected: enable incremental reclamation for new databases and make bounded, best-effort progress on compatible databases after initialization and aggregate deletion. Preserve retained records and existing NONE/FULL modes; do not perform a full startup VACUUM.
Legacy database conversion and live-event retention remain outside this issue, tracked by #31526 and #33356. Draft #47589 implements this limited fix; this issue records its acceptance scope without closing those broader reports.
Plugins
None in the isolated reproduction.
OpenCode version
Source dev at 337fd144d2ba144743368f78d9579a99cce175bd.
Steps to reproduce
- Initialize a disposable database through
Database.layerFromPath; inspect PRAGMA auto_vacuum (currently 0).
- In a separate disposable fixture, enable INCREMENTAL offline, insert and delete a 4-MiB payload, retaining one small record.
- Reopen through the same layer: the freelist does not decrease. Aggregate deletion likewise has no reclamation pass.
Reproduction tests are in #47589: from packages/core, run bun test test/database-storage.test.ts. The new-file and reclamation assertions fail against the original implementation and pass with the proposed patch. No real session data is needed.
Operating System
macOS, Bun 1.3.14; additional production-layer smoke test under Node 24.15.0.
AI-assisted investigation and report. Reacted by Emre Şafak and Michał Wielgosz ActivityGladSI commented on Oct 25, 2026 Measured on a real data dir: 95% of the file was free pages
Independent data point, v2.1.4 (Linux, standalone binary), a data dir that started on V1 and was upgraded in place:
|
before |
after VACUUM |
opencode.db |
17 203 MiB |
780 MiB |
page_count × 4096 |
4 403 971 (16.8 GiB) |
199 584 |
freelist_count |
4 201 935 = 16 414 MiB (95%) |
0 |
PRAGMA quick_check |
— |
ok |
Row counts were unchanged by the vacuum (133 sessions, 8 607 messages, 9 credentials). Live payload for scale: message 434 MiB (29 772 rows), part 181 MiB (124 309), session_message 69 MiB (8 568), instruction_blob 0.5 MiB.
Two things that support the acceptance scope:
- There is no reclamation path at all today.
packages/core/src/database/database.ts:40-44 sets journal_mode=WAL, synchronous=NORMAL, busy_timeout, cache_size and a wal_checkpoint(PASSIVE), and grep -rn "VACUUM\|auto_vacuum\|incremental_vacuum" packages --include="*.ts" returns no hits anywhere in the tree (tests included). So the only way to recover the pages is to stop the daemon and run VACUUM by hand — which is what I had to do, and no user would guess it.
- The peak is driven by the event log. In this database
event held 0 rows across 140 event_sequence aggregates: events are appended with their full payload and deleted per aggregate (packages/core/src/bus.ts). The file therefore tracks the busiest session's peak, and every later delete leaves those pages in the freelist permanently — the 16 GiB here are the shadow of a peak that no longer exists.
The cost argument for doing reclamation eagerly is stronger than it looks: VACUUM INTO a copy of the same live database (read-only connection, daemon still running) took 5.9 s and produced a valid 780 MiB file (quick_check ok, row counts identical). A bounded incremental_vacuum pass after initialization cannot plausibly be what makes startup slow. opencodejr commented on Oct 28, 2026 Thanks for the detailed, well-scoped write-up and the tests.
To restate the request: newly created core databases should use auto_vacuum=INCREMENTAL, and databases already in INCREMENTAL mode should make a small, capped reclaim pass after initialization and after an event aggregate is deleted. Existing NONE/FULL databases would stay as they are, with no full VACUUM at startup.
The current dev code never sets auto_vacuum or runs incremental_vacuum, so this is an improvement to how storage is maintained rather than a regression. We're tracking it as a feature request, and maintainers will consider it.
Related issues and PRs:
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|