Add analyzeall tool. Updates, fixes.
authorRadek Brich <radek.brich@devl.cz>
Tue, 07 Feb 2012 10:40:35 +0100
changeset 27 5fb4883604d6
parent 26 7f219da7ab71
child 28 27fc0504663d
Add analyzeall tool. Updates, fixes.
analyzeall.py
longqueries.py
pgtoolkit/pgdatadiff.py
--- /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()
+
--- a/longqueries.py	Fri Jan 13 15:49:24 2012 +0100
+++ b/longqueries.py	Tue Feb 07 10:40:35 2012 +0100
@@ -1,6 +1,7 @@
 #!/usr/bin/env python3.2
 
 from pgtoolkit import pgstats, toolbase
+from pgtoolkit.highlight import highlight
 
 
 class LongQueriesTool(toolbase.SimpleTool):
@@ -11,9 +12,9 @@
     def main(self):
         stats = pgstats.PgStats(self.pgm.get_conn('target'))
 
-        for ln in stats.list_long_queries():
+        for ln in stats.list_long_queries('0m'):
+            print(highlight(1), 'backend PID: ', ln['procpid'], ', query_start: ', ln['query_start'].strftime('%F %T'), highlight(0), sep='')
             print(ln['query'])
-            print('   ', 'query_start:', ln['query_start'].strftime('%F %T'))
             print()
 
 
--- a/pgtoolkit/pgdatadiff.py	Fri Jan 13 15:49:24 2012 +0100
+++ b/pgtoolkit/pgdatadiff.py	Tue Feb 07 10:40:35 2012 +0100
@@ -34,7 +34,8 @@
         '+' : BOLD | GREEN,
         '-' : BOLD | RED,
         '*' : BOLD | YELLOW,
-        'V' : BOLD | WHITE}
+        'V' : BOLD | WHITE,
+        'K' : BOLD | BLUE}
     
     def __init__(self, change, cols1, cols2, id=None):
         self.change = change
@@ -44,7 +45,7 @@
     
     def format(self):
         out = []
-        
+                
         out.append(highlight(1, self.COLORS[self.change]))
         out.extend([self.change, ' '])
         
@@ -62,20 +63,25 @@
         
         return method[self.change](table)
 
-    def _format_changes(self):
+    def _format_changes(self):        
         if self.cols1 and not self.cols2:
             return [', '.join([self._format_value_del(*x) for x in self.cols1.items()])]
         if not self.cols1 and self.cols2:
             return [', '.join([self._format_value_add(*x) for x in self.cols2.items()])]
         
+        out = []        
+        if self.id:
+            out.extend([highlight(1, self.COLORS['*']), self.id[0], ': ', highlight(0), self.id[1], ', '])
+
         items = []
         for i in range(len(self.cols1)):
             items.append((
                 list(self.cols1.keys())[i],
                 list(self.cols1.values())[i],
                 list(self.cols2.values())[i]))
-            
-        return [', '.join([self._format_value_change(*x) for x in items])]
+        out.extend([', '.join([self._format_value_change(*x) for x in items])])
+        
+        return out
 
     def _format_value_del(self, k, v):
         fs = (highlight(1, self.COLORS['-']) + '{}: ' + highlight(0) + '{}')
@@ -191,6 +197,8 @@
     def _select(self):
         browser = pgbrowser.PgBrowser(self.conn1)
         columns = browser.list_columns(schema=self.schema1, table=self.table1, order=1)
+        if not columns:
+            raise Exception('Table %s.%s not found.' % (self.schema1, self.table1))
         columns_sel = ', '.join(['"' + x['name'] + '"' for x in columns])
         self.colnames = [x['name'] for x in columns]