pgtoolkit/tools/bigtables.py
changeset 101 2a2d0d5df03b
parent 95 6adcb7ee4517
child 103 24e94a3da209
equal deleted inserted replaced
100:d6088dba8fea 101:2a2d0d5df03b
       
     1 from pgtoolkit.toolbase import SimpleTool
       
     2 from pgtoolkit import pgbrowser
       
     3 from pycolib.prettysize import prettysize_short
       
     4 from pycolib.ansicolor import highlight
       
     5 
       
     6 
       
     7 class BigTablesTool(SimpleTool):
       
     8 
       
     9     """List largest tables.
       
    10 
       
    11     Reads size statistics of tables and indexes from pgcatalog.
       
    12 
       
    13     """
       
    14 
       
    15     def __init__(self):
       
    16         SimpleTool.__init__(self, name='bigtables')
       
    17 
       
    18     def specify_args(self):
       
    19         SimpleTool.specify_args(self)
       
    20         self.parser.add_argument('-n', '--limit', metavar='NUM', dest='limit', type=int, default=5, help='Show NUM biggest tables.')
       
    21         self.parser.add_argument('-v', '--details', dest='details', action='store_true', help='Show sizes of data and individual indexes.')
       
    22 
       
    23     def main(self):
       
    24         browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
       
    25 
       
    26         # scan all tables from all shemas, remember names and sizes
       
    27         all_tables = []
       
    28         all_indexes = []
       
    29         schemas = browser.list_schemas()
       
    30         for schema in schemas:
       
    31             tables = browser.list_tables(schema['name'])
       
    32             for table in tables:
       
    33                 table_name = '%s.%s' % (schema['name'], table['name'])
       
    34                 indexes = browser.list_indexes(table['name'], schema['name'])
       
    35                 for index in indexes:
       
    36                     all_indexes.append({'name': index['name'], 'table': table_name, 'size': index['size']})
       
    37                 all_tables.append({'name': table_name, 'size': table['size'], 'indexes': indexes})
       
    38 
       
    39         # print names and sizes of 20 largest tables
       
    40         for table in sorted(all_tables, reverse=True, key=lambda x: x['size'])[:self.args.limit]:
       
    41             size_of_indexes = sum(index['size'] for index in table['indexes'])
       
    42             print(highlight(1) + prettysize_short(table['size'] + size_of_indexes, trailing_zeros=True).rjust(8) + highlight(0),
       
    43                   '(total)'.ljust(8),
       
    44                   highlight(1) + table['name'] + highlight(0), sep='  ')
       
    45             if self.args.details:
       
    46                 print(prettysize_short(table['size'], trailing_zeros=True).rjust(8),
       
    47                       '(data)'.ljust(8), sep='  ')
       
    48                 for index in sorted(table['indexes'], reverse=True, key=lambda x: x['size']):
       
    49                     print(prettysize_short(index['size'], trailing_zeros=True).rjust(8),
       
    50                           '(index)'.ljust(8), index['name'], sep='  ')
       
    51             print()
       
    52 
       
    53 
       
    54 cls = BigTablesTool
       
    55