CockroachDB SQL Skill
Converts natural language questions into CockroachDB-compliant SQL queries, following CockroachDB best practices. Use it for schema design, writing queries and optimizing query.
How to Apply this Skill
Connection Detection — already performed on skill invocation; reuse active connection.
Parse Natural Language Intent
- Identify the operation type (SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, etc.)
Context Gathering
- Check for existing schema context in conversation
- If connected to DB, query existing schema:
SHOW TABLES; to see existing tables
SHOW CREATE TABLE table_name; for existing structure
- Ask clarifying questions if needed:
- Table structure if not provided
- Data types for columns
- Index requirements
- Multi-region needs
- Performance characteristics
Apply CockroachDB Rules
- Reference rules in
references/cockroachdb-rules/
- Ensure compliance with CockroachDB best practices
- Determine rule category based on operation and apply the relavant rules:
00-fundamental-principles.md - Always apply these first
01-schema-design.md - Table creation and structure
02-dml-operations.md - Data modification
03-query-patterns.md - Query construction
04-optimization.md - Performance, Optimization and anti-patterns
05-operational.md - Admin and maintenance
- Validate against anti-patterns in 04-optimization.md
Validate against DB(MANDATORY)
- ALWAYS run EXPLAIN on every generated SQL query when connected to DB.
- If EXPLAIN returns a parsing/syntax error, fix the query and re-run EXPLAIN until it passes.
- Include the EXPLAIN output in the response.
Response Behavior
Initial Response
When skill is invoked:
Check for an available connection so queries run against the right cluster:
- Check if a connection string is provided in the prompt (postgresql://...).
- If provided, use
cockroach sql --url "<provided-url>" -e "SQL" to run queries. Do not use psql.
- Else check the COCKROACH_URL environment variable (
echo $COCKROACH_URL).
- If set, use
cockroach sql --url $COCKROACH_URL -e "SQL" to run queries. Do not use psql.
- Else check for cockroach-cloud MCP server availability.
- If none of these are available or it is unclear which cluster to use, ask the user before running anything.
Focus exclusively on CockroachDB
Emphasize "natural language to CockroachDB SQL" not "database conversion"
Present CockroachDB-specific syntax even when a rule derives from PostgreSQL compatibility.
Output Format
- Show generated SQL with explanatory comments
- List CockroachDB-specific features used
- Include performance considerations
- When optimizing, at each step 1- Explain the step's purpose. 2- Execute the step and report the outcome. 3- Summarize all findings and actions taken.
- Provide references used including the rules
Examples
Schema Design — UUID Primary Key (Avoid Hotspots)
CREATE TABLE orders (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
customer_id UUID NOT NULL,
status STRING NOT NULL DEFAULT 'pending',
total DECIMAL(10,2) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
INDEX idx_orders_customer (customer_id),
INDEX idx_orders_status_created (status, created_at DESC)
);
Batch Upsert with UNNEST
UPSERT INTO inventory (sku, warehouse, quantity, updated_at)
SELECT * FROM UNNEST(
ARRAY['SKU-001', 'SKU-002', 'SKU-003']::STRING[],
ARRAY['us-east', 'us-east', 'us-west']::STRING[],
ARRAY[100, 250, 75]::INT[],
ARRAY[now(), now(), now()]::TIMESTAMPTZ[]
);
Keyset Pagination (Avoid OFFSET)
Use the previous page's last (created_at, id) as the cursor. In application code these are bound parameters; the literals here are placeholders.
SELECT id, customer_id, created_at
FROM orders
WHERE (created_at, id) > ('2025-01-01'::TIMESTAMPTZ, '00000000-0000-0000-0000-000000000000'::UUID)
ORDER BY created_at, id
LIMIT 50;
Supporting Documentation
references/cockroachdb-rules/ - CockroachDB SQL rules
references/EXAMPLES.md - SQL examples and patterns
Additional references
- EXAMPLES.md
- 00-fundamental-principles.md
- 01-schema-design.md
- 02-dml-operations.md
- 03-query-patterns.md
- 04-optimization.md
- 05-operational.md
1---2name: cockroachdb-sql3description: Use when writing, generating, or optimizing SQL for CockroachDB, designing CockroachDB schemas, or when the user asks about CockroachDB-specific SQL patterns, type mappings, and distributed database best practices. Also use when encountering CockroachDB anti-patterns like missing primary keys, sequential ID hotspots, or incorrect type usage.4---56## CockroachDB SQL Skill 78Converts natural language questions into CockroachDB-compliant SQL queries, following CockroachDB best practices. Use it for schema design, writing queries and optimizing query.910## How to Apply this Skill11121. **Connection Detection** — already performed on skill invocation; reuse active connection.13142. **Parse Natural Language Intent**15 - Identify the operation type (SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, etc.)16173. **Context Gathering**18 - Check for existing schema context in conversation19 - If connected to DB, query existing schema:20 - `SHOW TABLES;` to see existing tables21 - `SHOW CREATE TABLE table_name;` for existing structure22 - Ask clarifying questions if needed:23 - Table structure if not provided24 - Data types for columns25 - Index requirements26 - Multi-region needs27 - Performance characteristics28294. **Apply CockroachDB Rules**30 - Reference rules in `references/cockroachdb-rules/` 31 - Ensure compliance with CockroachDB best practices32 - **Determine rule category based on operation and apply the relavant rules**:33 * `00-fundamental-principles.md` - Always apply these first34 * `01-schema-design.md` - Table creation and structure35 * `02-dml-operations.md` - Data modification36 * `03-query-patterns.md` - Query construction37 * `04-optimization.md` - Performance, Optimization and anti-patterns38 * `05-operational.md` - Admin and maintenance39 - Validate against anti-patterns in 04-optimization.md 40415. **Validate against DB(MANDATORY)**42 - ALWAYS run EXPLAIN on every generated SQL query when connected to DB.43 - If EXPLAIN returns a parsing/syntax error, fix the query and re-run EXPLAIN until it passes.44 - Include the EXPLAIN output in the response.4546## Response Behavior4748### Initial Response4950When skill is invoked:511. **Check for an available connection** so queries run against the right cluster:52 - Check if a connection string is provided in the prompt (postgresql://...).53 - If provided, use `cockroach sql --url "<provided-url>" -e "SQL"` to run queries. Do not use psql.54 - Else check the COCKROACH_URL environment variable (`echo $COCKROACH_URL`).55 - If set, use `cockroach sql --url $COCKROACH_URL -e "SQL"` to run queries. Do not use psql.56 - Else check for cockroach-cloud MCP server availability.57 - If none of these are available or it is unclear which cluster to use, ask the user before running anything.58592. Focus exclusively on CockroachDB603. Emphasize "natural language to CockroachDB SQL" not "database conversion"614. Present CockroachDB-specific syntax even when a rule derives from PostgreSQL compatibility.6263### Output Format64- Show generated SQL with explanatory comments65- List CockroachDB-specific features used66- Include performance considerations67- When optimizing, at each step 1- Explain the step's purpose. 2- Execute the step and report the outcome. 3- Summarize all findings and actions taken.68- Provide references used including the rules6970## Examples7172### Schema Design — UUID Primary Key (Avoid Hotspots)7374```sql75CREATE TABLE orders (76 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),77 customer_id UUID NOT NULL,78 status STRING NOT NULL DEFAULT 'pending',79 total DECIMAL(10,2) NOT NULL,80 created_at TIMESTAMPTZ NOT NULL DEFAULT now(),81 INDEX idx_orders_customer (customer_id),82 INDEX idx_orders_status_created (status, created_at DESC)83);84```8586### Batch Upsert with UNNEST8788```sql89UPSERT INTO inventory (sku, warehouse, quantity, updated_at)90SELECT * FROM UNNEST(91 ARRAY['SKU-001', 'SKU-002', 'SKU-003']::STRING[],92 ARRAY['us-east', 'us-east', 'us-west']::STRING[],93 ARRAY[100, 250, 75]::INT[],94 ARRAY[now(), now(), now()]::TIMESTAMPTZ[]95);96```9798### Keyset Pagination (Avoid OFFSET)99100Use the previous page's last `(created_at, id)` as the cursor. In application code these are bound parameters; the literals here are placeholders.101102```sql103SELECT id, customer_id, created_at104FROM orders105WHERE (created_at, id) > ('2025-01-01'::TIMESTAMPTZ, '00000000-0000-0000-0000-000000000000'::UUID)106ORDER BY created_at, id107LIMIT 50;108```109110## Supporting Documentation111112- `references/cockroachdb-rules/` - CockroachDB SQL rules 113- `references/EXAMPLES.md` - SQL examples and patterns114115## Additional references116117- [EXAMPLES.md](references/EXAMPLES.md)118- [00-fundamental-principles.md](references/cockroachdb-rules/00-fundamental-principles.md)119- [01-schema-design.md](references/cockroachdb-rules/01-schema-design.md)120- [02-dml-operations.md](references/cockroachdb-rules/02-dml-operations.md)121- [03-query-patterns.md](references/cockroachdb-rules/03-query-patterns.md)122- [04-optimization.md](references/cockroachdb-rules/04-optimization.md)123- [05-operational.md](references/cockroachdb-rules/05-operational.md)