Class: PostgreStats

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, logger = nil) ⇒ PostgreStats

Returns a new instance of PostgreStats.



9
10
11
12
13
14
15
16
17
# File 'lib/postgrestats.rb', line 9

def initialize(config, logger = nil)
  @config = config
  
  initialize_log unless logger
  @log = logger if logger
  @log.error("Logging started")
  
  initialize_metrics
end

Instance Attribute Details

#aggregate_countersObject

Returns the value of attribute aggregate_counters.



7
8
9
# File 'lib/postgrestats.rb', line 7

def aggregate_counters
  @aggregate_counters
end

#aggregate_gaugesObject

Returns the value of attribute aggregate_gauges.



7
8
9
# File 'lib/postgrestats.rb', line 7

def aggregate_gauges
  @aggregate_gauges
end

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/postgrestats.rb', line 6

def config
  @config
end

#connectionObject

Returns the value of attribute connection.



6
7
8
# File 'lib/postgrestats.rb', line 6

def connection
  @connection
end

#dbObject

Returns the value of attribute db.



6
7
8
# File 'lib/postgrestats.rb', line 6

def db
  @db
end

#logObject

Returns the value of attribute log.



6
7
8
# File 'lib/postgrestats.rb', line 6

def log
  @log
end

#table_countersObject

Returns the value of attribute table_counters.



7
8
9
# File 'lib/postgrestats.rb', line 7

def table_counters
  @table_counters
end

#table_gaugesObject

Returns the value of attribute table_gauges.



7
8
9
# File 'lib/postgrestats.rb', line 7

def table_gauges
  @table_gauges
end

Instance Method Details

#connect(db) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/postgrestats.rb', line 182

def connect(db)
  return if connection and (@db == db)
  
  connection.close if @db == db
  
  tmp_config ={
    'host' => @config['host'],
    'user' => @config['user'],
    'password' => @config['password'],
    'dbname' => db
  }
  
  @connection = PG.connect(tmp_config)
  @db = db
end

#get_aggregate_ganglia_doubleObject



229
230
231
232
233
234
# File 'lib/postgrestats.rb', line 229

def get_aggregate_ganglia_double()
  return [
    aggregate_gauges.select{ |k, v| v.has_key?('ganglia_double') }.keys,
    aggregate_counters.select{ |k, v| v.has_key?('ganglia_double') }.keys
  ].flatten
end

#get_aggregate_metricsObject



218
219
220
# File 'lib/postgrestats.rb', line 218

def get_aggregate_metrics()
  return [aggregate_gauges.keys, aggregate_counters.keys].flatten
end

#get_blocks_fetched_per_database(db) ⇒ Object

  • number of blocks fetched per database



392
393
394
395
396
397
398
399
400
401
402
# File 'lib/postgrestats.rb', line 392

def get_blocks_fetched_per_database(db)
  query = <<-END_GET_BLOCKS_FETCHED_PER_DATABASE_QUERY
    SELECT pg_stat_get_db_blocks_fetched(oid)
    FROM pg_database
    WHERE datname = '#{db}'
  END_GET_BLOCKS_FETCHED_PER_DATABASE_QUERY
  
  ret = run_query(db, query)
  
  return ret.first['pg_stat_get_db_blocks_fetched'].to_i
end

#get_blocks_hit_per_database(db) ⇒ Object

  • number of blocks hit per database



406
407
408
409
410
411
412
413
414
415
416
# File 'lib/postgrestats.rb', line 406

def get_blocks_hit_per_database(db)
  query = <<-END_GET_BLOCK_HITS_PER_DATABASE_QUERY
    SELECT pg_stat_get_db_blocks_hit(oid)
    FROM pg_database
    WHERE datname = '#{db}'
  END_GET_BLOCK_HITS_PER_DATABASE_QUERY
  
  ret = run_query(db, query)
  
  return ret.first['pg_stat_get_db_blocks_hit'].to_i
end

#get_commits_per_database(db) ⇒ Object

  • number of commits per database



420
421
422
423
424
425
426
427
428
429
430
# File 'lib/postgrestats.rb', line 420

