Update bigtables tool: Add size of indexes.
--- a/bigtables.py Tue Feb 04 16:10:04 2014 +0100
+++ b/bigtables.py Thu Mar 20 16:33:34 2014 +0100
@@ -1,12 +1,15 @@
#!/usr/bin/env python3
from pgtoolkit import pgbrowser, toolbase
-from pycolib import prettysize
+from pycolib.prettysize import prettysize_short
+from pycolib.ansicolor import highlight
class BigTablesTool(toolbase.SimpleTool):
def __init__(self):
toolbase.SimpleTool.__init__(self, name='bigtables', desc='List largest tables.')
+ self.parser.add_argument('-n', '--limit', metavar='NUM', dest='limit', type=int, default=5, help='Show NUM biggest tables.')
+ self.parser.add_argument('-v', '--details', dest='details', action='store_true', help='Show sizes of data and individual indexes.')
self.init()
def main(self):
@@ -14,18 +17,33 @@
# scan all tables from all shemas, remember names and sizes
all_tables = []
+ all_indexes = []
schemas = browser.list_schemas()
for schema in schemas:
tables = browser.list_tables(schema['name'])
for table in tables:
table_name = '%s.%s' % (schema['name'], table['name'])
- all_tables.append({'name': table_name, 'size': table['size']})
+ indexes = browser.list_indexes(table['name'], schema['name'])
+ for index in indexes:
+ all_indexes.append({'name': index['name'], 'table': table_name, 'size': index['size']})
+ all_tables.append({'name': table_name, 'size': table['size'], 'indexes': indexes})
# print names and sizes of 20 largest tables
- for table in sorted(all_tables, reverse=True, key=lambda x: x['size'])[:20]:
- print(prettysize.prettysize_short(table['size']).rjust(8), table['name'], sep=' ')
+ for table in sorted(all_tables, reverse=True, key=lambda x: x['size'])[:self.args.limit]:
+ size_of_indexes = sum(index['size'] for index in table['indexes'])
+ print(highlight(1) + prettysize_short(table['size'] + size_of_indexes, trailing_zeros=True).rjust(8) + highlight(0),
+ '(total)'.ljust(8),
+ highlight(1) + table['name'] + highlight(0), sep=' ')
+ if self.args.details:
+ print(prettysize_short(table['size'], trailing_zeros=True).rjust(8),
+ '(data)'.ljust(8), sep=' ')
+ for index in sorted(table['indexes'], reverse=True, key=lambda x: x['size']):
+ print(prettysize_short(index['size'], trailing_zeros=True).rjust(8),
+ '(index)'.ljust(8), index['name'], sep=' ')
+ print()
-tool = BigTablesTool()
-tool.main()
+if __name__ == '__main__':
+ tool = BigTablesTool()
+ tool.main()
--- a/pgtoolkit/pgbrowser.py Tue Feb 04 16:10:04 2014 +0100
+++ b/pgtoolkit/pgbrowser.py Thu Mar 20 16:33:34 2014 +0100
@@ -55,7 +55,7 @@
class Index:
def __init__(self, browser, table,
name, primary, unique, clustered, valid, definition,
- columns):
+ columns, size):
self.browser = browser
self.table = table
self.name = name
@@ -65,6 +65,7 @@
self.valid = valid
self.definition = definition
self.columns = columns
+ self.size = size
class Table:
@@ -345,7 +346,8 @@
i.indisclustered as "clustered",
i.indisvalid as "valid",
pg_catalog.pg_get_indexdef(i.indexrelid, 0, true) as "definition",
- ARRAY(SELECT a.attname FROM pg_catalog.pg_attribute a WHERE a.attrelid = c2.oid ORDER BY attnum) AS "columns"
+ ARRAY(SELECT a.attname FROM pg_catalog.pg_attribute a WHERE a.attrelid = c2.oid ORDER BY attnum) AS "columns",
+ pg_catalog.pg_relation_size(c2.oid) as "size"
--c2.reltablespace as "tablespace_oid"
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_index i ON c.oid = i.indrelid