Skip to main content
Glama
isdaniel

PostgreSQL-Performance-Tuner-Mcp

analyze_wait_events

Read-onlyIdempotent

Identify PostgreSQL performance bottlenecks by analyzing wait events for client backend processes, helping detect I/O issues, lock contention, and resource saturation in application queries.

Instructions

Analyze PostgreSQL wait events to identify bottlenecks.

Note: This tool focuses on client backend processes and excludes system background processes to help identify bottlenecks in your application queries.

Wait events indicate what processes are waiting for:

  • Lock: Waiting for locks on tables/rows

  • IO: Waiting for disk I/O

  • CPU: Waiting for CPU time

  • Client: Waiting for client communication

  • Extension: Waiting in extension code

This helps identify:

  • I/O bottlenecks

  • Lock contention patterns

  • Resource saturation

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
active_onlyNoOnly include active (running) queries

Implementation Reference

  • The run_tool method in WaitEventsToolHandler class that implements the core logic of the 'analyze_wait_events' tool. It queries pg_stat_activity for current wait events on client backends, groups them by type and event, analyzes for issues like lock contention or I/O waits, and returns structured JSON output with recommendations.
    async def run_tool(self, arguments: dict[str, Any]) -> Sequence[TextContent]:
        try:
            active_only = arguments.get("active_only", True)
    
            # Get user filter for excluding specific user IDs
            user_filter = get_user_filter()
            activity_filter = user_filter.get_activity_filter()
    
            state_filter = "AND state = 'active'" if active_only else ""
    
            # Current wait events
            query = f"""
                SELECT
                    wait_event_type,
                    wait_event,
                    COUNT(*) as count,
                    array_agg(DISTINCT pid) as pids
                FROM pg_stat_activity
                WHERE wait_event IS NOT NULL
                  AND backend_type = 'client backend'
                  {state_filter}
                  {activity_filter}
                GROUP BY wait_event_type, wait_event
                ORDER BY count DESC
            """
    
            result = await self.sql_driver.execute_query(query)
    
            # Detailed breakdown by wait type
            type_query = f"""
                SELECT
                    wait_event_type,
                    COUNT(*) as count
                FROM pg_stat_activity
                WHERE wait_event_type IS NOT NULL
                  AND backend_type = 'client backend'
                  {state_filter}
                  {activity_filter}
                GROUP BY wait_event_type
                ORDER BY count DESC
            """
    
            type_result = await self.sql_driver.execute_query(type_query)
    
            analysis = {
                "issues": [],
                "recommendations": []
            }
    
            # Analyze wait types
            if type_result:
                for row in type_result:
                    wait_type = row.get("wait_event_type")
                    count = row.get("count", 0)
    
                    if wait_type == "Lock" and count > 5:
                        analysis["issues"].append(f"{count} processes waiting on locks")
                        analysis["recommendations"].append(
                            "Investigate lock contention using pg_locks and pg_blocking_pids()"
                        )
                    elif wait_type == "IO" and count > 10:
                        analysis["issues"].append(f"{count} processes waiting on I/O")
                        analysis["recommendations"].append(
                            "Consider tuning I/O settings or increasing shared_buffers"
                        )
                    elif wait_type == "BufferPin" and count > 0:
                        analysis["issues"].append(f"{count} processes waiting on buffer pins")
                        analysis["recommendations"].append(
                            "This may indicate contention on frequently accessed pages"
                        )
    
            output = {
                "wait_events": result,
                "by_type": type_result,
                "analysis": analysis
            }
    
            return self.format_json_result(output)
    
        except Exception as e:
            return self.format_error(e)
  • The get_tool_definition method defining the tool schema, including name 'analyze_wait_events', description, input schema with 'active_only' parameter, and annotations.
    def get_tool_definition(self) -> Tool:
        return Tool(
            name=self.name,
            description=self.description,
            inputSchema={
                "type": "object",
                "properties": {
                    "active_only": {
                        "type": "boolean",
                        "description": "Only include active (running) queries",
                        "default": True
                    }
                },
                "required": []
            },
            annotations=self.get_annotations()
        )
  • The complete WaitEventsToolHandler class that defines and implements the 'analyze_wait_events' tool, including name, description, schema, and execution logic.
    class WaitEventsToolHandler(ToolHandler):
        """Tool handler for analyzing wait events."""
    
        name = "analyze_wait_events"
        title = "Wait Events Analyzer"
        read_only_hint = True
        destructive_hint = False
        idempotent_hint = True
        open_world_hint = False
        description = """Analyze PostgreSQL wait events to identify bottlenecks.
    
    Note: This tool focuses on client backend processes and excludes system
    background processes to help identify bottlenecks in your application queries.
    
    Wait events indicate what processes are waiting for:
    - Lock: Waiting for locks on tables/rows
    - IO: Waiting for disk I/O
    - CPU: Waiting for CPU time
    - Client: Waiting for client communication
    - Extension: Waiting in extension code
    
    This helps identify:
    - I/O bottlenecks
    - Lock contention patterns
    - Resource saturation"""
    
        def __init__(self, sql_driver: SqlDriver):
            self.sql_driver = sql_driver
    
        def get_tool_definition(self) -> Tool:
            return Tool(
                name=self.name,
                description=self.description,
                inputSchema={
                    "type": "object",
                    "properties": {
                        "active_only": {
                            "type": "boolean",
                            "description": "Only include active (running) queries",
                            "default": True
                        }
                    },
                    "required": []
                },
                annotations=self.get_annotations()
            )
    
        async def run_tool(self, arguments: dict[str, Any]) -> Sequence[TextContent]:
            try:
                active_only = arguments.get("active_only", True)
    
                # Get user filter for excluding specific user IDs
                user_filter = get_user_filter()
                activity_filter = user_filter.get_activity_filter()
    
                state_filter = "AND state = 'active'" if active_only else ""
    
                # Current wait events
                query = f"""
                    SELECT
                        wait_event_type,
                        wait_event,
                        COUNT(*) as count,
                        array_agg(DISTINCT pid) as pids
                    FROM pg_stat_activity
                    WHERE wait_event IS NOT NULL
                      AND backend_type = 'client backend'
                      {state_filter}
                      {activity_filter}
                    GROUP BY wait_event_type, wait_event
                    ORDER BY count DESC
                """
    
                result = await self.sql_driver.execute_query(query)
    
                # Detailed breakdown by wait type
                type_query = f"""
                    SELECT
                        wait_event_type,
                        COUNT(*) as count
                    FROM pg_stat_activity
                    WHERE wait_event_type IS NOT NULL
                      AND backend_type = 'client backend'
                      {state_filter}
                      {activity_filter}
                    GROUP BY wait_event_type
                    ORDER BY count DESC
                """
    
                type_result = await self.sql_driver.execute_query(type_query)
    
                analysis = {
                    "issues": [],
                    "recommendations": []
                }
    
                # Analyze wait types
                if type_result:
                    for row in type_result:
                        wait_type = row.get("wait_event_type")
                        count = row.get("count", 0)
    
                        if wait_type == "Lock" and count > 5:
                            analysis["issues"].append(f"{count} processes waiting on locks")
                            analysis["recommendations"].append(
                                "Investigate lock contention using pg_locks and pg_blocking_pids()"
                            )
                        elif wait_type == "IO" and count > 10:
                            analysis["issues"].append(f"{count} processes waiting on I/O")
                            analysis["recommendations"].append(
                                "Consider tuning I/O settings or increasing shared_buffers"
                            )
                        elif wait_type == "BufferPin" and count > 0:
                            analysis["issues"].append(f"{count} processes waiting on buffer pins")
                            analysis["recommendations"].append(
                                "This may indicate contention on frequently accessed pages"
                            )
    
                output = {
                    "wait_events": result,
                    "by_type": type_result,
                    "analysis": analysis
                }
    
                return self.format_json_result(output)
    
            except Exception as e:
                return self.format_error(e)
  • The register_all_tools function where WaitEventsToolHandler is instantiated with the SQL driver and registered via add_tool_handler at line 161. This central registry adds the tool to the MCP server's tool_handlers dict.
    def register_all_tools() -> None:
        """
        Register all available tool handlers.
    
        This function serves as the central registry for all tools.
        New tool handlers should be added here for automatic registration.
        """
        driver = get_sql_driver()
        hypopg_service = HypoPGService(driver)
        index_advisor = IndexAdvisor(driver)
    
        # Performance analysis tools
        add_tool_handler(GetSlowQueriesToolHandler(driver))
        add_tool_handler(AnalyzeQueryToolHandler(driver))
        add_tool_handler(TableStatsToolHandler(driver))
    
        # Index tuning tools
        add_tool_handler(IndexAdvisorToolHandler(index_advisor))
        add_tool_handler(ExplainQueryToolHandler(driver, hypopg_service))
        add_tool_handler(HypoPGToolHandler(hypopg_service))
        add_tool_handler(UnusedIndexesToolHandler(driver))
    
        # Database health tools
        add_tool_handler(DatabaseHealthToolHandler(driver))
        add_tool_handler(ActiveQueriesToolHandler(driver))
        add_tool_handler(WaitEventsToolHandler(driver))
        add_tool_handler(DatabaseSettingsToolHandler(driver))
    
        # Bloat detection tools (using pgstattuple extension)
        add_tool_handler(TableBloatToolHandler(driver))
        add_tool_handler(IndexBloatToolHandler(driver))
        add_tool_handler(DatabaseBloatSummaryToolHandler(driver))
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

