Skip to content

A deployment can ask for superseded chunks to give up their vector after a set number of days, so a re-pushed base stops carrying every past version in its index - #1492

Merged
WaylandYang merged 4 commits into
deeplethe:devfrom
re-cinq:feat/prune-superseded-chunks
Nov 16, 2026

Conversation

@gedaiu

@gedaiu gedaiu commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

For #1481.

A deployment can now ask for a superseded chunk to give up its vector after a number of days. It is off by default, so every existing deployment keeps what it has today. Setting UTOPIA_SUPERSEDED_VECTOR_DAYS turns on a daily prune_superseded job per base, which clears the vector of a chunk superseded longer ago than that window. Only the vector goes. The row, its text and everything that points at it stay, so version replay and chunk fetch at any as_of read as before, and the vector can be computed again from the text.

The window is clamped to 1 to 3650 days, so zero or a negative number cannot clear everything and a huge one cannot overflow. A deleted document's chunks are left whole, so a restore brings it back searchable by meaning.

What a deployment that opts in gives up is vector recall of a replaced paragraph at an as_of further back than its window. ADR 0019 has a dated note for this, and the comment in replace_chunks now says the same thing as the code.

On the HNSW index, I have not measured it yet and the ADR note says so. pgvector does not index a NULL vector, so a cleared chunk leaves the graph once VACUUM removes the dead tuple and that space is reused. I expect the file itself to shrink only after a REINDEX. I will post the real numbers from our large base once it has run there.

Migration 0126 adds a partial index on (kb_id, superseded_at) so the daily pass reads only superseded chunks.

Tests: an_old_version_keeps_no_vector (the row stays and the vector goes, a trashed document is untouched) and the retention::window unit tests for the clamp.

…hing cites is removed, so a re-pushed base stops carrying every past version in its index

Signed-off-by: Bogdan Szabo <bogdan@re-cinq.com>
…a document restored from the trash is still found by meaning

Signed-off-by: Bogdan Szabo <bogdan@re-cinq.com>

@WaylandYang WaylandYang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the numbers in #1481, and sorry it waited without an answer. The problem is real for a large 3072-dim base. We'd like a narrower shape:

  • Vectors only. Drop the embedding of an old superseded chunk, but keep the row. History stays readable for as-of replay and the deletion can't lose anything we can't recompute.
  • Opt-in. Off by default; a deployment turns it on and sets the window. The default behaviour for every other deployment stays as it is.
  • Record it. Revise ADR 0019 for the retention choice, and update the comment in `replace_chunks` (documents.rs, "向量留着"), which now argues the opposite of the code.
  • Clamp `days` to a sane range: 0 or less currently prunes every superseded chunk, and a huge value panics in `Duration::days`.

If you measured that the HNSW index actually shrinks after the prune (or needs a REINDEX), please note it in the ADR.

…tor, so a deployment that does not ask keeps its history searchable and one that does keeps every row

Signed-off-by: Bogdan Szabo <bogdan@re-cinq.com>
@gedaiu gedaiu changed the title A superseded chunk older than a week gives up its vector, and one nothing cites is removed, so a re-pushed base stops carrying every past version in its index A deployment can ask for superseded chunks to give up their vector after a set number of days, so a re-pushed base stops carrying every past version in its index Nov 10, 2026
@gedaiu

gedaiu commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Thank you, and no need to apologise. The narrower shape is better, and it is in 381eafc.

  • The job clears only the vector and keeps every row.
  • It is off unless a deployment sets UTOPIA_SUPERSEDED_VECTOR_DAYS, and the window is clamped to 1 to 3650 days.
  • ADR 0019 has a dated note for the retention choice, and the comment in replace_chunks agrees with the code again.

I have not measured the HNSW index after a prune yet, so the note says that plainly and describes what I expect, space reused after VACUUM and a smaller file only after a REINDEX. I will post the real numbers from our large base once it has run there. The title and description are updated to match.

@gedaiu

gedaiu commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

I looked at our large base for the index numbers. They are real, but they are not the clean before and after you asked for, so I would rather say exactly what they are.

5 Nov (from #1481) 12 Nov
chunk rows 714k 613k
superseded rows still holding a vector 458k 11k
live rows holding a vector 74k
HNSW index 5 GB 760 MB
chunks TOAST 8.5 GB

Two caveats.

  • This base has run our earlier shape since 6 Nov, with a 7 day window and the row delete, not the vectors-only job in this PR.
  • The database moved clusters in that week. 760 MB is about one page per vector for 85k vectors, so the index was rebuilt somewhere along the way. I can't show what VACUUM alone would have left.

What the numbers do show is the other half. The TOAST relation is still 8.5 GB for about 1 GB of vectors, so the space a cleared vector frees is reused and not given back. That matches the note in the ADR. If you would like it, I can put these numbers into the ADR note in place of "not yet measured".

@WaylandYang WaylandYang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is the shape we wanted. On the numbers: since they come from the earlier row-delete shape and a rebuilt index, I'd keep them in this thread rather than the ADR; the note as written is accurate. Optional follow-up: making chunks_superseded_idx partial on superseded_at IS NOT NULL AND embedding IS NOT NULL would let cleared rows leave the index, so the daily sweep doesn't rescan all history. Merging.

@WaylandYang
WaylandYang merged commit 0002406 into deeplethe:dev Nov 16, 2026
6 checks passed
gedaiu added a commit to re-cinq/utopia that referenced this pull request Nov 16, 2026
Sync upstream dev into re-dev (2026-11-16): deeplethe#1489, deeplethe#1492 and 11 more
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.

2 participants