Overview
CockroachDB is a distributed, resilient SQL database that:- Uses PostgreSQL wire protocol
- Supports most PostgreSQL syntax
- Provides distributed transactions
- Offers horizontal scalability
- Ensures high availability
Smart Query Import
The CockroachDB Smart Query is based on the PostgreSQL query with filters for CockroachDB system schemas.CockroachDB Query
CockroachDB-Specific Filters
The query filters out CockroachDB internal schemas:- Foreign keys
- Primary keys
- Columns
- Indexes
- Tables
- Views
What the Smart Query Retrieves
Same as PostgreSQL, plus:Identity Columns
Identity Columns
Detects CockroachDB’s
unique_rowid() function as identity:System Schema Filtering
System Schema Filtering
Excludes CockroachDB system schemas:
crdb_internal: Internal CockroachDB tablespg_extension: PostgreSQL extension tables
SQL Import/Export
CockroachDB uses PostgreSQL-compatible SQL syntax.Import Features
All PostgreSQL import features work, including:- CREATE TABLE with all PostgreSQL syntax
- Serial types (serial, bigserial, smallserial)
- ENUM types
- Arrays
- JSONB
- Foreign keys and constraints
- Indexes
- Views
- Multi-schema support
CockroachDB-Specific Syntax
unique_rowid() Function:Export Example
CockroachDB-Specific Features
Distributed Architecture
CockroachDB’s distributed nature affects schema design: Primary Keys:- Should avoid sequential values for better distribution
- Use UUID or
unique_rowid()instead ofserial
Secondary Indexes
CockroachDB indexes can store extra columns:- Covering indexes for better query performance
- No need to access table data
- Faster reads at the cost of more storage
JSONB Support
Full PostgreSQL-compatible JSONB:Computed Columns
Multi-Region Features
CockroachDB supports multi-region deployments:Multi-region syntax is not captured by the Smart Query but can be documented in comments.
PostgreSQL Compatibility
CockroachDB aims for PostgreSQL compatibility but has some differences:Supported PostgreSQL Features
- Most PostgreSQL data types
- Serial types (but avoid for distributed performance)
- JSONB and JSON operators
- Arrays
- ENUMs
- Foreign keys
- CHECK constraints
- Views
- CTEs and window functions
Unsupported Features
- Stored procedures (use user-defined functions instead)
- Triggers (use change data capture)
- Some PostgreSQL extensions
- SEQUENCE with all PostgreSQL options
- Table inheritance
- Temporary tables (session-based only)
Differences to Note
Transaction Retries
Transaction Retries
CockroachDB may retry transactions. Use client-side retry logic or savepoints.
Serializable Isolation
Serializable Isolation
Default isolation level is SERIALIZABLE (stricter than PostgreSQL’s READ COMMITTED).
Clock Skew
Clock Skew
Distributed transactions may observe different timestamps. Use cluster time for ordering.
Best Practices
Troubleshooting
Query returns internal tables
Query returns internal tables
The Smart Query should filter out
crdb_internal and pg_extension schemas. If you see them, check the query filters.Transaction retry errors
Transaction retry errors
CockroachDB may return retry errors. Implement client-side retry logic or use savepoints:
Slow sequential scans
Slow sequential scans
Create appropriate secondary indexes or use STORING clauses for covering indexes.
Hot spot issues
Hot spot issues
Avoid sequential primary keys. Use UUID or
unique_rowid() for better distribution.Migration from PostgreSQL
Migrating from PostgreSQL to CockroachDB:Additional Resources
CockroachDB Docs
Official CockroachDB documentation
PostgreSQL Compatibility
PostgreSQL feature compatibility matrix
Best Practices
CockroachDB performance best practices
Multi-Region
Multi-region deployment guide
