Skip to content

Plan checks

Español: es/PLAN_CHECKS.md

Experimental: not covered by the 1.x stability guarantee (see STABILITY.md); it may change in a minor release.

The guard reads SQL; it does not know how big your tables are or which indexes exist. A query that passes every rule can still read a 50-million-row table in full. Plan checks ask the database what it would do, without running the query, and judge the answer: estimated cost, estimated rows, and which tables are read in full.

The MCP server does this with --max-cost, --max-plan-rows and --plan-full-scan. PlanGuard is the same logic for code that calls Guard directly. It touches no database and adds no dependency: it builds the EXPLAIN statements, you run them on your own connection, and it reads the plan.

from sqlguardllm import Guard
from sqlguardllm.plan import PlanGuard, sqlalchemy_runner

plans = PlanGuard("postgresql", max_cost=50_000, max_rows=1_000_000,
                  full_scan_tables={"events", "orders"}, on_exceed="deny")

decision = guard.check(sql, context)          # the rewritten SQL
with engine.connect() as conn:
    verdict = plans.check(decision, sqlalchemy_runner(conn))
    conn.rollback()
if not verdict.ok:
    raise RuntimeError(verdict.message())      # e.g. "full_scan: events is read in full"

What is explained

check() takes the CheckResult the guard returned and only that. It refuses a rejected result, so the SQL that is explained is always the rewritten SQL, with the row filters, masks and limit applied, never the model's own text. It never uses ANALYZE, which would run the query.

The verdict

verdict.action is allow, warn or deny, and verdict.ok is false only for deny. verdict.reasons lists what broke, each with a stable code (max_cost, max_rows, full_scan, plan_unavailable) and a detail that holds a number, a table name or an exception type, never the SQL, a context value or a database error message. verdict.estimate has the cost, rows and scans the plan reported.

Rules

Setting Refuses when Engines
max_cost The planner's cost is above it (the engine's own units) PostgreSQL, MySQL, SQL Server
max_rows The largest estimated row count is above it PostgreSQL, MySQL, SQL Server, DuckDB
full_scan_tables The plan reads one of these tables in full, whatever the cost all five, SQLite included

on_exceed="warn" lets the query run and returns the reasons in the verdict. A plan that can't be read follows the same setting, and is never an allowance. A rule the engine can't answer (max_cost on SQLite) raises ValueError when you build the PlanGuard.

Running the statements yourself

sqlalchemy_runner(conn) works for SQLAlchemy. For another driver, plans.statements(decision) returns the statements to run, in order, on one connection, and check() accepts any function that takes them and returns the plan text:

Engine Statements Plan text
PostgreSQL EXPLAIN (FORMAT JSON) <sql> first column of the first row
MySQL EXPLAIN FORMAT=JSON <sql> first column of the first row
DuckDB EXPLAIN (FORMAT JSON) <sql> second column (explain_value)
SQLite EXPLAIN QUERY PLAN <sql> the last column of every row, one per line
SQL Server SET SHOWPLAN_XML ON, <sql>, SET SHOWPLAN_XML OFF first column of the row the query returns

Permissions and limits

  • SELECT is enough for PostgreSQL and MySQL; SQL Server needs SHOWPLAN; SQLite and DuckDB need nothing extra.
  • The numbers are estimates from table statistics. Keep them fresh (ANALYZE, ANALYZE TABLE, UPDATE STATISTICS). Units differ by engine, so pick thresholds by trying typical queries.
  • Not covered: spill to disk (PostgreSQL reports it only in EXPLAIN ANALYZE, which runs the query) and Oracle (EXPLAIN PLAN writes to a table, which does not fit a read-only session). The proxy and protect have no database connection, so they do not run plan checks.
  • A DuckDB sequential scan counts as a full scan even when it pushes a filter down, and an index scan that reads the whole index counts too. SQLite and MySQL report aliases, which are resolved through the SQL.