Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Rails
  3. marginalia

marginalia

MITRuby

Attach comments to ActiveRecord SQL queries to trace their origin in Rails applications.

GitHubGitHub
1.8k stars148 forks0 contributors

What is marginalia?

Marginalia is a Ruby gem that attaches contextual comments to ActiveRecord SQL queries in Rails applications. It helps developers trace queries back to their source by appending information like application name, controller, and action directly into the SQL. This is particularly useful for identifying slow queries in logs and performance hotspots.

Target Audience

Rails developers and teams who need to debug database performance issues, trace query origins in production logs, or automate slow query analysis with tools like pt-query-digest.

Value Proposition

Developers choose Marginalia for its simplicity, configurability, and seamless integration with ActiveRecord. It provides a non-invasive way to add query tracing without modifying application logic, and it's battle-tested in production environments.

Overview

Attach comments to ActiveRecord's SQL queries

Use Cases

Best For

  • Identifying slow ActiveRecord queries in Rails application logs
  • Tracing SQL queries back to specific controllers and actions
  • Automating performance analysis with query digest tools
  • Debugging database performance hotspots in production
  • Adding custom metadata to SQL queries for auditing
  • Monitoring query origins in multi-tenant Rails applications

Not Ideal For

  • Rails 7+ applications, as query logging is built-in via ActiveRecord::QueryLogs
  • Applications heavily relying on prepared statements with dynamic metadata, due to potential resource exhaustion
  • Teams needing real-time query monitoring without log parsing, as Marginalia relies on post-query inspection

Pros & Cons

Pros

Lightweight Instrumentation

Automatically attaches comments to ActiveRecord queries without invasive code changes, as shown in the default setup adding controller and action names.

High Configurability

Allows reordering and adding custom comment components like line numbers or hostname, detailed in the customization section with module method examples.

Production Proven

Tested with MySQL, PostgreSQL, and SQLite, and used in production, ensuring reliability across various database environments.

Tool Integration

Designed to work with query digest tools like pt-query-digest, enabling automated identification of performance hotspots from logs.

Cons

Redundant in Modern Rails

Since Rails 7 includes ActiveRecord::QueryLogs, using Marginalia adds an unnecessary dependency for new projects, as noted in the README.

Prepared Statement Pitfalls

Using high-cardinality components like request_id can exhaust system resources by creating unique prepared statements, a caveat explicitly warned in the documentation.

Log-Dependent Value

Benefits are only realized through log parsing, which may not suit real-time debugging or applications where SQL logging is disabled for performance.

Frequently Asked Questions

Quick Stats

Stars1,768
Forks148
Contributors0
Open Issues5
Last commit14 days ago
CreatedSince 2012

Tags

#rails#query-logging#database-debugging#ruby-gem#activerecord#postgresql#performance-monitoring#sql-query#mysql#sqlite

Built With

M
MySQL
S
SQLite
R
Ruby
P
PostgreSQL
A
ActiveRecord

Included in

Ruby14.1kRails3.9k
Auto-fetched 12 hours ago

Related Projects

devisedevise

Flexible authentication solution for Rails with Warden.

Stars24,355
Forks5,473
Last commit2 months ago
spreespree

Open Source eCommerce Platform for B2B, Marketplace, and Enterprise. REST API, TypeScript SDK, and production-ready Next.js storefront. Self-host it. Own your stack. No vendor lock-in. Zero platform fees.

Stars15,672
Forks5,296
Last commit16 hours ago
activeadminactiveadmin

The administration framework for Ruby on Rails applications.

Stars9,708
Forks3,326
Last commit1 day ago
CarrierwaveCarrierwave

Classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks

Stars8,775
Forks1,648
Last commit1 day ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub