Class: Stats

Inherits:
Object show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/pg_statistics.rb

Overview

PostgreSQL Stats. Fetch information from pg_stat_* tables. Optionally run in a continuous loop, displaying deltas.

Constant Summary collapse

VERSION =
%q$Id$

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Stats

Returns a new instance of Stats.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/pg_statistics.rb', line 28

def initialize( opts )
	@opts = opts
	@db   = PG.connect(
		:dbname   => opts.database,
		:host     => opts.host,
		:port     => opts.port,
		:user     => opts.user,
		:password => opts.pass,
		:sslmode  => 'prefer'
	)
	@last = nil
end

Instance Method Details

#get_statsObject

Query the database for performance measurements. Returns a hash.



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
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/pg_statistics.rb', line 103

def get_stats
	res = @db.exec %Q{
		SELECT
			MAX(stat_db.xact_commit)       AS commits,
			MAX(stat_db.xact_rollback)     AS rollbks,
			MAX(stat_db.blks_read)         AS blksrd,
			MAX(stat_db.blks_hit)          AS blkshit,
			MAX(stat_db.numbackends)       AS bkends,
			SUM(stat_tables.seq_scan)      AS seqscan,
			SUM(stat_tables.seq_tup_read)  AS seqtprd,
			SUM(stat_tables.idx_scan)      AS idxscn,
			SUM(stat_tables.idx_tup_fetch) AS idxtrd,
			SUM(stat_tables.n_tup_ins)     AS ins,
			SUM(stat_tables.n_tup_upd)     AS upd,
			SUM(stat_tables.n_tup_del)     AS del,
			MAX(stat_locks.locks)          AS locks,
			MAX(activity.sess)             AS activeq
		FROM
			pg_stat_database    AS stat_db,
			pg_stat_user_tables AS stat_tables,
			(SELECT COUNT(*) AS locks FROM pg_locks ) AS stat_locks,
			(SELECT COUNT(*) AS sess FROM pg_stat_activity WHERE current_query <> '<IDLE>') AS activity
		WHERE
			stat_db.datname = '%s';
	} % [ @opts.database ]

	return res[0]
end

#runObject

Primary loop. Gather statistics and generate deltas.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/pg_statistics.rb', line 47

def run
	run_count = 0

	loop do
		current_stat = self.get_stats

		# First run, store and continue
		#
		if @last.nil?
			@last = current_stat
			sleep @opts.interval
			next
		end

		# headers
		#
		if run_count == 0 || run_count % 50 == 0
			puts "%-20s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s" % %w[
				time commits rollbks blksrd blkshit bkends seqscan
				seqtprd idxscn idxtrd ins upd del locks activeq
			]
		end

		# calculate deltas
		#
		delta = current_stat.inject({}) do |h, pair|
			stat, val = *pair

			if %w[ activeq locks bkends ].include?( stat )
				h[stat] = current_stat[stat].to_i
			else
				h[stat] = current_stat[stat].to_i - @last[stat].to_i
			end

			h
		end
		delta[ 'time' ] = Time.now.strftime('%F %T')

		# new values
		#
		puts "%-20s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s" % [
			delta['time'], delta['commits'], delta['rollbks'], delta['blksrd'],
			delta['blkshit'], delta['bkends'], delta['seqscan'],
			delta['seqtprd'], delta['idxscn'], delta['idxtrd'],
			delta['ins'], delta['upd'], delta['del'], delta['locks'], delta['activeq']
		]

		@last = current_stat
		run_count += 1
		sleep @opts.interval
	end
end