def get_commits_per_database(db)
  query = <<-END_GET_COMMITS_PER_DATABASE_QUERY
    SELECT pg_stat_get_db_xact_commit(oid)
    FROM pg_database
    WHERE datname = '#{db}'
  END_GET_COMMITS_PER_DATABASE_QUERY
  
  ret = run_query(db, query)
  
  return ret.first['pg_stat_get_db_xact_commit'].to_i
end

#get_connections_per_database(db = nil) ⇒ Object

  • number of connections



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/postgrestats.rb', line 346

def get_connections_per_database(db = nil)
  query = <<-END_GET_CONNECTIONS_PER_DATABASE_QUERY
    SELECT datname, count(*) FROM pg_stat_activity GROUP BY datname;
  END_GET_CONNECTIONS_PER_DATABASE_QUERY
  
  ret = run_query('postgres', query)
  
  ret.select!{ |v| not @config['exclude_dbs'].include?(v['datname']) }.map!{ |v| [v['datname'], v['count'].to_i] }
  
  if db
    res = Hash[*ret.flatten][db].to_i || 0
  else
    res = Hash[*ret.flatten]
  end
  
  return res
end

#get_databasesObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/postgrestats.rb', line 236

def get_databases()
  query = <<-END_GET_DATABASES_QUERY
    SELECT pg_database.datname AS "Database", pg_user.usename AS "Owner"
    FROM pg_database, pg_user
    WHERE pg_database.datdba = pg_user.usesysid
    UNION
    SELECT pg_database.datname AS "Database", NULL AS "Owner"
    FROM pg_database
    WHERE pg_database.datdba NOT IN (SELECT usesysid FROM pg_user)
    ORDER BY "Database"
  END_GET_DATABASES_QUERY
  
  ret = run_query('postgres', query)
  
  ret.select!{ |v| not @config['exclude_dbs'].include?(v['Database']) }.map!{ |v| v['Database'] }
  
  return ret
end

#get_dead_per_table(db) ⇒ Object

  • number of dead rows per table



620
621
622
623
624
625
626
627
628
629
630
631
632
633
# File 'lib/postgrestats.rb', line 620

def get_dead_per_table(db)
  query = <<-END_GET_DEAD_PER_TABLE_QUERY
    SELECT c.relname, pg_stat_get_dead_tuples(c.oid) FROM pg_catalog.pg_class c
    LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
    AND pg_catalog.pg_table_is_visible(c.oid)
  END_GET_DEAD_PER_TABLE_QUERY

  ret = run_query(db, query)

  ret.map!{ |v| [v['relname'], v['pg_stat_get_dead_tuples'].to_i] }

  return Hash[*ret.flatten]
end

#get_deletes_per_database(db) ⇒ Object

  • number of rows deleted per database



479
480
481
482
483
484
485
486
487
488
489
# File 'lib/postgrestats.rb', line 479

def get_deletes_per_database(db)
  query = <<-END_GET_DELETES_PER_DATABASE_QUERY
    SELECT pg_stat_get_db_tuples_deleted(oid)
    FROM pg_database
    WHERE datname = '#{db}'
  END_GET_DELETES_PER_DATABASE_QUERY
  
  ret = run_query(db, query)
  
  return ret.first['pg_stat_get_db_tuples_deleted'].to_i
end

#get_deletes_per_table(db) ⇒ Object

  • number of deleted rows per table



603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/postgrestats.rb', line 603

def get_deletes_per_table(db)
   query = <<-END_GET_DELETES_PER_TABLE_QUERY
     SELECT c.relname, pg_stat_get_tuples_deleted(c.oid) FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
     WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
     AND pg_catalog.pg_table_is_visible(c.oid)
   END_GET_DELETES_PER_TABLE_QUERY

   ret = run_query(db, query)

   ret.map!{ |v| [v['relname'], v['pg_stat_get_tuples_deleted'].to_i] }

   return Hash[*ret.flatten]
end

#get_fetched_per_table(db) ⇒ Object

  • number of fetched rows per table



537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/postgrestats.rb', line 537

def get_fetched_per_table(db)
   query = <<-END_GET_FETCHED_PER_TABLE_QUERY
     SELECT c.relname, pg_stat_get_tuples_fetched(c.oid) FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
     WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
     AND pg_catalog.pg_table_is_visible(c.oid)
   END_GET_FETCHED_PER_TABLE_QUERY

   ret = run_query(db, query)

   ret.map!{ |v| [v['relname'], v['pg_stat_get_tuples_fetched'].to_i] }

   return Hash[*ret.flatten]
