AgentSkillsCN

vector-db-storage

在 Elixir 中为向量数据库选择合适的存储后端与索引方案。当您需要权衡持久化存储方式,决定采用精确匹配还是近似 k-NN 搜索,或希望将 pgvector、文件存储,或 ETS 机制整合到向量存储中时,此功能将助您做出明智抉择。

SKILL.md
--- frontmatter
name: vector-db-storage
description: Storage backends and indexing for vector databases in Elixir. Use when choosing persistence, implementing exact vs approximate k-NN, or integrating pgvector, file, or ETS for vector storage.

Vector DB Storage and Indexing

Storage Backends

BackendUse casePersistenceScale
ETSPrototype, small index, low latencyNo< ~100k–500k vectors in memory
FileSimple persistence, no DBYes (binary/term)Same as ETS, plus disk
PostgreSQL + pgvectorProduction, SQL + vectorsYesLarge; use IVFFlat/HNSW indexes
Qdrant (client)Dedicated vector engineYes (external)Very large

In-Memory (ETS)

  • :set table, key = vector id.
  • Value: {id, vector, metadata}; vector as list or binary.
  • No persistence: load from file on GenServer init if needed (see File below).
  • Exact k-NN: iterate all rows, compute distance, sort, take k. O(n) per query.

File Persistence

  • On shutdown: GenServer terminate/2 or :gen_server.cast(pid, :persist) → write ETS contents to file (e.g. :erlang.term_to_binary(entries) or custom binary format).
  • On start: read file, recreate ETS and insert all entries.
  • Keep dimension and metadata format in header or config so reload is consistent.

PostgreSQL + pgvector

Use pgvector and Ecto for persistent, indexed storage.

  • Schema: table with id, embedding (pgvector type), metadata (jsonb/map).
  • Index: create HNSW or IVFFlat index on embedding for approximate k-NN.
  • Queries: use pgvector’s distance operators (<=>, <->) in raw SQL or Ecto fragments; limit k.
elixir
# Example: nearest neighbors (cosine distance)
# fragment("embedding <=> ?::vector", ^Nx.to_flat_list(query))
  • Handles large datasets; persistence and backups via Postgres.

Exact vs Approximate k-NN

MethodAccuracySpeedWhen
Exact (scan all)ExactO(n) per querySmall n (e.g. < 100k), correctness critical
IVFFlat (pgvector)ApproximateFastMedium/large, good recall with tuning
HNSW (pgvector)ApproximateVery fastLarge, high recall

For in-memory ETS, start with exact k-NN; move to pgvector (or Qdrant) when n grows or persistence is required.

Design Checklist

  • Persistence requirement clear: none, file, or Postgres
  • If file: define format (term vs binary) and load/save on start/stop
  • If pgvector: add migration for vector column and chosen index (HNSW/IVFFlat)
  • Dimension and distance metric match across insert and search