FAQ#

The questions below are the ones to ask before adding a test-time check to a large suite: what it costs, how often it is wrong, how to silence it, and where it does not fit. The answers marked as measured link to how.

How does this relate to SQLAlchemy's raiseload?#

raiseload is a loader option (relationship(lazy="raise"), or .options(raiseload(Order.customer)) per query) that makes SQLAlchemy raise InvalidRequestError the moment code touches a relationship that was not loaded. It prevents lazy loading everywhere the option applies, in tests and in production, and it needs a change in the model or the query.

pytest-queryguard changes nothing in your code. It watches the statements a test sends and reports patterns, including ones raiseload does not cover: the same statement repeated in a loop with session.get(), Core and text() statements, loops over deferred or expired columns, row-by-row writes and missing indexes. The two work together: raiseload blocks a lazy load, so a blocked access raises instead of reaching the database, and queryguard has no lazy load to count at that point. Use raiseload where you want a hard rule in the model, and queryguard where you want to find the places that need one.

Two parents each lazy-load one relationship. Is that really a finding?#

When one query loaded both, yes. The relationship rule counts distinct parent objects per query line that loaded them, and the default threshold is 2, so order.customer read for two orders of one select(Order) is reported as Order.customer was lazy-loaded 2 times. That is the smallest N+1, and test databases hold a few rows, so a higher default would miss loops in tests that only create a couple of objects. Two orders loaded separately, at two different lines (session.get(Order, 1) and session.get(Order, 2)), each reading its customer, are not an N+1 and are not reported. When the plugin cannot tell which query loaded a parent (an object the test created, or one loaded before the test body), that parent counts with the parents of every query line, so two such objects, or one of them and one looked up, are reported. If two is too strict for your code, raise it:

[tool.pytest.ini_options]
queryguard_relationship_threshold = 3

or silence one place, as in the next question.

How often is it wrong?#

A finding means that a pattern happened in the test: the same relationship loaded for several parents, or the same SELECT run five times or more. It does not measure production latency. The patterns that are reported although the code is fine, all listed on the Limitations page, are:

What it misses: statements outside SQLAlchemy, statements in fixtures, and a self-referential walk up a parent chain. The accuracy page measures precision and recall per rule on a corpus of realistic scenarios, and the comparison runs the same scenarios through other tools.

A finding is a false positive. How do I silence it?#

From the narrowest to the widest:

See Ignoring findings. Patterns that match nothing are listed as stale ignores, so they do not outlive the code.

My integration test makes several requests and fails with repeated-statement#

By default the repeat rules count everything one test runs. A test that calls an API five times, with a test client for example, runs the same lookups (authentication, the organisation, a webhook) once per request, each in its own SQLAlchemy Session. Nothing loops, but the count is five. The failure says so when it can: ran 9 times across 8 different Sessions or connection checkouts, with a note naming the option. Set it to count each Session on its own:

[tool.pytest.ini_options]
queryguard_scope = "session"

A loop inside one request, or any code that runs the same statement five times in one Session, is still reported. The same applies to lazy loads, column loads and write loops. See Tests with several sessions or requests for how the scope is chosen and where it stops being exact.

Can a tool get a callback for every finding?#

Not during the test run: pytest-queryguard has no pytest hook or callback that is called per finding. A tool that wants the findings (to send them to a dashboard, to count them over time, to annotate a pull request) reads the file that --queryguard-json=PATH writes at the end of the run. That file is the integration point for tooling: its format is versioned ("format": 1), the keys it promises are listed in Stability, and they are described in Query counts as JSON. Each test lists its findings with kind, label, count, file, line, function, stack and baselined:

import json
from pathlib import Path

report = json.loads(Path("queryguard.json").read_text(encoding="utf-8"))
if report["format"] != 1:
    raise SystemExit(f"unsupported queryguard JSON format {report['format']}")
