Skip to content

CockroachDBEngine API

Connection and transaction management.

langchain_cockroachdb.engine.CockroachDBEngine

Manages async SQLAlchemy engine for CockroachDB with retry support.

from_connection_string(connection_string, *, pool_size=10, max_overflow=20, pool_pre_ping=True, pool_recycle=3600, pool_timeout=30.0, retry_max_attempts=5, retry_initial_backoff=0.1, retry_max_backoff=10.0, retry_backoff_multiplier=2.0, retry_jitter=True, **kwargs) classmethod

Create engine from connection string.

Parameters:

Name Type Description Default
connection_string str

CockroachDB connection URL

required
pool_size int

Connection pool size (default: 10)

10
max_overflow int

Max connections beyond pool_size (default: 20)

20
pool_pre_ping bool

Enable connection health checks (default: True)

True
pool_recycle int

Recycle connections after N seconds (default: 3600)

3600
pool_timeout float

Connection timeout in seconds (default: 30.0)

30.0
retry_max_attempts int

Maximum retry attempts (default: 5)

5
retry_initial_backoff float

Initial backoff delay in seconds (default: 0.1)

0.1
retry_max_backoff float

Maximum backoff delay in seconds (default: 10.0)

10.0
retry_backoff_multiplier float

Backoff multiplier (default: 2.0)

2.0
retry_jitter bool

Add randomization to backoff (default: True)

True
**kwargs Any

Additional arguments for create_async_engine

{}

Returns:

Type Description
CockroachDBEngine

CockroachDBEngine instance

from_engine(engine) classmethod

Create from existing AsyncEngine.

Parameters:

Name Type Description Default
engine AsyncEngine

SQLAlchemy AsyncEngine

required

Returns:

Type Description
CockroachDBEngine

CockroachDBEngine instance

ainit_vectorstore_table(table_name, vector_dimension, *, schema='public', id_type='UUID', content_column='content', embedding_column='embedding', metadata_column='metadata', namespace_column=None, create_tsvector=False, drop_if_exists=False) async

Create vector store table with optional full-text search and namespace.

Uses retry logic configured on engine instance.

Parameters:

Name Type Description Default
table_name str

Name of the table to create

required
vector_dimension int

Dimension of vector embeddings

required
schema str

Database schema (default: public)

'public'
id_type str

Type for ID column (default: UUID)

'UUID'
content_column str

Name of content column

'content'
embedding_column str

Name of embedding column

'embedding'
metadata_column str

Name of metadata column

'metadata'
namespace_column str | None

Name of namespace column for multi-tenancy (default: None, disabled)

None
create_tsvector bool

Create TSVECTOR column for FTS

False
drop_if_exists bool

Drop table if it exists

False

init_vectorstore_table(table_name, vector_dimension, **kwargs)

Sync wrapper for ainit_vectorstore_table.

aclose() async

Close async engine.

close()

Sync wrapper for aclose.

Overview

The CockroachDBEngine manages database connections, connection pooling, and table initialization.

Class Methods

from_connection_string

Create engine from connection string.

engine = CockroachDBEngine.from_connection_string(
    "cockroachdb://user:pass@host:26257/db",
    pool_size=10,
    max_overflow=20,
)

from_engine

Create from existing SQLAlchemy engine.

from sqlalchemy.ext.asyncio import create_async_engine

async_engine = create_async_engine("cockroachdb://...")
engine = CockroachDBEngine.from_engine(async_engine)

Instance Methods

ainit_vectorstore_table

Create vector store table (async).

await engine.ainit_vectorstore_table(
    table_name="documents",
    vector_dimension=1536,
    drop_if_exists=False,
)

init_vectorstore_table

Create vector store table (sync).

engine.init_vectorstore_table(
    table_name="documents",
    vector_dimension=1536,
)

aclose / close

Close engine and connections.

await engine.aclose()  # Async
engine.close()          # Sync

Configuration Parameters

Parameter Type Default Description
pool_size int 10 Base connection pool size
max_overflow int 20 Additional connections allowed
pool_pre_ping bool True Health check before use
pool_recycle int 3600 Recycle connections after (seconds)
pool_timeout float 30.0 Wait timeout for connection
retry_max_attempts int 5 Maximum retry attempts
retry_initial_backoff float 0.1 Initial retry delay
retry_max_backoff float 10.0 Maximum retry delay
retry_backoff_multiplier float 2.0 Backoff multiplier
retry_jitter bool True Add randomization to backoff

Examples

See Configuration Guide for detailed examples.