Add pscale insights and pscale inspect commands for database diagnostics#1308
Draft
nickvanw wants to merge 3 commits into
Draft
Add pscale insights and pscale inspect commands for database diagnostics#1308nickvanw wants to merge 3 commits into
nickvanw wants to merge 3 commits into
Conversation
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
force-pushed
the
nick/inspect-insights
branch
from
July 23, 2026 01:35
03c5895 to
25cac7b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 rawpscale apicalls: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 messagesinsights anomalies <db> <branch>— detected resource anomaliesinsights recommendations <db>— schema recommendations with ready-to-apply DDLpscale inspect— live, point-in-time checks run over a direct connection using the same ephemeral-credential model aspscale 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— andinspect all, which runs every applicable check over a single connection and prints a combined report.All commands support
--format json(andcsvfor 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_stepsin JSON) so an agent that lands on either one discovers the other:insightsfor historical, traffic-aware analysis;inspectfor 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.outliersneedspg_stat_statements) — the output points at theinsightscommand that answers the same question server-side with no setup.Changes
internal/planetscale/insights.go— newQueryInsightsservice (ListQueries/ListErrors/ListAnomalies) plusWithSort/WithPeriodlist options; the already-presentSchemaRecommendationsservice is now actually used by a commandinternal/cmd/insights/— the four insights subcommandsinternal/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 treeinternal/sqlquery/session.go— reusableSession(one connection, many queries) extracted fromExecute; connection setup for both engines refactored intoopenMySQL/openPostgreswith no behavior change topscale sqlinternal/printer— expose the resource writer so commands with dynamic column sets can render CSVAGENTS.md— documents both surfaces and when to use whichNotes for reviewers:
inspectstatistics reflect one shard's MySQL instance per run.--keyspaceaccepts 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 withSHOW VITESS_SHARDS. Databases can have hundreds of shards, so nothing fans out automatically.--dbnameselects it, and the reader role can lack CONNECT on non-default databases — the error suggests--role admin.Also: every command's
--helpnow ends with a footer pointing agents atpscale agent-guide --format json, and the embedded guide is readable viapscale help agents.