Repository navigation
feat: pgbot sql explain — a query plan, read in plain English - #123
Merged
Merged
Conversation
Add a `sql` command group with `explain`: the plan-only form of EXPLAIN for one SELECT (GENERIC_PLAN on PG16+), run inside a READ ONLY transaction and never executed, rendered as a tree with a plain-English gloss per node, the planner's estimates, and a deterministic "What stands out" list (large seq scans, nested loops repeating a scan, sorts an index could avoid, filters applied after an index lookup). `--json` prints the raw plan; `--ai` appends a model's prose reading behind a disclosure prompt that names what is sent. The new internal/plan package (Prepare/Fetch/Parse/Gloss/Observe) is shared with the MCP explain_plan tool, which now also returns the observations. Cobra command matching is case-insensitive so `pgbot SQL EXPLAIN` works. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
alexshapalov
force-pushed
the
feat/sql-explain
branch
from
October 19, 2026 01:09
7db1cc7 to
db25756
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.
What
A new
sqlcommand group withexplain: the planner's plan for one SELECT, read back in plain English.GENERIC_PLANon PG16+, so$1needs no value) inside aREAD ONLYtransaction, through the same SELECT-only sanitizer asadviseand the MCP tool. The query is never executed — an integration test plansSELECT pg_sleep(30)and asserts it returns at once.--jsonfor the rawEXPLAIN (FORMAT JSON);--aiappends a model's prose reading of the same rendered text behind a disclosure prompt that names what is sent (SQL text + plan, which unlike findings can carry literals).Internals
internal/plan(Prepare / Fetch / Parse / Gloss / Observe / HasPlaceholders),internal/render/plan.go,internal/ai/plan.go,cmd/pgbot/sql.go.explain_plantool now goes throughplan.Fetchso the two paths cannot drift, and returns the sameobservationsplusgeneric_plan.confirmDisclosuregained a payload-naming variant; existing callers unchanged.cobra.EnableCaseInsensitive = true(no two pgbot commands differ only by case).Verification
go build,go vet,golangci-lint run: clean.go test -race ./...green, including the new integration tests against PG16 (PGBOT_TEST_DSN).--json, write refused with exit 64,--aiwithout a key fails before connecting.🤖 Generated with Claude Code