Skip to content

chore(db): drop unused ANN indexes on embedding - #7955

Merged
waleedlatif1 merged 2 commits into
stagingfrom
chore/drop-unused-embedding-hnsw-indexes
Sep 18, 2026
Merged

waleedlatif1 merged 2 commits into
stagingfrom
chore/drop-unused-embedding-hnsw-indexes

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Drops ten ANN indexes on embedding that are maintained on every chunk write but never scanned: the five per-width *_vector_hnsw_idx and the five *_binary_hnsw_idx expression indexes
  • Approximate retrieval is served by the compact embedding_search projection and its own HNSW indexes; the only vector ordering left on embedding is the exact rerank, wrapped as (distance) + 0 so the planner cannot match an index expression
  • The binary_quantize expression indexes were never referenced by any query in any shipped version — they appear only in the schema, and an existing integration test already asserts the distance expression never matches binary_quantize(embedding.embedding...
  • Removes a substantial amount of index storage and removes the write amplification from maintaining ten HNSW indexes during ingestion
  • Corrects the embeddingDistance TSDoc, which described an index this table no longer carries

Why this is safe now

The previous release still had a branch that ordered by a bare distance (orderBy(exact ? sql(distance) + 0 : distance)), which did use the index. That branch was removed in the release currently serving traffic, so this drop lands one release after the last consumer drained — the contract half of the expand/contract the schema comment called for.

Verified before dropping:

  • EXPLAIN on pgvector confirms (v <=> $1) + 0 turns Index Scan using ..._hnsw into Seq Scan → Sort, so the shipped query shape provably cannot use these indexes
  • Index scan counters over a 13-minute window with live traffic (emb_doc_id_idx +1,386) showed +0 scans on both embedding_vector_hnsw_idx and embedding_binary_hnsw_idx

Uses DROP INDEX CONCURRENTLY IF EXISTS behind the repo's COMMIT; / SET lock_timeout convention so no blocking lock is taken on a hot table.

Type of Change

  • Chore / maintenance

Testing

bun run check:migrations origin/staging passes (backward-compatible). Lint, check:audits (46 audits), docs-manifest:check, and type-check all pass. Knowledge vector/search suites pass (172 tests). Snapshot diff verified to contain exactly these ten index removals and no column or table changes.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

Approximate retrieval is served by the compact embedding_search projection.
The only vector ordering left on embedding is an exact rerank wrapped as
(distance) + 0, which the planner cannot match to an index expression, and
the binary_quantize expression indexes were never referenced by any query.
The last app version that ordered by a bare distance has drained, so these
ten indexes were maintained on every chunk write while never being scanned.
@vercel

vercel Bot commented Sep 18, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Sep 18, 2026 2:10am UTC

Request Review

@greptile-apps

greptile-apps Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the removed indexes have no identified current consumer, and approximate search retains its projection indexes.

Summary

This PR removes ten unused HNSW indexes from the source embedding table after approximate retrieval moved to the indexed embedding_search projection.

  • Drops the indexes concurrently through an ordered, replay-safe migration.
  • Removes their definitions from the Drizzle schema and generated snapshot.
  • Gives the scale benchmark a dedicated HNSW index built after corpus ingestion.
  • Updates documentation for the remaining exact-rerank path.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Source embedding rows] --> B[Derived embedding_search projection]
  B --> C[Retained HNSW indexes]
  C --> D[Approximate candidate retrieval]
  D --> E[Bounded exact rerank on embedding]
  F[Migration 0360] --> G[Drop ten unused embedding HNSW indexes]
Loading

Reviews (2) · Last reviewed commit: "fix(knowledge): let the scale benchmark ..."

@cubic-dev-ai cubic-dev-ai Bot 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.

All reported issues were addressed across 5 files

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Fix all with cubic | Re-trigger cubic

Comment thread packages/db/schema.ts
The bulk-seed path required embedding_vector_hnsw_idx to exist, dropped it
before seeding and rebuilt it after. With no ANN index left in the schema it
threw before measuring anything. The benchmark seeds embedding directly and
never populates embedding_search, so it now creates its own HNSW index after
the load rather than borrowing a schema one, for both seed paths.
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot 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.

No issues found across 6 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit 431966e into staging Sep 18, 2026
34 checks passed
@waleedlatif1
waleedlatif1 deleted the chore/drop-unused-embedding-hnsw-indexes branch September 18, 2026 02:15

This branch was previously deployed

1 inactive deployment
Preview a67e1e1c Deployed Sep 18, 2026 by vercel[bot]
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.

1 participant