Static N+1 scan¶
Runtime detection finds an N+1 only after something has run it, and only on a path your test traffic reached. The static scan reads your templates (and, for Django, your serializers) without running anything, and flags the patterns that are almost always an N+1. Use it in CI to catch the problem before it ships.
What it finds¶
Template loops¶
A loop whose body reads a relation off the loop variable:
{% for order in orders %}
<tr>
<td>{{ order.customer.name }}</td> {# one query per order, unless prefetched #}
<td>{{ order.items.count }}</td> {# a COUNT per order #}
<td>{% for i in order.items.all %}...{% endfor %}</td>
</tr>
{% endfor %}
Each of order.customer.name, order.items.count and order.items.all costs a query per
iteration unless the view fetched the relation up front with select_related or
prefetch_related.
The scanner understands Django and Jinja syntax: comments ({# #} and
{% comment %}), {% verbatim %} and {% raw %}, nested loops, {% with %} and
{% set %} aliases, filters (order.customer.name|upper) and Jinja subscripts
(order['customer'].name). A template it cannot make sense of is skipped with a
warning, never an exception. It is dependency-free and best-effort.
DRF serializers (Django)¶
A ModelSerializer with a nested relation serializer, used by a view whose queryset lacks
the matching select_related or prefetch_related. The check is on by default and skipped
with --no-drf.
Fewer false positives in Django¶
Without help, any x.a.b in a loop could be a plain attribute. In Django the scanner asks
the app registry whether the first hop is a real relation (a foreign key, one-to-one,
many-to-many or reverse relation) and keeps only those chains. It also uses the relation's
kind to choose the right fix: select_related for a forward foreign key, prefetch_related
for a reverse or many-to-many relation. Flask has no model registry, so the Jinja dialect
cannot check that a hop is a relation and you triage its findings by hand.
Running it¶
python manage.py redline_scan # every template directory
python manage.py redline_scan templates/shop/ # specific files or directories
python manage.py redline_scan --json # machine-readable
python manage.py redline_scan --fail # exit 1 if anything is found
python manage.py redline_scan --dialect jinja templates/jinja/
python manage.py redline_scan --no-drf # skip the serializer check
Text output is an aligned table:
LOCATION KIND SEVERITY CHAIN SUGGESTION
shop/orders/list.html:14 template warning order.customer.name select_related("customer")
shop/orders/list.html:17 template warning order.items.all prefetch_related("items")
shop/api/serializers.py:22 serializer warning items (nested) prefetch_related("items") on OrderViewSet.queryset
3 possible N+1 finding(s).
--json prints {"findings": [...], "warnings": [...]} where each finding is:
{
"file": "shop/orders/list.html",
"line": 14,
"loop_var": "order",
"chain": "order.customer.name",
"kind": "template",
"severity": "warning",
"message": "...",
"suggestion": "select_related(\"customer\")"
}
In CI¶
--fail exits with status 1 when anything is found. Start by running it without --fail
to see the baseline, fix or accept what it finds, then turn it on so a new loop with a
relation inside cannot slip in.
Limits¶
- It is static. It cannot know that the view already prefetched
customer, so a finding is "check this", not "this is slow". The runtime detector is the confirmation. - It reads templates and DRF serializers. Python code that does
for o in orders: o.customer.nameis what the runtime detector and its fix diff are for.
Source analysis: cross-module helpers, dynamic SQL and precision¶
The template scan above has two Python-source companions, redline analyze (performance
patterns such as a query inside a loop) and redline check-sql (raw SQL lint). Neither imports
your code; both read the syntax tree within the usual file and byte budgets.
Cross-module helpers. analyze follows a helper that runs a query across files of the
scanned project: from pkg.b import load_user, import pkg.b as b; b.load_user(),
from . import b and re-exports through a package __init__ all count. A loop in module A
that calls such a helper is reported as a query_in_loop candidate. Resolution is bounded
(a fixed number of propagation passes), has cycle protection, and only follows files included
in the scan; dynamic imports, wildcard imports and class dispatch may be missed.
Dynamic SQL. check-sql evaluates SQL text built from constants without running
anything: f-strings with constant parts, +, % and .format() on module-level constants,
" ".join([...]) of literals, += accumulation, and straight-line local reassignments inside
a function. Anything inside a branch, loop or try, a parameter, a call result or an
attribute stays unresolved. .format() fields with attribute or index access are refused.
Unresolved inputs are counted by reason in the report (--json):
Each entry of skipped also carries a category. Categories: unknown_name, call,
attribute_or_subscript, operand, unsafe_format, unsafe_helper,
sqlalchemy_expression, too_large, too_deep, limit, invalid_source, unreadable,
compilation and dynamic (anything else).
Measuring false positives. scripts/measure_static_precision.py runs both analyzers over a
labelled corpus (packages/redline-core/tests/fixtures/static_precision) and prints
precision, recall and false-positive rate; a test keeps precision above a floor. Lines
marked # expect: <kind> are true positives; # fp: <kind> marks a shape the analyzer is
known to report wrongly (it counts against precision until fixed).
uv run python scripts/measure_static_precision.py # the bundled corpus
uv run python scripts/measure_static_precision.py --json
On your own codebase:
uv run python scripts/measure_static_precision.py src/ > findings.txt # lists finding ids
# review them; put the ids that were wrong into reviewed.json, e.g. ["a1b2...", "c3d4..."]
uv run python scripts/measure_static_precision.py src/ --false-positives reviewed.json --json
The result gives the false-positive rate overall and per finding kind for your code, which is the number to use when deciding whether to gate CI on a rule.