跳转到主要内容

vectorscale

使用DiskANN算法对向量进行高效索引

概览

扩展包名版本分类许可证语言
pgvectorscale0.9.1RAGPostgreSQLRust
ID扩展名BinLibLoadCreateTrustReloc模式
1820vectorscale否是否是否否-

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY0.9.11817161514pgvectorscalevector
RPMPIGSTY0.9.11817161514pgvectorscale_$vpgvector_$v
DEBPIGSTY0.9.11817161514postgresql-$v-pgvectorscalepostgresql-$v-pgvector
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
d12.aarch64
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
d13.x86_64
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
d13.aarch64
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
u22.x86_64
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
u22.aarch64
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
u24.x86_64
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
u24.aarch64
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
u26.x86_64
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
u26.aarch64
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1
PIGSTY 0.9.1

构建

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

pig build pkg pgvectorscale         # 构建 RPM / DEB 包

安装

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

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

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

安装
pig install pgvectorscale;          # 当前活跃 PG 版本安装
pig
pig ext install -y pgvectorscale -v 18  # PG 18
pig ext install -y pgvectorscale -v 17  # PG 17
pig ext install -y pgvectorscale -v 16  # PG 16
pig ext install -y pgvectorscale -v 15  # PG 15
pig ext install -y pgvectorscale -v 14  # PG 14
dnf
dnf install -y pgvectorscale_18       # PG 18
dnf install -y pgvectorscale_17       # PG 17
dnf install -y pgvectorscale_16       # PG 16
dnf install -y pgvectorscale_15       # PG 15
dnf install -y pgvectorscale_14       # PG 14
apt
apt install -y postgresql-18-pgvectorscale   # PG 18
apt install -y postgresql-17-pgvectorscale   # PG 17
apt install -y postgresql-16-pgvectorscale   # PG 16
apt install -y postgresql-15-pgvectorscale   # PG 15
apt install -y postgresql-14-pgvectorscale   # PG 14

创建扩展:

CREATE EXTENSION vectorscale CASCADE;  -- 依赖: vector

用法

来源:

vectorscale 为 pgvector 增加 StreamingDiskANN 近似向量索引,diskann 访问方法支持 L2、内积、余弦距离和标签过滤。扩展依赖 vector,创建时需要超级用户权限。0.9.1 校验向量类型、维度及存储值布局,修复可能导致崩溃、内存泄露和越界写入的问题。

核心流程

声明明确的向量维度,并选择与查询距离相匹配的运算符类:

CREATE EXTENSION vectorscale CASCADE;
CREATE TABLE documents (
    id bigserial PRIMARY KEY,
    contents text,
    embedding vector(3),
    labels smallint[]
);
INSERT INTO documents(contents, embedding, labels)
VALUES ('PostgreSQL search', '[1,2,3]', ARRAY[1,3]::smallint[]);
CREATE INDEX documents_diskann ON documents
USING diskann (embedding vector_cosine_ops, labels);

SELECT id, contents FROM documents
WHERE labels && ARRAY[1]::smallint[]
ORDER BY embedding <=> '[1,2,3]'::vector
LIMIT 10;

vector_l2_ops 对应 <->,vector_ip_ops 对应 <#>,vector_cosine_ops 对应 <=>。标签使用 smallint[],&& 表示与任一请求标签重叠。也支持普通 WHERE 条件,但严格过滤可能减少返回数量,应结合实际工作负载验证。

调优与排序

diskann.query_search_list_size 控制图检索额外候选数量,默认 100;diskann.query_rescore 控制精确重评分数量,默认 50,0 表示禁用:

SET diskann.query_search_list_size = 200;
SET diskann.query_rescore = 100;

构建选项包括 storage_layout、num_neighbors、search_list_size 和 num_dimensions,默认使用压缩的内存优化存储。增加维护内存前,应考虑并发构建及数据集大小。此版本的并行构建要求支持的压缩布局,且不支持标签列。

DiskANN 采用宽松的距离排序。需要严格排序时,可对物化结果集再次排序;这只能重排已检索的候选,不能将近似检索变成全量精确搜索。空向量不建立索引,空标签视为空数组,标签数组中的空元素被忽略。不支持在 UNLOGGED 表上创建索引。

升级到 0.9.1

安装匹配的扩展文件后,更新每个数据库中的扩展:

ALTER EXTENSION vectorscale UPDATE TO '0.9.1';

升级会将运算符类绑定到 pgvector 的实际安装模式,并检查已有绑定。若已有运算符类指向错误的类型或运算符,升级会中止;应依照发布说明删除受影响的运算符类并重新创建扩展对象,同时处理依赖索引。

DiskANN 现在要求列类型具有有效的 vector(N) 维度。 没有维度约束的向量列无法建立索引。持久化维度无效的旧索引会在扫描、插入和清理时报告错误。修正列类型后,必须 删除并重新创建受影响的索引,REINDEX 无法修复这一问题。有效索引无需仅因安装 0.9.1 就全面重建。扩展创建后,SQL 对象不可重定位。

这个页面对您有帮助吗?