analyzeall.py
changeset 28 27fc0504663d
parent 27 5fb4883604d6
child 30 a8b7cd92f39f
equal deleted inserted replaced
27:5fb4883604d6 28:27fc0504663d
     5 
     5 
     6 
     6 
     7 class AnalyzeAllTool(toolbase.SimpleTool):
     7 class AnalyzeAllTool(toolbase.SimpleTool):
     8     def __init__(self):
     8     def __init__(self):
     9         toolbase.SimpleTool.__init__(self, name='analyzeall', desc='Analyze all tables.')
     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')
    10         self.parser.add_argument('-s', dest='schema', nargs='*', help='Schema filter')
    11         self.parser.add_argument('--vacuum', action='store_true', help='Call VACUUM ANALYZE')
    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')
    12         self.parser.add_argument('--full', action='store_true', help='Call VACUUM FULL ANALYZE')
    13         self.init()
    13         self.init()
    14 
    14 
    15     def main(self):
    15     def main(self):
    16         browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
    16         browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
    17         
    17         
    18         query = 'ANALYZE %s;'
    18         query_pattern = 'ANALYZE %s.%s;'
    19         if self.args.vacuum:
    19         if self.args.vacuum:
    20             query = 'VACUUM ANALYZE %s;'
    20             query_pattern = 'VACUUM ANALYZE %s.%s;'
    21         if self.args.full:
    21         if self.args.full:
    22             query = 'VACUUM FULL ANALYZE %s;'
    22             query_pattern = 'VACUUM FULL ANALYZE %s.%s;'
    23 
    23 
    24         for schema in self.args.schema:
    24         schema_list = self.args.schema
       
    25         if not schema_list:
       
    26             schema_list = [schema['name'] for schema in browser.list_schemas() if not schema['system']]
       
    27 
       
    28         for schema in schema_list:
    25             tables = browser.list_tables(schema=schema)
    29             tables = browser.list_tables(schema=schema)
    26             with self.pgm.cursor('target') as curs:
    30             with self.pgm.cursor('target') as curs:
    27                 for table in tables:
    31                 for table in tables:
    28                     print(query % table['name'])
    32                     query = query_pattern % (schema, table['name'])
    29                     curs.execute(query % table['name'], [])
    33                     print(query)
       
    34                     curs.execute(query, [])
    30 
    35 
    31 
    36 
    32 tool = AnalyzeAllTool()
    37 tool = AnalyzeAllTool()
    33 tool.main()
    38 tool.main()
    34 
    39