vector
pgvector : vector data type and ivfflat and hnsw access methods
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 1800 | vector | pgvector | 0.8.7 |
RAG | PostgreSQL | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
| --s-d-r | No | Yes | No | Yes | yes | no |
| Relationships | |
|---|---|
| Need By | documentdb pg_lion_vector pg_search pgmnemo vchord vectorize vectorscale |
| See Also | vchord vectorscale pgcontext vectorize pg_rrf pg_search vchord_bm25 pg_bestmatch pgml pg4ml |
Packages
| Linux / PG | PG19 | PG18 | PG17 | PG16 | PG15 |
|---|---|---|---|---|---|
| el8.x86_64 | N/A | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| el8.aarch64 | N/A | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| el9.x86_64 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| el9.aarch64 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| el10.x86_64 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| el10.aarch64 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| d12.x86_64 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| d12.aarch64 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| d13.x86_64 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| d13.aarch64 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| u22.x86_64 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| u22.aarch64 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| u24.x86_64 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| u24.aarch64 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| u26.x86_64 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
| u26.aarch64 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 | PGDG 0.8.7 |
Source
github.com/pgvector/pgvector
pgvector-0.8.7.tar.gz
Install
Make sure PGDG repo available:
Install this extension with pig:
Create this extension with:
Usage
Sources:
pgvector provides vector similarity search inside PostgreSQL. The extension name is vector, while Pigsty packages it as pgvector. It supports exact search, approximate nearest-neighbor search with HNSW and IVFFlat indexes, and multiple vector representations for dense, half-precision, binary, and sparse embeddings.
Version 0.8.7 is a focused correctness release. It retains the 0.8.x HNSW iterative-scan and maintenance improvements documented in the current README.
Create and Query Vectors
Common distance operators:
<->for L2 distance<#>for negative inner product<=>for cosine distance<+>for L1 distance<~>for Hamming distance on binary vectors<%>for Jaccard distance on binary vectors
Because PostgreSQL indexes scan in ascending order, <#> returns the negative inner product; multiply by -1 when displaying the actual inner product.
Vector Types
vector is the standard single-precision type. Use halfvec to reduce storage and memory pressure, bit for binary signatures, and sparsevec for high-dimensional sparse vectors.
Aggregates such as avg() and sum() can be used with vector columns:
HNSW Indexes
HNSW gives strong speed/recall tradeoffs and does not require a training step.
Choose the operator class that matches the distance:
Useful tuning settings include hnsw.ef_search, hnsw.iterative_scan, hnsw.max_scan_tuples, and hnsw.scan_mem_multiplier.
IVFFlat Indexes
IVFFlat requires representative data before index creation because it trains cluster lists at build time.
Increase lists for larger tables and increase ivfflat.probes for higher recall. For filtered queries, test whether an exact btree filter, a partial vector index, or partitioning gives better plans.
Filtering and Hybrid Search
Normal PostgreSQL filters can be combined with vector ordering:
For hybrid search, combine pgvector with PostgreSQL full text search, trigram search, or an external ranking expression:
Maintenance
HNSW indexes can be large and expensive to build. Use maintenance_work_mem for builds, monitor build notices, and schedule REINDEX when index bloat or recall drift matters.
Caveats
- Version
0.8.6fixes an IVFFlat build overflow on 32-bit systems, enforcement of the nonzero-element limit when casting an array tosparsevec, and memory growth during IVFFlat scans inside nested loops. It does not add a new SQL feature surface. RunALTER EXTENSION vector UPDATEafter installing new extension files when the database reports an older SQL version. - Use the operator class that matches the query operator. A cosine index will not accelerate an L2
ORDER BY. - Approximate indexes trade exact recall for speed. Validate recall with representative data and query filters.
- Build IVFFlat after loading data. If data distribution changes substantially, rebuild the index.
- Keep pgvector updated when using HNSW with heavy writes and vacuum activity; the
0.8.xline includes important HNSW maintenance fixes.
Version 0.8.7
Version 0.8.7 additionally fixes an IVFFlat build buffer overflow and averaging an empty set. Install the new library/SQL files and run ALTER EXTENSION vector UPDATE. No index-format migration or mandatory index rebuild is documented for 0.8.7.