Module: RailsPgExtras

Defined in:
lib/rails-pg-extras.rb,
lib/rails_pg_extras/web.rb,
lib/rails_pg_extras/version.rb,
lib/rails_pg_extras/index_info.rb,
lib/rails_pg_extras/table_info.rb,
lib/rails_pg_extras/configuration.rb,
lib/rails_pg_extras/diagnose_data.rb,
lib/rails_pg_extras/diagnose_print.rb,
lib/rails_pg_extras/index_info_print.rb,
lib/rails_pg_extras/table_info_print.rb

Defined Under Namespace

Modules: Web Classes: Configuration, DiagnoseData, DiagnosePrint, IndexInfo, IndexInfoPrint, Railtie, TableInfo, TableInfoPrint

Constant Summary collapse

QUERIES =
RubyPgExtras::QUERIES
DEFAULT_ARGS =
RubyPgExtras::DEFAULT_ARGS
NEW_PG_STAT_STATEMENTS =
RubyPgExtras::NEW_PG_STAT_STATEMENTS
PG_STAT_STATEMENTS_17 =
RubyPgExtras::PG_STAT_STATEMENTS_17
VERSION =
"5.5.1"

Class Method Summary collapse

Class Method Details

.configurationObject



22
23
24
# File 'lib/rails_pg_extras/configuration.rb', line 22

def self.configuration
  @configuration ||= Configuration.new(Configuration::DEFAULT_CONFIG)
end

.configure {|configuration| ... } ⇒ Object

Yields:



26
27
28
# File 'lib/rails_pg_extras/configuration.rb', line 26

def self.configure
  yield(configuration)
end

.connectionObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rails-pg-extras.rb', line 153

def self.connection
  if (db_url = ENV["RAILS_PG_EXTRAS_DATABASE_URL"])
    connector = ActiveRecord::Base.establish_connection(db_url)

    if connector.respond_to?(:connection)
      connector.connection
    elsif connector.respond_to?(:lease_connection)
      connector.lease_connection
    else
      raise "Unsupported connector: #{connector.class}"
    end
  else
    ActiveRecord::Base.connection
  end
end

.diagnose(in_format: :display_table) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rails-pg-extras.rb', line 58

def self.diagnose(in_format: :display_table)
  data = RailsPgExtras::DiagnoseData.call

  if in_format == :display_table
    RailsPgExtras::DiagnosePrint.call(data)
  elsif in_format == :hash
    data
  else
    raise "Invalid 'in_format' argument!"
  end
end

.index_info(args: {}, in_format: :display_table) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rails-pg-extras.rb', line 125

def self.index_info(args: {}, in_format: :display_table)
  data = RailsPgExtras::IndexInfo.call(args[:table_name])

  if in_format == :display_table
    RailsPgExtras::IndexInfoPrint.call(data)
  elsif in_format == :hash
    data
  elsif in_format == :array
    data.map(&:values)
  else
    raise "Invalid 'in_format' argument!"
  end
end

.measure_duration(&block) ⇒ Object



70
71
72
73
74
75
# File 'lib/rails-pg-extras.rb', line 70

def self.measure_duration(&block)
  starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  block.call
  ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  (ending - starting) * 1000
end

.measure_queries(&block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rails-pg-extras.rb', line 77

def self.measure_queries(&block)
  queries = {}
  sql_duration = 0

  method_name = if ActiveSupport::Notifications.respond_to?(:monotonic_subscribe)
      :monotonic_subscribe
    else
      :subscribe
    end

  subscriber = ActiveSupport::Notifications.public_send(method_name, "sql.active_record") do |_name, start, finish, _id, payload|
    unless payload[:name] =~ /SCHEMA/
      key = payload[:sql]
      queries[key] ||= { count: 0, total_duration: 0, min_duration: nil, max_duration: nil }
      queries[key][:count] += 1
      duration = (finish - start) * 1000
      queries[key][:total_duration] += duration
      sql_duration += duration

      if queries[key][:min_duration] == nil || queries[key][:min_duration] > duration
        queries[key][:min_duration] = duration.round(2)
      end

      if queries[key][:max_duration] == nil || queries[key][:max_duration] < duration
        queries[key][:max_duration] = duration.round(2)
      end
    end
  end

  total_duration = measure_duration do
    block.call
  end

  queries = queries.reduce({}) do |agg, val|
    val[1][:avg_duration] = (val[1][:total_duration] / val[1][:count]).round(2)
    val[1][:total_duration] = val[1][:total_duration].round(2)
    agg.merge(val[0] => val[1])
  end

  ActiveSupport::Notifications.unsubscribe(subscriber)
  {
    count: queries.reduce(0) { |agg, val| agg + val[1].fetch(:count) },
    queries: queries,
    total_duration: total_duration.round(2),
    sql_duration: sql_duration.round(2),
  }
end

.run_query(query_name:, in_format:, args: {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rails-pg-extras.rb', line 28

def self.run_query(query_name:, in_format:, args: {})
  if %i(calls outliers).include?(query_name)
    pg_stat_statements_version_sql = "SELECT installed_version
                                      FROM pg_available_extensions
                                      WHERE name = 'pg_stat_statements'"
    if (version = RailsPgExtras.connection.execute(pg_stat_statements_version_sql)
      .to_a[0].fetch("installed_version", nil))
      if Gem::Version.new(version) < Gem::Version.new(NEW_PG_STAT_STATEMENTS)
        query_name = "#{query_name}_legacy".to_sym
      elsif Gem::Version.new(version) >= Gem::Version.new(PG_STAT_STATEMENTS_17)
        query_name = "#{query_name}_17".to_sym
      end
    end
  end

  sql = if (custom_args = DEFAULT_ARGS[query_name].merge(args)) != {}
      RubyPgExtras.sql_for(query_name: query_name) % custom_args
    else
      RubyPgExtras.sql_for(query_name: query_name)
    end

  result = connection.execute(sql)

  RubyPgExtras.display_result(
    result,
    title: RubyPgExtras.description_for(query_name: query_name),
    in_format: in_format,
  )
end

.table_info(args: {}, in_format: :display_table) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rails-pg-extras.rb', line 139

def self.table_info(args: {}, in_format: :display_table)
  data = RailsPgExtras::TableInfo.call(args[:table_name])

  if in_format == :display_table
    RailsPgExtras::TableInfoPrint.call(data)
  elsif in_format == :hash
    data
  elsif in_format == :array
    data.map(&:values)
  else
    raise "Invalid 'in_format' argument!"
  end
end