skip to content

Query Tags & Tracing

Query log tags append the application, controller, action or job to each SQL statement as a comment, and verbose query logs show the calling line. Interviewers ask how you trace a slow query.

part ofRuby on Railsread it →
on this pageshow

explore

questions

5

In a Rails 8.1 development log, what do verbose_query_logs and verbose_redirect_logs add, and why are they left off in production?

level: juniorimportance: must knowfreq 35%

answer

  1. an arrow line under the log entry
  2. first frame from your own code
  3. set in the generated development.rb
  4. verbose_redirect_logs arrived in 8.1
  5. stack walk on every query

basics

~20 s

Both add a ↳ line naming the line of your own code behind the entry: verbose_query_logs under each SQL statement, verbose_redirect_logs (new in 8.1) under each redirect. Each walks the call stack, so only development enables them.

solid answer

~40 s

`config.active_record.verbose_query_logs = true` makes Active Record's log subscriber print, under every SQL line, a `↳` line with the first stack frame that belongs to the app, such as `↳ app/controllers/report_cards_controller.rb:9:in 'ReportCardsController#show'`; `Rails.backtrace_cleaner` skips gem and framework frames. `config.action_dispatch.verbose_redirect_logs`, added in Rails 8.1, does the same under `Redirected to ...` for `redirect_to` in a controller and for `redirect` in `config/routes.rb`. Both default to `false` in the framework; the `development.rb` that `rails new` writes sets both to `true`, so an older app has to add the redirect line itself. They are kept out of production because finding the frame means walking the call stack for every query or redirect, and production SQL lines are not even logged at the default `info` level. Production tracing uses query log tags instead.

code

ruby · 8 lines
ruby
# config/environments/development.rb (as rails new writes it in 8.1)
Rails.application.configure do
  # Highlight code that triggered database queries in logs.
  config.active_record.verbose_query_logs = true

  # Highlight code that triggered redirect in logs.
  config.action_dispatch.verbose_redirect_logs = true
end

go deeper

for a junior

Recall that the ↳ line under a SQL or redirect entry names the file and line in your code that caused it, and that the generated development.rb turns these on.

for a middle

Explain that the line is the first clean frame from Rails.backtrace_cleaner, that the framework defaults are false, and that verbose_redirect_logs is new in 8.1.

for a senior

Explain why these stay out of production, use the ↳ line as a lead rather than the fix site, and switch to query log tags for production tracing.

for a principal

Set team defaults for development logging so slow paths are visible before review, without paying stack-walk costs in production.

