debian-mirror-gitlab/lib/gitlab/database/migrations/observers/query_statistics.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

40 lines
1.1 KiB
Ruby
Raw Normal View History

2021-04-17 20:07:23 +05:30
# frozen_string_literal: true
module Gitlab
module Database
module Migrations
module Observers
# This observer gathers statistics from the pg_stat_statements extension.
# Notice that this extension is not installed by default. In case it cannot
# be found, the observer does nothing and doesn't throw an error.
class QueryStatistics < MigrationObserver
include Gitlab::Database::SchemaHelpers
def before
return unless enabled?
connection.execute('select pg_stat_statements_reset()')
end
2021-10-27 15:23:28 +05:30
def record
2021-04-17 20:07:23 +05:30
return unless enabled?
observation.query_statistics = connection.execute(<<~SQL)
SELECT query, calls, total_time, max_time, mean_time, rows
FROM pg_stat_statements
2022-07-16 23:28:13 +05:30
WHERE pg_get_userbyid(userid) = current_user
2021-04-17 20:07:23 +05:30
ORDER BY total_time DESC
SQL
end
private
def enabled?
function_exists?(:pg_stat_statements_reset) && connection.view_exists?(:pg_stat_statements)
end
end
end
end
end
end