跳转到主要内容

vector

向量数据类型和 ivfflat / hnsw 访问方法

概览

扩展包名版本分类许可证语言
pgvector0.8.7RAGPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
1800vector否是否是否是-
相关扩展vchord vectorscale pgcontext vectorize pg_rrf pg_search vchord_bm25 pg_bestmatch pgml pg4ml
下游依赖ai alloydb_scann avocado documentdb embedding_search hybrid_search maludb_core pg_cuvs pg_diskann pg_gembed pg_knowledge_graph pg_llm pg_llm_helper pg_search pg_semantic_cache pg_sentence_transformer pg_splade pg_turboquant pgedge_vectorizer pgmnemo pgpu pgturbohybrid pgvecutils rag rag_bge_small_en_v15 rag_jina_reranker_v1_tiny_en rds_ai rds_embedding vchord vectorize vectorscale

PGDG is the principal supplier by explicit catalog policy: RPM and DEB 0.8.7 cover PostgreSQL 14-18. Pigsty recipes are retained for confirmed gaps only.

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG0.8.71817161514pgvector-
RPMPGDG0.8.71817161514pgvector_$v-
DEBPGDG0.8.71817161514postgresql-$v-pgvector-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
PGDG 0.8.7
el9.x86_64.pg15 : pgvector_15
pgvector_15-0.8.7-1PGDG.rhel9.8.x86_64.rpm PGDG · 0.8.7 · 119.1KiB pgvector_15-0.8.6-1PGDG.rhel9.8.x86_64.rpm PGDG · 0.8.6 · 115.3KiB pgvector_15-0.8.5-1PGDG.rhel9.8.x86_64.rpm PGDG · 0.8.5 · 114.0KiB pgvector_15-0.8.4-1PGDG.rhel9.8.x86_64.rpm PGDG · 0.8.4 · 113.7KiB pgvector_15-0.8.3-1PGDG.rhel9.8.x86_64.rpm PGDG · 0.8.3 · 113.2KiB pgvector_15-0.8.2-1PGDG.rhel9.8.x86_64.rpm PGDG · 0.8.2 · 113.2KiB pgvector_15-0.8.2-1PGDG.rhel9.7.x86_64.rpm PGDG · 0.8.2 · 113.1KiB pgvector_15-0.8.2-1PGDG.rhel9.6.x86_64.rpm PGDG · 0.8.2 · 113.2KiB pgvector_15-0.8.1-1PGDG.rhel9.x86_64.rpm PGDG · 0.8.1 · 112.6KiB pgvector_15-0.8.0-1PGDG.rhel9.x86_64.rpm PGDG · 0.8.0 · 112.0KiB pgvector_15-0.7.4-1PGDG.rhel9.x86_64.rpm PGDG · 0.7.4 · 107.9KiB pgvector_15-0.7.3-1PGDG.rhel9.x86_64.rpm PGDG · 0.7.3 · 107.3KiB pgvector_15-0.7.2-1PGDG.rhel9.x86_64.rpm PGDG · 0.7.2 · 106.9KiB pgvector_15-0.7.1-1PGDG.rhel9.x86_64.rpm PGDG · 0.7.1 · 106.7KiB pgvector_15-0.7.0-1PGDG.rhel9.x86_64.rpm PGDG · 0.7.0 · 106.1KiB pgvector_15-0.6.2-2PGDG.rhel9.x86_64.rpm PGDG · 0.6.2 · 76.7KiB pgvector_15-0.6.2-1PGDG.rhel9.x86_64.rpm PGDG · 0.6.2 · 78.6KiB pgvector_15-0.6.1-1PGDG.rhel9.x86_64.rpm PGDG · 0.6.1 · 77.5KiB pgvector_15-0.6.0-1PGDG.rhel9.x86_64.rpm PGDG · 0.6.0 · 76.7KiB pgvector_15-0.5.1-1PGDG.rhel9.x86_64.rpm PGDG · 0.5.1 · 66.0KiB pgvector_15-0.5.0-1PGDG.rhel9.x86_64.rpm PGDG · 0.5.0 · 65.4KiB pgvector_15-0.4.4-1.rhel9.x86_64.rpm PGDG · 0.4.4 · 45.8KiB pgvector_15-0.4.1-1.rhel9.x86_64.rpm PGDG · 0.4.1 · 43.0KiB
PGDG 0.8.7
el9.x86_64.pg14 : pgvector_14
pgvector_14-0.8.7-1PGDG.rhel9.8.x86_64.rpm PGDG · 0.8.7 · 119.0KiB pgvector_14-0.8.6-1PGDG.rhel9.8.x86_64.rpm PGDG · 0.8.6 · 115.1KiB pgvector_14-0.8.5-1PGDG.rhel9.8.x86_64.rpm PGDG · 0.8.5 · 113.9KiB pgvector_14-0.8.4-1PGDG.rhel9.8.x86_64.rpm PGDG · 0.8.4 · 113.6KiB pgvector_14-0.8.3-1PGDG.rhel9.8.x86_64.rpm PGDG · 0.8.3 · 112.9KiB pgvector_14-0.8.2-1PGDG.rhel9.8.x86_64.rpm PGDG · 0.8.2 · 112.8KiB pgvector_14-0.8.2-1PGDG.rhel9.7.x86_64.rpm PGDG · 0.8.2 · 112.9KiB pgvector_14-0.8.2-1PGDG.rhel9.6.x86_64.rpm PGDG · 0.8.2 · 113.0KiB pgvector_14-0.8.1-1PGDG.rhel9.x86_64.rpm PGDG · 0.8.1 · 112.4KiB pgvector_14-0.8.0-1PGDG.rhel9.x86_64.rpm PGDG · 0.8.0 · 111.7KiB pgvector_14-0.7.4-1PGDG.rhel9.x86_64.rpm PGDG · 0.7.4 · 107.8KiB pgvector_14-0.7.3-1PGDG.rhel9.x86_64.rpm PGDG · 0.7.3 · 107.1KiB pgvector_14-0.7.2-1PGDG.rhel9.x86_64.rpm PGDG · 0.7.2 · 107.0KiB pgvector_14-0.7.1-1PGDG.rhel9.x86_64.rpm PGDG · 0.7.1 · 106.5KiB pgvector_14-0.7.0-1PGDG.rhel9.x86_64.rpm PGDG · 0.7.0 · 106.0KiB pgvector_14-0.6.2-2PGDG.rhel9.x86_64.rpm PGDG · 0.6.2 · 76.8KiB pgvector_14-0.6.2-1PGDG.rhel9.x86_64.rpm PGDG · 0.6.2 · 78.5KiB pgvector_14-0.6.1-1PGDG.rhel9.x86_64.rpm PGDG · 0.6.1 · 77.4KiB pgvector_14-0.6.0-1PGDG.rhel9.x86_64.rpm PGDG · 0.6.0 · 76.8KiB pgvector_14-0.5.1-1PGDG.rhel9.x86_64.rpm PGDG · 0.5.1 · 65.8KiB pgvector_14-0.5.0-1PGDG.rhel9.x86_64.rpm PGDG · 0.5.0 · 65.4KiB pgvector_14-0.4.4-1.rhel9.x86_64.rpm PGDG · 0.4.4 · 45.9KiB pgvector_14-0.4.1-1.rhel9.x86_64.rpm PGDG · 0.4.1 · 42.9KiB
el9.aarch64
PGDG 0.8.7
el9.aarch64.pg15 : pgvector_15
pgvector_15-0.8.7-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.7 · 104.8KiB pgvector_15-0.8.6-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.6 · 101.3KiB pgvector_15-0.8.5-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.5 · 100.0KiB pgvector_15-0.8.4-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.4 · 99.8KiB pgvector_15-0.8.3-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.3 · 99.6KiB pgvector_15-0.8.2-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.2 · 99.5KiB pgvector_15-0.8.2-1PGDG.rhel9.7.aarch64.rpm PGDG · 0.8.2 · 99.5KiB pgvector_15-0.8.2-1PGDG.rhel9.6.aarch64.rpm PGDG · 0.8.2 · 99.6KiB pgvector_15-0.8.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.8.1 · 98.8KiB pgvector_15-0.8.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.8.0 · 98.5KiB pgvector_15-0.7.4-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.4 · 94.2KiB pgvector_15-0.7.3-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.3 · 93.8KiB pgvector_15-0.7.2-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.2 · 93.5KiB pgvector_15-0.7.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.1 · 93.1KiB pgvector_15-0.7.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.0 · 92.3KiB pgvector_15-0.6.2-2PGDG.rhel9.aarch64.rpm PGDG · 0.6.2 · 73.5KiB pgvector_15-0.6.2-1PGDG.rhel9.aarch64.rpm PGDG · 0.6.2 · 73.1KiB pgvector_15-0.6.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.6.1 · 72.0KiB pgvector_15-0.6.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.6.0 · 71.3KiB pgvector_15-0.5.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.5.1 · 61.5KiB pgvector_15-0.5.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.5.0 · 60.9KiB pgvector_15-0.4.4-1.rhel9.aarch64.rpm PGDG · 0.4.4 · 43.4KiB pgvector_15-0.4.1-1.rhel9.aarch64.rpm PGDG · 0.4.1 · 40.6KiB
PGDG 0.8.7
el9.aarch64.pg14 : pgvector_14
pgvector_14-0.8.7-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.7 · 104.7KiB pgvector_14-0.8.6-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.6 · 101.2KiB pgvector_14-0.8.5-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.5 · 100.0KiB pgvector_14-0.8.4-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.4 · 100.0KiB pgvector_14-0.8.3-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.3 · 99.4KiB pgvector_14-0.8.2-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.2 · 99.1KiB pgvector_14-0.8.2-1PGDG.rhel9.7.aarch64.rpm PGDG · 0.8.2 · 99.0KiB pgvector_14-0.8.2-1PGDG.rhel9.6.aarch64.rpm PGDG · 0.8.2 · 99.2KiB pgvector_14-0.8.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.8.1 · 98.3KiB pgvector_14-0.8.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.8.0 · 98.1KiB pgvector_14-0.7.4-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.4 · 94.2KiB pgvector_14-0.7.3-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.3 · 93.8KiB pgvector_14-0.7.2-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.2 · 93.5KiB pgvector_14-0.7.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.1 · 93.1KiB pgvector_14-0.7.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.0 · 92.4KiB pgvector_14-0.6.2-2PGDG.rhel9.aarch64.rpm PGDG · 0.6.2 · 73.4KiB pgvector_14-0.6.2-1PGDG.rhel9.aarch64.rpm PGDG · 0.6.2 · 73.1KiB pgvector_14-0.6.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.6.1 · 72.0KiB pgvector_14-0.6.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.6.0 · 71.3KiB pgvector_14-0.5.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.5.1 · 61.5KiB pgvector_14-0.5.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.5.0 · 60.9KiB pgvector_14-0.4.4-1.rhel9.aarch64.rpm PGDG · 0.4.4 · 43.3KiB pgvector_14-0.4.1-1.rhel9.aarch64.rpm PGDG · 0.4.1 · 40.5KiB
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
u24.x86_64
u24.aarch64
u26.x86_64
u26.aarch64

