AsyncCockroachDBVectorStore is an implementation of a LangChain vector store using CockroachDB’s distributed SQL database with native vector support.
This notebook goes over how to use the AsyncCockroachDBVectorStore API.
The code lives in the integration package: langchain-cockroachdb.
Overview
CockroachDB is a distributed SQL database that provides:- Native vector support with the
VECTORdata type (v24.2+) - Distributed C-SPANN indexes for approximate nearest neighbor (ANN) search (v25.2+)
- SERIALIZABLE isolation by default for transaction correctness
- Horizontal scalability with automatic sharding and replication
- PostgreSQL wire-compatible for easy adoption
Key advantages for vector workloads
- Distributed vector indexes: C-SPANN indexes automatically shard across your cluster
- Multi-tenancy support: Prefix columns in indexes for efficient tenant isolation
- Strong consistency: SERIALIZABLE transactions prevent data anomalies
- High availability: Automatic failover with no data loss
Setup
Install
Install the integration library,langchain-cockroachdb.
CockroachDB cluster
You need a CockroachDB cluster with vector support (v24.2+). Choose one option:Option 1: CockroachDB Cloud (Recommended)
- Sign up at cockroachlabs.cloud
- Create a free cluster
- Get your connection string from the cluster details page
Option 2: Docker (Development)
Option 3: Local binary
Download from cockroachlabs.com/docs/releasesSet your connection values
Initialization
Create a connection engine
TheCockroachDBEngine manages a connection pool to your cluster:
Initialize a table
Create a table with the proper schema for vector storage:Create an embedding instance
Use any LangChain embeddings model.Initialize the vector store
Manage vector store
Add documents
Add documents with metadata:Add texts
Add text directly without structuring as documents:Delete documents
Delete documents by ID:Query vector store
Similarity search
Search for similar documents using natural language:Similarity search with scores
Get relevance scores with results:Search by vector
Search using a pre-computed embedding vector:Maximum marginal relevance (MMR) search
Retrieve diverse results that balance relevance and diversity:Vector indexes
Speed up similarity search with CockroachDB’s C-SPANN vector indexes (requires v25.2+).What is C-SPANN?
C-SPANN (CockroachDB Space Partition Approximate Nearest Neighbor) is a distributed vector index that:- Automatically shards across your cluster nodes
- Provides sub-second query performance at scale
- Supports cosine, Euclidean (L2), and inner product distances
- Works with prefix columns for multi-tenant architectures
Create a vector index
Distance strategies
Choose the distance metric that matches your use case:Tune index parameters
Adjust partition sizes for performance:Query-time tuning
Adjust search parameters at query time:Drop an index
Remove a vector index:Metadata filtering
Filter similarity searches using metadata fields.Supported operators
Filter examples
Sync interface
All async methods have sync equivalents using the sync wrapper:Usage for retrieval-augmented generation (RAG)
For implementing RAG with CockroachDB as your vector store, see the LangChain RAG tutorial. The CockroachDB vector store can be used in place of any other vector store in those patterns.Clean up
Drop the vector store table:API reference
For detailed documentation of all features and configurations:Additional resources
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

