# pgvector

**pgvector** 是一个开源 [PostgreSQL](https://www.edgepedia.org/zh/postgresql) 扩展，提供向量相似性搜索功能，为现有 Postgres 数据库添加了 `vector` 数据类型、距离函数以及用于精确和近似最近邻搜索的索引访问方法<sup>[1](https://www.postgresql.org/about/news/pgvector-082-released-3245/)</sup><sup> • </sup><sup>[2](https://theconsensus.dev/project/pgvector/source/v0.8.6/README.md)</sup>。它由 Andrew Kane 创建，首次提交可追溯至 2021 年 4 月，比大语言模型引发的兴趣激增早了约两年<sup>[2](https://theconsensus.dev/project/pgvector/source/v0.8.6/README.md)</sup><sup> • </sup><sup>[3](https://postgres.fm/episodes/pgvector/transcript)</sup>。由于向量与应用程序的其他数据存储在同一数据库中，它们继承了 Postgres 的特性，如 ACID 事务、时间点恢复和 JOIN<sup>[2](https://theconsensus.dev/project/pgvector/source/v0.8.6/README.md)</sup>。

| 关键事实 | 详情 |
|---|---|
| 是什么 | 用于向量相似性搜索的开源 PostgreSQL 扩展，采用 PostgreSQL 许可证<sup>[1](https://www.postgresql.org/about/news/pgvector-082-released-3245/)</sup><sup> • </sup><sup>[4](https://github.com/pgvector/pgvector/blob/master/META.json)</sup> |
| 创建者 | Andrew Kane；首次提交于 2021 年 4 月<sup>[2](https://theconsensus.dev/project/pgvector/source/v0.8.6/README.md)</sup> |
| 向量类型 | `vector` 最高 16,000 维，`halfvec` 最高 16,000 维，`bit` 最高 64,000 维，另有 `sparsevec`<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup> |
| 距离函数 | L2、内积、余弦、汉明和杰卡德<sup>[2](https://theconsensus.dev/project/pgvector/source/v0.8.6/README.md)</sup> |
| 索引类型 | HNSW（多层图）和 IVFFlat（带聚类的倒排文件）<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup> |
| 要求 | PostgreSQL 13 或更高版本<sup>[4](https://github.com/pgvector/pgvector/blob/master/META.json)</sup> |
| 当前版本 | 0.8.7（2026 年 10 月 1 日），修复 CVE-2026-103484 的安全版本<sup>[6](https://www.postgresql.org/about/news/pgvector-087-released-3392/)</sup> |

## 数据类型与距离函数

核心类型是 `vector`，即声明了维度的单精度固定维度向量，例如 `vector(3)`。pgvector 还提供三种压缩或专用表示<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup>：

- **halfvec** 将每个维度存储为两个字节而非四个字节，使存储和索引大小减半，支持最高 16,000 维（其 HNSW 和 IVFFlat 索引支持最高 4,000 维）<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup>。
- **bit** 存储最高 64,000 维的二进制向量，通过汉明距离或杰卡德距离查询。
- **sparsevec** 以 `{index:value}/dimensions` 格式存储稀疏向量，索引从 1 开始，适用于大多数维度为零的场景。

查询使用专用运算符：`<->` 表示欧几里得（L2）距离，`<=>` 表示余弦距离，`<#>` 表示负内积，`<~>` 表示[汉明距离](https://www.edgepedia.org/zh/hamming-distance)，`<%>` 表示杰卡德距离<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup><sup> • </sup><sup>[7](https://supabase.com/docs/guides/ai/vector-columns)</sup>。最近邻查询即对这些运算符之一使用带 `LIMIT` 的 `ORDER BY`，例如 `SELECT * FROM items ORDER BY embedding <=> '[1,2,3]' LIMIT 5`<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup>。

## 索引

默认情况下，pgvector 执行精确最近邻搜索，可获得完美的召回率。添加索引后切换为近似最近邻搜索，以部分召回率换取速度；与典型索引不同，添加近似索引后，同一查询的结果可能有所不同<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup>。

**HNSW**（分层可导航小世界）构建多层图。在速度与召回率的权衡中，其查询性能优于 IVFFlat，但构建时间更长、内存占用更高，且由于没有训练步骤，可以在空表上创建<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup>。**IVFFlat** 在构建时将向量聚类为列表，每次查询探测可配置数量的列表（默认为 1）；提高 `ivfflat.probes` 可改善召回率但牺牲速度，将其设为列表数量则产生精确搜索<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup>。工作负载使用的每个距离函数都需要单独的索引<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup>。

表达式索引将两种索引类型扩展到压缩表示：向量可以按 `halfvec` 索引以缩小索引体积，通过 `binary_quantize()` 量化以进行汉明搜索，或对子向量建立索引，并用原始全精度向量重新排序以恢复召回率<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup>。

## 过滤与迭代扫描

使用近似索引时，`WHERE` 子句在索引扫描*之后*应用。README 中的示例：如果条件匹配 10% 的行，而 HNSW 的默认 `hnsw.ef_search` 为 40，则平均只有约 4 行会匹配<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup>。2024 年 10 月 30 日发布的 0.8.0 版本添加了迭代索引扫描，会自动扫描更多索引，直到找到足够的结果或达到上限（`hnsw.max_scan_tuples`，默认 20,000，或 `ivfflat.max_probes`），同时改进了过滤查询的成本估算<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup><sup> • </sup><sup>[8](https://github.com/pgvector/pgvector/blob/master/CHANGELOG.md)</sup>。对于匹配行数较少的条件，在过滤列上建立索引可提供精确搜索；对于大量不同的过滤值，建议使用分区或部分索引<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup>。

## 混合搜索

pgvector 设计为与 Postgres 全文搜索结合实现混合搜索，使用倒数排名融合或交叉编码器等技术合并词汇和语义结果<sup>[5](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)</sup>。

## 实际性能

实测结果在很大程度上取决于规模和工作负载。在 2026 年一项针对七个向量数据库的基准测试中，语料规模从 175 到 220 万个块，pgvector 配合 HNSW 在 50,000 块规模下实现了 9.9 毫秒延迟和每秒 101 次查询，在所有测试系统中延迟排名第二，在该规模下其 IVFFlat 与 HNSW 索引之间测得 2.3 倍的性能差异<sup>[9](https://github.com/Kwaai-AI-Lab/vector_dbs_benchmarking)</sup>。同一研究发现，由于内存限制，单节点 HNSW 的可扩展性上限约为 100 万至 200 万个块<sup>[9](https://github.com/Kwaai-AI-Lab/vector_dbs_benchmarking)</sup>。

内存是主要的扩展约束。Pinecone 针对 pgvector 的基准测试测得，HNSW 索引内存约为原始数据集大小的 1.2 倍至 5 倍以上，具体取决于数据，一旦图不再能放入工作内存，构建吞吐量降至原来的十分之一以下<sup>[10](https://www.pinecone.io/compare/pgvector/)</sup>。pgvector 自身的扩展指南指出，`halfvec` 和带重排序的二进制量化是缩小索引的两种方法<sup>[10](https://www.pinecone.io/compare/pgvector/)</sup>。研究工作针对底层架构：一篇 CIDR 2026 论文报告称，将向量索引嵌入 Postgres 的面向页存储会产生开销，导致与专用系统之间的性能差距，并描述了一种解耦设计（PostgreSQL-V），在向量搜索中比 pgvector 快达 8.9 倍<sup>[11](https://vldb.org/cidrdb/papers/2026/p2-liu.pdf)</sup>。

2023 年一项针对 PostgreSQL 用户的调查发现，在使用 pgvector 处理 AI 和 LLM 工作负载的受访者中，37.7% 将向量与关系数据保留在同一数据库视为主要优势<sup>[12](https://s3.amazonaws.com/assets.timescale.com/state-of-postgresql/Report_State_of_PostgreSQL_2023.pdf?v2=)</sup>。

## 可用性与安全

pgvector 可针对 PostgreSQL 13 及更高版本编译，可通过 Docker、Homebrew、PGXN、APT、Yum、pkg、APK 和 conda-forge 安装，并随 Postgres.app 和许多托管服务商预装<sup>[4](https://github.com/pgvector/pgvector/blob/master/META.json)</sup><sup> • </sup><sup>[2](https://theconsensus.dev/project/pgvector/source/v0.8.6/README.md)</sup>。Amazon RDS for PostgreSQL 自 2023 年 5 月起在所有 AWS 区域运行 PostgreSQL 15.2 及更高版本的实例上支持该扩展<sup>[13](https://aws.amazon.com/about-aws/whats-new/2023/05/amazon-rds-postgresql-pgvector-ml-model-integration/)</sup>，Supabase 通过 pgvector 启用向量功能<sup>[7](https://supabase.com/docs/guides/ai/vector-columns)</sup>。

近期版本修复了索引构建漏洞。0.8.2 版本（2026 年 2 月 25 日）修复了并行 HNSW 索引构建中的缓冲区溢出漏洞 CVE-2026-3172，该漏洞可能泄露其他关系的敏感数据或导致服务器崩溃<sup>[1](https://www.postgresql.org/about/news/pgvector-082-released-3245/)</sup>。0.8.7 版本（2026 年 10 月 1 日）修复了 CVE-2026-103484，即 IVFFlat 索引构建中的缓冲区溢出漏洞，影响 0.8.6 及所有更早版本，能够创建 IVFFlat 索引的数据库用户可利用该漏洞进行越界写入，从而导致任意代码执行<sup>[6](https://www.postgresql.org/about/news/pgvector-087-released-3392/)</sup><sup> • </sup><sup>[14](https://devnews.news/news/languages/pgvector-0-8-7-fixes-ivfflat-buffer-overflow-cve-2026-103484/)</sup>。

## References

1. [PostgreSQL: pgvector 0.8.2 Released](https://www.postgresql.org/about/news/pgvector-082-released-3245/)
2. [pgvector README (v0.8.6 source view), The Consensus](https://theconsensus.dev/project/pgvector/source/v0.8.6/README.md)
3. [Postgres FM, Transcript: pgvector (Jonathan Katz)](https://postgres.fm/episodes/pgvector/transcript)
4. [pgvector META.json, GitHub](https://github.com/pgvector/pgvector/blob/master/META.json)
5. [pgvector README, GitHub](https://github.com/pgvector/pgvector/blob/master/README.md?plain=1)
6. [PostgreSQL: pgvector 0.8.7 Released](https://www.postgresql.org/about/news/pgvector-087-released-3392/)
7. [Supabase Docs: Vector columns](https://supabase.com/docs/guides/ai/vector-columns)
8. [pgvector CHANGELOG, GitHub](https://github.com/pgvector/pgvector/blob/master/CHANGELOG.md)
9. [Kwaai AI Lab: vector_dbs_benchmarking](https://github.com/Kwaai-AI-Lab/vector_dbs_benchmarking)
10. [Pinecone vs pgvector, Pinecone](https://www.pinecone.io/compare/pgvector/)
11. [Fast Vector Search in PostgreSQL: A Decoupled Approach, CIDR 2026](https://vldb.org/cidrdb/papers/2026/p2-liu.pdf)
12. [State of PostgreSQL 2023, Timescale](https://s3.amazonaws.com/assets.timescale.com/state-of-postgresql/Report_State_of_PostgreSQL_2023.pdf?v2=)
13. [Amazon RDS for PostgreSQL now supports pgvector, AWS](https://aws.amazon.com/about-aws/whats-new/2023/05/amazon-rds-postgresql-pgvector-ml-model-integration/)
14. [pgvector 0.8.7 fixes an IVFFlat index-build overflow, Dev News Daily](https://devnews.news/news/languages/pgvector-0-8-7-fixes-ivfflat-buffer-overflow-cve-2026-103484/)

---
*Initially written Oct 15, 2026 · Edited: Oct 25, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › Database engines and systems › Vector databases and similarity-search stores

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/pgvector

English original: https://www.edgepedia.org/pgvector
