Skip to content

feat(db): check free disk before a full vacuum - #164

Merged
neoromantic merged 1 commit into
mainfrom
feat/vacuum-disk-check
Nov 17, 2026
Merged

neoromantic merged 1 commit into
mainfrom
feat/vacuum-disk-check

Conversation

@neoromantic

Copy link
Copy Markdown
Owner

What and why

A full tg db vacuum (daemon stopped) needs free disk of about twice the cache at its peak: the new copy plus the WAL. A short disk used to show only when SQLite failed with SQLITE_FULL, after minutes of work, and the VACUUM was rolled back.

Now, before a full VACUUM, tg db vacuum reads the free disk of the cache's file system (statfs, bavail × bsize) and compares it with 2.2 times the cache file plus 64 MB. The storage benchmark's peaks were up to 2.16 times the file (3.37 GB for a 1.56 GB cache), so 2.2× covers every measured peak; the 64 MB covers small caches and what other programs write meanwhile. When the disk is short, it fails before it prunes or writes anything, with GENERAL_ERROR, details.reason: "disk_full" (the same reason the SQLite failure reports), needed_bytes and free_bytes. The suggestion: free some space, or, for a cache with incremental auto_vacuum, start the daemon and run tg db vacuum again, since the incremental mode needs no copy. The incremental mode (daemon running) is not checked.

tg status --format pretty shows the same check next to its vacuum hint when the hinted command would run a full vacuum.

Before / After

Before: tg db vacuum on a short disk ran for minutes, then failed with disk_full and rolled back.

After:

{
  "success": false,
  "error": {
    "code": "GENERAL_ERROR",
    "message": "Not enough disk space to vacuum the cache of account 1: a full VACUUM needs about 64.8 MB of free disk and only 19.1 MB is free. Nothing was changed.",
    "details": { "account_id": 1, "reason": "disk_full", "needed_bytes": 67973940, "free_bytes": 20000000 },
    "suggestion": "Free some disk space, or start the daemon (tg daemon start) and run tg db vacuum --account 1 again: then it releases the free pages in place, without a copy"
  }
}

tg status:

  The cache has free pages (over 10%, or left by a background migration); to give them back to the disk:
    tg db vacuum --account 1  needs 64.8 MB of free disk, 19.1 MB free

Tests

  • E2E (db.e2e.test.ts): enough free disk runs the full vacuum; too little fails at once with the details and the incremental suggestion, and leaves the pruned rows and free pages untouched; a cache without auto_vacuum gets the free-space suggestion only; the incremental mode ignores a tiny disk.
  • E2E (status.e2e.test.ts): the hint shows the shortfall for a full vacuum (daemon stopped, and daemon running with a cache without auto_vacuum), and not for the incremental mode.
  • Free disk is injected through the test-only TELEGRAM_SYNC_CLI_TEST_FREE_DISK_BYTES (documented in docs/testing.md).
  • bun run check, bun run test (2243 pass), bun run test:e2e (374 pass).

Docs: docs/cli-commands.md (the check, the error, the status hint), docs/storage.md (why 2.2× plus 64 MB), docs/testing.md, CHANGELOG.

Fixes #119

🤖 Generated with Claude Code

A full VACUUM needs free disk of about twice the cache at its peak, and a
short disk used to show only when SQLite failed after minutes of work.
tg db vacuum now compares the free disk of the cache's file system with
2.2 times the file plus 64 MB before it prunes or writes anything, and
fails at once with disk_full, the bytes needed and free. tg status shows
the same check next to its vacuum hint.

Fixes #119

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic force-pushed the feat/vacuum-disk-check branch from cc2ccba to 8cb99be Compare November 17, 2026 20:29
@neoromantic
neoromantic merged commit 885c0eb into main Nov 17, 2026
5 checks passed
@neoromantic
neoromantic deleted the feat/vacuum-disk-check branch November 17, 2026 20:33
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.

tg db vacuum: check free disk before a full VACUUM

1 participant