end

#get_heap_blocks_hit_per_table(db) ⇒ Object



705
706
707
708
709
710
711
712
713
714
715
716
717
# File 'lib/postgrestats.rb', line 705

def get_heap_blocks_hit_per_table(db)
  query = <<-END_GET_HEAP_BLOCKS_HIT_PER_TABLE_QUERY
    SELECT stat.relname AS relname, heap_blks_hit
    FROM pg_stat_user_tables stat
    RIGHT JOIN pg_statio_user_tables statio ON stat.relid=statio.relid;
  END_GET_HEAP_BLOCKS_HIT_PER_TABLE_QUERY

  ret = run_query(db, query)

  ret.map!{ |v| [v['relname'], v['heap_blks_hit'].to_i] }

  return Hash[*ret.flatten]
end

#get_heap_blocks_read_per_table(db) ⇒ Object



691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/postgrestats.rb', line 691

def get_heap_blocks_read_per_table(db)
  query = <<-END_GET_HEAP_BLOCKS_READ_PER_TABLE_QUERY
    SELECT stat.relname AS relname, heap_blks_read
    FROM pg_stat_user_tables stat
    RIGHT JOIN pg_statio_user_tables statio ON stat.relid=statio.relid;
  END_GET_HEAP_BLOCKS_READ_PER_TABLE_QUERY

  ret = run_query(db, query)

  ret.map!{ |v| [v['relname'], v['heap_blks_read'].to_i] }

  return Hash[*ret.flatten]
end

#get_hot_updates_per_table(db) ⇒ Object



586
587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/postgrestats.rb', line 586

def get_hot_updates_per_table(db)
   query = <<-END_GET_HOT_UPDATES_PER_TABLE_QUERY
     SELECT c.relname, pg_stat_get_tuples_hot_updated(c.oid) FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
     WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
     AND pg_catalog.pg_table_is_visible(c.oid)
   END_GET_HOT_UPDATES_PER_TABLE_QUERY

   ret = run_query(db, query)

   ret.map!{ |v| [v['relname'], v['pg_stat_get_tuples_hot_updated'].to_i] }

   return Hash[*ret.flatten]
end

#get_index_blocks_hit_per_table(db) ⇒ Object



733
734
735
736
737
738
739
740
741
742
743
744
745
# File 'lib/postgrestats.rb', line 733

def get_index_blocks_hit_per_table(db)
  query = <<-END_GET_INDEX_BLOCKS_HIT_PER_TABLE_QUERY
    SELECT stat.relname AS relname, idx_blks_hit
    FROM pg_stat_user_tables stat
    RIGHT JOIN pg_statio_user_tables statio ON stat.relid=statio.relid;
  END_GET_INDEX_BLOCKS_HIT_PER_TABLE_QUERY

  ret = run_query(db, query)

  ret.map!{ |v| [v['relname'], v['idx_blks_hit'].to_i] }

  return Hash[*ret.flatten]
end

#get_index_blocks_read_per_table(db) ⇒ Object



719
720
721
722
723
724
725
726
727
728
729
730
731
# File 'lib/postgrestats.rb', line 719

def get_index_blocks_read_per_table(db)
  query = <<-END_GET_INDEX_BLOCKS_READ_PER_TABLE_QUERY
    SELECT stat.relname AS relname, idx_blks_read
    FROM pg_stat_user_tables stat
    RIGHT JOIN pg_statio_user_tables statio ON stat.relid=statio.relid;
  END_GET_INDEX_BLOCKS_READ_PER_TABLE_QUERY

  ret = run_query(db, query)

  ret.map!{ |v| [v['relname'], v['idx_blks_read'].to_i] }

  return Hash[*ret.flatten]
end

#get_index_rows_fetched_per_table(db) ⇒ Object



677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'lib/postgrestats.rb', line 677

