DEDUPED Hot AI repos. Real use cases. What people build. Get the Monday 5

How to find the queries that eat the most database time with Pgbot

Rank statements by total time or call count to pick the first query to tune.

Short answer: Confirm the pg_stat_statements extension is installed on the target, since Pgbot prints install steps if it is missing; then set the connection with export DATABASE_URL="postgres://pgbot_ro@host:5432/db"; run pgbot queries to rank statements by total execution time. All steps.

Who it is forA developer or DBA who knows the app is slow and needs to see which SQL statements take the most database time.

The problemA cheap query run a million times can cost more than an expensive query run twice, and the two are hard to compare by hand.

The outcomeA ranked list of statements with total time, share of total time, call count, and mean time, ready to hand to a tuning pass.

What you need first

  • A PostgreSQL server version 14 to 18 that you can reach over the network
  • A login role with the pg_monitor role and no write grants (pgbot init generates the setup SQL)
  • The pgbot binary, installed with the curl install script or npx @pgbot/cli
  • Optional: pg_stat_statements for the queries section, and hypopg on PostgreSQL 16+ for advise

Steps

  1. Confirm the pg_stat_statements extension is installed on the target, since Pgbot prints install steps if it is missing.
  2. Set the connection with export DATABASE_URL="postgres://pgbot_ro@host:5432/db".
  3. Run pgbot queries to rank statements by total execution time.
  4. Run pgbot queries --by-calls to rank by call count and compare the two lists. (inferred, check the README)
  5. Run pgbot ask "why is it slow?" for a plain-language reading of the same findings, if you have a model key set.

Prompt for your agent

Use the pgbot tool from the pgrundev/pgbot repository to run `pgbot queries --json` against <target-db-url>. Return the top five statements by total time with their share and call count. Verify that the response has enabled true and available true before you report an empty list as a real empty workload.

Caveat. Query statistics only exist when pg_stat_statements is enabled, and Pgbot reports an extension it cannot read as unavailable rather than as an empty workload.

Use this in your AI agent

Ask your agent

Using the Deduped MCP, get the setup steps for pgrundev/pgbot and "Find the queries that eat the most database time"

Add Deduped to Claude Code once

claude mcp add --transport http deduped https://dedupedai.com/mcp

Cursor, ChatGPT, others

Share: X LinkedIn Hacker News Email Copy link

Third-party repos we don't own. AI-written use cases can be wrong. Disclaimer