Proposal: Add ability to filter queries with QueryLogs

I would like to introduce a new QueryLogs ‘feature’ to avoid commenting some specific queries.

The need I have that may probably be shares is to stop commenting PG utility commands such as:

  • COMMIT
  • BEGIN
  • ROLLBACK
  • SET...

Our issue is that with the use of pg_stats_statements with the comment we are “polluted” with a lot of BEGIN, COMMIT … queries that PG is unable to gather due to the comment that are different on those queries.

pg_stats_statements has a fixed size and PG has to perform a lot of eviction that consumes DB CPU and that is problematic for us as we need to optimize at most the usage of our DB.

I thought that there could be other use case for that. I would like to introduce a setting, for example ActiveRecord::QueryLogs.filtered_queries = %w[COMMIT BEGIN ROLLBACK SET] that would allow that

def call(sql, connection) # :nodoc:
  return sql if query_to_filter?(sql)
  comment = self.comment(connection)

  if comment.blank?
    sql
  elsif prepend_comment
    "#{comment} #{sql}"
  else
    "#{sql} #{comment}"
  end
end

I’m currently working locally on the implementation. First contribution on my part I’m unsure on how I should process to submit this.

I already have a tiny patch on my side but I would rather upstream the change

module QueryLogsExtension
  DONT_ANOTATE_SQL = %w[BEGIN COMMIT ROLLBACK].freeze
  def call(sql, connection)
    return sql if DONT_ANOTATE_SQL.include?(sql) || sql.start_with?('SET')
    super
  end
end

ActiveRecord::QueryLogs.singleton_class.prepend(QueryLogsExtension)

Many thanks