def get_index_rows_fetched_per_table(db)
  query = <<-END_GET_INDEX_ROWS_FETCHED_PER_TABLE_QUERY
    SELECT stat.relname AS relname, idx_tup_fetch
    FROM pg_stat_user_tables stat
    RIGHT JOIN pg_statio_user_tables statio ON stat.relid=statio.relid;
  END_GET_INDEX_ROWS_FETCHED_PER_TABLE_QUERY

  ret = run_query(db, query)

  ret.map!{ |v| [v['relname'], v['idx_tup_fetch'].to_i] }

  return Hash[*ret.flatten]
end

#get_index_scans_per_table(db) ⇒ Object



663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/postgrestats.rb', line 663

def get_index_scans_per_table(db)
  query = <<-END_GET_INDEX_SCANS_PER_TABLE_QUERY
    SELECT stat.relname AS relname, idx_scan
    FROM pg_stat_user_tables stat
    RIGHT JOIN pg_statio_user_tables statio ON stat.relid=statio.relid;
  END_GET_INDEX_SCANS_PER_TABLE_QUERY

  ret = run_query(db, query)

  ret.map!{ |v| [v['relname'], v['idx_scan'].to_i] }

  return Hash[*ret.flatten]
end

#get_index_sizes(db) ⇒ Object

  • size of indexes



286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/postgrestats.rb', line 286

def get_index_sizes(db)
  query = <<-END_GET_INDEX_SIZES_QUERY
    SELECT table_name,(pg_total_relation_size(table_name) - pg_relation_size(table_name)) AS size
    FROM information_schema.tables
    WHERE table_schema NOT IN ('information_schema', 'pg_catalog') AND table_type = 'BASE TABLE'
  END_GET_INDEX_SIZES_QUERY
  
  ret = run_query(db, query)
  
  ret.map!{ |v| [v['table_name'], v['size'].to_i] }
  
  return Hash[*ret.flatten]
end

#get_inserts_per_database(db) ⇒ Object

  • number of disk block reads per database

  • number of buffer hits per database

  • number of rows returned per database

  • number of rows inserted per database



451
452
453
454
455
456
457
458
459
460
461
# File 'lib/postgrestats.rb', line 451

def get_inserts_per_database(db)
  query = <<-END_GET_INSERTS_PER_DATABASE_QUERY
    SELECT pg_stat_get_db_tuples_inserted(oid)
    FROM pg_database
    WHERE datname = '#{db}'
  END_GET_INSERTS_PER_DATABASE_QUERY
  
  ret = run_query(db, query)
  
  return ret.first['pg_stat_get_db_tuples_inserted'].to_i
end

#get_inserts_per_table(db) ⇒ Object

  • number of inserted rows per table



554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/postgrestats.rb', line 554

def get_inserts_per_table(db)
   query = <<-END_GET_INSERTS_PER_TABLE_QUERY
     SELECT c.relname, pg_stat_get_tuples_inserted(c.oid) FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
     WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
     AND pg_catalog.pg_table_is_visible(c.oid)
   END_GET_INSERTS_PER_TABLE_QUERY

   ret = run_query(db, query)

   ret.map!{ |v| [v['relname'], v['pg_stat_get_tuples_inserted'].to_i] }

   return Hash[*ret.flatten]
end

#get_last_autovacuum_per_table(db) ⇒ Object

  • dead unused space taken up in a table or index

  • number of seconds since the last checkpoint per database

  • number of seconds since the last autovacuum per database/table



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/postgrestats.rb', line 368

def get_last_autovacuum_per_table(db)
  query = <<-END_GET_LAST_AUTOVACUUM_PER_DATABASE_QUERY
    SELECT current_database() AS datname, nspname AS sname, relname AS tname,
      CASE WHEN v IS NULL THEN -1 ELSE round(extract(epoch FROM now()-v)) END AS ltime
    FROM (
      SELECT nspname, relname, pg_stat_get_last_autovacuum_time(c.oid) AS v
      FROM pg_class c, pg_namespace n
      WHERE relkind = 'r'
      AND n.oid = c.relnamespace
      AND n.nspname <> 'information_schema'
      AND n.nspname = 'public'
      ORDER BY 3
     ) AS foo
  END_GET_LAST_AUTOVACUUM_PER_DATABASE_QUERY
  
  ret = run_query(db, query)
  
  ret.map!{ |v| [v['tname'], v['ltime']] }
  
  return Hash[*ret.flatten]
