Postgres Embedding is an open-source vector similarity search forPostgres
that usesHierarchical Navigable Small Worlds (HNSW)
for approximate nearest neighbor search.
It supports:This notebook shows how to use the Postgres vector database (
- exact and approximate nearest neighbor search using HNSW
- L2 distance
PGEmbedding
).
The PGEmbedding integration creates the pg_embedding extension for you, but you run the following Postgres query to add it:
OpenAIEmbeddings
.
Working with vectorstore in Postgres
Uploading a vectorstore in PG
Create HNSW Index
By default, the extension performs a sequential scan search, with 100% recall. You might consider creating an HNSW index for approximate nearest neighbor (ANN) search to speed upsimilarity_search_with_score
execution time. To create the HNSW index on your vector column, use a create_hnsw_index
function:
- maxelements: Defines the maximum number of elements indexed. This is a required parameter. The example shown above has a value of 3. A real-world example would have a much large value, such as 1000000. An “element” refers to a data point (a vector) in the dataset, which is represented as a node in the HNSW graph. Typically, you would set this option to a value able to accommodate the number of rows in your in your dataset.
- dims: Defines the number of dimensions in your vector data. This is a required parameter. A small value is used in the example above. If you are storing data generated using OpenAI’s text-embedding-ada-002 model, which supports 1536 dimensions, you would define a value of 1536, for example.
- m: Defines the maximum number of bi-directional links (also referred to as “edges”) created for each node during graph construction. The following additional index options are supported:
- efConstruction: Defines the number of nearest neighbors considered during index construction. The default value is 32.
- efsearch: Defines the number of nearest neighbors considered during index search. The default value is 32. For information about how you can configure these options to influence the HNSW algorithm, refer to Tuning the HNSW algorithm.