Skip to content

Add pscale insights and pscale inspect commands for database diagnostics#1308

Draft
nickvanw wants to merge 3 commits into
mainfrom
nick/inspect-insights
Draft

Add pscale insights and pscale inspect commands for database diagnostics#1308
nickvanw wants to merge 3 commits into
mainfrom
nick/inspect-insights

Conversation

@nickvanw

@nickvanw nickvanw commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds two complementary read-only diagnostic surfaces to the CLI, for both MySQL/Vitess and PostgreSQL databases:

  • pscale insights — surfaces PlanetScale's server-side analysis of production traffic, which until now was only reachable through the MCP server or raw pscale api calls:
    • insights queries <db> <branch> — top queries ranked by any insights metric (--sort totalTime|count|p99Latency|rowsRead|rowsReadPerReturned|…, plus --period, --dir, --limit)
    • insights errors <db> <branch> — failing query patterns with error messages
    • insights anomalies <db> <branch> — detected resource anomalies
    • insights recommendations <db> — schema recommendations with ready-to-apply DDL
  • pscale inspect — live, point-in-time checks run over a direct connection using the same ephemeral-credential model as pscale sql (always read-only, always bounded): table-sizes, index-sizes, unused-indexes, redundant-indexes, seq-scans, long-running-queries, locks, outliers, calls, bloat, vacuum-stats, replication-slots, subscriptions — and inspect all, which runs every applicable check over a single connection and prints a combined report.

All commands support --format json (and csv for single checks) for scripting and agents.

Why

Users (and increasingly, AI agents) diagnosing a slow or unhealthy database today have to bounce between the web app, the MCP server, and hand-written SQL through pscale shell. The platform already computes rich, traffic-aware analysis — query stats, anomalies, schema recommendations — but none of it was exposed as first-class CLI commands, and there was no safe, canned way to ask the database itself the standard DBA questions (what's big, what's unused, what's locked, what's bloated).

This gives both audiences a single obvious front door. The two surfaces deliberately cross-reference each other (help text, human output, and next_steps in JSON) so an agent that lands on either one discovers the other: insights for historical, traffic-aware analysis; inspect for live state and physical layout. When a check isn't available for an engine — or needs a PostgreSQL extension that isn't installed (e.g. outliers needs pg_stat_statements) — the output points at the insights command that answers the same question server-side with no setup.

Changes

  • internal/planetscale/insights.go — new QueryInsights service (ListQueries/ListErrors/ListAnomalies) plus WithSort/WithPeriod list options; the already-present SchemaRecommendations service is now actually used by a command
  • internal/cmd/insights/ — the four insights subcommands
  • internal/cmd/inspect/ — check catalog (per-engine SQL with invariants enforced by tests: single statement, read-only, LIMIT-bounded, system/internal schemas filtered) and the command tree
  • internal/sqlquery/session.go — reusable Session (one connection, many queries) extracted from Execute; connection setup for both engines refactored into openMySQL/openPostgres with no behavior change to pscale sql
  • internal/printer — expose the resource writer so commands with dynamic column sets can render CSV
  • AGENTS.md — documents both surfaces and when to use which

Notes for reviewers:

  • On sharded Vitess databases, inspect statistics reflect one shard's MySQL instance per run. --keyspace accepts vtgate shard targets (mykeyspace/-80, mykeyspace/-80@replica) to pin an exact shard — shard-targeted connections bypass the planner and read that shard's statistics directly. Enumerate shards with SHOW VITESS_SHARDS. Databases can have hundreds of shards, so nothing fans out automatically.
  • On PostgreSQL, stats are scoped to one database; --dbname selects it, and the reader role can lack CONNECT on non-default databases — the error suggests --role admin.
  • Verified live against a sharded Vitess database and PostgreSQL databases (all three output formats, extension-missing and engine-mismatch paths).
    Also: every command's --help now ends with a footer pointing agents at pscale agent-guide --format json, and the embedded guide is readable via pscale help agents.

nickvanw added 2 commits July 22, 2026 18:22
pscale insights surfaces PlanetScale's server-side analysis of production
traffic: top queries by latency/rows-read/time (queries), failing queries
(errors), resource anomalies (anomalies), and schema recommendations with
ready-to-apply DDL (recommendations). The insights API endpoints were
previously only reachable via the MCP server or raw pscale api calls; this
adds a typed QueryInsights service to the internal SDK and wires the
already-present-but-unused SchemaRecommendations service to a command.

pscale inspect runs read-only, bounded diagnostic checks over a direct
connection using the same ephemeral-credential model as pscale sql:
table/index sizes, unused and redundant indexes, full-table scans,
long-running queries, locks, bloat/fragmentation, vacuum stats, and
replication state. Checks adapt per engine (information_schema/sys for
MySQL/Vitess, pg_catalog/pg_stat for PostgreSQL); checks that don't apply
to an engine point at the equivalent surface instead. pscale inspect all
runs every applicable check over a single connection and prints a combined
report.

The two surfaces cross-reference each other in help text, human output,
and JSON next_steps so agents discover both: insights for traffic-aware,
historical analysis; inspect for live state and physical layout.

Supporting changes: sqlquery gains a reusable Session (one connection,
many queries) extracted from Execute; the printer exposes its resource
writer for commands that render dynamic column sets.
Agents that discover pscale via --help (rather than a repo AGENTS.md) had
no pointer to the embedded agent guide outside the root command's help and
JSON error next_steps. Every command's help/usage output now ends with a
footer pointing at pscale agent-guide, and the full guide is readable as a
help topic via pscale help agents.

--keyspace on sql and inspect now accepts vtgate shard targets, e.g.
mykeyspace/-80 or mykeyspace/-80@replica. Shard-targeted connections
bypass the query planner and send statements directly to that shard's
MySQL instance, so inspect checks can read one specific shard's
statistics on sharded databases (enumerate shards with SHOW
VITESS_SHARDS; rows are keyspace/shard). Databases can have hundreds of
shards, so nothing fans out automatically — one target per run. The DSN
is now built with the driver's Config/FormatDSN so the slash and at-sign
in shard targets survive; previously they produced an invalid-DSN error.
@nickvanw
nickvanw force-pushed the nick/inspect-insights branch from 03c5895 to 25cac7b Compare July 23, 2026 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant