analyzeall: analyze tables from all schemas by default
authorRadek Brich <radek.brich@devl.cz>
Tue, 07 Feb 2012 11:32:07 +0100
changeset 28 27fc0504663d
parent 27 5fb4883604d6
child 29 8636719a30f6
analyzeall: analyze tables from all schemas by default
analyzeall.py
--- a/analyzeall.py	Tue Feb 07 10:40:35 2012 +0100
+++ b/analyzeall.py	Tue Feb 07 11:32:07 2012 +0100
@@ -7,7 +7,7 @@
 class AnalyzeAllTool(toolbase.SimpleTool):
     def __init__(self):
         toolbase.SimpleTool.__init__(self, name='analyzeall', desc='Analyze all tables.')
-        self.parser.add_argument('-s', dest='schema', nargs='*', default=['public'], help='Schema filter')
+        self.parser.add_argument('-s', dest='schema', nargs='*', help='Schema filter')
         self.parser.add_argument('--vacuum', action='store_true', help='Call VACUUM ANALYZE')
         self.parser.add_argument('--full', action='store_true', help='Call VACUUM FULL ANALYZE')
         self.init()
@@ -15,18 +15,23 @@
     def main(self):
         browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
         
-        query = 'ANALYZE %s;'
+        query_pattern = 'ANALYZE %s.%s;'
         if self.args.vacuum:
-            query = 'VACUUM ANALYZE %s;'
+            query_pattern = 'VACUUM ANALYZE %s.%s;'
         if self.args.full:
-            query = 'VACUUM FULL ANALYZE %s;'
+            query_pattern = 'VACUUM FULL ANALYZE %s.%s;'
 
-        for schema in self.args.schema:
+        schema_list = self.args.schema
+        if not schema_list:
+            schema_list = [schema['name'] for schema in browser.list_schemas() if not schema['system']]
+
+        for schema in schema_list:
             tables = browser.list_tables(schema=schema)
             with self.pgm.cursor('target') as curs:
                 for table in tables:
-                    print(query % table['name'])
-                    curs.execute(query % table['name'], [])
+                    query = query_pattern % (schema, table['name'])
+                    print(query)
+                    curs.execute(query, [])
 
 
 tool = AnalyzeAllTool()