Precise N+1 Detection for Go via Interprocedural Dataflow Analysis #6648
Replies: 1 comment
|
Message to the community This repo contains several binary files. The code is not Go idiomatic and the design is weird. The author seems to think that spamming the Go community (not only golangci-lint) with AI generated discussion is something good... I'm recommendation is to not use this analyzer. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone,
I'm working on Query Controller, a static analyzer for Go that detects true, data-dependent N+1 database query patterns — not just "there is a query inside a loop."
The Problem
Most N+1 detectors (and many linters) use simple heuristics: if a DB call appears inside a
for/range, flag it. That produces a lot of noise. Many loop queries are perfectly fine when arguments are constants or don't depend on the iteration variable.What I care about is the case where loop data actually flows into query arguments across function boundaries — the classic pattern:
What Query Controller Does
It uses IFDS-style tabulation over Go SSA.
Phase 1 — Trace Query Arguments
Seed GORM-style query sinks (
Where,Raw,Query,Exec, etc.) and trace arguments backward through:Phase 2 — Detect True N+1 Patterns
Report only when the traced value:
Find,Scan,First, etc.)Implementations
There are currently two implementations in the repository:
1. Standalone CLI
A stack-based IFDS prototype located at the repository root.
2. golangci-lint Plugin
Located under
analyzerVersion/.The plugin performs cross-package analysis using:
SinkParamFactReturnToParamFactExecutorFactExample Diagnostic
Current Status
✅ GORM-style APIs are modeled
✅ Cross-package analysis works in the golangci-lint plugin
✅ Test fixtures cover:
Known Limitations
sqlx,ent,pgx, etc. yet)golangci-lintbuildQuestions for the Community
I'd love feedback on the following:
Usefulness
Is precision-focused N+1 detection something you'd run in CI, or is batching/preload guidance generally enough?
ORM Coverage
Which Go database libraries should be supported next?
sqlxentdatabase/sqlpgxIntegration
Which delivery model would you prefer?
golangci-lintplugingo vet-style analyzerFalse Negatives / Positives
What N+1 patterns in real codebases would you expect this approach to miss or incorrectly flag?
Contributions
Any interest in helping with:
Repository
Repo: https://github.com/kiarash8112/query-controller
The README contains setup instructions for both the standalone CLI and the
golangci-lintplugin.Thanks! Any thoughts, war stories, edge cases, or feedback would be greatly appreciated.
All reactions