## The problem they solve A development log shows *what* ran, not *who asked*. In a school-grades portal, a report card page might log forty `Grade Load` lines, and the SQL alone does not say whether they came from the controller, a helper, a partial or a model callback. Rails has a family of **verbose log settings** that answer "which line of my code did this?" by printing the caller under the log entry. | Setting | Adds a `↳` line under | Framework default | `rails new` development.rb | |---|---|---|---| | `config.active_record.verbose_query_logs` | each SQL statement | `false` | `true` | | `config.action_dispatch.verbose_redirect_logs` | each `Redirected to ...` line | `false` | `true` (8.1 apps) | | `config.active_job.verbose_enqueue_logs` | each enqueue line | `false` | `true` | The third belongs to Active Job and is listed only to show the pattern. ## `verbose_query_logs` With it on, a request log looks like this: ``` Student Load (0.3ms) SELECT "students".* FROM "students" WHERE "students"."id" = ? LIMIT ? [["id", 42], ["LIMIT", 1]] ↳ app/controllers/report_cards_controller.rb:9:in 'ReportCardsController#show' Grade Load (1.2ms) SELECT "grades".* FROM "grades" WHERE "grades"."student_id" = ? [["student_id", 42]] ↳ app/models/student.rb:18:in 'Student#term_average' ``` How the `↳` line is chosen: 1. Active Record's log subscriber writes the SQL line at the `debug` level. 2. If `ActiveRecord.verbose_query_logs` is true, it asks its backtrace cleaner for the **first clean frame** of the current call stack. 3. The cleaner is `Rails.backtrace_cleaner`, which silences frames from gems and Rails itself, so the first frame left is in `app/` (or `lib/`) code. 4. That frame is logged, also at `debug`. The frame is the *nearest* line of your code, which is not always the line you need to change: a query fired from a partial points at the template line that iterated the association, not at the controller that forgot to preload it. `bin/rails console` switches the setting off for console sessions. ## `verbose_redirect_logs` (Rails 8.1) Rails 8.1 added `config.action_dispatch.verbose_redirect_logs`: ``` Redirected to http://localhost:3000/report_cards/42 ↳ app/controllers/grades_controller.rb:31:in 'GradesController#update' ``` - For `redirect_to` in a controller, the line comes from Action Controller's log subscriber, using the same first-clean-frame lookup. - For a `redirect(...)` route in `config/routes.rb`, Action Dispatch records the caller when the route is drawn and logs it with the redirect. - New 8.1 apps get `config.action_dispatch.verbose_redirect_logs = true` in `config/environments/development.rb`. An app generated earlier and upgraded keeps the framework default, `false`, until someone adds the line; it is not driven by `load_defaults`. This helps most when a `before_action` deep in a concern redirects and the log shows only the target URL. ## Why not production - **Cost**: each `↳` line requires walking the Ruby call stack and filtering frames, for every query or redirect. On a busy app that is extra CPU and allocation on the hot path. - **They would not appear anyway**: SQL lines and their `↳` lines are logged at `debug`, and a generated `production.rb` logs at `info` (from `RAILS_LOG_LEVEL`) by default. - **A better tool exists**: query log tags put the controller, action or job into the SQL comment itself, which is cheap and visible to the database, where production slow queries are actually found. ## Where the frame comes from `Rails.backtrace_cleaner` decides what counts as "your code". Its silencer keeps only frames whose path, relative to the app root, starts with `app/`, `config/`, `lib/` or `test/`; installed gems, Rails itself and `vendor/` are skipped. A frame inside a compiled template has its generated method name stripped, so a query fired from a view reads simply `↳ app/views/report_cards/show.html.erb:8`. ## Reading them well - Treat the `↳` line as a starting point and follow the call upward when it points at a view or a model method. - Repeated identical `↳` lines under near-identical SQL are the usual signature of a loop issuing one query per record. - If no `↳` line appears, check that the setting is on and that the log level is `debug`.

  • You upgraded an app from Rails 8.0 to 8.1 and set load_defaults 8.1, but redirects still show no ↳ line. Why?
    `verbose_redirect_logs` is not a `load_defaults` setting. The framework default stays `false`; only the `development.rb` generated by `rails new` in 8.1 turns it on. Add `config.action_dispatch.verbose_redirect_logs = true` to `config/environments/development.rb` yourself.
  • The ↳ line under a slow query points at a line in a view. Is that where the fix goes?
    Often not. The line is the first frame in your own code, which for a lazy relation is where it was iterated, typically the template. The fix, such as preloading the association or moving the query, usually belongs in the controller or model that built the relation.

  • verbose_query_logs is on in every environment by default.
  • load_defaults 8.1 turns on verbose_redirect_logs in an upgraded app.
  • The ↳ line shows the Active Record method inside the gem that ran the query.
  • verbose_query_logs is the recommended way to trace production queries.
  • verbose_redirect_logs covers redirect_to only, never routes.rb redirects.

Assumes Rails 8.1: config.action_dispatch.verbose_redirect_logs was added in 8.1.0 and only new 8.1 apps set it in development.rb; verbose_query_logs is older.

open as a page

In a Rails development log, what do Grade Load (12.4ms), CACHE Grade Load (0.0ms) and ActiveRecord: 350.1ms (48 queries, 6 cached) each measure?

level: middleimportance: should knowfreq 35%

basics

~20 s

Grade Load (12.4ms) is the time one SELECT took inside the database adapter, as measured from Ruby. CACHE means Active Record's query cache answered it without the database. ActiveRecord: 350.1ms (48 queries, 6 cached) totals every statement in the action.

open as a page

In Rails 8.1, what does config.active_record.query_log_tags_enabled add to each SQL statement, and how do the SQLCommenter and legacy formats differ?

level: middleimportance: should knowfreq 25%

basics

~20 s

It makes Active Record append a SQL comment naming the application and the controller and action, or the job, to every statement it sends. SQLCommenter, the default since load_defaults 7.1, writes key='url-encoded value' pairs; the legacy format writes key:value.

open as a page

A slow statement shows up in the production database's logs of a Rails 8.1 grades portal; how do you trace it to the controller action or job, and what does that cost?

level: seniorimportance: should knowfreq 25%

basics

~20 s

Enable query log tags in production so every statement carries a comment naming the controller and action, or the job; the slow statement in the database's logs then names its origin. The cost is prepared statements and some work per query.

open as a page

In Rails, how do you add a custom query log tag such as the current school's id, and when does its lambda receive a context?

level: middleimportance: nice to knowfreq 12%

basics

~20 s

Add a Hash to config.active_record.query_log_tags, such as { school_id: -> { Current.school&.id } }. A one-parameter lambda receives a context hash with the controller, job and connection; a zero-parameter lambda is just called. Nil drops the tag.

open as a page