From cockroachdb
Analyzes CockroachDB range distribution across tables and indexes using SHOW RANGES to identify range count, size patterns, leaseholder placement, and replication health. Use when investigating hotspots, uneven data distribution, range fragmentation, or validating zone configuration effects without DB Console access.
How this skill is triggered — by the user, by Claude, or both
Slash command
/cockroachdb:analyzing-range-distributionThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Analyzes CockroachDB range distribution, leaseholder placement, and zone configuration compliance using `SHOW RANGES` and `SHOW ZONE CONFIGURATIONS` commands. Identifies range count anomalies, size imbalances, leaseholder hotspots, and replication issues - entirely via SQL without requiring DB Console access.
Analyzes CockroachDB range distribution, leaseholder placement, and zone configuration compliance using SHOW RANGES and SHOW ZONE CONFIGURATIONS commands. Identifies range count anomalies, size imbalances, leaseholder hotspots, and replication issues - entirely via SQL without requiring DB Console access.
Complement to profiling skills: This skill analyzes range-level data distribution; for query performance patterns, see profiling-statement-fingerprints. For schema change storage planning, see analyzing-schema-change-storage-risk.
For schema change planning: Use analyzing-schema-change-storage-risk to estimate storage requirements before CREATE INDEX or ADD COLUMN operations.
ZONECONFIG system privilegeSHOW ZONE CONFIGURATION FOR RANGE default)Check your privileges:
SHOW SYSTEM GRANTS FOR <username>; -- Should show admin or ZONECONFIG
See permissions reference for RBAC setup.
Range: Contiguous key space segment (default 512MB max size, configurable via zone config range_max_bytes)
Raft group: Each range replicated across nodes (default 3 replicas)
Leaseholder: Single replica handling reads and coordinating writes for a range
Critical: Ranges split automatically at range_max_bytes (default 512MB), but can fragment further due to load-based splitting during high write traffic.
Leaseholder concentration: Single node holding disproportionate leaseholders = read hotspot Load-based splitting: CockroachDB splits ranges experiencing high QPS, increasing range count Hotspot symptoms: High CPU on single node, slow reads on specific table/index
Fragmentation: Excessive range splits creating many small ranges (overhead from Raft coordination) Causes: High write throughput, sequential inserts (timestamp-based primary keys), load-based splitting Symptoms: High range count relative to data size, increased latency from Raft overhead
Fragmentation metric: Ranges per GB. With the 512MB default range_max_bytes, a fully-grown range covers 0.5 GB — so ~2 ranges/GB is the natural floor. Anything well above that (e.g., 10+ ranges/GB) suggests load-based splits or many small ranges; tune to your workload.
Zone config: Replication and placement policies for databases, tables, or indexes Replication factor: Number of replicas per range (default: 3) Constraints: Node placement rules (region, availability zone, node attributes)
Use case: Validate intended zone config matches actual range placement.
CRITICAL SAFETY WARNING: The WITH DETAILS option computes span_stats (range size, key counts) on-demand, causing:
Best practice: Always use LIMIT with DETAILS, target specific tables/indexes, avoid cluster-wide scans.
SELECT
table_name,
index_name,
COUNT(*) AS range_count
FROM [SHOW RANGES FROM TABLE your_table_name]
GROUP BY table_name, index_name
ORDER BY range_count DESC;
Interpretation: High range count (1000s) on small tables indicates fragmentation. Cross-reference with table size.
Safety: No DETAILS option = production-safe, minimal overhead.
SELECT
range_id,
start_key,
end_key,
(span_stats->>'approximate_disk_bytes')::INT / 1048576 AS size_mb,
lease_holder,
replicas
FROM [SHOW RANGES FROM TABLE your_table_name WITH DETAILS]
ORDER BY (span_stats->>'approximate_disk_bytes')::INT DESC
LIMIT 50;
Interpretation: Ranges close to or above range_max_bytes (default 512MB) indicate split lag; many small ranges (<10MB) indicate fragmentation.
CRITICAL: Always include LIMIT and target specific tables. Never run SHOW RANGES WITH DETAILS on entire database.
SELECT
lease_holder,
COUNT(*) AS leaseholder_count,
ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) AS percentage
FROM [SHOW RANGES FROM TABLE your_table_name]
GROUP BY lease_holder
ORDER BY leaseholder_count DESC;
Interpretation: >40% leaseholders on single node in balanced cluster = hotspot. Check if table has zone constraints favoring specific nodes.
Remediation: Use ALTER TABLE ... CONFIGURE ZONE USING lease_preferences to spread leaseholders.
SELECT
range_id,
start_key,
replicas,
array_length(replicas, 1) AS replica_count,
voting_replicas,
array_length(voting_replicas, 1) AS voting_replica_count,
lease_holder
FROM [SHOW RANGES FROM TABLE your_table_name]
WHERE array_length(replicas, 1) < 3 -- Under-replicated
ORDER BY range_id
LIMIT 100;
Interpretation: replica_count < 3 = under-replicated (data loss risk). Check for node failures, decommissioning operations, or zone config mismatches.
Safety: No DETAILS = production-safe.
SHOW ZONE CONFIGURATIONS;
Output columns:
target: Database, table, or indexraw_config_sql: Zone config SQL (replication factor, constraints)Use case: Validate intended replication factor and placement constraints match expected design.
Cross-reference: Compare zone configs with Query 3 (leaseholder distribution) and Query 4 (replica health) to validate actual placement.
WITH range_counts AS (
SELECT
table_name,
index_name,
COUNT(*) AS range_count
FROM [SHOW RANGES FROM TABLE your_table_name]
GROUP BY table_name, index_name
),
table_sizes AS (
SELECT
table_name,
SUM((span_stats->>'approximate_disk_bytes')::INT) / 1073741824.0 AS size_gb
FROM [SHOW RANGES FROM TABLE your_table_name WITH DETAILS]
GROUP BY table_name
)
SELECT
rc.table_name,
rc.index_name,
rc.range_count,
ts.size_gb,
ROUND(rc.range_count / NULLIF(ts.size_gb, 0), 2) AS ranges_per_gb
FROM range_counts rc
JOIN table_sizes ts ON rc.table_name = ts.table_name
ORDER BY ranges_per_gb DESC;
Interpretation:
CRITICAL: This query uses DETAILS - only run on targeted tables with known size, never cluster-wide.
Remediation: Increase range_max_bytes via zone config (with caution), or accept fragmentation if caused by necessary load-based splitting.
See sql-queries reference for complete query variations and guardrails.
Scenario: Single node experiencing high CPU, slow reads on specific table.
Steps:
Example:
-- Check leaseholder distribution
SELECT lease_holder, COUNT(*) FROM [SHOW RANGES FROM TABLE hot_table] GROUP BY lease_holder;
-- Validate zone config
SHOW ZONE CONFIGURATION FOR TABLE hot_table;
-- Spread leaseholders if concentrated
ALTER TABLE hot_table CONFIGURE ZONE USING lease_preferences = '[[+region=us-west]]';
Scenario: After configuring multi-region setup, validate ranges are placed according to constraints.
Steps:
replicas array for node IDsSHOW REGIONS (cluster-wide) or read the locality column of cockroach node statusExample:
-- Show zone config
SHOW ZONE CONFIGURATION FOR TABLE multi_region_table;
-- Check replica placement
SELECT range_id, replicas FROM [SHOW RANGES FROM TABLE multi_region_table] LIMIT 20;
-- Map node IDs to regions (cluster-level view)
SHOW REGIONS;
-- For per-node locality strings, use the CLI:
-- cockroach node status --certs-dir=<certs-dir> --host=<any-live-node>
Scenario: Table with high range count relative to size, experiencing latency.
Steps:
range_max_bytes (with caution - larger ranges = slower splits), or investigate reducing write hotspotsCRITICAL: range_max_bytes defaults to 512MB. Raising it further without understanding the impact on split/rebalance performance is risky.
Resource impact:
Mitigation strategies:
FROM TABLE table_name, never cluster-wide SHOW RANGES WITH DETAILSAdmin role: Full cluster access, use with caution in production ZONECONFIG privilege: Limited to viewing ranges and zone configs, safer for read-only analysis
Best practice: Grant ZONECONFIG instead of admin for range analysis operators.
See permissions reference for granting minimal privileges.
Read-only operations: All queries are SELECT or SHOW statements with no writes.
Performance considerations:
| Query Type | Impact | Safe for Production? |
|---|---|---|
| Basic SHOW RANGES | Minimal CPU, metadata-only | Yes |
| SHOW RANGES WITH DETAILS (targeted, LIMIT 50) | Moderate CPU spike | Yes (low-traffic window) |
| SHOW RANGES WITH DETAILS (no LIMIT) | High CPU, timeout risk | NO - NEVER USE |
| SHOW ZONE CONFIGURATIONS | Minimal, metadata-only | Yes |
| Issue | Cause | Fix |
|---|---|---|
| Permission denied | Missing admin or ZONECONFIG privilege | Grant ZONECONFIG: GRANT SYSTEM ZONECONFIG TO user |
| Query timeout with DETAILS | Too many ranges without LIMIT | Add LIMIT 50, target specific table |
| Empty span_stats column | Missing DETAILS keyword | Add WITH DETAILS to SHOW RANGES |
| Unexpected high range count | Load-based splitting or fragmentation | Run Query 6 to calculate ranges/GB, review write patterns |
| Leaseholder = 0 or NULL | Range in transition during rebalancing | Normal during cluster changes, retry query |
| Under-replicated ranges | Node failure, decommission, zone mismatch | Check node status, validate zone config constraints |
| SHOW ZONE CONFIGURATIONS shows no custom configs | Using default cluster-wide config | Normal if no table/database-level overrides set |
range_max_bytes is 512MB (verify with SHOW ZONE CONFIGURATION FOR RANGE default)SHOW REGIONS for cluster-level locality, or cockroach node status for per-node localitySkill references:
Official CockroachDB Documentation:
Related skills:
npx claudepluginhub cockroachdb/claude-plugin --plugin cockroachdbCreates bite-sized, testable implementation plans from specs or requirements, with file structure and task decomposition. Activates before coding multi-step tasks.