跳转到主要内容

vchord

使用Rust重写的高性能向量扩展

概览

扩展包名版本分类许可证语言
vchord1.1.1RAGAGPL-3.0Rust
ID扩展名BinLibLoadCreateTrustReloc模式
1810vchord否是是是否是-

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY1.1.11817161514vchordvector
RPMPIGSTY1.1.11817161514vchord_$vpgvector_$v
DEBPIGSTY1.1.11817161514postgresql-$v-vchordpostgresql-$v-pgvector
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
u22.x86_64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
u22.aarch64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
u24.x86_64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
u24.aarch64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
u26.x86_64
u26.aarch64

构建

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

pig build pkg vchord         # 构建 RPM / DEB 包

安装

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

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

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

安装
pig install vchord;          # 当前活跃 PG 版本安装
pig
pig ext install -y vchord -v 18  # PG 18
pig ext install -y vchord -v 17  # PG 17
pig ext install -y vchord -v 16  # PG 16
pig ext install -y vchord -v 15  # PG 15
pig ext install -y vchord -v 14  # PG 14
dnf
dnf install -y vchord_18       # PG 18
dnf install -y vchord_17       # PG 17
dnf install -y vchord_16       # PG 16
dnf install -y vchord_15       # PG 15
dnf install -y vchord_14       # PG 14
apt
apt install -y postgresql-18-vchord   # PG 18
apt install -y postgresql-17-vchord   # PG 17
apt install -y postgresql-16-vchord   # PG 16
apt install -y postgresql-15-vchord   # PG 15
apt install -y postgresql-14-vchord   # PG 14

预加载配置:

shared_preload_libraries = 'vchord';

创建扩展:

CREATE EXTENSION vchord CASCADE;  -- 依赖: vector

用法

来源:

vchord 使用 pgvector 的类型,为 PostgreSQL 增加近似向量索引,提供基于分区的 vchordrq 和基于图的 vchordg 访问方法。扩展依赖 vector,要求共享预加载,创建时需要超级用户权限。

创建与查询索引

将库加入已有预加载列表,保留其他条目,然后重启 PostgreSQL:

shared_preload_libraries = 'vchord'
CREATE EXTENSION vchord CASCADE;
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
INSERT INTO items(embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
CREATE INDEX items_embedding_idx ON items
USING vchordrq (embedding vector_l2_ops);

SELECT id FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
SELECT vchordrq_prewarm('items_embedding_idx'::regclass);

vector_l2_ops 对应 <->,vector_ip_ops 对应 <#>,vector_cosine_ops 对应 <=>。内积运算符返回负值,以适配索引升序扫描。图访问方法可使用同样的运算符类,应按工作负载选择索引方案:

CREATE INDEX items_embedding_graph_idx ON items
USING vchordg (embedding vector_l2_ops);

范围查询与调优

扩展为范围检索提供显式球体谓词:

SELECT id FROM items
WHERE embedding <<->> sphere('[1,2,3]'::vector, 0.5);

SET vchordrq.probes = '100';
SET vchordrq.epsilon = 1.9;
SET vchordg.ef_search = 64;

<<->>、<<#>> 和 <<=>> 分别是 L2、内积和余弦度量的球体谓词。探测数量取决于分区布局,应使用代表性数据调优。epsilon 设置控制重排序的权衡,图检索设置控制候选搜索范围。两种索引方法都是近似检索,选择参数前应检查召回率、过滤条件和查询计划。

1.1.1 的量化接口

rabitq8 和 rabitq4 保存量化向量。quantize_to_rabitq8 与 quantize_to_rabitq4 接受 vector 或 halfvec。1.1.1 为这两种量化类型新增 dequantize_to_vector 和 dequantize_to_halfvec 重载:

SELECT dequantize_to_vector(quantize_to_rabitq8('[1,2,3]'::vector));
SELECT dequantize_to_halfvec(quantize_to_rabitq4('[1,2,3]'::halfvec));

量化会损失精度,反量化返回近似结果。本次发布还替换了量化实现。安装匹配的库和 SQL 文件后,先为预加载库重启服务,再更新每个数据库中的扩展:

ALTER EXTENSION vchord UPDATE TO '1.1.1';

1.1.0 到 1.1.1 的脚本新增这四个转换重载,未声明索引格式迁移。更早版本的升级要求取决于起始版本。索引构建和预热会消耗资源,应按数据集大小安排。vchordg_prewarm 是对应的图索引辅助函数。control 声明扩展可重定位,在非默认模式中安装时,应限定扩展对象的模式或将安装模式加入搜索路径。

这个页面对您有帮助吗?