2
# Copyright 2004-2005 Canonical Ltd. All rights reserved.
4
Generate some statistics about a PostgreSQL database suitable for
13
def percentage(num, total):
14
"""Return a percentage string of num/total"""
18
return '%3.2f%%' % ( (num * 100.0) / total, )
21
def print_row(key, value):
22
print '%(key)-20s: %(value)s' % vars()
26
con = psycopg.connect("dbname=%s" % dbname)
29
print 'Statistics for %s' % dbname
30
print '===============' + '=' * (len(dbname))
32
# Database level statistics
34
SELECT blks_hit, blks_read, numbackends,xact_commit, xact_rollback
36
WHERE datname=%(dbname)s
38
hit, read, backends, commits, rollbacks = cur.fetchone()
40
hit_rate = percentage(hit, hit + read)
42
print_row("Cache hit rate", hit_rate)
43
print_row("Number of backends", backends)
45
commit_rate = percentage(commits, commits + rollbacks)
47
print_row("Commit rate", commit_rate)
49
# Unused indexes, ignoring primary keys.
50
# TODO: We should identify constraints used to enforce uniqueness too
52
SELECT relname, indexrelname
53
FROM pg_stat_user_indexes AS u JOIN pg_indexes AS i
54
ON u.schemaname = i.schemaname
55
AND u.relname = i.tablename
56
AND u.indexrelname = i.indexname
59
AND indexrelname NOT LIKE '%_pkey'
60
AND indexdef NOT LIKE 'CREATE UNIQUE %'
65
print_row('Unused indexes', 'N/A')
67
print_row('Unused indexes', rows[0][1])
68
for table, index in rows[1:]:
71
if __name__ == '__main__':
72
if len(sys.argv) != 2:
73
print >> sys.stderr, "Usage: %s [DBNAME]" % sys.argv[0]