Class: PoolLint::Inspectors::PostgreSQL

Inherits:
Object
  • Object
show all
Defined in:
lib/poollint/inspectors/postgresql.rb

Defined Under Namespace

Classes: SettingValue, Snapshot

Constant Summary collapse

SPECIAL_SETTINGS =
%w[role session_authorization].freeze
SETTINGS_SQL =
<<~SQL
  WITH requested(name) AS (
    SELECT unnest(ARRAY[%<settings>s]::text[])
  )
  SELECT requested.name,
         current_setting(requested.name, true) AS setting,
         pg_settings.reset_val
    FROM requested
    LEFT JOIN pg_settings ON pg_settings.name = requested.name
   ORDER BY requested.name
SQL
ADVISORY_LOCKS_SQL =
<<~SQL
  SELECT database::text,
         classid::text,
         objid::text,
         objsubid,
         mode
    FROM pg_locks
   WHERE locktype = 'advisory'
     AND pid = pg_backend_pid()
     AND granted
   ORDER BY database, classid, objid, objsubid, mode
SQL

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ PostgreSQL

Returns a new instance of PostgreSQL.



35
36
37
38
39
# File 'lib/poollint/inspectors/postgresql.rb', line 35

def initialize(configuration)
  @configuration = configuration
  @allowed_settings = AllowedSettings.new(configuration.allowed_settings)
  @capture = PostgreSQLCapture.new(configuration)
end

Instance Method Details

#establish_baseline(connection, state) ⇒ Object



41
42
43
44
45
46
# File 'lib/poollint/inspectors/postgresql.rb', line 41

def establish_baseline(connection, state)
  snapshot = capture_with_timeout(connection, settings_for(state))
  state.capture_baseline(snapshot)
  ConnectionState.attach(connection, state)
  snapshot
end

#inspect(connection, state, inspection_point:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/poollint/inspectors/postgresql.rb', line 48

def inspect(connection, state, inspection_point:)
  baseline = state.baseline
  return establish_baseline(connection, state) unless baseline
  return unless state.dirty?

  snapshot = capture_with_timeout(connection, settings_for(state, baseline))
  unless state_attached?(connection, state)
    return reattach_after_reconnect(connection, state, snapshot)
  end

  report = build_report(baseline, snapshot, state, inspection_point)
  state.finish_inspection(
    snapshot: snapshot,
    rebaseline: report.leak? && @configuration.rebaseline_after_report
  )
  report if report.leak?
end