Welcome to mirror list, hosted at ThFree Co, Russian Federation.

query_statistics.rb « observers « migrations « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2d026f0c8d233f2a98c742cc0f8a44dea5e6cfd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

          def record
            return unless enabled?

            observation.query_statistics = connection.execute(<<~SQL)
              SELECT query, calls, total_time, max_time, mean_time, rows
              FROM pg_stat_statements
              WHERE pg_get_userbyid(userid) = current_user
              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