构建

您可以使用 pig build 命令构建 pgvector 扩展的 RPM / DEB 包:

pig build pkg pgvector         # 构建 RPM / DEB 包

安装

您可以直接安装 pgvector 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:

pig repo add pgdg -u          # 添加 PGDG 仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

安装
pig install pgvector;          # 当前活跃 PG 版本安装
pig
pig ext install -y pgvector -v 18  # PG 18
pig ext install -y pgvector -v 17  # PG 17
pig ext install -y pgvector -v 16  # PG 16
pig ext install -y pgvector -v 15  # PG 15
pig ext install -y pgvector -v 14  # PG 14
dnf
dnf install -y pgvector_18       # PG 18
dnf install -y pgvector_17       # PG 17
dnf install -y pgvector_16       # PG 16
dnf install -y pgvector_15       # PG 15
dnf install -y pgvector_14       # PG 14
apt
apt install -y postgresql-18-pgvector   # PG 18
apt install -y postgresql-17-pgvector   # PG 17
apt install -y postgresql-16-pgvector   # PG 16
apt install -y postgresql-15-pgvector   # PG 15
apt install -y postgresql-14-pgvector   # PG 14

创建扩展:

CREATE EXTENSION vector;

用法

来源:

pgvector 在 PostgreSQL 内提供向量相似性搜索。扩展名为 vector,Pigsty 将其打包为 pgvector。它支持精确搜索、使用 HNSW 与 IVFFlat 索引的近似最近邻搜索,以及用于稠密、半精度、二进制和稀疏嵌入的多种向量表示。

