Skills2026年4月15日·1 分钟阅读

pgvector — Vector Similarity Search Inside PostgreSQL

A PostgreSQL extension that adds a native `vector` type, HNSW and IVFFlat indexes, and distance operators so semantic search, RAG and recommendation workloads can reuse the same database as the rest of the app.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
pgvector Guide
直接安装命令
npx -y tokrepo@latest install 121fb0d5-3920-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

TL;DR
pgvector adds vector types and similarity search indexes to PostgreSQL for RAG, semantic search, and recommendations.
§01

What it is

pgvector is a PostgreSQL extension that adds a native vector data type, HNSW and IVFFlat indexes, and distance operators for similarity search. It lets you run semantic search, RAG retrieval, and recommendation workloads inside the same database as your application data.

The extension targets teams that already use PostgreSQL and want to add vector search without deploying a separate vector database. It keeps your operational complexity low by reusing your existing database infrastructure.

The project is actively maintained and suitable for both individual developers and teams looking to integrate it into their existing toolchain. Documentation and community support are available for onboarding.

§02

How it saves time or tokens

pgvector eliminates the need to sync data between PostgreSQL and a dedicated vector store. Embeddings live in the same table as your application data, enabling SQL JOINs between vectors and structured data. Transactional consistency means your vector index stays in sync with your data automatically. One backup, one monitoring stack, one connection pool.

§03

How to use

  1. Install the pgvector extension: CREATE EXTENSION vector;
  2. Add a vector column to your table: ALTER TABLE items ADD COLUMN embedding vector(1536);
  3. Insert embeddings generated by your embedding model (OpenAI, Cohere, local models).
  4. Create an index and query by similarity using distance operators.
§04

Example

-- Enable the extension
CREATE EXTENSION vector;

-- Create a table with embeddings
CREATE TABLE documents (
  id SERIAL PRIMARY KEY,
  title TEXT,
  content TEXT,
  embedding vector(1536)
);

-- Create an HNSW index for fast approximate search
CREATE INDEX ON documents
  USING hnsw (embedding vector_cosine_ops)
  WITH (m = 16, ef_construction = 64);

-- Find the 5 most similar documents
SELECT id, title, 1 - (embedding <=> $1::vector) AS similarity
FROM documents
ORDER BY embedding <=> $1::vector
LIMIT 5;
§05

Related on TokRepo

§06

Common pitfalls

  • Not creating an index before querying. Without an HNSW or IVFFlat index, pgvector does exact (brute-force) search, which is slow for tables with more than 10,000 rows.
  • Using the wrong distance operator. <=> is cosine distance, <-> is L2 distance, <#> is inner product. Match the operator to how your embeddings were trained.
  • Storing embeddings with the wrong dimension. The vector column dimension must match your embedding model's output dimension exactly. A mismatch causes insertion errors.
  • Not reading the changelog before upgrading. Breaking changes between versions can cause unexpected failures in production. Pin your version and review release notes.

常见问题

How does pgvector compare to Pinecone or Milvus?+

pgvector runs inside PostgreSQL, so there is no additional infrastructure. Pinecone and Milvus are purpose-built vector databases with more advanced features (filtering, sharding, hybrid search) at very high scale. pgvector is sufficient for most applications with up to millions of vectors.

Which index type should I use?+

HNSW is recommended for most use cases. It offers better query performance with slightly higher memory usage. IVFFlat uses less memory but requires a separate training step and may have lower recall. HNSW is the default choice.

Can pgvector handle millions of vectors?+

Yes. pgvector with HNSW indexes handles millions of vectors with sub-10ms query latency on typical hardware. For tens of millions of vectors, consider dedicated vector databases or sharding strategies.

Does pgvector support filtering with vector search?+

Yes. You can combine vector similarity search with standard SQL WHERE clauses. PostgreSQL's query planner uses the HNSW index alongside standard B-tree indexes for filtered queries.

How do I generate embeddings for pgvector?+

Use an embedding model (OpenAI text-embedding-3-small, Cohere embed, or open-source models via Hugging Face). Generate embeddings in your application code and insert them as vector values. pgvector stores and indexes them; it does not generate embeddings itself.

引用来源 (3)

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产