Skip to main content
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 VECTOR data 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:
  1. Sign up at cockroachlabs.cloud
  2. Create a free cluster
  3. Get your connection string from the cluster details page

Option 2: Docker (Development)

Option 3: Local binary

Download from cockroachlabs.com/docs/releases

Set your connection values

Initialization

Create a connection engine

The CockroachDBEngine manages a connection pool to your cluster:

Initialize a table

Create a table with the proper schema for vector storage:
Optional: Specify a schema name

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:
Performance note: CockroachDB’s vector indexes work best with smaller batch sizes. The default batch_size=100 is optimized for vector inserts. Large batch inserts of VECTOR types can cause performance degradation.

Delete documents

Delete documents by ID:

Query vector store

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:
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

⚠️ This operation cannot be undone
Drop the vector store table:

API reference

For detailed documentation of all features and configurations:

Additional resources