首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >pgvector 源码学习: 1 pgvector 简介 (Introduction to pgvector)

pgvector 源码学习: 1 pgvector 简介 (Introduction to pgvector)

作者头像
用户4035096
发布2026-07-09 19:42:38
发布2026-07-09 19:42:38
140
举报

pgvector不用多介绍, PostgreSQL的向量索引插件, 也可能是数据库领域最早一批支持向量搜索功能的开源插件!

今天把这一系列文章一把梭了(接下来每半个小时发一篇, 敬请关注本公众号)。祝贺PolarDB第二届国赛开赛, 祝愿同学们取得好成绩! ( 必须提前声明一下, 我对赛题细节一无所知! 如果想取得优异的成绩, 建议多关注官方渠道和“PolarDB公众号”! )

本文介绍 pgvector PostgreSQL 扩展(PostgreSQL extension)的高级概述(high-level overview),包括其架构(architecture)、核心功能(core capabilities)和关键组件(key components)。分享帮助理解 pgvector 如何在 PostgreSQL 中实现向量相似性搜索vector similarity search)所需的基本概念。


什么是 pgvector? (What is pgvector?)

pgvector 是一个开源的 PostgreSQL 扩展(extension),它为 PostgreSQL 添加了向量相似性搜索vector similarity search)功能。它支持存储和查询高维向量嵌入vector embeddings)以及常规关系数据,并完全支持 PostgreSQL 的 ACID 属性、时间点恢复point-in-time recovery, PITR)、JOINs(连接)和事务语义transactional semantics)。

该扩展实现了针对生产使用优化的近似最近邻approximate nearest neighbor, ANN)搜索算法,使应用程序能够高效地在从数千到数十亿个向量的数据集中查找相似向量。

来源:README.md 1-15


核心功能 (Core Capabilities)

pgvector 提供了以下功能:

Capability (功能)

Description (描述)

Details (详情)

Search Modes (搜索模式)

Exact and approximate nearest neighbor search (精确和近似最近邻搜索)

Exact search for perfect recall (精确搜索实现完美召回); ANN indexes for speed-recall tradeoff (ANN 索引实现速度-召回率的权衡)

Vector Types (向量类型)

Four vector representations (四种向量表示)

vector (float32), halfvec (float16), sparsevec (sparse float32, 稀疏 float32), bit (binary, 二进制)

Distance Metrics (距离度量)

Six distance functions (六种距离函数)

L2 distance (L2 距离), inner product (内积), cosine distance (余弦距离), L1 distance (L1 距离), Hamming distance (汉明距离), Jaccard distance (Jaccard 距离)

Index Methods (索引方法)

Two ANN algorithms (两种 ANN 算法)

HNSW (hierarchical graph, 分层图), IVFFlat (k-means clustering, k-均值聚类)

Language Support (语言支持)

Any language with PostgreSQL client (任何带有 PostgreSQL 客户端的语言)

30+ language bindings available (30 多种语言绑定可用)

PostgreSQL Integration (PostgreSQL 集成)

Full feature compatibility (全面的功能兼容性)

ACID compliance (ACID 合规性), replication (复制), partitioning (分区), parallel queries (并行查询)

来源:README.md 5-12


系统架构概览 (System Architecture Overview)

下图展示了 pgvector 如何与 PostgreSQL 集成并暴露其功能:

关键集成点 (Key Integration Points):

  1. Type System (类型系统): 向 PostgreSQL 的类型系统type system)注册四种自定义数据类型(vectorhalfvecsparsevecbit
  2. Operators (运算符): 定义与 PostgreSQL 运算符框架operator framework)集成的距离运算符(<-><#><=><+><~><%>
  3. Access Methods (访问方法): 实现两个 IndexAmRoutine 处理器hnswhandlerivfflathandler),它们接入 PostgreSQL 的索引扫描机制index scan machinery
  4. Cost Estimation (成本估算): 提供成本估算函数cost estimation functions),帮助查询规划器选择最优执行计划optimal execution plans

来源:README.md 1-15README.md 194-203README.md 330-332


关键组件和代码结构 (Key Components and Code Structure)

下图将系统组件映射到其实现文件:

来源:README.md 942-1059


文件组织 (File Organization)

pgvector 的源代码被组织成功能模块functional modules):

核心类型系统 (Core Type System)

  • src/vector.c - vector 类型实现(float32 密集向量)
  • src/halfvec.c - halfvec 类型实现(float16 密集向量)
  • src/sparsevec.c - sparsevec 类型实现(sparse vectors, 稀疏向量)
  • src/bitvector.c - 二进制量化Binary quantization)和位向量操作bit vector operations