end

#get_locks_per_database(db) ⇒ Object

  • list of locks



496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/postgrestats.rb', line 496

def get_locks_per_database(db)
  query = <<-END_GET_LOCKS_PER_DATABASE_QUERY
    SELECT pid, virtualxid, datname, relname, locktype, mode
    FROM pg_locks l 
    LEFT JOIN pg_database d ON (d.oid=l.database)
    LEFT JOIN pg_class c on (c.oid=l.relation)
    WHERE datname = '#{db}' AND NOT relname ~ 'pg_'
  END_GET_LOCKS_PER_DATABASE_QUERY
  
  ret = run_query(db, query)
  
  return ret
end

#get_number_of_locks_per_database(db) ⇒ Object

  • number of locks



511
512
513
# File 'lib/postgrestats.rb', line 511

def get_number_of_locks_per_database(db)
  return get_locks_per_database(db).size
end

#get_returned_per_table(db) ⇒ Object

  • number of returned rows per table



520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/postgrestats.rb', line 520

def get_returned_per_table(db)
   query = <<-END_GET_RETURNED_PER_TABLE_QUERY
     SELECT c.relname, pg_stat_get_tuples_returned(c.oid) FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
     WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
     AND pg_catalog.pg_table_is_visible(c.oid)
   END_GET_RETURNED_PER_TABLE_QUERY

   ret = run_query(db, query)

   ret.map!{ |v| [v['relname'], v['pg_stat_get_tuples_returned'].to_i] }

   return Hash[*ret.flatten]
end

#get_rollbacks_per_database(db) ⇒ Object

  • number of rollbacks per database



434
435
436
437
438
439
440
441
442
443
444
# File 'lib/postgrestats.rb', line 434

def get_rollbacks_per_database(db)
  query = <<-END_GET_ROLLBACKS_PER_DATABASE_QUERY
    SELECT pg_stat_get_db_xact_rollback(oid)
    FROM pg_database
    WHERE datname = '#{db}'
  END_GET_ROLLBACKS_PER_DATABASE_QUERY
  
  ret = run_query(db, query)
  
  return ret.first['pg_stat_get_db_xact_rollback'].to_i
end

#get_sequential_rows_read_per_table(db) ⇒ Object



649
650
651
652
653
654
655
656
657
658
659
660
661
# File 'lib/postgrestats.rb', line 649

def get_sequential_rows_read_per_table(db)
  query = <<-END_GET_SEQ_ROWS_READ_PER_TABLE_QUERY
    SELECT stat.relname AS relname, seq_tup_read
    FROM pg_stat_user_tables stat
    RIGHT JOIN pg_statio_user_tables statio ON stat.relid=statio.relid;
  END_GET_SEQ_ROWS_READ_PER_TABLE_QUERY

  ret = run_query(db, query)

  ret.map!{ |v| [v['relname'], v['seq_tup_read'].to_i] }

  return Hash[*ret.flatten]
end

#get_sequential_scans_per_table(db) ⇒ Object



635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'lib/postgrestats.rb', line 635

def get_sequential_scans_per_table(db)
  query = <<-END_GET_SEQ_SCANS_PER_TABLE_QUERY
    SELECT stat.relname AS relname, seq_scan
    FROM pg_stat_user_tables stat
    RIGHT JOIN pg_statio_user_tables statio ON stat.relid=statio.relid;
  END_GET_SEQ_SCANS_PER_TABLE_QUERY

  ret = run_query(db, query)

  ret.map!{ |v| [v['relname'], v['seq_scan'].to_i] }

  return Hash[*ret.flatten]
end

#get_table_estimated_rows(db, table) ⇒ Object

  • estimated rows per database/table



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/postgrestats.rb', line 317

def get_table_estimated_rows(db, table)
  query = <<-END_GET_TABLE_ESTIMATED_ROWS_QUERY
    SELECT (
      CASE 
      WHEN reltuples > 0 THEN pg_relation_size('#{table}')/(8192*relpages::bigint/reltuples)
      ELSE 0
      END
    )::bigint AS estimated_row_count
    FROM pg_class
    WHERE oid = '#{table}'::regclass;
  END_GET_TABLE_ESTIMATED_ROWS_QUERY
  
  ret = run_query(db, query)
  
  return ret.first['estimated_row_count'].to_i