The description adds valuable behavioral context beyond what annotations provide. While annotations indicate read-only, non-destructive, and idempotent operations, the description explains what wait events represent (Lock, IO, CPU, Client, Extension) and what insights can be gained (I/O bottlenecks, lock contention patterns, resource saturation). This helps the agent understand the tool's analytical focus and output interpretation.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured and appropriately sized. It starts with the core purpose, adds an important note about scope, then provides explanatory context about wait events and insights. Every sentence adds value without redundancy. The bulleted lists efficiently convey information without unnecessary verbosity.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's analytical nature, the absence of an output schema, and comprehensive annotations, the description provides good contextual completeness. It explains what wait events are, what they indicate, and what insights can be derived. However, it doesn't describe the format or structure of the analysis results, which would be helpful since there's no output schema provided.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 100% schema description coverage for the single parameter 'active_only', the schema already fully documents this parameter. The description doesn't add any additional parameter information beyond what's in the schema. According to scoring rules, when schema coverage is high (>80%), the baseline is 3 even with no parameter info in the description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Analyze PostgreSQL wait events to identify bottlenecks.' It specifies the resource (PostgreSQL wait events), the verb (analyze), and distinguishes it from siblings by focusing on client backend processes while excluding system background processes. This differentiation from tools like 'get_active_queries' or 'get_slow_queries' is explicit.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear context for when to use this tool: 'to help identify bottlenecks in your application queries.' It explicitly excludes system background processes, which helps differentiate it from general monitoring tools. However, it doesn't name specific alternatives among the sibling tools or provide explicit 'when-not-to-use' guidance beyond the exclusion mentioned.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/isdaniel/pgtuner-mcp'

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