Skip to content

Annotate SQL queries with code origin comments

We can observe slow SQL queries in Postgres logs. For example, we regularly see a SELECT COUNT(*) FROM projects but from this point-of-view, we don't have an idea where that query comes from.

The idea here is to annotate all SQL queries with their origin (in terms of application code). We would add a comment with details how to trace the SQL query. In this case, we would see something like SELECT COUNT(*) FROM projects -- models/some_model.rb#342 or similar.

marginalia annotates with Rails controller, action et al. - maybe that provides enough insights already. https://github.com/basecamp/marginalia