Skip to content

vector

pgvector : vector data type and ivfflat and hnsw access methods

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
1800
vector
pgvector
0.8.5
RAG
PostgreSQL
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-r
No
Yes
No
Yes
yes
no
Relationships
Need By
documentdb
pgmnemo
vchord
vectorize
vectorscale
See Also
pg_bestmatch
pg_summarize
pg_tiktoken
pg4ml
pgml
pg_similarity
smlar
pg_search

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PGDG
0.8.5
18
17
16
15
14
pgvector-
RPM
PGDG
0.8.5
18
17
16
15
14
pgvector_$v-
DEB
PGDG
0.8.4
18
17
16
15
14
postgresql-$v-pgvector-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
el8.aarch64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
el9.x86_64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
el9.aarch64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
el10.x86_64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
el10.aarch64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
d12.x86_64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
d12.aarch64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
d13.x86_64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
d13.aarch64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
u22.x86_64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
u22.aarch64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
u24.x86_64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
u24.aarch64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
u26.x86_64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
u26.aarch64
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PGDG 0.8.5
PackageVersionOSORGSIZEFile URL
pgvector_180.8.5el8.x86_64pgdg109.5 KiBpgvector_18-0.8.5-1PGDG.rhel8.10.x86_64.rpm
pgvector_180.8.4el8.x86_64pigsty115.1 KiBpgvector_18-0.8.4-1PIGSTY.el8.x86_64.rpm
pgvector_180.8.4el8.x86_64pgdg109.3 KiBpgvector_18-0.8.4-1PGDG.rhel8.10.x86_64.rpm
pgvector_180.8.3el8.x86_64pgdg108.2 KiBpgvector_18-0.8.3-1PGDG.rhel8.10.x86_64.rpm
pgvector_180.8.2el8.x86_64pgdg107.3 KiBpgvector_18-0.8.2-1PGDG.rhel8.10.x86_64.rpm
pgvector_180.8.1el8.x86_64pgdg106.9 KiBpgvector_18-0.8.1-1PGDG.rhel8.x86_64.rpm
pgvector_180.8.5el8.aarch64pgdg99.0 KiBpgvector_18-0.8.5-1PGDG.rhel8.10.aarch64.rpm
pgvector_180.8.4el8.aarch64pigsty106.7 KiBpgvector_18-0.8.4-1PIGSTY.el8.aarch64.rpm
pgvector_180.8.4el8.aarch64pgdg98.8 KiBpgvector_18-0.8.4-1PGDG.rhel8.10.aarch64.rpm
pgvector_180.8.3el8.aarch64pgdg97.9 KiBpgvector_18-0.8.3-1PGDG.rhel8.10.aarch64.rpm
pgvector_180.8.2el8.aarch64pgdg97.4 KiBpgvector_18-0.8.2-1PGDG.rhel8.10.aarch64.rpm
pgvector_180.8.1el8.aarch64pgdg96.7 KiBpgvector_18-0.8.1-1PGDG.rhel8.aarch64.rpm
pgvector_180.8.5el9.x86_64pgdg109.9 KiBpgvector_18-0.8.5-1PGDG.rhel9.8.x86_64.rpm
pgvector_180.8.4el9.x86_64pigsty108.2 KiBpgvector_18-0.8.4-1PIGSTY.el9.x86_64.rpm
pgvector_180.8.4el9.x86_64pgdg109.6 KiBpgvector_18-0.8.4-1PGDG.rhel9.8.x86_64.rpm
pgvector_180.8.3el9.x86_64pgdg108.9 KiBpgvector_18-0.8.3-1PGDG.rhel9.8.x86_64.rpm
pgvector_180.8.2el9.x86_64pgdg108.7 KiBpgvector_18-0.8.2-1PGDG.rhel9.8.x86_64.rpm
pgvector_180.8.2el9.x86_64pgdg108.7 KiBpgvector_18-0.8.2-1PGDG.rhel9.7.x86_64.rpm
pgvector_180.8.2el9.x86_64pgdg108.8 KiBpgvector_18-0.8.2-1PGDG.rhel9.6.x86_64.rpm
pgvector_180.8.1el9.x86_64pgdg108.5 KiBpgvector_18-0.8.1-1PGDG.rhel9.x86_64.rpm
pgvector_180.8.5el9.aarch64pgdg95.7 KiBpgvector_18-0.8.5-1PGDG.rhel9.8.aarch64.rpm
pgvector_180.8.4el9.aarch64pigsty98.0 KiBpgvector_18-0.8.4-1PIGSTY.el9.aarch64.rpm
pgvector_180.8.4el9.aarch64pgdg95.3 KiBpgvector_18-0.8.4-1PGDG.rhel9.8.aarch64.rpm
pgvector_180.8.3el9.aarch64pgdg94.8 KiBpgvector_18-0.8.3-1PGDG.rhel9.8.aarch64.rpm
pgvector_180.8.2el9.aarch64pgdg94.7 KiBpgvector_18-0.8.2-1PGDG.rhel9.8.aarch64.rpm
pgvector_180.8.2el9.aarch64pgdg94.8 KiBpgvector_18-0.8.2-1PGDG.rhel9.7.aarch64.rpm
pgvector_180.8.2el9.aarch64pgdg94.9 KiBpgvector_18-0.8.2-1PGDG.rhel9.6.aarch64.rpm
pgvector_180.8.1el9.aarch64pgdg94.2 KiBpgvector_18-0.8.1-1PGDG.rhel9.aarch64.rpm
pgvector_180.8.5el10.x86_64pgdg106.1 KiBpgvector_18-0.8.5-1PGDG.rhel10.2.x86_64.rpm
pgvector_180.8.4el10.x86_64pigsty109.2 KiBpgvector_18-0.8.4-1PIGSTY.el10.x86_64.rpm
pgvector_180.8.4el10.x86_64pgdg105.9 KiBpgvector_18-0.8.4-1PGDG.rhel10.2.x86_64.rpm
pgvector_180.8.3el10.x86_64pgdg105.0 KiBpgvector_18-0.8.3-1PGDG.rhel10.2.x86_64.rpm
pgvector_180.8.2el10.x86_64pgdg105.1 KiBpgvector_18-0.8.2-1PGDG.rhel10.2.x86_64.rpm
pgvector_180.8.2el10.x86_64pgdg105.1 KiBpgvector_18-0.8.2-1PGDG.rhel10.1.x86_64.rpm
pgvector_180.8.2el10.x86_64pgdg105.7 KiBpgvector_18-0.8.2-1PGDG.rhel10.0.x86_64.rpm
pgvector_180.8.1el10.x86_64pgdg104.9 KiBpgvector_18-0.8.1-1PGDG.rhel10.x86_64.rpm
pgvector_180.8.5el10.aarch64pgdg98.0 KiBpgvector_18-0.8.5-1PGDG.rhel10.2.aarch64.rpm
pgvector_180.8.4el10.aarch64pigsty100.3 KiBpgvector_18-0.8.4-1PIGSTY.el10.aarch64.rpm
pgvector_180.8.4el10.aarch64pgdg97.7 KiBpgvector_18-0.8.4-1PGDG.rhel10.2.aarch64.rpm
pgvector_180.8.3el10.aarch64pgdg96.9 KiBpgvector_18-0.8.3-1PGDG.rhel10.2.aarch64.rpm
pgvector_180.8.2el10.aarch64pgdg96.9 KiBpgvector_18-0.8.2-1PGDG.rhel10.2.aarch64.rpm
pgvector_180.8.2el10.aarch64pgdg96.9 KiBpgvector_18-0.8.2-1PGDG.rhel10.1.aarch64.rpm
pgvector_180.8.2el10.aarch64pgdg96.9 KiBpgvector_18-0.8.2-1PGDG.rhel10.0.aarch64.rpm
pgvector_180.8.1el10.aarch64pgdg96.8 KiBpgvector_18-0.8.1-1PGDG.rhel10.aarch64.rpm
postgresql-18-pgvector0.8.5d12.x86_64pgdg261.3 KiBpostgresql-18-pgvector_0.8.5-1.pgdg12+1_amd64.deb
postgresql-18-pgvector0.8.4d12.x86_64pgdg261.0 KiBpostgresql-18-pgvector_0.8.4-1.pgdg12+1_amd64.deb
postgresql-18-pgvector0.8.4d12.x86_64pigsty254.5 KiBpostgresql-18-pgvector_0.8.4-1PIGSTY~bookworm_amd64.deb
postgresql-18-pgvector0.8.3d12.x86_64pgdg258.4 KiBpostgresql-18-pgvector_0.8.3-1.pgdg12+1_amd64.deb
postgresql-18-pgvector0.8.5d12.aarch64pgdg231.3 KiBpostgresql-18-pgvector_0.8.5-1.pgdg12+1_arm64.deb
postgresql-18-pgvector0.8.4d12.aarch64pgdg231.0 KiBpostgresql-18-pgvector_0.8.4-1.pgdg12+1_arm64.deb
postgresql-18-pgvector0.8.4d12.aarch64pigsty229.2 KiBpostgresql-18-pgvector_0.8.4-1PIGSTY~bookworm_arm64.deb
postgresql-18-pgvector0.8.3d12.aarch64pgdg228.9 KiBpostgresql-18-pgvector_0.8.3-1.pgdg12+1_arm64.deb
postgresql-18-pgvector0.8.5d13.x86_64pgdg262.1 KiBpostgresql-18-pgvector_0.8.5-1.pgdg13+1_amd64.deb
postgresql-18-pgvector0.8.4d13.x86_64pgdg261.9 KiBpostgresql-18-pgvector_0.8.4-1.pgdg13+1_amd64.deb
postgresql-18-pgvector0.8.4d13.x86_64pigsty254.8 KiBpostgresql-18-pgvector_0.8.4-1PIGSTY~trixie_amd64.deb
postgresql-18-pgvector0.8.3d13.x86_64pgdg259.3 KiBpostgresql-18-pgvector_0.8.3-1.pgdg13+1_amd64.deb
postgresql-18-pgvector0.8.5d13.aarch64pgdg232.5 KiBpostgresql-18-pgvector_0.8.5-1.pgdg13+1_arm64.deb
postgresql-18-pgvector0.8.4d13.aarch64pgdg232.3 KiBpostgresql-18-pgvector_0.8.4-1.pgdg13+1_arm64.deb
postgresql-18-pgvector0.8.4d13.aarch64pigsty230.3 KiBpostgresql-18-pgvector_0.8.4-1PIGSTY~trixie_arm64.deb
postgresql-18-pgvector0.8.3d13.aarch64pgdg229.9 KiBpostgresql-18-pgvector_0.8.3-1.pgdg13+1_arm64.deb
postgresql-18-pgvector0.8.5u22.x86_64pgdg264.0 KiBpostgresql-18-pgvector_0.8.5-1.pgdg22.04+1_amd64.deb
postgresql-18-pgvector0.8.4u22.x86_64pgdg264.0 KiBpostgresql-18-pgvector_0.8.4-1.pgdg22.04+1_amd64.deb
postgresql-18-pgvector0.8.4u22.x86_64pigsty272.5 KiBpostgresql-18-pgvector_0.8.4-1PIGSTY~jammy_amd64.deb
postgresql-18-pgvector0.8.3u22.x86_64pgdg262.0 KiBpostgresql-18-pgvector_0.8.3-1.pgdg22.04+1_amd64.deb
postgresql-18-pgvector0.8.5u22.aarch64pgdg232.0 KiBpostgresql-18-pgvector_0.8.5-1.pgdg22.04+1_arm64.deb
postgresql-18-pgvector0.8.4u22.aarch64pgdg231.7 KiBpostgresql-18-pgvector_0.8.4-1.pgdg22.04+1_arm64.deb
postgresql-18-pgvector0.8.4u22.aarch64pigsty246.4 KiBpostgresql-18-pgvector_0.8.4-1PIGSTY~jammy_arm64.deb
postgresql-18-pgvector0.8.3u22.aarch64pgdg230.0 KiBpostgresql-18-pgvector_0.8.3-1.pgdg22.04+1_arm64.deb
postgresql-18-pgvector0.8.5u24.x86_64pgdg257.7 KiBpostgresql-18-pgvector_0.8.5-1.pgdg24.04+1_amd64.deb
postgresql-18-pgvector0.8.4u24.x86_64pgdg257.7 KiBpostgresql-18-pgvector_0.8.4-1.pgdg24.04+1_amd64.deb
postgresql-18-pgvector0.8.4u24.x86_64pigsty261.9 KiBpostgresql-18-pgvector_0.8.4-1PIGSTY~noble_amd64.deb
postgresql-18-pgvector0.8.3u24.x86_64pgdg255.2 KiBpostgresql-18-pgvector_0.8.3-1.pgdg24.04+1_amd64.deb
postgresql-18-pgvector0.8.5u24.aarch64pgdg227.5 KiBpostgresql-18-pgvector_0.8.5-1.pgdg24.04+1_arm64.deb
postgresql-18-pgvector0.8.4u24.aarch64pgdg227.6 KiBpostgresql-18-pgvector_0.8.4-1.pgdg24.04+1_arm64.deb
postgresql-18-pgvector0.8.4u24.aarch64pigsty239.8 KiBpostgresql-18-pgvector_0.8.4-1PIGSTY~noble_arm64.deb
postgresql-18-pgvector0.8.3u24.aarch64pgdg225.4 KiBpostgresql-18-pgvector_0.8.3-1.pgdg24.04+1_arm64.deb
postgresql-18-pgvector0.8.5u26.x86_64pgdg256.2 KiBpostgresql-18-pgvector_0.8.5-1.pgdg26.04+1_amd64.deb
postgresql-18-pgvector0.8.4u26.x86_64pgdg255.9 KiBpostgresql-18-pgvector_0.8.4-1.pgdg26.04+1_amd64.deb
postgresql-18-pgvector0.8.4u26.x86_64pigsty261.2 KiBpostgresql-18-pgvector_0.8.4-1PIGSTY~resolute_amd64.deb
postgresql-18-pgvector0.8.3u26.x86_64pgdg253.6 KiBpostgresql-18-pgvector_0.8.3-1.pgdg26.04+1_amd64.deb
postgresql-18-pgvector0.8.5u26.aarch64pgdg227.2 KiBpostgresql-18-pgvector_0.8.5-1.pgdg26.04+1_arm64.deb
postgresql-18-pgvector0.8.4u26.aarch64pgdg226.6 KiBpostgresql-18-pgvector_0.8.4-1.pgdg26.04+1_arm64.deb
postgresql-18-pgvector0.8.4u26.aarch64pigsty238.8 KiBpostgresql-18-pgvector_0.8.4-1PIGSTY~resolute_arm64.deb
postgresql-18-pgvector0.8.3u26.aarch64pgdg224.4 KiBpostgresql-18-pgvector_0.8.3-1.pgdg26.04+1_arm64.deb

