analyzeall.py
changeset 27 5fb4883604d6
child 28 27fc0504663d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/analyzeall.py	Tue Feb 07 10:40:35 2012 +0100
@@ -0,0 +1,34 @@
+#!/usr/bin/env python3.2
+
+from pgtoolkit import pgbrowser, toolbase
+from pgtoolkit.highlight import highlight
+
+
+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('--vacuum', action='store_true', help='Call VACUUM ANALYZE')
+        self.parser.add_argument('--full', action='store_true', help='Call VACUUM FULL ANALYZE')
+        self.init()
+
+    def main(self):
+        browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
+        
+        query = 'ANALYZE %s;'
+        if self.args.vacuum:
+            query = 'VACUUM ANALYZE %s;'
+        if self.args.full:
+            query = 'VACUUM FULL ANALYZE %s;'
+
+        for schema in self.args.schema:
+            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'], [])
+
+
+tool = AnalyzeAllTool()
+tool.main()
+