Class: Tobias::Evaluations::WorkMem

Inherits:
Base
  • Object
show all
Defined in:
lib/tobias/evaluations/work_mem.rb

Instance Attribute Summary

Attributes inherited from Base

#container, #database, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize, #render_table, #run

Constructor Details

This class inherits a constructor from Tobias::Evaluations::Base

Instance Method Details

#current_work_memObject



10
11
12
# File 'lib/tobias/evaluations/work_mem.rb', line 10

def current_work_mem
  @current_work_mem ||= Tobias::WorkMem.from_sql(database.fetch("SHOW work_mem").first[:work_mem])
end

#descriptionObject



14
15
16
# File 'lib/tobias/evaluations/work_mem.rb', line 14

def description
  "Optimal work_mem settings"
end

#run_each(name, query) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tobias/evaluations/work_mem.rb', line 18

def run_each(name, query)
  database.run("CREATE EXTENSION IF NOT EXISTS pg_stat_statements")

  work_mems.each do |value|
    database.transaction do
      database.run("SET LOCAL work_mem = '#{value.to_sql}'")
      database.select(Sequel.function(:pg_stat_reset)).first
      container.run_query(query)

      stats = database[:pg_stat_database].
        where(datname: Sequel.function(:current_database)).
        first

      if stats[:temp_files] == 0 && stats[:temp_bytes] == 0
        return Result.new(name: name, value: value)
      end
    end
  end

  # Fallback to the highest work_mem setting if no results are found.
  Result.new(name: name, value: work_mems.last)
end

#to_markdown(results) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tobias/evaluations/work_mem.rb', line 41

def to_markdown(results)
  "    ## \#{description}\n\n    \#{render_table(headers: [\"Query\", \"Required work_mem\"], body: results.map { |r| [r.name, r.value.to_sql] })}\n\n    I see that your current `work_mem` setting is `\#{current_work_mem.to_sql}`.\n\n    Your application will need to run with at least `\#{results.max.value.to_sql}` of `work_mem`.\n\n    To apply my recommendations, run the following SQL:\n\n    ```sql\n    ALTER SYSTEM SET work_mem = '\#{results.max.value.to_sql}';\n    SELECT pg_reload_conf();\n    ```\n  MARKDOWN\nend\n"

#work_memsObject



6
7
8
# File 'lib/tobias/evaluations/work_mem.rb', line 6

def work_mems
  @work_mems ||= Tobias::WorkMem.valid_for(database)
end