LanceDB stores are never compacted: fragments accumulate per upsert and inflate storage, query cost and memory
Summary
LanceDB stores accumulate one table version and one data fragment per upsert and are never compacted. On a long-lived self-hosted deployment this grows without bound, inflating storage, query cost and process memory.
Mechanism
LanceDBAdapter.py writes through merge_insert("id"):
collection.merge_insert("id") # LanceDBAdapter.py:522Every merge_insert creates a new table version and a new data fragment. LanceDB does not compact on its own — that is what table.optimize() / compact_files() / cleanup_old_versions() are for.
Grepping the codebase, the only optimize() call is inside a one-off migration:
cognee/modules/migrations/versions/_vector_rekey.py:143: await optimize()Nothing in normal operation ever compacts, so fragments accumulate for the life of the deployment.
Measured on a real deployment
Self-hosted cognee/cognee:latest (reports 1.5.3-local), defaults throughout: sqlite relational, lancedb vector, ladybug graph, fastembed / BAAI/bge-small-en-v1.5 at 384 dims. Roughly one week of steady agent writes.
| before compaction | after | |
|---|---|---|
| version files | 6,592 | 48 |
| data fragments | 6,557 | 27 |
storage (/cognee-storage/system) |
937 MB | 411 MB |
| container anon memory, floor after restart | 1046 MiB | 718 MiB |
Worst single table:
EdgeType_relationship_name.lance versions=1446 fragments=1451 269 MB
SessionQAVector_text.lance versions= 878 fragments= 876 63 MB
TextSummary_text.lance versions= 859 fragments= 857 60 MB
DocumentChunk_text.lance versions= 859 fragments= 857 62 MB
Entity_name.lance versions= 828 fragments= 828 83 MBThe underlying corpus is 5.3 MB of ingested data producing a graph of 2,461 nodes and 4,117 edges. Nearly all of the 937 MB was version and fragment overhead.
Why it matters beyond disk
The container's memory climbed roughly 230 MiB/hour under normal use and hit its cgroup ceiling repeatedly, forcing reclaim. We first assumed a Python leak, then glibc arena retention, and MALLOC_ARENA_MAX=2 did not fix it. Fragment count tracks the growth far better: every query has to open every fragment, and the count only ever rises.
Reproduction
- Run the published image with default providers.
- Drive normal
add+cognifytraffic for a few days. find /cognee-storage/system/databases -path "*.lance/data/*" -type f | wc -l
Workaround
Offline compaction, roughly 12 seconds for the store above:
import lancedb
from datetime import timedelta
db = lancedb.connect(store_path)
for name in db.table_names(): # note: list_tables() returns paginated
db.open_table(name).optimize( # tuples in lancedb 0.33.0
cleanup_older_than=timedelta(seconds=0)
)Row counts are unchanged either side (verified: 20 tables, 60,056 rows, zero drift).
Suggestion
Compact as part of normal operation rather than leaving it to operators. Options, roughly in order of how invasive they are:
- Call
optimize()on a table after N upserts, tracked per table. - A periodic maintenance task in the server, interval configurable.
- At minimum, document that self-hosted deployments need a compaction job, since nothing in the current docs mentions it.
Happy to send a PR if you have a preference on which of these fits the architecture.
Environment
cognee/cognee:latest, image built 2026-08-24, reports1.5.3-locallancedb0.33.0- Linux, Docker, 4 vCPU / 15 GiB, container capped at 4 GiB
Fix in flight (PR topoteretes/cognee#5150**, supersedes contributor PR** topoteretes/cognee#4833**):** topoteretes/cognee#4833 had three reproduced defects — exporting the new setting into the vector factory raised TypeError; the default subprocess table rejected cleanup_older_than, so maintenance was skipped; and immediate removal of old versions made a reader on a recent snapshot fail with a missing-file error. topoteretes/cognee#5150 keeps the contributor's interval-based maintenance but calls optimize() with LanceDB's default retention window (supported by both local and subprocess tables); the interval stays a vector setting read by the adapter. Contributor tests 3 passed; new snapshot/proxy regressions 2 failed before / pass after.
Source: topoteretes/cognee