Skip to content

SYSTEM Cited by 1 source

Marginalia gem

What it is

Marginalia is a Ruby gem originally published by Basecamp that instruments ActiveRecord to append a trailing SQL comment carrying structured metadata (controller, action, hostname, pid, etc.) to every query emitted during a Rails request. It is the canonical pre-Rails-7 way to add per-query source-attribution tags in a Rails application.

Why it matters here

Marginalia is the pre-history of Rails 7's native query_log_tags feature. The approach — attach a comment to every ActiveRecord query with a structured payload of request-scoped metadata — is the same; the key change in Rails 7 is that the framework absorbed the functionality so the external gem is no longer required for the default case.

The emission format Marginalia used is similar to Rails 7's default: /*application:Api,controller:users,action:show*/not SQLCommenter format, which uses key='value' with single-quoted values. Tools that parse SQLCommenter natively can't consume Marginalia-tagged queries; consumers historically had their own parsers for the Marginalia format.

Seen in

  • sources/2026-04-21-planetscale-identifying-slow-rails-queries-with-sqlcommenter — named verbatim as the pre-history of Rails 7 query-log-tags: "First, we had the Marginalia gem, which adds comments to all your queries. This allows you to see which controller or job a query came from by reading your logs." Establishes Marginalia as the canonical Rails-6-and-earlier solution that Rails 7 absorbed, and frames the 2022-06-29 post's SQLCommenter-format migration argument as "it's a small change that makes our query comments machine-readable and more valuable in logging and performance monitoring tools."

Scope

  • Basecamp (Ruby-on-Rails creator DHH's company) maintains the canonical Marginalia gem; various forks exist with additional tag types.
  • Supersedence: Rails 7+ ships query_log_tags natively. New Rails 7+ apps do not need Marginalia for the default per-controller/per-action/per-job tagging use case. Existing Rails-6-and-earlier apps can continue using Marginalia without migration.
  • SQLCommenter-format emission requires an additional shim — Marginalia's own format is Rails-convention, not SQLCommenter. New PlanetScale-authored activerecord-sql_commenter gem bridges that gap on top of Rails 7's query_log_tags.
Last updated · 378 distilled / 1,213 read