Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Open-source vector similarity search for PostgresPro

License

NotificationsYou must be signed in to change notification settings

postgrespro/pgvector

 
 

Repository files navigation

Open-source vector similarity search for Postgres

Store your vectors with the rest of your data. Supports:

  • exact and approximate nearest neighbor search
  • single-precision, half-precision, binary, and sparse vectors
  • L2 distance, inner product, cosine distance, L1 distance, Hamming distance, and Jaccard distance
  • anylanguage with a Postgres client

PlusACID compliance, point-in-time recovery, JOINs, and all of the othergreat features of Postgres

Build Status

Installation

Linux and Mac

Compile and install the extension (supports Postgres 13+)

cd /tmpgit clone --branch v0.8.0 https://github.com/pgvector/pgvector.gitcd pgvectormakemake install# may need sudo

See theinstallation notes if you run into issues

You can also install it withDocker,Homebrew,PGXN,APT,Yum,pkg, orconda-forge, and it comes preinstalled withPostgres.app and manyhosted providers. There are also instructions forGitHub Actions.

Windows

EnsureC++ support in Visual Studio is installed, and run:

call"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"

Note: The exact path will vary depending on your Visual Studio version and edition

Then usenmake to build:

set"PGROOT=C:\Program Files\PostgreSQL\16"cd%TEMP%git clone --branch v0.8.0 https://github.com/pgvector/pgvector.gitcd pgvectornmake /F Makefile.winnmake /F Makefile.win install

Note: Postgres 17 is not supported with MSVC yet due to anupstream issue

See theinstallation notes if you run into issues

You can also install it withDocker orconda-forge.

Getting Started

Enable the extension (do this once in each database where you want to use it)

CREATE EXTENSION vector;

Create a vector column with 3 dimensions

CREATETABLEitems (idbigserialPRIMARY KEY, embedding vector(3));

Insert vectors

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

Get the nearest neighbors by L2 distance

SELECT*FROM itemsORDER BY embedding<->'[3,1,2]'LIMIT5;

