Skip to main content
Glama

Top queries

pg_top_queries
Read-onlyIdempotent

Rank PostgreSQL queries from pg_stat_statements by calls, total or mean time, rows, blocks, temp files, or WAL to identify slow and expensive statements for tuning.

Instructions

Rank the queries of the current database from pg_stat_statements: calls, total and mean time, rows, share of the total time, blocks read and hit, temporary file volume, WAL generated and planning time (WAL and planning on PostgreSQL 13+).

Use it to find what is slow or expensive. order_by is one of total_time, mean_time, calls, rows, shared_blks_read, temp_blks_written or wal_bytes. min_calls ignores statements that ran fewer times. Needs the pg_stat_statements extension. The query text is normalized (constants replaced by $n) and cut; pass it to pg_explain to see the plan.

Tool call template

This tool declares no required parameters. Add optional parameters from the input schema as needed.

{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "arguments": {},
    "name": "pg_top_queries"
  }
}

Input Schema

NameRequiredDescriptionDefault
limitNo
order_byNototal_time
min_callsNo
connectionNo

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv0.1.0

TDQS

A4.3/5.0
Behavior5/5

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

Beyond the annotations (readOnly, idempotent, non-destructive, closed-world), the description discloses three non-obvious traits: the pg_stat_statements extension prerequisite, version-dependent columns (WAL/planning on PG13+), and that query text is normalized with constants replaced by $n and truncated. The truncation caveat in particular prevents an agent from expecting exact-source SQL.

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

Conciseness4/5

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

Front-loaded with purpose, then constraints, then the pg_explain handoff — a sensible order with no filler sentences. The opening clause is a long comma-chained column list, slightly heavy, but each item conveys what the output contains.

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?

With no output schema, the description carries the burden of describing the result columns and does so thoroughly, including caveats about truncation and version support. The only real gap is the undocumented connection parameter, which matters because it can silently change which database is profiled.

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?

Schema coverage is 0%, so the description must carry parameter meaning. It explains min_calls ('ignores statements that ran fewer times') and restates the order_by enum, but says nothing about limit (pagination/row cap) and nothing about connection, which is the least self-explanatory parameter — presumably selecting a different database connection/target.

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 states a specific verb and resource ('Rank the queries of the current database from pg_stat_statements') and enumerates the exact metrics returned: calls, total/mean time, rows, time share, blocks read/hit, temp files, WAL, planning time. An agent can tell this apart from siblings like pg_activity or pg_database_stats without opening a schema.

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?

It gives explicit intent guidance ('Use it to find what is slow or expensive') and a concrete handoff: pass the normalized query text to pg_explain to see the plan. It stops short of stating when another sibling (e.g. pg_activity or pg_locks) is the better choice, so there is no explicit exclusion set.

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