get_hg_execution_plan
Analyze and retrieve the execution plan with runtime statistics for a SQL query in Hologres databases to optimize performance and debug queries effectively.
Instructions
Get actual execution plan with runtime statistics for a SQL query in Hologres database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The SQL query to analyze in Hologres database |
Implementation Reference
- Specific dispatch handler for the 'get_hg_execution_plan' tool within the @app.call_tool() function. It extracts the input query, validates its presence, and modifies it by prefixing 'EXPLAIN ANALYZE' to retrieve the execution plan with runtime statistics.elif name == "get_hg_execution_plan": query = arguments.get("query") if not query: raise ValueError("Query is required") query = f"EXPLAIN ANALYZE {query}"
- src/hologres_mcp_server/server.py:436-449 (registration)Registration of the 'get_hg_execution_plan' tool in the @app.list_tools() function, including its name, description, and input schema definition.Tool( name="get_hg_execution_plan", description="Get actual execution plan with runtime statistics for a SQL query in Hologres database", inputSchema={ "type": "object", "properties": { "query": { "type": "string", "description": "The SQL query to analyze in Hologres database" } }, "required": ["query"] } ),
- Input schema definition for the 'get_hg_execution_plan' tool, specifying a required 'query' string parameter.inputSchema={ "type": "object", "properties": { "query": { "type": "string", "description": "The SQL query to analyze in Hologres database" } }, "required": ["query"] }
- Shared helper function 'handle_call_tool' that executes the SQL query (prefixed for this tool) on the Hologres database using psycopg, handles serverless mode if applicable, formats SELECT results as CSV-like text, and returns success/error messages.def handle_call_tool(tool_name, query, serverless = False): """Handle callTool method.""" config = get_db_config() try: with connect_with_retry() as conn: with conn.cursor() as cursor: # 特殊处理 serverless computing 查询 if serverless: cursor.execute("set hg_computing_resource='serverless'") # Execute the query cursor.execute(query) # 特殊处理 ANALYZE 命令 if tool_name == "gather_hg_table_statistics": return f"Successfully {query}" # 处理其他有返回结果的查询 if cursor.description: # SELECT query columns = [desc[0] for desc in cursor.description] rows = cursor.fetchall() result = [",".join(map(str, row)) for row in rows] return "\n".join([",".join(columns)] + result) elif tool_name == "execute_dml_sql": # Non-SELECT query row_count = cursor.rowcount return f"Query executed successfully. {row_count} rows affected." else: return "Query executed successfully" except Exception as e: return f"Error executing query: {str(e)}"