Skip to content

Commit 47dc39a

Browse files
committed
chore(db): drop unused ANN indexes on embedding
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.
1 parent 1db2a00 commit 47dc39a

5 files changed

Lines changed: 27795 additions & 71 deletions

File tree

‎apps/sim/lib/knowledge/vector-columns.ts‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,17 @@ export function embeddingVectorValues(
5858
}
5959

6060
/**
61-
* Cosine distance between a chunk's vector and the query vector, in the exact
62-
* form the width's HNSW index was built on.
61+
* Cosine distance between a chunk's vector and the query vector, used for the
62+
* search layer's exact rerank over an already-bounded candidate set.
6363
*
64-
* The 3,072 column is compared through a `halfvec` cast because pgvector
65-
* indexes `vector` only up to 2,000 dimensions, so its index is on that cast
66-
* expression. Postgres matches an expression index by the expression, so a
67-
* plain `<=>` against the column here would silently drop to a sequential scan
68-
* — and the cast belongs here rather than at each call site precisely because
69-
* getting it wrong is invisible in the results and only shows up as latency.
70-
* `packages/db/schema.ts` records what the half-precision comparison costs.
64+
* `embedding` carries no ANN index — approximate retrieval runs against the
65+
* compact `embedding_search` projection — so this expression is never expected
66+
* to match one, and its caller wraps it to keep the planner from trying.
67+
*
68+
* The 3,072 column keeps its `halfvec` cast: the width that made the cast
69+
* necessary is unchanged, and `packages/db/schema.ts` records that the
70+
* half-precision comparison moved no distance measurably for the one model
71+
* that emits this width.
7172
*/
7273
export function embeddingDistance(
7374
dimensions: KbEmbeddingDimensions,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-- Drops the ten unused ANN indexes on "embedding". Approximate retrieval moved to the
2+
-- compact "embedding_search" projection; the only vector ordering left on this table is an
3+
-- exact rerank wrapped as (distance) + 0, which the planner cannot match to an index.
4+
-- The last app version that ordered by a bare distance has drained, so nothing reads these.
5+
COMMIT;--> statement-breakpoint
6+
SET lock_timeout = 0;--> statement-breakpoint
7+
-- migration-safe: DROP INDEX CONCURRENTLY IF EXISTS is idempotent on replay and takes no blocking lock.
8+
DROP INDEX CONCURRENTLY IF EXISTS "embedding_vector_hnsw_idx";--> statement-breakpoint
9+
-- migration-safe: DROP INDEX CONCURRENTLY IF EXISTS is idempotent on replay and takes no blocking lock.
10+
DROP INDEX CONCURRENTLY IF EXISTS "embedding_384_vector_hnsw_idx";--> statement-breakpoint
11+
-- migration-safe: DROP INDEX CONCURRENTLY IF EXISTS is idempotent on replay and takes no blocking lock.
12+
DROP INDEX CONCURRENTLY IF EXISTS "embedding_768_vector_hnsw_idx";--> statement-breakpoint
13+
-- migration-safe: DROP INDEX CONCURRENTLY IF EXISTS is idempotent on replay and takes no blocking lock.
14+
DROP INDEX CONCURRENTLY IF EXISTS "embedding_1024_vector_hnsw_idx";--> statement-breakpoint
15+
-- migration-safe: DROP INDEX CONCURRENTLY IF EXISTS is idempotent on replay and takes no blocking lock.
16+
DROP INDEX CONCURRENTLY IF EXISTS "embedding_3072_vector_hnsw_idx";--> statement-breakpoint
17+
-- migration-safe: DROP INDEX CONCURRENTLY IF EXISTS is idempotent on replay and takes no blocking lock.
18+
DROP INDEX CONCURRENTLY IF EXISTS "embedding_binary_hnsw_idx";--> statement-breakpoint
19+
-- migration-safe: DROP INDEX CONCURRENTLY IF EXISTS is idempotent on replay and takes no blocking lock.
20+
DROP INDEX CONCURRENTLY IF EXISTS "embedding_384_binary_hnsw_idx";--> statement-breakpoint
21+
-- migration-safe: DROP INDEX CONCURRENTLY IF EXISTS is idempotent on replay and takes no blocking lock.
22+
DROP INDEX CONCURRENTLY IF EXISTS "embedding_768_binary_hnsw_idx";--> statement-breakpoint
23+
-- migration-safe: DROP INDEX CONCURRENTLY IF EXISTS is idempotent on replay and takes no blocking lock.
24+
DROP INDEX CONCURRENTLY IF EXISTS "embedding_1024_binary_hnsw_idx";--> statement-breakpoint
25+
-- migration-safe: DROP INDEX CONCURRENTLY IF EXISTS is idempotent on replay and takes no blocking lock.
26+
DROP INDEX CONCURRENTLY IF EXISTS "embedding_3072_binary_hnsw_idx";--> statement-breakpoint
27+
SET lock_timeout = '5s';

0 commit comments

Comments
 (0)