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:
- pagination or a batched
IN (...)load that runs five or more pages or chunks; - a test that makes several independent requests, each repeating the same
lookup (use
queryguard_scope = session, see below); - two parent objects that each load a relationship once (the default threshold of 2, see the next question);
IN (...)queries with lists of different lengths: every length is one statement shape, so five such queries in one test are reported.
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:
- a few lines of code, in a test or in the application:
with pytest_queryguard.ignore("relationship-load"):; - one finding, in one test:
@pytest.mark.queryguard_ignore("Order.customer", function="list_orders"); - one finding, in the whole suite: a line in
queryguard_ignore, for examplerelationship-load Order.customer function=list_orders; - a finding you cannot fix yet: put it in a baseline;
- a kind you do not want to fail tests:
queryguard_fail_on, which makes the other kinds warnings; - a table that is fine to scan (index check):
queryguard_index_ignore_tablesor@pytest.mark.allow_missing_index.
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 loop is in the test itself, or in a helper, factory or
conftest.pyundertests/: that is test code, and its N+1s are left out by default. The run then ends with aqueryguard: test codesection that counts them and names the tests. Put the loop in a module of the application (the code you ship), or runpytest -o queryguard_test_code=warnto see them; see Test code; - the code under test ran in a fixture, not in the test body (see the next question);
- the statements did not come through SQLAlchemy (a raw
sqlite3or other DB-API connection, Peewee, Tortoise ORM): the plugin does not see them, and--queryguard-reportshows 0 statements for the test; queryguard_mode = off, or the finding is in the baseline or an ignore pattern;- the count is below the threshold.
--queryguard-reportshows how many statements each test ran.
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:
- only the block is recorded: a relationship the test reads later, after the fixture returned, is a lazy load outside it (and is measured with the test body instead);
- the block records statements from every thread while it runs, so keep unrelated background work, such as a server thread started by another fixture, out of it.
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 missing-index check (PostgreSQL) adds a few database round trips per
distinct statement per test, about 1 ms per statement in the benchmark in
Missing-index check. On SQLite it adds, over the "on" column
above, +13.5 us for a repeated statement shape, +31.5 us for a repeated ORM
statement and +34.5 us for a new shape that must be explained
(
scripts/bench_overhead.py --index-check). - Eager-load tracking (
queryguard_unused_eager_load) wraps attribute reads: 171 ns per attribute read with the option off, 207 ns with it on but idle (outside a test body), 306 ns while a test body is tracked (scripts/bench_eager_overhead.py). - Unused-column tracking (
queryguard_unused_columns) wraps the same reads and listens to each loaded row: about 170 ns per attribute read and 1.3 us per loaded row while a test body is tracked (scripts/bench_columns_overhead.py, see Unused columns).
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?#
- For one run:
pytest -p no:queryguarddoes not load the plugin at all (markers such asmax_queriesare then unknown, which pytest reports as a warning, or an error under--strict-markers);pytest --queryguard-mode=offloads it but stops N+1 detection, while budgets still apply. - For one test:
@pytest.mark.queryguard_ignorewithout arguments. - For the suite:
queryguard_mode = "off"in the configuration.
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?#
- Your data access goes through neither SQLAlchemy 2 nor the Django ORM: Peewee, Tortoise ORM, raw DB-API connections. The plugin sees nothing there, and says nothing.
- You want to see N+1 queries in production: the in-application mode is for development and staging only. An APM tool fits production better.
- Most of the database work in your suite happens in fixtures, or the tests that matter are end-to-end tests against a deployed service: only the test function body is measured, in the same process.
- Every relationship already uses
lazy="raise"and you only need that rule:raiseloadgives a hard error with no plugin (see the first question). Repeated statements, budgets and the index check are still beyond it. - You need to assert the exact SQL a block sends: that is a snapshot tool's job; this plugin looks for patterns.