In 2022, we explored methods to log database query generation details from web applications using Dima, the contributor behind this improvement, noted that while the Marginalia :line configuration option was resource-intensive to enable in production, the recent enhancements have reduced the associated overhead significantly. This is a welcome development for developers seeking efficient logging methods.

When contemplating the implementation of line-level logging, its recommended to begin with local development environments. Developers can utilize environment variables to selectively enable this feature without activating it globally. For instance, for Marginalia, this can be configured as follows:

MARGINALIA_LINE_NUMBER_ENABLED=true

In the config/initializers/marginalia.rb file, the configuration would look like this:

Marginalia :: Comment . components = [ :application , :controller , :action ] if ENV [ 'MARGINALIA_LINE_NUMBER_ENABLED' ] Marginalia :: Comment . components . append ( :line ) end

For Query Logs, a similar configuration can be applied in the config/application.rb file, tailored for specific environments:

config . active_record . query_log_tags_enabled = true config . active_record . query_log_tags = [ :application , :controller , :action , :source_location ]

This configuration has been verified with Rails version 7.2.2, allowing for conditional activation of the :source_location option using environment variables. If your team has leveraged the :source_location in Query Logs, I would love to hear your experiences!

In conclusion, having the capability to log source code line-level data for aggregated query statistics is crucial for backend engineers addressing database performance issues. This detailed information enables them to pinpoint heavy queries, allowing for a thorough investigation into the source code for redesigning, refactoring, or even eliminating problematic queries.

Looking ahead, while the pg_stat_statements extension is integral to this workflow, there remain opportunities for enhancement. One notable challenge is the presence of duplicate or nearly duplicate entries, complicating the process of analysis. Fortunately, improvements are on the horizon! Stay tuned for a forthcoming post that will delve into advancements in newer versions of Postgres aimed at de-duplicating pg_stat_statements entries, along with strategies to accomplish this with Ruby on Rails even when using older Postgres versions.

If youd like to stay updated on these developments, consider subscribing to my newsletter, where I share occasional updates featuring blog posts, conference information, industry news, and more! Thank you for taking the time to read this article.