Class: Tobias::WorkMem

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount) ⇒ WorkMem

Returns a new instance of WorkMem.



22
23
24
# File 'lib/tobias/work_mem.rb', line 22

def initialize(amount)
  @amount = amount
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



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

def amount
  @amount
end

Class Method Details

.allObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/tobias/work_mem.rb', line 66

def self.all
  [
    new(64.kilobytes),
    new(128.kilobytes),
    new(256.kilobytes),
    new(512.kilobytes),
    new(1.megabyte),
    new(4.megabytes),
    new(8.megabytes),
    new(16.megabytes),
    new(32.megabytes),
    new(64.megabytes),
    new(128.megabytes),
    new(256.megabytes),
    new(512.megabytes),
    new(1.gigabyte),
    new(2.gigabytes),
    new(4.gigabytes),
    new(8.gigabytes),
  ].sort_by(&:amount)
end

.from_sql(sql) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tobias/work_mem.rb', line 7

def self.from_sql(sql)
  case sql
  when /^\d+B$/
    new(sql.to_i)
  when /^\d+kB$/
    new(sql.to_i * 1024)
  when /^\d+MB$/
    new(sql.to_i * 1024 * 1024)
  when /^\d+GB$/
    new(sql.to_i * 1024 * 1024 * 1024)
  else
    raise "Invalid work_mem setting: #{sql}"
  end
end

.valid_for(database) ⇒ Object

Inspects the database to determine the valid work_mem settings for the current user. We’ll need at least connection_limit / effective_cache_size for an optimal work_mem setting, otherwise a user could run out of memory at max connections.



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
# File 'lib/tobias/work_mem.rb', line 91

def self.valid_for(database)
  role_conn_limit = database.select(:rolconnlimit).
    from(:pg_roles).
    where(rolname: Sequel.lit("current_user")).
    first

  max_connections = database.select(:setting).
    from(:pg_settings).
    where(name: "max_connections").
    first

  effective_cache_size = database.select(:setting, :unit).
    from(:pg_settings).
    where(name: "effective_cache_size").
    first

  effective_cache_size_bytes = effective_cache_size[:setting].to_i * 8 * 1024

  connection_limit = if role_conn_limit[:rolconnlimit] > 0
    role_conn_limit[:rolconnlimit]
  else
    max_connections[:setting].to_i
  end

  bytes_per_connection = effective_cache_size_bytes / connection_limit

  self.all.select { |work_mem| work_mem.amount < bytes_per_connection.to_i }
end

Instance Method Details

#<(other) ⇒ Object



30
31
32
# File 'lib/tobias/work_mem.rb', line 30

def <(other)
  @amount < other.amount
end

#<=(other) ⇒ Object



38
39
40
# File 'lib/tobias/work_mem.rb', line 38

def <=(other)
  @amount <= other.amount
end

#<=>(other) ⇒ Object



42
43
44
# File 'lib/tobias/work_mem.rb', line 42

def <=>(other)
  @amount <=> other.amount
end

#>(other) ⇒ Object



26
27
28
# File 'lib/tobias/work_mem.rb', line 26

def >(other)
  @amount > other.amount
end

#>=(other) ⇒ Object



34
35
36
# File 'lib/tobias/work_mem.rb', line 34

def >=(other)
  @amount >= other.amount
end

#inspectObject



62
63
64
# File 'lib/tobias/work_mem.rb', line 62

def inspect
  to_sql
end

#to_sqlObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tobias/work_mem.rb', line 46

def to_sql
  case @amount
  when 0...1024
    "#{@amount}B"
  when 1024...1048576
    kb = @amount / 1024.0
    kb == kb.to_i ? "#{kb.to_i}kB" : "#{kb}kB"
  when 1048576...1073741824
    mb = @amount / 1048576.0
    mb == mb.to_i ? "#{mb.to_i}MB" : "#{mb}MB"
  else
    gb = @amount / 1073741824.0
    gb == gb.to_i ? "#{gb.to_i}GB" : "#{gb}GB"
  end
end