Skip to content

vector

vector data type and ivfflat and hnsw access methods

Overview

PackageVersionCategoryLicenseLanguage
pgvector0.8.6RAGPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
1800vectorNoYesNoYesNoYes-
Relatedvchord vectorscale pgcontext vectorize pg_rrf pg_search vchord_bm25 pg_bestmatch pgml pg4ml
Depended Byai alloydb_scann avocado documentdb embedding_search hybrid_search maludb_core pg_cuvs pg_diskann pg_gembed pg_knowledge_graph pg_llm pg_llm_helper pg_search pg_semantic_cache pg_sentence_transformer pg_splade pg_turboquant pgcontext_pgvector pgedge_vectorizer pgmnemo pgpu pgturbohybrid pgvecutils rag rag_bge_small_en_v15 rag_jina_reranker_v1_tiny_en rds_ai rds_embedding vchord vectorize vectorscale

Upstream and source archive are at pgvector 0.8.6; indexed PGDG RPM and DEB packages remain at 0.8.5 for PostgreSQL 14-18.

Version

TypeRepoVersionPG VerPackageDeps
EXTPGDG0.8.61817161514pgvector-
RPMPGDG0.8.61817161514pgvector_$v-
DEBPGDG0.8.61817161514postgresql-$v-pgvector-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
PGDG 0.8.6
PGDG 0.8.6
el9.aarch64
PGDG 0.8.6
el9.aarch64.pg15 : pgvector_15
pgvector_15-0.8.6-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.6 · 101.3KiB pgvector_15-0.8.5-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.5 · 100.0KiB pgvector_15-0.8.4-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.4 · 99.8KiB pgvector_15-0.8.3-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.3 · 99.6KiB pgvector_15-0.8.2-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.2 · 99.5KiB pgvector_15-0.8.2-1PGDG.rhel9.7.aarch64.rpm PGDG · 0.8.2 · 99.5KiB pgvector_15-0.8.2-1PGDG.rhel9.6.aarch64.rpm PGDG · 0.8.2 · 99.6KiB pgvector_15-0.8.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.8.1 · 98.8KiB pgvector_15-0.8.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.8.0 · 98.5KiB pgvector_15-0.7.4-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.4 · 94.2KiB pgvector_15-0.7.3-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.3 · 93.8KiB pgvector_15-0.7.2-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.2 · 93.5KiB pgvector_15-0.7.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.1 · 93.1KiB pgvector_15-0.7.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.0 · 92.3KiB pgvector_15-0.6.2-2PGDG.rhel9.aarch64.rpm PGDG · 0.6.2 · 73.5KiB pgvector_15-0.6.2-1PGDG.rhel9.aarch64.rpm PGDG · 0.6.2 · 73.1KiB pgvector_15-0.6.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.6.1 · 72.0KiB pgvector_15-0.6.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.6.0 · 71.3KiB pgvector_15-0.5.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.5.1 · 61.5KiB pgvector_15-0.5.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.5.0 · 60.9KiB pgvector_15-0.4.4-1.rhel9.aarch64.rpm PGDG · 0.4.4 · 43.4KiB pgvector_15-0.4.1-1.rhel9.aarch64.rpm PGDG · 0.4.1 · 40.6KiB
PGDG 0.8.6
el9.aarch64.pg14 : pgvector_14
pgvector_14-0.8.6-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.6 · 101.2KiB pgvector_14-0.8.5-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.5 · 100.0KiB pgvector_14-0.8.4-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.4 · 100.0KiB pgvector_14-0.8.3-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.3 · 99.4KiB pgvector_14-0.8.2-1PGDG.rhel9.8.aarch64.rpm PGDG · 0.8.2 · 99.1KiB pgvector_14-0.8.2-1PGDG.rhel9.7.aarch64.rpm PGDG · 0.8.2 · 99.0KiB pgvector_14-0.8.2-1PGDG.rhel9.6.aarch64.rpm PGDG · 0.8.2 · 99.2KiB pgvector_14-0.8.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.8.1 · 98.3KiB pgvector_14-0.8.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.8.0 · 98.1KiB pgvector_14-0.7.4-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.4 · 94.2KiB pgvector_14-0.7.3-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.3 · 93.8KiB pgvector_14-0.7.2-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.2 · 93.5KiB pgvector_14-0.7.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.1 · 93.1KiB pgvector_14-0.7.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.7.0 · 92.4KiB pgvector_14-0.6.2-2PGDG.rhel9.aarch64.rpm PGDG · 0.6.2 · 73.4KiB pgvector_14-0.6.2-1PGDG.rhel9.aarch64.rpm PGDG · 0.6.2 · 73.1KiB pgvector_14-0.6.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.6.1 · 72.0KiB pgvector_14-0.6.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.6.0 · 71.3KiB pgvector_14-0.5.1-1PGDG.rhel9.aarch64.rpm PGDG · 0.5.1 · 61.5KiB pgvector_14-0.5.0-1PGDG.rhel9.aarch64.rpm PGDG · 0.5.0 · 60.9KiB pgvector_14-0.4.4-1.rhel9.aarch64.rpm PGDG · 0.4.4 · 43.3KiB pgvector_14-0.4.1-1.rhel9.aarch64.rpm PGDG · 0.4.1 · 40.5KiB
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
u24.x86_64
u24.aarch64
u26.x86_64
u26.aarch64

Build

You can build the RPM / DEB packages for pgvector using pig build:

pig build pkg pgvector         # build RPM / DEB packages

Install

You can install pgvector directly. First, make sure the PGDG repository is added and enabled:

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

Install the extension using pig or apt/yum/dnf:

Install
pig install pgvector;          # Install for current active PG version
pig
pig ext install -y pgvector -v 18  # PG 18
pig ext install -y pgvector -v 17  # PG 17
pig ext install -y pgvector -v 16  # PG 16
pig ext install -y pgvector -v 15  # PG 15
pig ext install -y pgvector -v 14  # PG 14
dnf
dnf install -y pgvector_18       # PG 18
dnf install -y pgvector_17       # PG 17
dnf install -y pgvector_16       # PG 16
dnf install -y pgvector_15       # PG 15
dnf install -y pgvector_14       # PG 14
apt
apt install -y postgresql-18-pgvector   # PG 18
apt install -y postgresql-17-pgvector   # PG 17
apt install -y postgresql-16-pgvector   # PG 16
apt install -y postgresql-15-pgvector   # PG 15
apt install -y postgresql-14-pgvector   # PG 14

Create Extension:

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.

Version 0.8.6 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

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.

Normal PostgreSQL filters can be combined with vector ordering:

ALTER TABLE items ADD COLUMN tenant_id bigint;
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:

CREATE TABLE docs (
  id bigint PRIMARY KEY,
  body text NOT NULL,
  text_tsv tsvector GENERATED ALWAYS AS
    (to_tsvector('english', body)) STORED,
  embedding vector(3)
);

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

  • Version 0.8.6 fixes an IVFFlat build overflow on 32-bit systems, enforcement of the nonzero-element limit when casting an array to sparsevec, and memory growth during IVFFlat scans inside nested loops. It does not add a new SQL feature surface. Run ALTER EXTENSION vector UPDATE after 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.x line includes important HNSW maintenance fixes.

Was this page helpful?