Module: RubyPgExtras

Defined in:
lib/ruby-pg-extras.rb,
lib/ruby_pg_extras/version.rb,
lib/ruby_pg_extras/index_info.rb,
lib/ruby_pg_extras/table_info.rb,
lib/ruby_pg_extras/ignore_list.rb,
lib/ruby_pg_extras/size_parser.rb,
lib/ruby_pg_extras/diagnose_data.rb,
lib/ruby_pg_extras/diagnose_print.rb,
lib/ruby_pg_extras/detect_fk_column.rb,
lib/ruby_pg_extras/index_info_print.rb,
lib/ruby_pg_extras/table_info_print.rb,
lib/ruby_pg_extras/missing_fk_indexes.rb,
lib/ruby_pg_extras/missing_fk_constraints.rb

Defined Under Namespace

Classes: DetectFkColumn, DiagnoseData, DiagnosePrint, IgnoreList, IndexInfo, IndexInfoPrint, MissingFkConstraints, MissingFkIndexes, SizeParser, TableInfo, TableInfoPrint

Constant Summary collapse

NEW_PG_STAT_STATEMENTS =
"1.8"
PG_STAT_STATEMENTS_17 =
"1.11"
QUERIES =
i(
  add_extensions bloat blocking cache_hit db_settings
  calls extensions table_cache_hit tables index_cache_hit
  indexes index_size index_usage index_scans null_indexes locks all_locks
  long_running_queries mandelbrot outliers
  records_rank seq_scans table_index_scans table_indexes_size
  table_size total_index_size total_table_size
  unused_indexes duplicate_indexes vacuum_stats vacuum_progress vacuum_io_stats
  analyze_progress
  kill_all kill_pid
  pg_stat_statements_reset buffercache_stats
  buffercache_usage ssl_used connections
  table_schema table_schemas
  table_foreign_keys foreign_keys
)
DEFAULT_SCHEMA =
ENV["PG_EXTRAS_SCHEMA"] || "public"
REQUIRED_ARGS =
{
  table_schema: [:table_name],
  table_foreign_keys: [:table_name],
}
DEFAULT_ARGS =
Hash.new({}).merge({
  calls: { limit: 10 },
  calls_legacy: { limit: 10 },
  calls_17: { limit: 10 },
  long_running_queries: { threshold: "500 milliseconds" },
  locks: { limit: 20 },
  blocking: { limit: 20 },
  outliers: { limit: 10 },
  outliers_legacy: { limit: 10 },
  outliers_17: { limit: 10 },
  vacuum_progress: {},
  vacuum_progress_17: {},
  vacuum_io_stats: {},
  vacuum_io_stats_legacy: {},
  analyze_progress: {},
  buffercache_stats: { limit: 10 },
  buffercache_usage: { limit: 20 },
  unused_indexes: { max_scans: 50, schema: DEFAULT_SCHEMA },
  null_indexes: { min_relation_size_mb: 10 },
  index_usage: { schema: DEFAULT_SCHEMA },
  index_cache_hit: { schema: DEFAULT_SCHEMA },
  table_cache_hit: { schema: DEFAULT_SCHEMA },
  table_size: { schema: DEFAULT_SCHEMA },
  table_schemas: { schema: DEFAULT_SCHEMA },
  index_scans: { schema: DEFAULT_SCHEMA },
  cache_hit: { schema: DEFAULT_SCHEMA },
  seq_scans: { schema: DEFAULT_SCHEMA },
  table_index_scans: { schema: DEFAULT_SCHEMA },
  records_rank: { schema: DEFAULT_SCHEMA },
  tables: { schema: DEFAULT_SCHEMA },
  foreign_keys: { schema: DEFAULT_SCHEMA },
  kill_pid: { pid: 0 },
})
VERSION =
"5.6.15"
@@database_url =
nil

Class Method Summary collapse

Class Method Details

.connectionObject



245
246
247
# File 'lib/ruby-pg-extras.rb', line 245

def self.connection
  @_connection ||= PG.connect(database_url)
end

.database_urlObject



253
254
255
# File 'lib/ruby-pg-extras.rb', line 253

def self.database_url
  @@database_url || ENV["RUBY_PG_EXTRAS_DATABASE_URL"] || ENV.fetch("DATABASE_URL")
end

.database_url=(value) ⇒ Object



249
250
251
# File 'lib/ruby-pg-extras.rb', line 249

def self.database_url=(value)
  @@database_url = value
end

.description_for(query_name:) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/ruby-pg-extras.rb', line 227

