analyzeall.py
changeset 27 5fb4883604d6
child 28 27fc0504663d
equal deleted inserted replaced
26:7f219da7ab71 27:5fb4883604d6
       
     1 #!/usr/bin/env python3.2
       
     2 
       
     3 from pgtoolkit import pgbrowser, toolbase
       
     4 from pgtoolkit.highlight import highlight
       
     5 
       
     6 
       
     7 class AnalyzeAllTool(toolbase.SimpleTool):
       
     8     def __init__(self):
       
     9         toolbase.SimpleTool.__init__(self, name='analyzeall', desc='Analyze all tables.')
       
    10         self.parser.add_argument('-s', dest='schema', nargs='*', default=['public'], help='Schema filter')
       
    11         self.parser.add_argument('--vacuum', action='store_true', help='Call VACUUM ANALYZE')
       
    12         self.parser.add_argument('--full', action='store_true', help='Call VACUUM FULL ANALYZE')
       
    13         self.init()
       
    14 
       
    15     def main(self):
       
    16         browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
       
    17         
       
    18         query = 'ANALYZE %s;'
       
    19         if self.args.vacuum:
       
    20             query = 'VACUUM ANALYZE %s;'
       
    21         if self.args.full:
       
    22             query = 'VACUUM FULL ANALYZE %s;'
       
    23 
       
    24         for schema in self.args.schema:
       
    25             tables = browser.list_tables(schema=schema)
       
    26             with self.pgm.cursor('target') as curs:
       
    27                 for table in tables:
       
    28                     print(query % table['name'])
       
    29                     curs.execute(query % table['name'], [])
       
    30 
       
    31 
       
    32 tool = AnalyzeAllTool()
       
    33 tool.main()
       
    34