pgtop connects to a PostgreSQL instance, reads the workload statistics the
server already keeps, and prints ranked findings with a ready-to-run fix.
Not a dashboard: every line is advice, every finding shows its evidence, and the
suggested SQL can be pasted into psql.
- Why
- What it reports
- Quick start
- Output formats
- Snapshots and diffing
- How the rules decide
- Architecture
- Build and tooling
- Flake outputs
- Quality gates
- Requirements
- Limitations
- Roadmap
- License
pg_stat_* is full of answers, but it reports counters, not conclusions. You
still have to know that 3.6K sequential scans over a million rows matter, that an
index with zero scans costs write throughput, and that an idempotent
CREATE INDEX CONCURRENTLY is the safe way to fix the first one.
pgtop does that reading for you:
- Actionable, not descriptive: each finding carries a severity, the numbers behind it, and SQL that fixes it.
- Ranked: critical first, then by the rule's own impact estimate.
- Read-only: only queries the statistics views;
EXPLAINruns are opt-in and rolled back. - Honest about uncertainty: when it cannot infer an index column it says so and hands you the query that will name it.
| Rule | Looks at | Severity driven by | Fix |
|---|---|---|---|
missing-index |
tables read mostly by sequential scans | rows walked × table size | CREATE INDEX CONCURRENTLY on the inferred filter column |
unused-index |
valid, non-constraint indexes with no scans | index size | DROP INDEX CONCURRENTLY |
vacuum-debt |
dead tuples above a ratio and an absolute floor | estimated dead bytes | VACUUM (ANALYZE) + autovacuum thresholds |
duplicate-index |
indexes with the same table, method, keys, predicate and uniqueness | reclaimable size | drop the redundant twin |
expensive-statement |
statements with the largest cumulative time | total execution time | EXPLAIN (ANALYZE, BUFFERS) on the real statement |
A finding looks like this:
1 CRITICAL Sequential scans dominate reads on public.orders
rule missing-index · public.orders
3.6K sequential scans against 1 index scans over 1.0M live rows. Each scan
walks the table (60.1 MiB), so roughly 362.5 GiB of reads are full-table
work an index path could avoid.
evidence
sequential scans 3.6K
sequential scan share 100.0%
inferred filter column status
fix Index public.orders on (status); it leads the filters of 1.2K calls
seen in pg_stat_statements.
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_orders_status ON public.orders (status);
· CONCURRENTLY cannot run inside a transaction block and briefly doubles index write cost.
nix develop # JDK 21, Mill, scala-cli, scalafmt, scalafix, Postgres
export DATABASE_URL=postgresql://postgres@localhost:5432/app
mill cli.run # terminal report
mill cli.run -f markdown -o report.md # Markdown export
mill cli.run -m high --fail-on high # exits 3 when something is that badWithout Nix, use a Mill 1.3 installation and JDK 21.
Settings come from flags or the usual libpq environment:
| Flag | Environment | Meaning |
|---|---|---|
-u, --url |
DATABASE_URL, PGURL |
postgresql://user:pass@host:5432/db |
--host |
PGHOST |
host, default localhost |
--port |
PGPORT |
port, default 5432 |
-d, --dbname |
PGDATABASE |
database, default the user name |
--user |
PGUSER |
user |
PGPASSWORD |
password, kept out of ps |
|
--sslmode |
PGSSLMODE |
e.g. require, verify-full |
Credentials inside the URL are accepted and moved into driver properties.
PGPASSWORD is the supported way to pass a password: a flag would be visible in
ps and in shell history.
| Code | Meaning |
|---|---|
0 |
report produced, nothing at or above --fail-on |
1 |
could not connect or read the statistics |
2 |
bad usage |
3 |
--fail-on was triggered |
| Format | Flag | Use |
|---|---|---|
| Terminal | -f terminal (default) |
ranked, coloured, one finding at a time |
| Markdown | -f markdown |
evidence tables and fenced SQL for a ticket or PR |
Colour follows the usual convention: --color forces it, --no-color disables
it, and NO_COLOR is honoured. Piped output is plain by default.
--explain additionally runs EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) on the
most expensive statements and renders a plan summary. Only read-only statements
are explained, inside a transaction that is rolled back, because `EXPLAIN
ANALYZE executes the statement.
The single-shot report answers "what is wrong now". To answer "is this regressing", save snapshots and diff them:
pgtop save prod.json -u postgresql://app@db.example.com:5432/app
# ... a day, a deploy, a traffic spike ...
pgtop save prod2.json -u postgresql://app@db.example.com:5432/app
pgtop diff prod.json prod2.json
pgtop diff prod.json prod2.json -f markdown -o regression.mddiff pairs the two files by capture time, whichever order they come in,
turns cumulative counters into deltas and per-hour rates over the window, and
reports:
- Scan traffic: tables ranked by scan growth per hour, with dead-tuple and write movement alongside.
- Indexes untouched in the window: existed before, still valid, and not one
scan or tuple read since. That is the honest
unused-index: unused since this snapshot, not since an unknown reset. - Objects created or dropped during the window.
- Statements, ranked by added execution time, with calls, rows and temp spill.
A pg_stat_reset() between the two captures is detected and flagged, because
the deltas then undercount; so are mismatched databases. The files are JSON with
a format and version envelope, readable with jq, and carry the counters
exactly as collected, so a later pgtop can re-run rules over an old snapshot:
$ jq -r '.capturedAt, (.tables | length)' prod.json
2026-11-22T09:41:02.117Z
14Through Mill, the commands are mill cli.run -- save prod.json … and
mill cli.run -- diff prod.json prod2.json.
Thresholds live in pgtop.heuristics.Thresholds and default to conservative
values: a finding that cannot be justified from the snapshot is not printed.
| Rule | Fires when | Default |
|---|---|---|
missing-index |
seq_scan ≥ N, n_live_tup ≥ M, seq_scan ≥ R × idx_scan |
1000 scans, 10K rows, 10× |
unused-index |
idx_scan ≤ N, size ≥ B, not constraint-backed, valid |
50 scans, 4 MiB |
vacuum-debt |
n_dead_tup ≥ N and dead ratio ≥ R |
1000 tuples, 10% |
duplicate-index |
two valid indexes share table, method, keys, predicate, uniqueness | exact match |
expensive-statement |
cumulative time ≥ T, top K | 60 s, top 5 |
The missing-index rule infers a candidate column by scanning pg_stat_statements
for statements touching the table and reading the shape
column <op> <parameter|literal> inside WHERE. That is deliberately narrow: a
SQL parser is out of scope, and inventing a column would be worse than asking you
to look. When no column can be inferred the fix becomes the diagnostic query that
lists the offending statements.
core/ snapshot, tables, indexes, statements, findings, severity, suggested fixes
collector/ JDBC reads of pg_stat_statements, pg_stat_user_tables, pg_stat_user_indexes, pg_stat_activity
heuristics/ pure rules, a registry, query-shape inference
explain/ optional EXPLAIN (ANALYZE, FORMAT JSON) runner, parsed with circe
store/ snapshot files: versioned JSON, circe codecs
report/ terminal and Markdown rendering
cli/ argument parsing, wiring, exit codes
itest/ integration test against a throwaway PostgreSQLmoduleDeps makes the layering a compile-time fact:
core ← collector collector never mentions findings
core ← heuristics pure, no IO, no JDBC
core ← report pure, no terminal state, no JDBC
core ← store snapshot files: JSON codecs, no JDBC
core ← collector ← explain
all of the above ← cliTwo consequences that keep the code honest: heuristics cannot accidentally query the database, and the renderers cannot accidentally decide what matters.
- Mill 1.3, not sbt. One
build.millin plain Scala, no plugin resolution and noproject/directory.moduleDepsstates the layering in one place, and the lint tasks are ordinary methods. For a single CLI with seven internal layers, sbt's plugin ecosystem and publishing conventions buy nothing. - Scala 3.7 with
-Werror,-Wunused:alland-Xsemanticdb: a warning fails the build. - scalafmt 3.11 for formatting and scalafix 0.14 with
OrganizeImportsandDisableSyntax(novar,throw,null,return). Both run frommill lint.check. - Nix flake (flake-parts) for the dev shell, the CLI launcher and the gate.
| Output | What it is |
|---|---|
devShells.default |
JDK 21, Mill, scala-cli, scalafmt, scalafix 0.14, coursier, PostgreSQL 17 |
packages.default |
pgtop launcher, builds into ~/.cache/pgtop on first use |
apps.default |
same as nix run . -- … |
apps.check |
lint.check + unit tests with the dev shell toolchain |
apps.integration |
the testcontainers suite, needs Docker |
The gate runs as an app rather than a checks derivation: Mill resolves Maven
dependencies and fetches its own JDK at build time, and a sandboxed Nix build has
neither a dependency lock nor that JDK. nix flake check validates the flake
outputs; nix run .#check runs the gate.
mill lint.check # scalafmt --test + scalafix --check
mill __.test # 88 unit tests: core, collector, heuristics, report, store, cli
mill itest.test # 6 integration tests against postgres:17-alpine
nix run .#check # the same gate through the flake| Suite | Tests | Covers |
|---|---|---|
core.test |
18 | severity ordering, ranking, formatting, signatures, snapshot diffing |
collector.test |
8 | connection URLs, credentials, libpq environment |
heuristics.test |
33 | every rule against fixture snapshots, registry, query shapes |
report.test |
15 | terminal and Markdown rendering, limits, severity, diff view |
store.test |
8 | JSON round-trip, envelope checks, refusal paths, file IO |
cli.test |
6 | argument parsing for run, save and diff |
itest.test |
6 | collection, findings, rendering, EXPLAIN, snapshot files, diff CLI |
GitHub Actions runs both gates on every push and pull request: ci.yml runs the
six unit suites and the testcontainers integration test, lint.yml runs
mill lint.check. The badges at the top of this file report their status.
- PostgreSQL 12 or newer (
indnkeyattsinpg_index). pg_stat_statementsfor the statement-level rules. Without it,pgtopstill reports table and index findings and says so in a note.- A role that can read
pg_stat_activityand the statistics views;pg_monitoris enough on a managed instance.
- The missing-index rule infers a single leading filter column and only from
column <op> valueshapes. Anything more elaborate becomes a diagnostic query. - Counters are cumulative since the last
pg_stat_reset(); the report prints the reset time so the numbers can be judged. - Duplicate detection uses exact signature equality, not prefix coverage:
(a)and(a, b)are not reported as duplicates yet. EXPLAINneeds literal statements; parameterised queries are skipped and noted.pgtop diffpairs statements byqueryid: the same statement text run by different users merges into one entry, and a table dropped and recreated under its old name restarts its counters.
- Rule inputs switch from a snapshot to a diff, so
unused-indexbecomes "unused for 14 days" andmissing-indexbecomes "seq scans growing since the last release". - Prefix-covered duplicate detection.
- Fold
--explainoutput into the statement findings instead of a separate section. - SARIF export so findings annotate a pull request.
GPL-3.0. See LICENSE.