HNSW 索引 (HNSW Index)

  • src/hnsw.c - 主 HNSW 访问方法处理器access method handler
  • src/hnswbuild.c - 索引构建逻辑Index construction logic
  • src/hnswinsert.c - 插入更新操作Insert and update operations
  • src/hnswscan.c - 搜索算法实现Search algorithm implementation
  • src/hnswvacuum.c - VACUUM(清理)和维护操作maintenance operations
  • src/hnswutils.c - HNSW 实用函数(Utility functions

IVFFlat 索引 (IVFFlat Index)

  • src/ivfflat.c - 主 IVFFlat 访问方法处理器access method handler
  • src/ivfbuild.c - 使用 k-means(k-均值)进行索引构建
  • src/ivfinsert.c - 插入操作(Insert operations
  • src/ivfscan.c - 基于列表的搜索实现List-based search implementation
  • src/ivfvacuum.c - VACUUM(清理)操作
  • src/ivfkmeans.c - Elkan 的 k-均值聚类算法Elkan's k-means clustering algorithm
  • src/ivfutils.c - IVFFlat 实用函数(Utility functions

支持文件 (Support Files)

  • src/halfutils.c - 使用 F16C 进行半精度浮点转换Half-precision float conversion
  • src/bitutils.c - 带有 AVX512 优化位操作Bit manipulation
  • sql/vector--*.sql - 类型、运算符、函数的 SQL 定义SQL definitions

来源:README.md 1-15


扩展生命周期 (Extension Lifecycle)

下图展示了 pgvector 如何与 PostgreSQL 的扩展系统extension system)集成:

来源:README.md 52-57README.md 204-263


版本信息与兼容性 (Version Information and Compatibility)

当前版本 (Current Version)

  • 最新版本Latest Release): 0.8.1 (2025-09-04)
  • PostgreSQL 支持PostgreSQL Support): Versions 13 through 19 (including rc1)

关键版本里程碑 (Key Version Milestones)

Version (版本)

Release Date (发布日期)

Major Changes (主要变化)

0.8.1

2025-09-04

Postgres 18 rc1 支持,binary_quantize性能改进(performance improvements)

0.8.0

2024-10-30

迭代索引扫描(Iterative index scans),改进的成本估算(cost estimation),停止支持 Postgres 12

0.7.0

2024-04-29

添加了 halfvec、sparsevec、bit索引(indexing),HNSW 支持 L1 距离(L1 distance)

0.6.0

2024-01-29

HNSW并行构建(Parallel HNSW builds),将向量存储(vector storage)改为外部存储(external)

0.5.0

2023-08-28

HNSW 索引类型(HNSW index type)添加,IVFFlat并行构建(parallel builds)

平台支持 (Platform Support)

  • 操作系统Operating Systems): Linux, macOS, Windows, FreeBSD
  • 架构Architectures): x86-64 (带有 SIMD 优化), ARM64, i386
  • CPU 特性CPU Features): 对 AVXF16CFMAAVX512运行时调度Runtime dispatch

来源:CHANGELOG.md 1-226README.md 16-50


维度限制 (Dimension Limits)

不同的向量类型和索引方法有不同的维度限制dimensional limitations):

Vector Type (向量类型)

Maximum Dimensions (Storage) (最大维度(存储))

Maximum Dimensions (HNSW Index) (最大维度(HNSW 索引))

Maximum Dimensions (IVFFlat Index) (最大维度(IVFFlat 索引))

vector

16,000

2,000

2,000

halfvec

16,000

4,000

4,000

sparsevec

16,000 non-zero (16,000 个非零元素)

1,000 non-zero (1,000 个非零元素)

Not supported (不支持)

bit

64,000

64,000

64,000

对于更高维度,可以采用半精度索引half-precision indexing)、二进制量化binary quantization)或降维dimensionality reduction)等技术。

来源:README.md 250-254README.md 368-373README.md 813-816README.md 942-945


存储要求 (Storage Requirements)

每种向量类型都有不同的内存占用memory footprints):

Type (类型)

Storage Formula (存储公式)

Example (3 dimensions) (示例(3 维))

vector

4 × dimensions + 8 bytes

20 bytes

halfvec

2 × dimensions + 8 bytes

14 bytes

sparsevec

8 × non-zero + 16 bytes

40 bytes (3 non-zero, 3 个非零元素)

bit

dimensions / 8 + 8 bytes

9 bytes

这些存储特性storage characteristics)会影响索引大小index size)和查询性能query performance)。选择合适的类型需要在精度要求precision requirements)与存储和性能约束之间进行权衡。

来源:README.md 944-945README.md 982-983README.md 1020-1021README.md 1037-1039

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-11-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 digoal德哥 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 什么是 pgvector? (What is pgvector?)
  • 核心功能 (Core Capabilities)
  • 系统架构概览 (System Architecture Overview)
  • 关键组件和代码结构 (Key Components and Code Structure)
  • 文件组织 (File Organization)
    • 核心类型系统 (Core Type System)
    • HNSW 索引 (HNSW Index)
    • IVFFlat 索引 (IVFFlat Index)
    • 支持文件 (Support Files)
  • 扩展生命周期 (Extension Lifecycle)
  • 版本信息与兼容性 (Version Information and Compatibility)
    • 当前版本 (Current Version)
    • 关键版本里程碑 (Key Version Milestones)
    • 平台支持 (Platform Support)
  • 维度限制 (Dimension Limits)
  • 存储要求 (Storage Requirements)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档