bigtables.py
author Radek Brich <radek.brich@devl.cz>
Mon, 09 Dec 2013 11:39:28 +0100
changeset 91 fba99a54ec49
parent 83 515fadd3d286
child 93 b72591087495
permissions -rwxr-xr-x
Add to runquery tool: Parameters for query, output file and format for output file. Fix loopquery tool.

#!/usr/bin/env python3.2

from pgtoolkit import pgbrowser, toolbase
from pycolib import prettysize


class BigTablesTool(toolbase.SimpleTool):
    def __init__(self):
        toolbase.SimpleTool.__init__(self, name='bigtables', desc='List largest tables.')
        self.init()

    def main(self):
        browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))

        # scan all tables from all shemas, remember names and sizes
        all_tables = []
        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']})

        # 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='  ')


tool = BigTablesTool()
tool.main()