Files
FabledGrimoire/alembic/versions/0139_embedding_vectors_inline.py
T
bvandeusenandClaude Opus 5.5 efba891f5e
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 14s
CI & Build / TypeScript typecheck (push) Successful in 33s
CI & Build / Python tests (push) Successful in 2m7s
CI & Build / integration (push) Failing after 2m15s
CI & Build / Build & push image (push) Skipped
perf(8190): the exact search reads vectors from the row and text only for the winners
Step 1 of milestone 669 "Knowledge reaches every project, work stays in its
own: a fast exact search, cross-project recall ranked and labelled", for
#6834 "auto_inject p90 latency has climbed since 10-23".

A search with no project in scope took about 1 s, because it ranked about
26k chunks exactly. Two costs on that path, neither of which changes a result:

- pgvector's vector type defaults to EXTERNAL storage, and a chunk row
  (about 1.5 KB of vector plus its text) is over the 2 KB toast threshold,
  so every vector sat in TOAST and each distance paid a lookup (151k buffer
  hits for 26k rows). Migration 0139 sets STORAGE MAIN on the four
  embedding tables and rewrites the existing rows once.
- The materialized scope carried every chunk's text beside its distance and
  spilled the sort to disk. It now carries the id, chunk index and distance,
  and the text is joined back for the rows that survive the cut.

Measured on the live corpus: 1,151 ms as before, 393 ms with the text out of
the scope, before the storage change.

The HNSW index stays out of the ranking. With pgvector 0.8's iterative
scan it returned 7 to 60 of the true 144 nearest chunks under a narrow
scope, at every setting tried.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 16:10:00 -05:00

43 lines
1.6 KiB
Python

"""embedding vectors stored inline, not in TOAST
Revision ID: 0139
Revises: 0138
Create Date: 2026-11-28
Every semantic search ranks its in-scope chunks exactly, one distance per
chunk (see embeddings._rank_scoped). pgvector's `vector` type defaults to
EXTERNAL storage, and a chunk row (a 384-dimension vector, about 1.5 KB, plus
the chunk's text) is over Postgres's 2 KB toast threshold, so the vector was
moved out of line and every distance paid a TOAST lookup: several buffer
reads per chunk, on every search.
`STORAGE MAIN` keeps the vector in the row. When a row is still too wide, the
chunk text (EXTENDED) is compressed or moved out instead, which costs nothing
on the ranking path: the text is read only for the rows a search returns.
A storage change applies to rows written after it, so the existing rows are
rewritten once. Casting through `real[]` produces a fresh value, which the
row is stored with under the new setting; assigning the column to itself
would keep the old out-of-line pointer.
"""
from alembic import op
revision = "0139"
down_revision = "0138"
branch_labels = None
depends_on = None
TABLES = ("note_embeddings", "rule_embeddings", "milestone_embeddings", "system_embeddings")
def upgrade() -> None:
for table in TABLES:
op.execute(f"ALTER TABLE {table} ALTER COLUMN embedding SET STORAGE MAIN")
op.execute(f"UPDATE {table} SET embedding = embedding::real[]::vector")
def downgrade() -> None:
# Rows already inline stay inline; new rows follow the type's default.
for table in TABLES:
op.execute(f"ALTER TABLE {table} ALTER COLUMN embedding SET STORAGE EXTERNAL")