版本 0.8.7 是一个专注于正确性的修复版本,同时保留了当前 README 中介绍的 0.8.x HNSW 迭代扫描与维护改进。

创建与查询向量

CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE items (
  id bigserial PRIMARY KEY,
  embedding vector(3)
);

INSERT INTO items (embedding)
VALUES ('[1,2,3]'), ('[4,5,6]');

SELECT *
FROM items
ORDER BY embedding <-> '[3,1,2]'
LIMIT 5;

常用距离操作符:

  • <->:L2 距离
  • <#>:负内积
  • <=>:余弦距离
  • <+>:L1 距离
  • <~>:二进制向量的汉明距离
  • <%>:二进制向量的杰卡德距离

由于 PostgreSQL 索引按升序扫描,<#> 返回负内积;显示实际内积时应乘以 -1。

向量类型

CREATE TABLE embeddings (
  id bigserial PRIMARY KEY,
  dense      vector(768),
  half_dense halfvec(768),
  binary_sig bit(1024),
  sparse     sparsevec(100000)
);

vector 是标准单精度类型。可使用 halfvec 降低存储和内存压力,使用 bit 表示二进制签名,并使用 sparsevec 表示高维稀疏向量。

avg() 和 sum() 等聚合函数可以用于向量列:

SELECT avg(embedding) FROM items;

