pgbot
In-database observability for PostgreSQL.
One static binary connects read-only, reads Postgres's own statistics views,
and prints a findings-first health report — plus what changed since last time.
No agent, no external service, no write privilege anywhere in the path.
Quickstart ·
Install ·
Setup ·
Commands ·
CI ·
MCP ·
JSON contract ·
Troubleshooting ·
Provider notes
Status: beta. The --json contract is versioned (currently 1.6.0, JSON
Schema published in schema/) and breaking changes to it are
treated as breaking changes to the tool. The human-readable report is not
a stable interface — parse --json, not the terminal output.
Quickstart

curl -fsSL https://pgbot.dev/install | sh
pgbot inspect "postgres://pgbot_ro@host:5432/db"
Or set the connection once in the environment and drop the argument — handy for
CI and shells, and it keeps the password out of your history and ps:
export DATABASE_URL="postgres://pgbot_ro@host:5432/db"
pgbot inspect
pgbot reads the argument first, then $DATABASE_URL, then $PGBOT_DATABASE_URL,
then $PGSERVICE (if you keep your connections in a
connection service file,
just export PGSERVICE=mydb and drop the argument too).
(Shell note: export DATABASE_URL="…" — no $ on the left, no spaces around =.)
Everything pgbot takes from the environment fits in one block — the connection,
and (only if you want the optional ask/explain AI layer) one model key:
export DATABASE_URL="postgres://pgbot_ro:…@host:5432/db?sslmode=require"
# optional, for `pgbot ask` / `pgbot explain` — one of:
export OPENAI_API_KEY=sk-… # → OpenAI (gpt-5.6-terra by default)
export GEMINI_API_KEY=… # → Google Gemini (AI Studio key)
export ANTHROPIC_API_KEY=… # → Anthropic (claude-opus-5 by default)
export XAI_API_KEY=… # → xAI (grok-4.6 by default)
# …or any OpenAI-compatible endpoint, local ones included — see "explain — optional AI layer"
Everything else — inspect, queries, indexes, MCP, CI — is fully
deterministic and needs no key; nothing leaves your machine. Provider
pinning and model/endpoint overrides: the AI layer.
connected · db.example.com · postgres 17.4 · read-only · 6h20m window
cache hit [████████████████████] 99.4% ok
lock wait [████████████░░░░░░░░] 61.0% query 4f2a
rollbacks [██░░░░░░░░░░░░░░░░░░] 12.0% watch
idle idx [██████░░░░░░░░░░░░░░] 18.0 GiB review
checked · queries · vacuum · replication · checkpoints
· connections · settings · deadlocks
Database health: 82/100
CRITICAL
● transaction-id age 1.8B — 84% toward wraparound
WARNING
● orders queries 3.2× slower (8 → 26 ms mean)
● 3 unused indexes consume 18 GB
● connection usage reached 87%
Details: pgbot inspect --full · Machine-readable: --json
Ask it: pgbot ask "why is it slow?"
The default report is a graded read: a four-row gauge strip of vital signs
(cache hit, lock wait with the culprit query, rollbacks, idle index bytes as a
share of the database), a checked line naming the subsystems that came back
clean (a tool that names what it verified reads like a colleague who looked,
not an alarm), a health score, then findings bucketed CRITICAL / WARNING /
NOTE. pgbot inspect --full adds a subsystem status board plus
the section tables and per-finding caveats; focused commands (indexes,
queries, tables, vacuum) each drill into one signal; pgbot ask "…" and
pgbot explain put a plain-language AI reading on top of the same findings.
--json is the complete, versioned contract for agents and scripts.
$ pgbot ask "what's wrong?"
Your database is mostly healthy.
1 critical issue:
orders queries became 3.2× slower in the last 6 hours.
Likely cause:
sequential scans increased after the orders table grew 18%.
Recommended:
review an index on customer_id + created_at.
Why pgbot
|
|
| Read-only by role, not by flag |
The guarantee is a pg_monitor login role with no write grants. Session pinning (default_transaction_read_only, statement_timeout=15s, lock_timeout=2s) and BEGIN READ ONLY are defence in depth on top of it. |
| It remembers |
Every run writes a local baseline, so from the third run on it tells you what changed and why it matters — a query that got slower, a table that started sequential-scanning, an index that stopped being used. |
| Findings are deterministic |
Every finding is computed in Go from SQL. The optional AI layer explains findings; it never generates them. |
| Nothing to deploy |
One static binary. No collector, no time-series database, no service to run. |
| Built for agents |
--json is a versioned, PII-free contract; pgbot mcp exposes the same findings over the Model Context Protocol, with a skill and a Claude Code plugin on top. |
How it compares to pganalyze, PMM, pgwatch
pgbot is a point-in-time diagnostic you run, not a monitoring platform you
operate. If you want dashboards, alerting, long retention, and multi-host
rollups, run pganalyze / Percona PMM / pgwatch — pgbot doesn't replace them.
Reach for pgbot when you want an answer in ten seconds without deploying
anything, when you're triaging a database you don't own, or when an AI agent
needs structured Postgres findings it can reason over.
Requirements
- PostgreSQL 14–18 (16–18 fully supported, 14–15 best-effort); collectors
degrade rather than fail on older feature sets — see
Version support.
- A login role holding
pg_monitor — see
Setup.
pg_stat_statements for the queries section (optional; pgbot prints the
provider-specific install steps when it's missing).
advise additionally needs the hypopg
extension and PostgreSQL 16+.
- Linux, macOS, Windows — amd64 and arm64.
See it
pgbot inspect --full — a subsystem status board (one row per subsystem,
colored ok / warn / fail), followed by the detailed section tables.

pgbot indexes — zero-scan indexes with sizes, and the caveat that matters:
on a primary those scan counts are per-node, so a replica may still be using an
index that looks unused here. It tells you what not to drop.

pgbot queries — the top statements from pg_stat_statements, ranked by
total execution time (the query quietly eating your database) with a share
column for each query's slice of total time. Add --by-calls to rank by call
count instead — a cheap query run a million times can outweigh an expensive one
run twice. Transaction-control and session-SET noise is filtered out.
An enabled extension whose statistics could not be read is reported as unavailable,
not as an empty workload. The MCP top_queries response preserves enabled and
adds available to distinguish those states; a successful empty workload has
enabled: true, available: true, and an empty queries array.
$ pgbot queries "$DATABASE_URL"
total share calls mean query
4h11m 61.0% 812.4k 18.55 ms SELECT * FROM orders WHERE user_id = $1 AND …
22m3s 17.8% 1.3k 1.02 s SELECT count(*) FROM events WHERE created_at …
15m2s 12.0% 99.8k 9.04 ms INSERT INTO audit_log (actor, action, …) VAL …
pgbot vacuum — autovacuum health per table: dead tuples, dead-tuple ratio,
when autovacuum last ran, and a computed due? — whether the table's dead tuples
have passed Postgres' default autovacuum trigger (50 + 20% of live rows). Rising
dead tuples with due? yes and no recent run is autovacuum falling behind, the
early signal for bloat and, eventually, wraparound risk.
$ pgbot vacuum "$DATABASE_URL"
table live dead dead% last autovacuum due?
public.demo_events 42.9k 33.8k 44.1% 4m ago yes
public.churny 5.0k 10.0k 66.7% never yes
pgbot tables — the largest tables by total size (heap + indexes + TOAST),
each with row count, dead-tuple ratio, and sequential-vs-index scan counts. It's
storage accounting and a missing-index radar: a large table with heavy seq scans and few idx scans is a likely index candidate.
$ pgbot tables "$DATABASE_URL"
size rows dead% seq scans idx scans table
38.7 GiB 19.7M 8.3% 1.5k 112.3M public.performance_events
20.0 GiB 1.3M 10.8% 2.5M 121.6M public.events ← 2.5M seq scans
7.1 GiB 5.6M 0.0% 5.0k 46.6M public.log_entries
pgbot ask "why is it slow?" — a plain-language reading of the same
deterministic findings. It leads with the lock contention and refuses to
recommend dropping the indexes because replication is active — the caveat is
carried into the advice, not lost.

Commands and flags
Every command takes the connection the same way — an argument, $DATABASE_URL,
$PGBOT_DATABASE_URL, or $PGSERVICE.
| Command |
What it does |
inspect |
the full findings-first health report (--full for the section tables) |
lint |
schema-only check, safe on an empty CI database (inspect --profile=schema --no-store) |
init |
generate the read-only role setup SQL — nothing is executed (--verify checks an existing role) |
diff |
compare two baseline snapshots offline |
why |
explain a regression from baseline history: symptom ← mechanism ← antecedent, with numbers and onset times (offline; --duration 10s adds a live wait diagnosis) |
indexes · queries · tables · vacuum |
drill into one signal |
logs |
the server log over SQL — newest entries or --live follow (experimental) |
waits |
sample where database time goes — wait classes, blockers, contention (experimental) |
schema snapshot · schema diff · schema compare |
catalog-only schema snapshots; diff reviews what a migration did on the same database (table rewrites, blocking index builds, validating scans, drops, renames), compare lists structural differences between two servers (for example PGX and PostgreSQL); diff/compare are offline |
erd |
the schema as an ER diagram, drawn in the terminal (--mermaid for GitHub) |
activity |
live sessions right now — PIDs, ages, waits, and what each is running |
report |
the full inspection as one self-contained HTML page (pgbot report > report.html) |
advise |
planner-validated missing-index suggestions (needs hypopg) |
sql explain "<SELECT …>" |
one query's plan, read in plain English — plan-only EXPLAIN, never executed (--ai for a prose reading) |
ask "…" · explain |
a plain-language AI reading of the same deterministic findings |
explain-finding <id> |
the catalogue page for a finding, offline |
mcp |
serve the findings to an AI agent over MCP |
monitor |
scheduled checks with native notifications on your Mac (macOS only) — see PgBot Monitor |
config · baselines |
manage .pgbot.toml and the local baseline store |
Key inspect flags:
| Flag |
|
--json · --format=text|json|sarif|junit|prometheus |
output format; SARIF uploads to the GitHub Security tab |
--fail-on=critical|warn|info|none |
the severity that makes the exit code non-zero (the CI gate) |