Commit 3777ccf2 authored by Stan Hu's avatar Stan Hu

Prepend the Marginalia comment in SQL query

By default, PostgreSQL only records the first 1024 characters of a SQL
query. This means for long queries, we often lose the source of the
query, which makes it difficult to track down expensive queries, such as
one described in https://gitlab.com/gitlab-org/gitlab/-/issues/292047.

Increasing the `track_activity_query_size` in PostgreSQL will help, but
this requires a reboot of the database. To avoid needing to depend on
https://gitlab.com/gitlab-com/gl-infra/infrastructure/-/issues/12314, we
prepend the comment as described in
https://github.com/basecamp/marginalia#prepend-comments.

We disable this for tests because a number of tests expect raw SQL to
start the string.
parent 5fc11a16
---
title: Prepend the Marginalia comment in SQL query
merge_request: 54015
author:
type: changed
......@@ -9,6 +9,11 @@ require 'marginalia'
# Refer: https://github.com/basecamp/marginalia/blob/v1.8.0/lib/marginalia/railtie.rb#L67
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(Gitlab::Marginalia::ActiveRecordInstrumentation)
# By default, PostgreSQL only tracks the first 1024 bytes of a SQL
# query. Prepending the comment allows us to trace the source of the
# query without having to increase the `track_activity_query_size`
# parameter.
Marginalia::Comment.prepend_comment = true unless Rails.env.test? # Some tests do string matching against raw SQL
Marginalia::Comment.components = [:application, :controller, :action, :correlation_id, :jid, :job_class]
# As mentioned in https://github.com/basecamp/marginalia/pull/93/files,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment