analyzeall.py
changeset 88 b8b2d28a7f35
parent 83 515fadd3d286
child 93 b72591087495
equal deleted inserted replaced
87:3ef717ee9253 88:b8b2d28a7f35
    14 class AnalyzeAllTool(toolbase.SimpleTool):
    14 class AnalyzeAllTool(toolbase.SimpleTool):
    15     def __init__(self):
    15     def __init__(self):
    16         toolbase.SimpleTool.__init__(self, name='analyzeall', desc='Analyze all tables.')
    16         toolbase.SimpleTool.__init__(self, name='analyzeall', desc='Analyze all tables.')
    17         self.parser.add_argument('-s', dest='schema', nargs='*', help='Schema filter')
    17         self.parser.add_argument('-s', dest='schema', nargs='*', help='Schema filter')
    18         self.parser.add_argument('--vacuum', action='store_true', help='Call VACUUM ANALYZE')
    18         self.parser.add_argument('--vacuum', action='store_true', help='Call VACUUM ANALYZE')
    19         self.parser.add_argument('--full', action='store_true', help='Call VACUUM FULL ANALYZE')
    19         self.parser.add_argument('--vacuum-full', action='store_true', help='Call VACUUM FULL ANALYZE')
    20         self.parser.add_argument('--reindex', action='store_true', help='Call REINDEX TABLE')
    20         self.parser.add_argument('--reindex', action='store_true', help='Call REINDEX TABLE')
    21         self.target_isolation_level = 'autocommit'
    21         self.target_isolation_level = 'autocommit'
    22         self.init()
    22         self.init()
    23 
    23 
    24     def main(self):
    24     def main(self):
    25         browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
    25         browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
    26 
    26 
    27         query_pattern = 'ANALYZE %s.%s;'
    27         query_patterns = ['ANALYZE %s.%s;']
    28         if self.args.vacuum:
    28         if self.args.vacuum:
    29             query_pattern = 'VACUUM ANALYZE %s.%s;'
    29             query_patterns = ['VACUUM ANALYZE %s.%s;']
    30         if self.args.full:
    30         if self.args.vacuum_full:
    31             query_pattern = 'VACUUM FULL ANALYZE %s.%s;'
    31             query_patterns = ['VACUUM FULL ANALYZE %s.%s;']
    32         if self.args.reindex:
    32         if self.args.reindex:
    33             query_pattern = 'REINDEX TABLE %s.%s;'
    33             query_patterns += ['REINDEX TABLE %s.%s;']
    34 
    34 
    35         schema_list = self.args.schema
    35         schema_list = self.args.schema
    36         if not schema_list:
    36         if not schema_list:
    37             schema_list = [schema['name'] for schema in browser.list_schemas() if not schema['system']]
    37             schema_list = [schema['name'] for schema in browser.list_schemas() if not schema['system']]
    38 
    38 
    39         for schema in schema_list:
    39         for schema in schema_list:
    40             tables = browser.list_tables(schema=schema)
    40             tables = browser.list_tables(schema=schema)
    41             with self.pgm.cursor('target') as curs:
    41             with self.pgm.cursor('target') as curs:
    42                 for table in tables:
    42                 for table in tables:
    43                     query = query_pattern % (schema, table['name'])
    43                     for query_pattern in query_patterns:
    44                     self.log.info(query)
    44                         query = query_pattern % (schema, table['name'])
    45                     curs.execute(query, [])
    45                         self.log.info(query)
       
    46                         curs.execute(query, [])
    46 
    47 
    47 
    48 
    48 tool = AnalyzeAllTool()
    49 tool = AnalyzeAllTool()
    49 tool.main()
    50 tool.main()
    50 
    51