"""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")