Source

pig build pkg pgvector;		# build rpm

Install

Make sure PGDG repo available:

pig repo add pgdg -u    # add pgdg repo and update cache

Install this extension with pig:

pig install pgvector;		# install via package name, for the active PG version
pig install vector;		# install by extension name, for the current active PG version

pig install vector -v 18;   # install for PG 18
pig install vector -v 17;   # install for PG 17
pig install vector -v 16;   # install for PG 16
pig install vector -v 15;   # install for PG 15
pig install vector -v 14;   # install for PG 14

Create this extension with:

CREATE EXTENSION vector;

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.

v0.8.4 is a maintenance release after the 0.8.x HNSW/vacuum fixes. Use it instead of older 0.8.x builds when maintaining HNSW indexes under write-heavy workloads.

Create and Query Vectors

CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE items (
  id bigserial PRIMARY KEY,
  embedding vector(3)
);

INSERT INTO items (embedding)
VALUES ('[1,2,3]'), ('[4,5,6]');

SELECT *
FROM items
ORDER BY embedding <-> '[3,1,2]'
LIMIT 5;

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

CREATE TABLE embeddings (
  id bigserial PRIMARY KEY,
  dense      vector(768),
  half_dense halfvec(768),
  binary_sig bit(1024),
  sparse     sparsevec(100000)
);

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:

