Using VectorChord

VectorChord is a powerful extension that enhances the capabilities of pgvector for high-dimensional vector indexing. To leverage its full potential, it's important to understand how to configure and query VectorChord indices effectively within your AI pipelines.

Implementing VectorChord involves several key steps, from configuring the index parameters to integrating it with your existing retrieval functions.

Step 1: Define the data structure

Create a table that includes a vector column. This column stores the high-dimensional embeddings generated by your AI models.

CREATE TABLE items (
  id bigint PRIMARY KEY,
  embedding vector(1024)
);

The vector(1024) type defines the dimensionality of the embeddings, ensuring compatibility with models that output 1024-length vectors.

Step 2: Create a VectorChord index

VectorChord supports multiple index methods. The following example uses vchordrq (IVF-RaBitQ), one option designed for efficient Approximate Nearest Neighbor (ANN) searches. Other available methods include HNSW, configurable via aidb.vector_index_chord_hnsw_config().

CREATE INDEX items_embedding_vchordrq
  ON items
  USING vchordrq (embedding vector_l2_ops);

By using vchordrq with the vector_l2_ops operator class, the database can perform high-speed similarity lookups using Euclidean distance (L2) distance rather than performing an exhaustive scan of the entire table.

Run a query to find the most relevant items base on a query vector.

SELECT id
FROM items
ORDER BY embedding <-> :query_vec
LIMIT 10;

The <-> operator calculates the L2 distance between the stored embeddings and the input :query_vec, returning the 10 closest matches.

While the manual SQL above shows how VectorChord works at a low level, in EDB Postgres AI, these indices are typically managed through the KnowledgeBase pipeline step. You can configure VectorChord during pipeline creation using helper functions like aidb.vector_index_chord_hnsw_config() or aidb.vector_index_chord_vchordq_config().


Could this page be better? Report a problem or suggest an addition!