end

#get_table_ganglia_doubleObject



222
223
224
225
226
227
# File 'lib/postgrestats.rb', line 222

def get_table_ganglia_double()
  return [
    table_gauges.select{ |k, v| v.has_key?('ganglia_double') }.keys,
    table_counters.select{ |k, v| v.has_key?('ganglia_double') }.keys
  ].flatten
end

#get_table_metricsObject



214
215
216
# File 'lib/postgrestats.rb', line 214

def get_table_metrics()
  return [table_gauges.keys, table_counters.keys].flatten
end

#get_table_sizes(db) ⇒ Object

  • size per database/table



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/postgrestats.rb', line 271

def get_table_sizes(db)
  query = <<-END_GET_TABLE_SIZES_QUERY
    SELECT table_name,pg_relation_size(table_name) AS size
    FROM information_schema.tables
    WHERE table_schema NOT IN ('information_schema', 'pg_catalog') AND table_type = 'BASE TABLE'
  END_GET_TABLE_SIZES_QUERY
  
  ret = run_query(db, query)
  
  ret.map!{ |v| [v['table_name'], v['size'].to_i] }
  
  return Hash[*ret.flatten]
end

#get_tables(db) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/postgrestats.rb', line 255

def get_tables(db)
  query = <<-END_GET_TABLES_QUERY
    SELECT c.relname FROM pg_catalog.pg_class c
    LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
    AND pg_catalog.pg_table_is_visible(c.oid)
  END_GET_TABLES_QUERY
  
  ret = run_query(db, query)
  
  ret.map!{ |v| v['relname'] }
  
  return ret
end

#get_tables_estimated_rows(db) ⇒ Object

  • estimated rows for every table in the database



335
336
337
338
339
340
341
342
343
# File 'lib/postgrestats.rb', line 335

def get_tables_estimated_rows(db)
  ret = {}
  
  get_tables(db).each do |table|
     ret[table] = get_table_estimated_rows(db, table)
  end
  
  return ret
end

#get_updates_per_database(db) ⇒ Object

  • number of rows updates per database



465
466
467
468
469
470
471
472
473
474
475
# File 'lib/postgrestats.rb', line 465

def get_updates_per_database(db)
  query = <<-END_GET_UPDATES_PER_DATABASE_QUERY
    SELECT pg_stat_get_db_tuples_updated(oid)
    FROM pg_database
    WHERE datname = '#{db}'
  END_GET_UPDATES_PER_DATABASE_QUERY
  
  ret = run_query(db, query)
  
  return ret.first['pg_stat_get_db_tuples_updated'].to_i
end

#get_updates_per_table(db) ⇒ Object

  • number of updates rows per table



571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/postgrestats.rb', line 571

def get_updates_per_table(db)
   query = <<-END_GET_UPDATES_PER_TABLE_QUERY
     SELECT c.relname, pg_stat_get_tuples_updated(c.oid) FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
     WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
     AND pg_catalog.pg_table_is_visible(c.oid)
   END_GET_UPDATES_PER_TABLE_QUERY

   ret = run_query(db, query)

   ret.map!{ |v| [v['relname'], v['pg_stat_get_tuples_updated'].to_i] }

   return Hash[*ret.flatten]
end

#initialize_logObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/postgrestats.rb', line 19

def initialize_log
  @config['log'] = {
    'file' => STDOUT,
    'level' => 'INFO'
  }.merge(@config['log'] || {})
  
  log_initialize = [@config['log']['file']]
  log_initialize << @config['log']['shift_age'] if @config['log']['shift_age']
  log_initialize << @config['log']['shift_size'] if @config['log']['shift_size']
  
  begin
    @log = Logger.new(*log_initialize)
    @log.level = Logger.const_get(@config['log']['level'])
  rescue Exception => e
    @config['log'] = {
      'file' => STDOUT,
      'level' => 'INFO'
    }
    @log = Logger.new(@config['log']['file'])
    @log.level = Logger.const_get(@config['log']['level'])
    @log.error("Caught a problem with log settings")
    @log.error("#{e.message}")
    @log.error("Setting log settings to defaults")
  end