for test_id, test in report["tests"].items():
    for finding in test["findings"]:
        if finding["baselined"]:
            continue
        print(test_id, finding["kind"], finding["label"], finding["count"],
              f"{finding['file']}:{finding['line']}")
if not report["complete"]:
    print("the run stopped early; tests are missing")

Do not parse the terminal report or the message field: their wording is not stable. For code scanning, the SARIF file is the other stable output. In a running application (not in tests), the in-application mode calls a report callable for every request with findings or over its budget, with a RequestReport that lists them.

Does it work with pytest-xdist?#

Yes for detection, budgets, the baseline and SARIF: each worker measures its own tests, and the controller merges what the workers saw: the session report (--queryguard-report), the index check's summary and the stale-ignore list are the same as in a serial run, and so are the JSON, SARIF, baseline, budget and patch files. The test suite runs the plugin under -n 2 to -n 4, with each --dist mode, in CI and compares the outputs with a serial run.

Which PostgreSQL drivers work?#

N+1 detection and budgets hook SQLAlchemy events, not a driver, so they do not depend on the driver. The index check needs a driver it can talk to through SQLAlchemy: CI runs it with pg8000 (sync) and asyncpg (async), on PostgreSQL 14 to 18, and runs the whole suite once more with psycopg 3 as the sync driver, on PostgreSQL 17. psycopg 3 and psycopg2 are LGPL-licensed, so the package does not depend on them; psycopg 3 is installed only in that CI run, as a test dependency that is not shipped. psycopg2 is not run in CI; it was checked by hand during review and gave the same findings for plain statements, % in SQL, expanding IN, autocommit and stream_results. On 2026-10-12 the 14 index scenarios of the comparison also gave the same results through psycopg 3 (3.3.6) as through pg8000, on PostgreSQL 17.11.

