Skip to content

Writing your own integration

Query Doctor reads a SQLCommenter comment at the end of each query. When no integration exists for your ORM, driver or language, add the comment yourself. Any code that changes the SQL text before it reaches Postgres can do it, such as a driver wrapper or an ORM hook.

Query Doctor reads the last comment in the query

Section titled “Query Doctor reads the last comment in the query”

Append one comment to the end of the query:

SELECT id, name FROM users WHERE active = true /*db_driver='my-orm',file='src%2Fusers%2Fservice.ts%3A42%3A5',func_name='UserService.listActive',method='GET',route='%2Fapi%2Fusers'*/

Query Doctor parses it like this:

  • Only the last /* ... */ in the query is read, and only when nothing but whitespace or a ; follows it.
  • Pairs are separated by , and written as key=value.
  • Values are URL-decoded. One single quote at each end of a value is removed, so 'GET' and GET both read as GET. Keys are trimmed but not decoded.
  • A , or = inside a value has to be URL-encoded (%2C, %3D), or the pair is split in the wrong place. Encoding every value with your language’s URL-component encoder (encodeURIComponent, urllib.parse.quote(value, safe=""), Uri.EscapeDataString) does this.
  • A pair that fails to decode is skipped, and the rest are kept.
  • file is the call site as path:line:column. A caller stack can go in one value, ;-separated, with the query’s own call site first. Query Doctor shows the file on the query as a link that opens it in your IDE. When the first entry is a test file (*.test.ts, tests/, *_test.go and similar), the query is left out of CI checks and alerts.
  • func_name is the enclosing function or method, such as UserService.listActive. It names the query in the pull request comment and on the query page, and makes it searchable by name. With file, it pairs a rewritten query with the query it replaced in the previous run. symbol is accepted as another spelling.
  • route is the HTTP route pattern, such as /api/users/:id. It fills the route column on the Queries page, which you can filter by.
  • method is the HTTP method, shown next to the route. http_method is accepted as another spelling.
  • db_driver names your ORM or driver, and is shown on the query page. driver is accepted as another spelling.
  • Any other key is shown on the query page with its value.

Use the route pattern rather than the request path. /api/users/:id groups every request to that endpoint; /api/users/42 makes a new route for each user.

Send file even if you send nothing else. Without it, a query from a test can fail a CI check, and the query has no link to your code.

An integration does three things per query

Section titled “An integration does three things per query”

For each query:

  1. Find the caller. Walk the stack trace and skip the frames that belong to your ORM, driver and the integration itself. The first frame left is the call site: take its file, line and column for file, and its function name for func_name. Leave func_name out when the function is anonymous.
  2. Collect the request context. Route and method usually come from a middleware that stores them in request-scoped storage (AsyncLocalStorage in Node.js, contextvars in Python, AsyncLocal<T> in .NET), which the integration reads when the query runs.
  3. Append the comment. Skip a query that already ends with one: Query Doctor reads only the last comment, so a second one hides the first.

A minimal version in TypeScript, for a driver that hands you the SQL string before sending it:

function sqlComment(tags: Record<string, string | undefined>): string {
const pairs = Object.entries(tags)
.filter((entry): entry is [string, string] => entry[1] !== undefined)
.map(([key, value]) => `${key}='${encodeURIComponent(value)}'`)
.sort();
return pairs.length > 0 ? `/*${pairs.join(",")}*/` : "";
}
function tagQuery(sql: string, caller: { file: string; funcName?: string }) {
if (sql.trimEnd().endsWith("*/")) return sql;
const request = requestContext.getStore(); // AsyncLocalStorage set by your middleware
return (
sql +
sqlComment({
db_driver: "my-orm",
file: caller.file,
func_name: caller.funcName,
route: request?.route,
method: request?.method,
})
);
}

The published integrations do all three, stack walking included. The Node.js ones are in nodejs/sqlcommenter-nodejs/packages, and the EF Core one is in dotnet/sqlcommenter-efcore.

Run a query through your integration and read its text in your driver’s debug output or in pg_stat_statements. The comment must be the last thing in it. Then open the query on the Queries page, where the file, function and route appear.

Queries that pg_stat_statements recorded before the integration was in place keep their untagged text. See Reset pg_stat_statements after enabling.

Or contribute it to Query-Doctor/sqlcommenter

Section titled “Or contribute it to Query-Doctor/sqlcommenter”

If other teams use the same ORM or driver, the integration can live in Query-Doctor/sqlcommenter instead of your own repository. Open a pull request that adds it to its language’s folder, next to the existing ones, with tests and a README.