Skip to content

Verified queries

Español: es/CATALOG.md

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

A generated query can pass every policy check and still calculate the wrong metric: gross sales where the business means net sales. The guard cannot tell. A catalog lets a data team publish the reviewed definition once, in git, and lets an agent ask for it by name with typed values. The server owns the SQL, so the definition is maintained, tested and audited in one place.

agent ──run_verified_query("net_sales_by_month", {from_date, to_date})──▶ server
                                          the server owns the SQL, values are bound, never written in

Approval grants no extra privileges. A catalog query goes through the same policy as any query: allowed tables and columns, masks, row filters bound to the trusted context, the row limit, the read-only transaction and the timeout, plus taint tracking, row budgets and plan checks when you have them on.

Set it up

# 1. The example data, on a disposable PostgreSQL database
createdb sg_catalog
psql sg_catalog -f examples/catalog/fixtures.sql

# 2. Check the catalog against the policy, for each role (also fine in CI, no database needed)
sqlguardllm catalog check examples/catalog/catalog.yaml \
  --policy examples/catalog/policy.yaml --role manager --context @examples/catalog/context.json

# 3. Serve it
DATABASE_URL=postgresql+psycopg://you@localhost/sg_catalog \
sqlguardllm mcp --policy examples/catalog/policy.yaml --role manager \
  --context @examples/catalog/context.json --catalog examples/catalog/catalog.yaml

# 4. Run the checks of the expected business results (they run in CI too)
SQLGUARD_PG_URL=postgresql+psycopg://you@localhost/postgres \
uv run pytest tests/engines/test_catalog_engines.py -v

The server then has two more tools next to query, which does not change: list_verified_queries (the ids, descriptions and typed parameters this role may run) and run_verified_query (an id and its values).

The file

YAML or JSON, kept in git next to the policy. The example, examples/catalog/catalog.yaml:

version: "2026-12-01"          # a string or an integer; recorded in every audit event
dialect: postgres               # must match the policy's dialect
queries:
  - id: net_sales_by_month
    description: Net sales per month: invoice total minus its discount.
    roles: [analyst, manager]   # optional; every role when omitted
    params:
      from_date: {type: date, description: First day included, required: true}
      to_date: {type: date, description: First day NOT included, required: true}
    sql: >
      SELECT to_char(issued_at, 'YYYY-MM') AS month, SUM(total - discount) AS net_sales
      FROM invoices
      WHERE issued_at >= :from_date AND issued_at < :to_date
      GROUP BY 1 ORDER BY 1
  • id: lowercase letters, digits and _, from a letter. Stable: agents and audits refer to it.
  • sql: exactly one statement, with :name placeholders. Nothing else binds: ?, $1 and %s are refused, and a colon inside a string or a :: cast is never a parameter.
  • params: each parameter is declared and used. Types: string (max_length, at most 200, no NUL), integer (64-bit), number (finite), boolean, date (YYYY-MM-DD). Options: required (default true), default, enum, minimum, maximum, description.
  • roles: who may see and run it. A role outside the list gets the same answer as for an id that does not exist.

What is checked, and when

At startup (the server refuses to start, catalog check exits non-zero): invalid definitions, duplicate ids, undeclared or unused parameters, a catalog written for another dialect than the policy, unknown role names, and any query the policy would refuse for the role (a table not allowed, a hidden column). The query is also checked as the guard rewrites it: the rewrite must keep every parameter, as many times as the template uses it.

On every call, before anything runs: the id and the role, then each value against its declared type. A missing, unknown or invalid parameter is invalid_parameters and the message names the parameter, never the value. Values are then bound by the driver; nothing a caller sends is ever written into the SQL.

The result

Like query, plus the id and the version: status, rewrites (the row filters and masks that applied), truncated, applied_limit, warnings, and query_id and catalog_version. See what a result says.

The audit trail

Each decision is recorded with catalog_query (the id) and catalog_version. The event holds no parameter value and no row, even with raw SQL logging on: the SQL recorded is the template, with its :name placeholders.

Keeping a metric honest

The point of a catalog is that its definitions are reviewed. examples/catalog/expected.json lists what each query must return on the fixture data (checked by hand: see the comment in fixtures.sql), and tests/engines/test_catalog_engines.py runs them. Change net sales to gross sales and the SQL is still valid and the policy still passes, but the test fails, so the change cannot ship unnoticed. The same file proves tenant isolation, masks, the row limit and the audit trail.

Limits

  • Not a restriction. The agent can still call query. A catalog makes the right definition easy to use; it does not forbid free SQL. To forbid it, narrow the policy.
  • A LIMIT cannot be a parameter. The guard replaces a limit it cannot read with the policy's row limit, so the parameter would vanish; the server refuses such a catalog at startup and says so. Write a number, or let the row limit decide.
  • PostgreSQL first. The examples and their expected results run on PostgreSQL 16 in CI. The binding is also tested on SQLite, and the SQL conversion on MySQL and T-SQL spellings, but a catalog on another engine has not been run against that engine here.
  • With OIDC authentication the context comes from each caller's token, so the policy check that runs at startup otherwise runs on every call instead. Role names are still checked at startup.
  • A query is one statement and read-only. Writes belong to controlled writes, not to a catalog.