Repository navigation
Closed
Labels
enhancementNew feature or request
Description
#1 (PR #79) stops the cache from only growing: retention pruning, tg db vacuum, sizes in tg status, and a contentless search index. It doesn't settle how tg should store a whole account's history for years. This issue sets the goals for that, so later changes can be measured against them.
North star
- Predictable size. A user can tell how big their cache will get and keep it under a limit they choose. A busy account with years of history stays at a size people accept on a laptop.
- Big caches stay fast. Reads (
messages search,list,tg sql), sync writes and startup don't slow down noticeably as a cache grows to millions of messages. - Old data can leave hot storage. History past an age or size threshold can be compacted, moved to an archive file or dropped from the local copy (it can be fetched again), without breaking the "messages are eternal" rule for what stays.
- Nothing is lost silently. Compacting, offloading or migrating never drops data the user didn't ask to drop, and every step is reversible or documented.
tg sqlstays useful. Agents and people can still query the cache with SQL, and the schema stays documented.
What we know (from #79, made-up data)
raw_jsonis the biggest part: 43% of the file for text-only messages (about 540 B per message) and 61% with 1 in 10 photos (about 1.25 KB per message).- A photo message's
raw_jsonis about 7.7 KB becauseUint8Arrayfields (fileReference, the stripped thumbnail) serialize as one JSON key per byte. Base64 took one such message from 7.1 KB to 1.3 KB. Per-row gzip helped little (706 B to 448 B). - The contentless FTS5 index saved 11–18% of the file.
Questions to answer
raw_json. Encode binary fields compactly (base64), drop fields we can always rebuild, store a normalised subset, or compress with a shared dictionary (zstd with a trained dictionary instead of per-row gzip). What doestg sqllose with each?- Media metadata. File references expire anyway; do we keep them, and in what form?
- Offloading. Archive old messages into separate files (per account and period, SQLite or Parquet), into a cold table, or drop them and re-fetch on demand? How do search and
tg sqlreach archived data? - Limits. A configurable size or age budget per account, what happens at the limit, and how
tg statusreports it. - Storage engine. Keep SQLite (
bun:sqlite, built in, no native deps) or move part or all of the data to something else:- DuckDB: columnar and compressed, good for analytics and archives, but weaker at many small writes and needs a native package;
- Turso / libSQL, or the Rust rewrite of SQLite: SQLite-compatible, but what do they add for a single-user local cache?
- Parquet files for archives only, next to SQLite for hot data.
Compare them on size, write rate for the daemon, query speed, binary size, cross-compiling all five release targets, and Bun support.
- Big caches. WAL size, index bloat, migration time on a multi-GB file, VACUUM time and the disk space it needs.
Done when
- A benchmark on generated made-up data (for example 1M and 10M messages with a realistic mix) reports bytes per message, write rate and query times, and can run again later.
- A short design doc in
docs/picks the approach for each question above, with numbers. - Follow-up issues exist for each change the doc picks.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or request
Activity
neoromantic commentedon Oct 22, 2026
Related: #81 (getting the text and snippets back from the search index in
tg sql) and #82 (semantic search, which adds vectors to the storage budget).neoromantic commentedon Oct 28, 2026
Decisions, 2026-10-28.
Done in this round (in progress as PRs):
--phrasefor the old behaviour (tg messages search treats the whole query as one phrase #84);--since/--until,--sort relevance(bm25),--count(Search filters: date range, relevance order and match count #85);--snippetand amessage_search_textview fortg sql(tg sql: the message text and highlighted snippets for search matches #81);tg sqlread-only at the SQLite level (tg sql lets PRAGMA writes through #88).raw_jsonbyte arrays as base64 for new rows (raw_json: store byte arrays as base64 #86). Unchanged re-fetched messages are no longer rewritten or re-indexed, and the unusedfetched_atindex goes (A re-fetched message is rewritten and re-indexed even when nothing changed #87).Answers to the questions above
raw_json: keep the full TL object, never a normalised subset. Byte fields as base64 (raw_json: store byte arrays as base64 #86, existing rows via Background, resumable migrations for big caches #89); dropfileReferenceonce attachment metadata exists (raw_json: stop storing fileReference #95); the stripped thumbnail stays, because after a deletion on Telegram it is the only trace of a picture. zstd with a trained dictionary only if the benchmark shows a large saving andtg sqlkeeps working (raw_json: measure zstd with a trained dictionary #97).bun:sqlite: no native dependency, one binary for all five targets, FTS5 built in). DuckDB needs a native package and is weak at the daemon's many small writes; libSQL/Turso adds nothing clear for a single-user local cache; Parquet only makes sense with an archive (Archive old messages into separate files #101). Revisit only if the 10M-message benchmark hits a wall.Still open here: the repeatable 1M/10M benchmark and
docs/storage.mdwith numbers, as in "Done when" above.feat(bench): storage benchmark on made-up data