· 2 min read
PostgreSQL extension - PgVector
Store vector embeddings in PostgreSQL
PgVector is the open source PostgreSQL extension for vector similarity search that powers generative artificial intelligence (AI) applications using techniques such as semantic search and retrieval-augmented generation (RAG).
In use cases such as semantic search and RAG, PostgreSQL with pgvector extension serves as a vector store knowledge base that contains data that the large language model (LLM) wasn’t trained on. This could be your business data or more recent data that became available after the model was trained. This data, or a link to the text data, is saved in a row and is associated with a vector generated by an embedding model Vector search is then used to find the most similar vector in comparison to a user or application-initiated knowledge base query vector. In the most basic form, this is accomplished by c omparing the query vector with every vector in the database using a distance function to determine the k nearest neighbors (K-NN). This full scan process can make vector search hard to scale even though vector comparison is fast.
To improve vector search, pgvector implemented approximate nearest neighbor (ANN) distance measures and indexes. These constructs allow us to accelerate data scans by performing vector similarity searches over a subset of the database. pgvector supports two ANN index types: Inverted File Flat (IVFFlat) and Hierarchical Navigable Small World (HNSW).
IVFFlat divides vectors into clusters, and then searches a subset of those clusters that are closest to the query vector. HNSW uses a multi-layered, graph-based approach designed for billions of rows vector search. HNSW is fast, efficient at scale, and popular.
References
- PASE: PostgreSQL Ultra-High-Dimensional Approximate Nearest Neighbor Search Extension
- Faiss: A Library for Efficient Similarity Search and Clustering of Dense Vectors
- Using the Triangle Inequality to Accelerate k-means
- k-means++: The Advantage of Careful Seeding
- Concept Decompositions for Large Sparse Text Data using Clustering
- Efficient and Robust Approximate Nearest Neighbor Search using Hierarchical Navigable Small World Graphs