def self.description_for(query_name:)
  first_line = File.open(
    sql_path_for(query_name: query_name)
  ) { |f| f.readline }

  first_line[/\/\*(.*?)\*\//m, 1].strip
end

.diagnose(in_format: :display_table) ⇒ Object



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

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

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

.display_result(result, title:, in_format:) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/ruby-pg-extras.rb', line 202

def self.display_result(result, title:, in_format:)
  case in_format
  when :array
    result.values
  when :hash
    result.to_a
  when :raw
    result
  when :display_table
    headings = if result.count > 0
        result[0].keys
      else
        ["No results"]
      end

    puts Terminal::Table.new(
      title: title,
      headings: headings,
      rows: (result.try(:values) || result.map(&:values)),
    )
  else
    raise "Invalid in_format option"
  end
end

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



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ruby-pg-extras.rb', line 166

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

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

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



198
199
200
# File 'lib/ruby-pg-extras.rb', line 198

def self.missing_fk_constraints(args: {}, in_format: :display_table)
  RubyPgExtras::MissingFkConstraints.call(args[:table_name], ignore_list: args[:ignore_list])
end

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



194
195
196
# File 'lib/ruby-pg-extras.rb', line 194

def self.missing_fk_indexes(args: {}, in_format: :display_table)
  RubyPgExtras::MissingFkIndexes.call(args[:table_name])
end

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



142
143
144
145
146
147
148
149
150
# File 'lib/ruby-pg-extras.rb', line 142

def self.run_query(query_name:, in_format:, args: {})
  run_query_base(
    query_name: query_name,
    conn: connection,
    exec_method: :exec,
    in_format: in_format,
    args: args,
  )
end

.run_query_base(query_name:, conn:, exec_method:, in_format:, args: {}) ⇒ Object



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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ruby-pg-extras.rb', line 90

def self.run_query_base(query_name:, conn:, exec_method:, in_format:, args: {})
  if i(calls outliers).include?(query_name)
    pg_stat_statements_ver = conn.send(exec_method, "select installed_version from pg_available_extensions where name='pg_stat_statements'")
      .to_a[0].fetch("installed_version", nil)
    if pg_stat_statements_ver != nil
      if Gem::Version.new(pg_stat_statements_ver) < Gem::Version.new(NEW_PG_STAT_STATEMENTS)
        query_name = "#{query_name}_legacy".to_sym
      elsif Gem::Version.new(pg_stat_statements_ver) >= Gem::Version.new(PG_STAT_STATEMENTS_17)
        query_name = "#{query_name}_17".to_sym
      end
    end
  end

  # vacuum_progress uses pg_stat_progress_vacuum only and does not depend on pg_stat_statements,
  # so we switch it based on the server_version_num instead of the pg_stat_statements version.
  if query_name == :vacuum_progress
    server_version_num = conn.send(exec_method, "SHOW server_version_num").to_a[0].values[0].to_i
    if server_version_num >= 170000
      query_name = :vacuum_progress_17
    end
  end

  # vacuum_io_stats relies on pg_stat_io which is available starting from PostgreSQL 16.
  # For older versions we fall back to vacuum_io_stats_legacy which just indicates
  # that this feature is not available on the current server.
  if query_name == :vacuum_io_stats
    server_version_num = conn.send(exec_method, "SHOW server_version_num").to_a[0].values[0].to_i
    if server_version_num < 160000
      query_name = :vacuum_io_stats_legacy
    end
  end

  REQUIRED_ARGS.fetch(query_name) { [] }.each do |arg_name|
    if args[arg_name].nil?
      raise ArgumentError, "'#{arg_name}' is required"
    end
  end

  sql = if (custom_args = DEFAULT_ARGS.fetch(query_name, {}).merge(args)) != {}
      sql_for(query_name: query_name) % custom_args
    else
      sql_for(query_name: query_name)
    end
  result = conn.send(exec_method, sql)

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

.sql_for(query_name:) ⇒ Object



235
236
237
238
239
# File 'lib/ruby-pg-extras.rb', line 235

def self.sql_for(query_name:)
  File.read(
    sql_path_for(query_name: query_name)
  )
end

.sql_path_for(query_name:) ⇒ Object



241
242
243
# File 'lib/ruby-pg-extras.rb', line 241

def self.sql_path_for(query_name:)
  File.join(File.dirname(__FILE__), "/ruby_pg_extras/queries/#{query_name}.sql")
end

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



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ruby-pg-extras.rb', line 180

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

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