Also supports inner product (<#>), cosine distance (<=>), and L1 distance (<+>, added in 0.7.0)

Note:<#> returns the negative inner product since Postgres only supportsASC order index scans on operators

Storing

Create a new table with a vector column

CREATETABLEitems (idbigserialPRIMARY KEY, embedding vector(3));

Or add a vector column to an existing table

ALTERTABLE items ADD COLUMN embedding vector(3);

Also supportshalf-precision,binary, andsparse vectors

Insert vectors

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

Or load vectors in bulk usingCOPY (example)

COPY items (embedding)FROM STDIN WITH (FORMAT BINARY);

Upsert vectors

INSERT INTO items (id, embedding)VALUES (1,'[1,2,3]'), (2,'[4,5,6]')ON CONFLICT (id) DOUPDATESET embedding=EXCLUDED.embedding;

Update vectors

UPDATE itemsSET embedding='[1,2,3]'WHERE id=1;

Delete vectors

DELETEFROM itemsWHERE id=1;

Querying

Get the nearest neighbors to a vector

SELECT*FROM itemsORDER BY embedding<->'[3,1,2]'LIMIT5;

Supported distance functions are:

  • <-> - L2 distance
  • <#> - (negative) inner product
  • <=> - cosine distance
  • <+> - L1 distance (added in 0.7.0)
  • <~> - Hamming distance (binary vectors, added in 0.7.0)
  • <%> - Jaccard distance (binary vectors, added in 0.7.0)

Get the nearest neighbors to a row

SELECT*FROM itemsWHERE id!=1ORDER BY embedding<-> (SELECT embeddingFROM itemsWHERE id=1)LIMIT5;

Get rows within a certain distance

SELECT*FROM itemsWHERE embedding<->'[3,1,2]'<5;

Note: Combine withORDER BY andLIMIT to use an index

Distances

Get the distance

SELECT embedding<->'[3,1,2]'AS distanceFROM items;

For inner product, multiply by -1 (since<#> returns the negative inner product)

SELECT (embedding<#>'[3,1,2]')*-1AS inner_productFROM items;

For cosine similarity, use 1 - cosine distance

SELECT1- (embedding<=>'[3,1,2]')AS cosine_similarityFROM items;

Aggregates

Average vectors

SELECTAVG(embedding)FROM items;

Average groups of vectors

SELECT category_id,AVG(embedding)FROM itemsGROUP BY category_id;

Indexing

By default, pgvector performs exact nearest neighbor search, which provides perfect recall.

You can add an index to use approximate nearest neighbor search, which trades some recall for speed. Unlike typical indexes, you will see different results for queries after adding an approximate index.

Supported index types are:

HNSW

An HNSW index creates a multilayer graph. It has better query performance than IVFFlat (in terms of speed-recall tradeoff), but has slower build times and uses more memory. Also, an index can be created without any data in the table since there isn’t a training step like IVFFlat.

Add an index for each distance function you want to use.

L2 distance

CREATEINDEXON items USING hnsw (embedding vector_l2_ops);

Note: Usehalfvec_l2_ops forhalfvec andsparsevec_l2_ops forsparsevec (and similar with the other distance functions)

Inner product

CREATEINDEXON items USING hnsw (embedding vector_ip_ops);

Cosine distance

CREATEINDEXON items USING hnsw (embedding vector_cosine_ops);

L1 distance - added in 0.7.0

CREATEINDEXON items USING hnsw (embedding vector_l1_ops);

Hamming distance - added in 0.7.0

CREATEINDEXON items USING hnsw (embedding bit_hamming_ops);

Jaccard distance - added in 0.7.0

CREATEINDEXON items USING hnsw (embedding bit_jaccard_ops);

Supported types are:

  • vector - up to 2,000 dimensions
  • halfvec - up to 4,000 dimensions (added in 0.7.0)
  • bit - up to 64,000 dimensions (added in 0.7.0)
  • sparsevec - up to 1,000 non-zero elements (added in 0.7.0)

Index Options

Specify HNSW parameters

  • m - the max number of connections per layer (16 by default)
  • ef_construction - the size of the dynamic candidate list for constructing the graph (64 by default)
CREATEINDEXON items USING hnsw (embedding vector_l2_ops) WITH (m=16, ef_construction=64);

A higher value ofef_construction provides better recall at the cost of index build time / insert speed.

Query Options

Specify the size of the dynamic candidate list for search (40 by default)

SEThnsw.ef_search=100;

A higher value provides better recall at the cost of speed.

UseSET LOCAL inside a transaction to set it for a single query

BEGIN;SET LOCALhnsw.ef_search=100;SELECT ...COMMIT;

Index Build Time

Indexes build significantly faster when the graph fits intomaintenance_work_mem

SET maintenance_work_mem='8GB';

A notice is shown when the graph no longer fits

NOTICE:  hnsw graph no longer fits into maintenance_work_mem after 100000 tuplesDETAIL:  Building will take significantly more time.HINT:  Increase maintenance_work_mem to speed up builds.

Note: Do not setmaintenance_work_mem so high that it exhausts the memory on the server

Like other index types, it’s faster to create an index after loading your initial data

Starting with 0.6.0, you can also speed up index creation by increasing the number of parallel workers (2 by default)

SET max_parallel_maintenance_workers=7;-- plus leader

For a large number of workers, you may also need to increasemax_parallel_workers (8 by default)

Indexing Progress

Checkindexing progress

SELECT phase, round(100.0* blocks_done/ nullif(blocks_total,0),1)AS"%"FROM pg_stat_progress_create_index;

The phases for HNSW are:

  1. initializing
  2. loading tuples

IVFFlat

An IVFFlat index divides vectors into lists, and then searches a subset of those lists that are closest to the query vector. It has faster build times and uses less memory than HNSW, but has lower query performance (in terms of speed-recall tradeoff).

Three keys to achieving good recall are:

  1. Create the indexafter the table has some data
  2. Choose an appropriate number of lists - a good place to start isrows / 1000 for up to 1M rows andsqrt(rows) for over 1M rows
  3. When querying, specify an appropriate number ofprobes (higher is better for recall, lower is better for speed) - a good place to start issqrt(lists)

Add an index for each distance function you want to use.

L2 distance

CREATEINDEXON items USING ivfflat (embedding vector_l2_ops) WITH (lists=100);

Note: Usehalfvec_l2_ops forhalfvec (and similar with the other distance functions)

Inner product

CREATEINDEXON items USING ivfflat (embedding vector_ip_ops) WITH (lists=100);

Cosine distance

CREATEINDEXON items USING ivfflat (embedding vector_cosine_ops) WITH (lists=100);

Hamming distance - added in 0.7.0

CREATEINDEXON items USING ivfflat (embedding bit_hamming_ops) WITH (lists=100);

Supported types are:

  • vector - up to 2,000 dimensions
  • halfvec - up to 4,000 dimensions (added in 0.7.0)
  • bit - up to 64,000 dimensions (added in 0.7.0)

Query Options

Specify the number of probes (1 by default)

SETivfflat.probes=10;

A higher value provides better recall at the cost of speed, and it can be set to the number of lists for exact nearest neighbor search (at which point the planner won’t use the index)

UseSET LOCAL inside a transaction to set it for a single query

BEGIN;SET LOCALivfflat.probes=10;SELECT ...COMMIT;

Index Build Time

Speed up index creation on large tables by increasing the number of parallel workers (2 by default)

SET max_parallel_maintenance_workers=7;-- plus leader

For a large number of workers, you may also need to increasemax_parallel_workers (8 by default)

Indexing Progress

Checkindexing progress

SELECT phase, round(100.0* tuples_done/ nullif(tuples_total,0),1)AS"%"FROM pg_stat_progress_create_index;

The phases for IVFFlat are:

  1. initializing
  2. performing k-means
  3. assigning tuples
  4. loading tuples

Note:% is only populated during theloading tuples phase

Filtering

There are a few ways to index nearest neighbor queries with aWHERE clause.

SELECT*FROM itemsWHERE category_id=123ORDER BY embedding<->'[3,1,2]'LIMIT5;

A good place to start is creating an index on the filter column. This can provide fast, exact nearest neighbor search in many cases. Postgres has a number ofindex types for this: B-tree (default), hash, GiST, SP-GiST, GIN, and BRIN.

CREATEINDEXON items (category_id);

For multiple columns, consider amulticolumn index.

CREATEINDEXON items (location_id, category_id);

Exact indexes work well for conditions that match a low percentage of rows. Otherwise,approximate indexes can work better.

CREATEINDEXON items USING hnsw (embedding vector_l2_ops);

With approximate indexes, filtering is appliedafter the index is scanned. If a condition matches 10% of rows, with HNSW and the defaulthnsw.ef_search of 40, only 4 rows will match on average. For more rows, increasehnsw.ef_search.

SEThnsw.ef_search=200;

Starting with 0.8.0, you can enableiterative index scans, which will automatically scan more of the index when needed.

SEThnsw.iterative_scan= strict_order;

If filtering by only a few distinct values, considerpartial indexing.

CREATEINDEXON items USING hnsw (embedding vector_l2_ops)WHERE (category_id=123);

If filtering by many different values, considerpartitioning.

CREATETABLEitems (embedding vector(3), category_idint) PARTITION BY LIST(category_id);

Iterative Index Scans

Added in 0.8.0

With approximate indexes, queries with filtering can return less results since filtering is appliedafter the index is scanned. Starting with 0.8.0, you can enable iterative index scans, which will automatically scan more of the index until enough results are found (or it reacheshnsw.max_scan_tuples orivfflat.max_probes).

Iterative scans can use strict or relaxed ordering.

Strict ensures results are in the exact order by distance

SEThnsw.iterative_scan= strict_order;

Relaxed allows results to be slightly out of order by distance, but provides better recall

SEThnsw.iterative_scan= relaxed_order;# orSETivfflat.iterative_scan= relaxed_order;

With relaxed ordering, you can use amaterialized CTE to get strict ordering

WITH relaxed_resultsAS MATERIALIZED (SELECT id, embedding<->'[1,2,3]'AS distanceFROM itemsWHERE category_id=123ORDER BY distanceLIMIT5)SELECT*FROM relaxed_resultsORDER BY distance;

For queries that filter by distance, use a materialized CTE and place the distance filter outside of it for best performance (due to thecurrent behavior of the Postgres executor)

WITH nearest_resultsAS MATERIALIZED (SELECT id, embedding<->'[1,2,3]'AS distanceFROM itemsORDER BY distanceLIMIT5)SELECT*FROM nearest_resultsWHERE distance<5ORDER BY distance;

Note: Place any other filters inside the CTE

Iterative Scan Options

Since scanning a large portion of an approximate index is expensive, there are options to control when a scan ends.

HNSW

Specify the max number of tuples to visit (20,000 by default)

SEThnsw.max_scan_tuples=20000;

Note: This is approximate and does not affect the initial scan

Specify the max amount of memory to use, as a multiple ofwork_mem (1 by default)

SEThnsw.scan_mem_multiplier=2;

Note: Try increasing this if increasinghnsw.max_scan_tuples does not improve recall

IVFFlat

Specify the max number of probes

SETivfflat.max_probes=100;

Note: If this is lower thanivfflat.probes,ivfflat.probes will be used

Half-Precision Vectors

Added in 0.7.0

Use thehalfvec type to store half-precision vectors

CREATETABLEitems (idbigserialPRIMARY KEY, embedding halfvec(3));

Half-Precision Indexing

Added in 0.7.0

Index vectors at half precision for smaller indexes

CREATEINDEXON items USING hnsw ((embedding::halfvec(3)) halfvec_l2_ops);

Get the nearest neighbors

SELECT*FROM itemsORDER BY embedding::halfvec(3)<->'[1,2,3]'LIMIT5;

Binary Vectors

Use thebit type to store binary vectors (example)

CREATETABLEitems (idbigserialPRIMARY KEY, embeddingbit(3));INSERT INTO items (embedding)VALUES ('000'), ('111');

Get the nearest neighbors by Hamming distance (added in 0.7.0)

SELECT*FROM itemsORDER BY embedding<~>'101'LIMIT5;

Or (before 0.7.0)

SELECT*FROM itemsORDER BY bit_count(embedding# '101') LIMIT 5;

Also supports Jaccard distance (<%>)

Binary Quantization

Added in 0.7.0

Use expression indexing for binary quantization

CREATEINDEXON items USING hnsw ((binary_quantize(embedding)::bit(3)) bit_hamming_ops);

Get the nearest neighbors by Hamming distance

SELECT*FROM itemsORDER BY binary_quantize(embedding)::bit(3)<~> binary_quantize('[1,-2,3]')LIMIT5;

Re-rank by the original vectors for better recall

SELECT*FROM (SELECT*FROM itemsORDER BY binary_quantize(embedding)::bit(3)<~> binary_quantize('[1,-2,3]')LIMIT20)ORDER BY embedding<=>'[1,-2,3]'LIMIT5;

Sparse Vectors

Added in 0.7.0

Use thesparsevec type to store sparse vectors

CREATETABLEitems (idbigserialPRIMARY KEY, embedding sparsevec(5));

Insert vectors

INSERT INTO items (embedding)VALUES ('{1:1,3:2,5:3}/5'), ('{1:4,3:5,5:6}/5');

The format is{index1:value1,index2:value2}/dimensions and indices start at 1 like SQL arrays

Get the nearest neighbors by L2 distance

SELECT*FROM itemsORDER BY embedding<->'{1:3,3:1,5:2}/5'LIMIT5;

Hybrid Search

Use together with Postgresfull-text search for hybrid search.

SELECT id, contentFROM items, plainto_tsquery('hello search') queryWHERE textsearch @@ queryORDER BY ts_rank_cd(textsearch, query)DESCLIMIT5;

You can useReciprocal Rank Fusion or across-encoder to combine results.

Indexing Subvectors

Added in 0.7.0

Use expression indexing to index subvectors

CREATEINDEXON items USING hnsw ((subvector(embedding,1,3)::vector(3)) vector_cosine_ops);

Get the nearest neighbors by cosine distance

SELECT*FROM itemsORDER BY subvector(embedding,1,3)::vector(3)<=> subvector('[1,2,3,4,5]'::vector,1,3)LIMIT5;

Re-rank by the full vectors for better recall

SELECT*FROM (SELECT*FROM itemsORDER BY subvector(embedding,1,3)::vector(3)<=> subvector('[1,2,3,4,5]'::vector,1,3)LIMIT20)ORDER BY embedding<=>'[1,2,3,4,5]'LIMIT5;

Performance

Tuning

Use a tool likePgTune to set initial values for Postgres server parameters. For instance,shared_buffers should typically be 25% of the server’s memory. You can find the config file with:

SHOW config_file;

And check individual settings with:

SHOW shared_buffers;

Be sure to restart Postgres for changes to take effect.

Loading

UseCOPY for bulk loading data (example).

COPY items (embedding)FROM STDIN WITH (FORMAT BINARY);

Add any indexesafter loading the initial data for best performance.

Indexing

See index build time forHNSW andIVFFlat.

In production environments, create indexes concurrently to avoid blocking writes.

CREATEINDEXCONCURRENTLY ...

Querying

UseEXPLAIN ANALYZE to debug performance.

EXPLAIN ANALYZESELECT*FROM itemsORDER BY embedding<->'[3,1,2]'LIMIT5;

Exact Search

To speed up queries without an index, increasemax_parallel_workers_per_gather.

SET max_parallel_workers_per_gather=4;

If vectors are normalized to length 1 (likeOpenAI embeddings), use inner product for best performance.

SELECT*FROM itemsORDER BY embedding<#>'[3,1,2]'LIMIT5;

Approximate Search

To speed up queries with an IVFFlat index, increase the number of inverted lists (at the expense of recall).

CREATEINDEXON items USING ivfflat (embedding vector_l2_ops) WITH (lists=1000);

Vacuuming

Vacuuming can take a while for HNSW indexes. Speed it up by reindexing first.

REINDEX INDEX CONCURRENTLY index_name;VACUUM table_name;

Monitoring

Monitor performance withpg_stat_statements (be sure to add it toshared_preload_libraries).

CREATE EXTENSION pg_stat_statements;

Get the most time-consuming queries with:

SELECT query, calls, ROUND((total_plan_time+ total_exec_time)/ calls)AS avg_time_ms,    ROUND((total_plan_time+ total_exec_time)/60000)AS total_time_minFROM pg_stat_statementsORDER BY total_plan_time+ total_exec_timeDESCLIMIT20;

Note: Replacetotal_plan_time + total_exec_time withtotal_time for Postgres < 13

Monitor recall by comparing results from approximate search with exact search.

BEGIN;SET LOCAL enable_indexscan= off;-- use exact searchSELECT ...COMMIT;

Scaling

Scale pgvector the same way you scale Postgres.

Scale vertically by increasing memory, CPU, and storage on a single instance. Use existing tools totune parameters andmonitor performance.

Scale horizontally withreplicas, or useCitus or another approach for sharding (example).

Languages

Use pgvector from any language with a Postgres client. You can even generate and store vectors in one language and query them in another.

LanguageLibraries / Examples
Cpgvector-c
C++pgvector-cpp
C#, F#, Visual Basicpgvector-dotnet
Crystalpgvector-crystal
Dpgvector-d
Dartpgvector-dart
Elixirpgvector-elixir
Erlangpgvector-erlang
Fortranpgvector-fortran
Gleampgvector-gleam
Gopgvector-go
Haskellpgvector-haskell
Java, Kotlin, Groovy, Scalapgvector-java
JavaScript, TypeScriptpgvector-node
Juliapgvector-julia
Lisppgvector-lisp
Luapgvector-lua
Nimpgvector-nim
OCamlpgvector-ocaml
Perlpgvector-perl
PHPpgvector-php
Pythonpgvector-python
Rpgvector-r
Rakupgvector-raku
Rubypgvector-ruby,Neighbor
Rustpgvector-rust
Swiftpgvector-swift
Zigpgvector-zig

Frequently Asked Questions

How many vectors can be stored in a single table?

A non-partitioned table has a limit of 32 TB by default in Postgres. A partitioned table can have thousands of partitions of that size.

Is replication supported?

Yes, pgvector uses the write-ahead log (WAL), which allows for replication and point-in-time recovery.

What if I want to index vectors with more than 2,000 dimensions?

You can usehalf-precision indexing to index up to 4,000 dimensions orbinary quantization to index up to 64,000 dimensions. Another option isdimensionality reduction.

Can I store vectors with different dimensions in the same column?

You can usevector as the type (instead ofvector(3)).

CREATETABLEembeddings (model_idbigint, item_idbigint, embedding vector,PRIMARY KEY (model_id, item_id));

However, you can only create indexes on rows with the same number of dimensions (usingexpression andpartial indexing):

CREATEINDEXON embeddings USING hnsw ((embedding::vector(3)) vector_l2_ops)WHERE (model_id=123);

and query with:

SELECT*FROM embeddingsWHERE model_id=123ORDER BY embedding::vector(3)<->'[3,1,2]'LIMIT5;

Can I store vectors with more precision?

You can use thedouble precision[] ornumeric[] type to store vectors with more precision.

CREATETABLEitems (idbigserialPRIMARY KEY, embeddingdouble precision[]);-- use {} instead of [] for Postgres arraysINSERT INTO items (embedding)VALUES ('{1,2,3}'), ('{4,5,6}');

Optionally, add acheck constraint to ensure data can be converted to thevector type and has the expected dimensions.

ALTERTABLE items ADDCHECK (vector_dims(embedding::vector)=3);

Useexpression indexing to index (at a lower precision):

CREATEINDEXON items USING hnsw ((embedding::vector(3)) vector_l2_ops);

and query with:

SELECT*FROM itemsORDER BY embedding::vector(3)<->'[3,1,2]'LIMIT5;

Do indexes need to fit into memory?

No, but like other index types, you’ll likely see better performance if they do. You can get the size of an index with:

SELECT pg_size_pretty(pg_relation_size('index_name'));

Troubleshooting

Why isn’t a query using an index?

The query needs to have anORDER BY andLIMIT, and theORDER BY must be the result of a distance operator (not an expression) in ascending order.

-- indexORDER BY embedding<=>'[3,1,2]'LIMIT5;-- no indexORDER BY1- (embedding<=>'[3,1,2]')DESCLIMIT5;

You can encourage the planner to use an index for a query with:

BEGIN;SET LOCAL enable_seqscan= off;SELECT ...COMMIT;

Also, if the table is small, a table scan may be faster.

Why isn’t a query using a parallel table scan?

The planner doesn’t considerout-of-line storage in cost estimates, which can make a serial scan look cheaper. You can reduce the cost of a parallel scan for a query with:

BEGIN;SET LOCAL min_parallel_table_scan_size=1;SET LOCAL parallel_setup_cost=1;SELECT ...COMMIT;

or choose to store vectors inline:

ALTERTABLE items ALTER COLUMN embeddingSET STORAGE PLAIN;

Why are there less results for a query after adding an HNSW index?

Results are limited by the size of the dynamic candidate list (hnsw.ef_search), which is 40 by default. There may be even less results due to dead tuples or filtering conditions in the query. Enablingiterative index scans can help address this.

Also, note thatNULL vectors are not indexed (as well as zero vectors for cosine distance).

Why are there less results for a query after adding an IVFFlat index?

The index was likely created with too little data for the number of lists. Drop the index until the table has more data.

DROPINDEX index_name;

Results can also be limited by the number of probes (ivfflat.probes). Enablingiterative index scans can address this.

Also, note thatNULL vectors are not indexed (as well as zero vectors for cosine distance).

Reference

Vector Type

Each vector takes4 * dimensions + 8 bytes of storage. Each element is a single-precision floating-point number (like thereal type in Postgres), and all elements must be finite (noNaN,Infinity or-Infinity). Vectors can have up to 16,000 dimensions.

Vector Operators

OperatorDescriptionAdded
+element-wise addition
-element-wise subtraction
*element-wise multiplication0.5.0
||concatenate0.7.0
<->Euclidean distance
<#>negative inner product
<=>cosine distance
<+>taxicab distance0.7.0

Vector Functions

FunctionDescriptionAdded
binary_quantize(vector) → bitbinary quantize0.7.0
cosine_distance(vector, vector) → double precisioncosine distance
inner_product(vector, vector) → double precisioninner product
l1_distance(vector, vector) → double precisiontaxicab distance0.5.0
l2_distance(vector, vector) → double precisionEuclidean distance
l2_normalize(vector) → vectorNormalize with Euclidean norm0.7.0
subvector(vector, integer, integer) → vectorsubvector0.7.0
vector_dims(vector) → integernumber of dimensions
vector_norm(vector) → double precisionEuclidean norm

Vector Aggregate Functions

FunctionDescriptionAdded
avg(vector) → vectoraverage
sum(vector) → vectorsum0.5.0

Halfvec Type

Each half vector takes2 * dimensions + 8 bytes of storage. Each element is a half-precision floating-point number, and all elements must be finite (noNaN,Infinity or-Infinity). Half vectors can have up to 16,000 dimensions.

Halfvec Operators

OperatorDescriptionAdded
+element-wise addition0.7.0
-element-wise subtraction0.7.0
*element-wise multiplication0.7.0
||concatenate0.7.0
<->Euclidean distance0.7.0
<#>negative inner product0.7.0
<=>cosine distance0.7.0
<+>taxicab distance0.7.0

Halfvec Functions

FunctionDescriptionAdded
binary_quantize(halfvec) → bitbinary quantize0.7.0
cosine_distance(halfvec, halfvec) → double precisioncosine distance0.7.0
inner_product(halfvec, halfvec) → double precisioninner product0.7.0
l1_distance(halfvec, halfvec) → double precisiontaxicab distance0.7.0
l2_distance(halfvec, halfvec) → double precisionEuclidean distance0.7.0
l2_norm(halfvec) → double precisionEuclidean norm0.7.0
l2_normalize(halfvec) → halfvecNormalize with Euclidean norm0.7.0
subvector(halfvec, integer, integer) → halfvecsubvector0.7.0
vector_dims(halfvec) → integernumber of dimensions0.7.0

Halfvec Aggregate Functions

FunctionDescriptionAdded
avg(halfvec) → halfvecaverage0.7.0
sum(halfvec) → halfvecsum0.7.0

Bit Type

Each bit vector takesdimensions / 8 + 8 bytes of storage. See thePostgres docs for more info.

Bit Operators

OperatorDescriptionAdded
<~>Hamming distance0.7.0
<%>Jaccard distance0.7.0

Bit Functions

FunctionDescriptionAdded
hamming_distance(bit, bit) → double precisionHamming distance0.7.0
jaccard_distance(bit, bit) → double precisionJaccard distance0.7.0

Sparsevec Type

Each sparse vector takes8 * non-zero elements + 16 bytes of storage. Each element is a single-precision floating-point number, and all elements must be finite (noNaN,Infinity or-Infinity). Sparse vectors can have up to 16,000 non-zero elements.

Sparsevec Operators

OperatorDescriptionAdded
<->Euclidean distance0.7.0
<#>negative inner product0.7.0
<=>cosine distance0.7.0
<+>taxicab distance0.7.0

Sparsevec Functions

FunctionDescriptionAdded
cosine_distance(sparsevec, sparsevec) → double precisioncosine distance0.7.0
inner_product(sparsevec, sparsevec) → double precisioninner product0.7.0
l1_distance(sparsevec, sparsevec) → double precisiontaxicab distance0.7.0
l2_distance(sparsevec, sparsevec) → double precisionEuclidean distance0.7.0
l2_norm(sparsevec) → double precisionEuclidean norm0.7.0
l2_normalize(sparsevec) → sparsevecNormalize with Euclidean norm0.7.0

Installation Notes - Linux and Mac

Postgres Location

If your machine has multiple Postgres installations, specify the path topg_config with:

export PG_CONFIG=/Library/PostgreSQL/17/bin/pg_config

Then re-run the installation instructions (runmake clean beforemake if needed). Ifsudo is needed formake install, use:

sudo --preserve-env=PG_CONFIG make install

A few common paths on Mac are:

  • EDB installer -/Library/PostgreSQL/17/bin/pg_config
  • Homebrew (arm64) -/opt/homebrew/opt/postgresql@17/bin/pg_config
  • Homebrew (x86-64) -/usr/local/opt/postgresql@17/bin/pg_config

Note: Replace17 with your Postgres server version

Missing Header

If compilation fails withfatal error: postgres.h: No such file or directory, make sure Postgres development files are installed on the server.

For Ubuntu and Debian, use:

sudo apt install postgresql-server-dev-17

Note: Replace17 with your Postgres server version

Missing SDK

If compilation fails and the output includeswarning: no such sysroot directory on Mac, reinstall Xcode Command Line Tools.

Portability

By default, pgvector compiles with-march=native on some platforms for best performance. However, this can lead toIllegal instruction errors if trying to run the compiled extension on a different machine.

To compile for portability, use:

make OPTFLAGS=""

Installation Notes - Windows

Missing Header

If compilation fails withCannot open include file: 'postgres.h': No such file or directory, make surePGROOT is correct.

Permissions

If installation fails withAccess is denied, re-run the installation instructions as an administrator.

Additional Installation Methods

Docker

Get theDocker image with:

docker pull pgvector/pgvector:pg17

This adds pgvector to thePostgres image (replace17 with your Postgres server version, and run it the same way).

You can also build the image manually:

git clone --branch v0.8.0 https://github.com/pgvector/pgvector.gitcd pgvectordocker build --pull --build-arg PG_MAJOR=17 -t myuser/pgvector.

Homebrew

With Homebrew Postgres, you can use:

brew install pgvector

Note: This only adds it to thepostgresql@17 andpostgresql@14 formulas

PGXN

Install from thePostgreSQL Extension Network with:

pgxn install vector

APT

Debian and Ubuntu packages are available from thePostgreSQL APT Repository. Follow thesetup instructions and run:

sudo apt install postgresql-17-pgvector

Note: Replace17 with your Postgres server version

Yum

RPM packages are available from thePostgreSQL Yum Repository. Follow thesetup instructions for your distribution and run:

sudo yum install pgvector_17# orsudo dnf install pgvector_17

Note: Replace17 with your Postgres server version

pkg

Install the FreeBSD package with:

pkg install postgresql16-pgvector

or the port with:

cd /usr/ports/databases/pgvectormake install

conda-forge

With Conda Postgres, install fromconda-forge with:

conda install -c conda-forge pgvector

This method iscommunity-maintained by@mmcauliffe

Postgres.app

Download thelatest release with Postgres 15+.

Hosted Postgres

pgvector is available onthese providers.

Upgrading

Install the latest version (use the same method as the original installation). Then in each database you want to upgrade, run:

ALTER EXTENSION vectorUPDATE;

You can check the version in the current database with:

SELECT extversionFROM pg_extensionWHERE extname='vector';

Upgrade Notes

0.6.0

Postgres 12

If upgrading with Postgres 12, remove this line fromsql/vector--0.5.1--0.6.0.sql:

ALTERTYPE vectorSET (STORAGE= external);

Then runmake install andALTER EXTENSION vector UPDATE;.

Docker

The Docker image is now published in thepgvector org, and there are tags for each supported version of Postgres (rather than alatest tag).

docker pull pgvector/pgvector:pg16# ordocker pull pgvector/pgvector:0.6.0-pg16

Also, if you’ve increasedmaintenance_work_mem, make sure--shm-size is at least that size to avoid an error with parallel HNSW index builds.

docker run --shm-size=1g ...

Thanks

Thanks to:

History

View thechangelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/pgvector/pgvector.gitcd pgvectormakemake install

To run all tests:

make installcheck# regression testsmake prove_installcheck# TAP tests

To run single tests:

make installcheck REGRESS=functions# regression testmake prove_installcheck PROVE_TESTS=test/t/001_ivfflat_wal.pl# TAP test

To enable assertions:

make clean&& PG_CFLAGS="-DUSE_ASSERT_CHECKING" make&& make install

To enable benchmarking:

make clean&& PG_CFLAGS="-DIVFFLAT_BENCH" make&& make install

To show memory usage:

make clean&& PG_CFLAGS="-DHNSW_MEMORY -DIVFFLAT_MEMORY" make&& make install

To get k-means metrics:

make clean&& PG_CFLAGS="-DIVFFLAT_KMEANS_DEBUG" make&& make install

Resources for contributors

About

Open-source vector similarity search for PostgresPro

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C77.0%
  • Perl22.3%
  • Other0.7%

[8]ページ先頭

©2009-2025 Movatter.jp