Async drivers other than asyncpg (for example psycopg's async mode) are skipped by the index check, with a notice in the session summary; N+1 detection and budgets work on them, and CI runs psycopg's async mode too.

Why was nothing reported for my test?#

The code I want to check runs in a fixture#

Only the test body is measured, on purpose: seeding data is meant to run many queries, and a session-scoped fixture would otherwise charge its setup to whichever test happens to run first. When the fixture is the code under test, wrap that code in record_queries() and check the log:

import pytest

from pytest_queryguard import record_queries


@pytest.fixture
def report_data(session):
    with record_queries() as log:
        data = build_report_data(session)  # include the relationship reads
    log.assert_no_nplusone()
    return data

An N+1 in build_report_data then fails the setup of every test that uses the fixture, with the usual message:

E       pytest_queryguard.NPlusOneDetected: queryguard: N+1 detected
E         Order.customer was lazy-loaded 3 times
E         first at test_recipe.py:40 in build_report_data

Two things to keep in mind:

Thanks to @Muguett-DBY for the recipe.

Does it slow my tests down?#

It adds work per statement: a stack walk for the call site and a timer around the cursor call. scripts/bench_overhead.py measures it, and CI compares it with a stored baseline.

Measured with scripts/bench_overhead.py on an Intel Core i5-13500H, Python 3.13, SQLAlchemy 2.1.1, median of 15 runs of 5000 statements pinned to one core, each configuration in a fresh process, against an in-memory SQLite database:

Per statement Measurement off On Added
Core select by key 41 us 50 us +9 us (+22 %)
ORM select by primary key 76 us 107 us +31 us (+40 %)

This table is from an earlier round of measurements. Since 0.10.0 the stack walk goes up to the test function, to tell a loop from calls on separate lines and test code from the application; measured on the same laptop, the added cost is then about 16 us per Core statement and 34 to 36 us per ORM statement (see The call path).

Two rounds differ by a few points, so read these as "about a fifth to two fifths" and not as exact figures. The percentages are large because an in-memory SQLite statement is almost free: the added cost is a fixed 9 to 31 microseconds, and a statement that goes to a real database takes far longer, so the share is much smaller there. These are micro-benchmarks. On a generated suite of 300 tests against a file-based SQLite database and against PostgreSQL the difference between running with and without the plugin was smaller than the run-to-run noise while each test waited for the disk; when they did not (about 5 ms of CPU a test), the plugin added 13 to 20 % to the run. See Performance for the method and numbers.

The options that are off by default cost nothing until you turn them on:

The numbers depend on your machine and database.

Does it work with Django, Peewee or raw SQL?#

Django's ORM, yes (no longer experimental since 1.1.0): see Django. Not with Peewee or Tortoise ORM. Raw SQL that goes through SQLAlchemy (text(), Core) is seen; a raw DB-API connection that SQLAlchemy does not manage is not.

Can it break my test suite?#

It is designed not to. If one of its listeners raises, the plugin stops measuring for the rest of the session instead of failing your tests, issues a QueryGuardInternalWarning that names the listener, and prints a "queryguard: measurement stopped" section at the end of the session with the error and the line that raised it. A test then passes or fails on its own assertions. Invalid configuration values are the exception: they stop pytest at start-up with a usage error, before any test runs.

On SQLAlchemy 2.0.20 to 2.0.49, every do_orm_execute listener makes the outer statement's yield_per and autoflush options reach its selectinload statement (SQLAlchemy issue 13301): yield_per then fails with "Can't use the ORM yield_per feature in conjunction with unique()". The plugin has such a listener only for strict mode and the unused eager-load check (both off by default) and undoes the effect for them, so a test does not fail only because the plugin is installed. A listener of your own triggers the issue with or without the plugin; upgrading to SQLAlchemy 2.0.50 or later fixes the cause.

The checks do not change the SQL your code sends. The index check explains statements without running them: on PostgreSQL inside a savepoint that is rolled back, on SQLite and MySQL with EXPLAIN, so data and the test's transaction are left as they were.

How do I turn it off?#

Does it work in a monorepo?#

Yes. File paths in reports, the baseline and ignore patterns are relative to pytest's rootdir, so they depend on where pytest is configured, not on the directory you start it from. With one pytest.ini at the repository root and pythonpath = services/a services/b, a finding reads first at services/a/app/orders.py:10 and the baseline stores that path. If each service has its own configuration file and its own rootdir, keep one baseline file per service: the same finding is app/orders.py there.

Call sites skip everything in site-packages. Install the monorepo's own packages editable (pip install -e, uv sync), or a finding in a shared library points to the test instead of the library line. For GitHub code scanning from a service directory, the action's working-directory input keeps SARIF paths relative to the repository root (see SARIF).

Does it work with async tests?#

Yes. AsyncSession runs on a sync engine underneath, which the plugin watches, so an async def test run by pytest-asyncio or anyio is measured like any other, and the call site is the await in your code. Lazy loads through awaitable_attrs are reported as relationship loads. The pytest-asyncio recipe covers configuration mistakes that look like database problems (strict mode, async fixtures, event loop scopes), and Getting started has an async FastAPI example. On SQLAlchemy 2.1 and newer install sqlalchemy[asyncio] for greenlet.

Does it work on free-threaded Python?#

The free-threaded builds (CPython 3.13t and 3.14t, no GIL) are not in the package classifiers, so they are not a supported configuration. CI runs only the in-app mode's tests (tests/inapp) on 3.14t, not the whole suite. The whole suite was run once on CPython 3.14.4t with SQLAlchemy 2.1.1 and passed; 3.13t was not tried. On those builds the counters of a measurement (statements, loads and database time) are updated under a lock, so statements from several threads are all counted and budgets hold; on a build with the GIL no lock is taken. The stored events are list appends and the call-site caches are dictionaries, which CPython keeps consistent without the GIL; the sequence numbers that order Sessions and executions (itertools.count) stayed unique from 8 threads on 3.14.4t. The missing-index check and the unused-eager-load tracker check and then update shared sets, so with several threads they may do one step twice; those were not examined further. Every listener fails open, so a race there costs accuracy, not the test run.

When should I not use it?#

Edit this page on GitHub