SELECT avg(embedding) FROM items;

HNSW Indexes

HNSW gives strong speed/recall tradeoffs and does not require a training step.

CREATE INDEX items_embedding_hnsw
ON items USING hnsw (embedding vector_l2_ops);

SET hnsw.ef_search = 100;

SELECT *
FROM items
ORDER BY embedding <-> '[3,1,2]'
LIMIT 10;

Choose the operator class that matches the distance:

CREATE INDEX ON items USING hnsw (embedding vector_ip_ops);
CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);
CREATE INDEX ON items USING hnsw (embedding vector_l1_ops);
CREATE INDEX ON embeddings USING hnsw (half_dense halfvec_l2_ops);
CREATE INDEX ON embeddings USING hnsw (sparse sparsevec_l2_ops);
CREATE INDEX ON embeddings USING hnsw (binary_sig bit_hamming_ops);

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.

CREATE INDEX items_embedding_ivfflat
ON items USING ivfflat (embedding vector_l2_ops)
WITH (lists = 100);

SET ivfflat.probes = 10;

SELECT *
FROM items
ORDER BY embedding <-> '[3,1,2]'
LIMIT 10;

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:

CREATE INDEX ON items (tenant_id);

SELECT *
FROM items
WHERE tenant_id = 42
ORDER BY embedding <=> '[0.1,0.2,0.3]'
LIMIT 20;

For hybrid search, combine pgvector with PostgreSQL full text search, trigram search, or an external ranking expression:

SELECT id,
       ts_rank_cd(text_tsv, plainto_tsquery('database')) AS text_score,
       1 - (embedding <=> '[0.1,0.2,0.3]') AS vector_score
FROM docs
WHERE text_tsv @@ plainto_tsquery('database')
ORDER BY vector_score DESC
LIMIT 20;

Maintenance

VACUUM items;
REINDEX INDEX CONCURRENTLY items_embedding_hnsw;
ANALYZE items;

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

  • Pigsty local metadata may lag this upstream version; this stub tracks upstream pgvector 0.8.4 while the local package row may still show an older package version until the package catalog is refreshed.
  • 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; v0.8.x includes important HNSW maintenance fixes.
Last updated on