end

#initialize_metricsObject



45
46
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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/postgrestats.rb', line 45

def initialize_metrics
  @table_gauges = {
    'table_size' => {
      'units' => 'bytes',
      'handler' => :get_table_sizes
    },
    'index_size' => {
      'units' => 'bytes',
      'handler' => :get_index_sizes
    },
    'estimated_rows' => {
      'units' => 'rows',
      'handler' => :get_tables_estimated_rows
    },
    'dead' => {
      'units' => 'rows',
      'handler' => :get_dead_per_table
    }, 
    'last_vacuum' => {
      'units' => 'seconds since',
      'handler' => :get_last_autovacuum_per_table
    }
  }
  
  @table_counters = {
    'inserts' => {
      'units' => 'inserts/s',
      'ganglia_double' => true,
      'handler' => :get_inserts_per_table
    }, 
    'updates' => {
      'units' => 'updates/s',
      'ganglia_double' => true,
      'handler' => :get_updates_per_table
    },
    'deletes' => {
      'units' => 'deletes/s',
      'ganglia_double' => true,
      'handler' => :get_deletes_per_table
    },
    'hot_updates' => {
      'units' => 'updates/s',
      'ganglia_double' => true,
      'handler' => :get_hot_updates_per_table
    },
    'seq_scan' => {
      'units' => 'scans/s',
      'ganglia_double' => true,
      'handler' => :get_sequential_scans_per_table
    },
    'idx_scan' => {
      'units' => 'scans/s',
      'ganglia_double' => true,
      'handler' => :get_index_scans_per_table
    },
    'seq_tup_read' => {
      'units' => 'rows/s',
      'ganglia_double' => true,
      'handler' => :get_sequential_rows_read_per_table
    },
    'idx_tup_fetch' => {
      'units' => 'rows/s',
      'ganglia_double' => true,
      'handler' => :get_index_rows_fetched_per_table
    },
    'heap_blks_read' => {
      'units' => 'blocks/s',
      'ganglia_double' => true,
      'handler' => :get_heap_blocks_read_per_table
    },
    'heap_blks_hit' => {
      'units' => 'blocks/s',
      'ganglia_double' => true,
      'handler' => :get_heap_blocks_hit_per_table
    },
    'idx_blks_read' => {
      'units' => 'blocks/s',
      'ganglia_double' => true,
      'handler' => :get_index_blocks_read_per_table
    },
    'idx_blks_hit' => {
      'units' => 'blocks/s',
      'ganglia_double' => true,
      'handler' => :get_index_blocks_hit_per_table
    }
  }
  
  @aggregate_gauges = {
    'locks' => {
      'units' => 'locks',
      'handler' => :get_number_of_locks_per_database
    },
    'connections' => {
      'units' => 'connections',
      'handler' => :get_connections_per_database
    }
  }
  
  @aggregate_counters = {
    'blocks_fetched' => {
      'units' => 'blocks/s',
      'ganglia_double' => true,
      'handler' => :get_blocks_fetched_per_database
    },
    'blocks_hit' => {
      'units' => 'blocks/s',
      'ganglia_double' => true,
      'handler' => :get_blocks_hit_per_database
    },
    'commits' => {
      'units' => 'commits/s',
      'ganglia_double' => true,
      'handler' => :get_commits_per_database
    },
    'rollbacks' => {
      'units' => 'rollbacks/s',
      'ganglia_double' => true,
      'handler' => :get_rollbacks_per_database
    },
    'inserts' => {
      'units' => 'inserts/s',
      'ganglia_double' => true,
      'handler' => :get_inserts_per_database
    },
    'updates' => {
      'units' => 'updates/s',
      'ganglia_double' => true,
      'handler' => :get_updates_per_database
    },
    'deletes' => {
      'units' => 'deletes/s',
      'ganglia_double' => true,
      'handler' => :get_deletes_per_database
    }
  }
end

#run_query(db, query) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/postgrestats.rb', line 198

def run_query(db, query)
  connect(db)
  
  result = @connection.exec(query)
  fields = result.fields
  ret = []
  
  result.each do |row|
    item = {}
    fields.each { |field| item[field] = row[field] }
    ret << item
  end
  
  return ret
end