Skip to main content
Glama
bpamiri

CockroachDB MCP Server

by bpamiri

get_table_stats

Retrieve table statistics including row count and size from CockroachDB to analyze data volume and optimize database performance.

Instructions

Get statistics for a table.

Args:
    table: Table name (schema.table or just table).

Returns:
    Table statistics including row count and size.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tableYes

Implementation Reference

  • Core handler function that executes the tool logic: parses table name, checks schema permissions, queries row count and table size using CockroachDB pg_total_relation_size.
    async def get_table_stats(table: str) -> dict[str, Any]:
        """Get statistics for a table.
    
        Args:
            table: Table name (schema.table or just table).
    
        Returns:
            Table statistics.
        """
        conn = await connection_manager.ensure_connected()
    
        # Parse schema and table name
        if "." in table:
            schema, table_name = table.rsplit(".", 1)
        else:
            schema = "public"
            table_name = table
    
        # Check if schema is allowed
        if not _is_allowed_schema(schema):
            return {"status": "error", "error": f"Schema '{schema}' is not allowed"}
    
        try:
            # Get table size and row count
            async with conn.cursor() as cur:
                # Row count
                await cur.execute(f"SELECT COUNT(*) as count FROM {schema}.{table_name}")
                count_row = await cur.fetchone()
                row_count = count_row["count"] if count_row else 0
    
                # Table size using CockroachDB specific query
                await cur.execute(
                    """
                    SELECT
                        pg_size_pretty(pg_total_relation_size(%s::regclass)) as total_size,
                        pg_total_relation_size(%s::regclass) as total_bytes
                """,
                    (f"{schema}.{table_name}", f"{schema}.{table_name}"),
                )
                size_row = await cur.fetchone()
    
            return {
                "schema": schema,
                "table": table_name,
                "full_name": f"{schema}.{table_name}",
                "row_count": row_count,
                "total_size": size_row.get("total_size") if size_row else None,
                "total_bytes": size_row.get("total_bytes") if size_row else None,
            }
        except Exception as e:
            return {"status": "error", "error": str(e)}
  • MCP tool registration via @mcp.tool() decorator. Thin wrapper that delegates to the core implementation in tables.get_table_stats and handles exceptions.
    @mcp.tool()
    async def get_table_stats(table: str) -> dict[str, Any]:
        """Get statistics for a table.
    
        Args:
            table: Table name (schema.table or just table).
    
        Returns:
            Table statistics including row count and size.
        """
        try:
            return await tables.get_table_stats(table)
        except Exception as e:
            return {"status": "error", "error": str(e)}

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/bpamiri/cockroachdb-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server