HNSW 索引

HNSW 在速度与召回率之间提供良好的权衡,且不需要训练步骤。

CREATE INDEX items_embedding_hnsw
ON items USING hnsw (embedding vector_l2_ops);

SET hnsw.ef_search = 100;

SELECT *
FROM items
ORDER BY embedding <-> '[3,1,2]'
LIMIT 10;

请选择与距离匹配的操作符类:

CREATE INDEX ON items USING hnsw (embedding vector_ip_ops);
CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);
CREATE INDEX ON items USING hnsw (embedding vector_l1_ops);
CREATE INDEX ON embeddings USING hnsw (half_dense halfvec_l2_ops);
CREATE INDEX ON embeddings USING hnsw (sparse sparsevec_l2_ops);
CREATE INDEX ON embeddings USING hnsw (binary_sig bit_hamming_ops);

常用调优设置包括 hnsw.ef_search、hnsw.iterative_scan、hnsw.max_scan_tuples 和 hnsw.scan_mem_multiplier。

IVFFlat 索引

IVFFlat 在构建索引时需要训练聚类列表,因此创建索引之前必须已有具备代表性的数据。

CREATE INDEX items_embedding_ivfflat
ON items USING ivfflat (embedding vector_l2_ops)
WITH (lists = 100);

SET ivfflat.probes = 10;

SELECT *
FROM items
ORDER BY embedding <-> '[3,1,2]'
LIMIT 10;

表越大,应增加 lists;要提高召回率,则增加 ivfflat.probes。对于带过滤条件的查询,应测试精确的 btree 过滤器、部分向量索引或分区中哪一种能产生更好的执行计划。

过滤与混合搜索

普通 PostgreSQL 过滤条件可以与向量排序组合使用:

ALTER TABLE items ADD COLUMN tenant_id bigint;
CREATE INDEX ON items (tenant_id);

SELECT *
FROM items
WHERE tenant_id = 42
ORDER BY embedding <=> '[0.1,0.2,0.3]'
LIMIT 20;

进行混合搜索时,可以将 pgvector 与 PostgreSQL 全文搜索、三元组搜索或外部排序表达式组合使用:

CREATE TABLE docs (
  id bigint PRIMARY KEY,
  body text NOT NULL,
  text_tsv tsvector GENERATED ALWAYS AS
    (to_tsvector('english', body)) STORED,
  embedding vector(3)
);

SELECT id,
       ts_rank_cd(text_tsv, plainto_tsquery('database')) AS text_score,
       1 - (embedding <=> '[0.1,0.2,0.3]') AS vector_score
FROM docs
WHERE text_tsv @@ plainto_tsquery('database')
ORDER BY vector_score DESC
LIMIT 20;

维护

VACUUM items;
REINDEX INDEX CONCURRENTLY items_embedding_hnsw;
ANALYZE items;

HNSW 索引可能很大,构建成本也可能很高。构建时使用 maintenance_work_mem,监控构建通知,并在索引膨胀或召回率漂移成为问题时安排 REINDEX。

注意事项

  • 版本 0.8.6 修复了 32 位系统上的 IVFFlat 构建溢出、将数组转换为 sparsevec 时未强制执行非零元素上限,以及嵌套循环中的 IVFFlat 扫描内存增长问题。它没有新增 SQL 功能界面。安装新的扩展文件后,如果数据库报告的是较旧 SQL 版本,请运行 ALTER EXTENSION vector UPDATE。
  • 使用与查询操作符匹配的操作符类。余弦索引无法加速 L2 ORDER BY。
  • 近似索引以精确召回率换取速度。请使用有代表性的数据和查询过滤条件验证召回率。
  • 应在数据加载后构建 IVFFlat。如果数据分布发生显著变化,请重建索引。
  • 在高强度写入和 vacuum 活动场景中使用 HNSW 时,请及时更新 pgvector;0.8.x 系列包含重要的 HNSW 维护修复。

0.8.7 版本变化

0.8.7 进一步修复 IVFFlat 构建缓冲区溢出及空集合求平均问题。安装新的库与 SQL 文件后执行 ALTER EXTENSION vector UPDATE。0.8.7 未声明索引格式迁移或强制重建索引要求。

这个页面对您有帮助吗?