skip to content

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%

answer

  1. a /* */ comment on the SQL sent
  2. application, controller, action, job
  3. keys in alphabetical order
  4. key='value' versus key:value
  5. sqlcommenter since load_defaults 7.1

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.

solid answer

~40 s

With `config.active_record.query_log_tags_enabled = true`, `ActiveRecord::QueryLogs` is installed as a query transformer, so every statement leaves Rails with a comment such as `/*action='show',application='GradesPortal',controller='report_cards'*/`. That text reaches the database, so its own logs and views of running queries show it, and in development it appears on the SQL log line too. Default tags are `:application` plus `:controller` and `:action` from Action Controller and `:job` from Active Job; tags whose value is nil are left out, and keys come out in alphabetical order. `config.active_record.query_log_tags_format` picks the format: `:sqlcommenter`, the default under `load_defaults` 7.1 and later, writes `key='value'` with URL-encoded values, which SQLCommenter-aware tools parse; `:legacy` writes `key:value`. The generated `development.rb` enables the tags; production does not. Enabling them also turns off prepared statements.

code

ruby · 11 lines
ruby
# config/environments/production.rb
Rails.application.configure do
  config.active_record.query_log_tags_enabled = true
  # Explicit, although load_defaults 7.1+ already chooses it:
  config.active_record.query_log_tags_format = :sqlcommenter
end

# A query from RecalculateAveragesJob then ends with:
#   /*application='GradesPortal',job='RecalculateAveragesJob'*/
# With :legacy it would end with:
#   /*application:GradesPortal,job:RecalculateAveragesJob*/

go deeper

for a junior

Recall that query log tags append a comment naming the app and the controller and action, or the job, to every SQL statement.

for a middle

Explain the default tags, nil omission, alphabetical order, and the difference between key='value' SQLCommenter and key:value legacy output.

for a senior

Explain that the comment reaches the database, which makes it useful for production tracing, and weigh the prepared-statement trade-off before enabling it there.

for a principal

Decide whether the comment format is part of your observability contract, so database tooling and tracing systems can rely on it across services.

## What the setting does `config.active_record.query_log_tags_enabled` (default `false`) switches on `ActiveRecord::QueryLogs`. When Rails finishes booting, it adds that module to Active Record's list of **query transformers**, functions every statement passes through just before the adapter sends it. `QueryLogs` builds a comment from a list of **tags** and appends it: ```sql SELECT "students".* FROM "students" ORDER BY "students"."last_name" ASC /*action='index',application='GradesPortal',controller='students'*/ ``` Because the comment is part of the SQL text sent to the database, it shows up wherever the statement does: the development log line, the database's own statement and slow-query logs, and its list of currently running queries. That is the point: a database-side view of a slow statement now names the Rails code path that issued it. ## The default tags | Tag | Added by | Value | |---|---|---| | `:application` | Active Record | the app's top-level module name, e.g. `GradesPortal` | | `:controller` | Action Controller | `controller_name`, e.g. `report_cards` | | `:action` | Action Controller | `action_name`, e.g. `show` | | `:job` | Active Job | the job class name, e.g. `RecalculateAveragesJob` | Details worth knowing: - Action Controller adds `:controller` only when you have not listed `:namespaced_controller` (which gives `admin/report_cards`), and adds `:action`. Active Job adds `:job`. They do so only while `config.action_controller.log_query_tags_around_actions` and `config.active_job.log_query_tags_around_perform` stay at their default `true`. - A tag whose value is `nil` is **omitted**, so a query from a job has `job=` but no `controller=`, and a query from a Rack middleware before the action runs has neither. - The pairs are **sorted by key** when the comment is built, so the order in the comment does not follow the order of your configuration. - Other built-in tags are available on request: `:pid`, `:socket`, `:db_host`, `:database` and `:source_location`. ## The two formats `config.active_record.query_log_tags_format` takes `:sqlcommenter` or `:legacy`; anything else raises `ArgumentError` at boot. | | `:legacy` | `:sqlcommenter` | |---|---|---| | Pair syntax | `controller:report_cards` | `controller='report_cards'` | | Value encoding | none | URL-encoded, so spaces and quotes are safe | | Separator | comma | comma | | Default | framework default | from `load_defaults` 7.1 onward | | Readers | humans, grep | tools that understand SQLCommenter | **SQLCommenter** is a published comment convention for attaching application context to SQL, which several database and tracing tools can parse. A Rails 8.1 app on `load_defaults 8.1` uses it; an older app still on `load_defaults` 7.0 or earlier keeps `:legacy` until it moves its defaults or sets the format explicitly. ## Placement and safety - The comment is **appended** by default. `config.active_record.query_log_tags_prepend_comment = true` puts it first, for tools that truncate long statements and would cut the tail. - Rails escapes comment delimiters inside values so a value cannot close the comment early, but the API docs still warn against putting untrusted user input into tags. ## Reading a tagged statement Given `/*action='create',application='GradesPortal',controller='grades'*/` at the end of a slow `UPDATE`, you know three things at once: it came from a web request, the request hit `GradesController#create`, and it is not the background recalculation job, whose statements would carry `job='RecalculateAveragesJob'` and no controller. Adding `:namespaced_controller` instead of `:controller` distinguishes `admin/grades` from `grades` when both exist. ## What turns it on - The `development.rb` that `rails new` writes sets `query_log_tags_enabled = true`, so the comment appears on development SQL lines. - `production.rb` does not; tracing slow production queries means adding it there deliberately. - Enabling it sets `ActiveRecord.disable_prepared_statements = true`, because a comment that varies by action makes statements unique; weigh that before turning it on in production.

  • Why does a query run by a job carry no controller or action tag?
    The controller and action tags read the controller instance Rails stores in the execution context during an action. Inside a job there is none, so both values are nil, and `QueryLogs` omits nil tags. The job tag reads the job instance instead, so it appears.
  • Your tags list :controller before :action, yet the comment shows action first. Is something misconfigured?
    No. `QueryLogs` sorts its tag handlers by key name when it builds them, so pairs always come out alphabetically, whatever order the configuration array uses.

  • Query log tags only change the Rails log; the database never sees them.
  • The legacy format is still the default in a new Rails 8.1 app.
  • A job's queries are tagged with the last controller and action that ran.
  • Tags appear in the comment in the order the config array lists them.
  • rails new enables query log tags in production.rb as well as development.rb.

Assumes Rails 8.1 with load_defaults 8.1: query_log_tags_format defaults to :sqlcommenter from load_defaults 7.1 on, while the framework default (load_defaults 7.0 and earlier) is :legacy.