longqueries.py
changeset 44 4fe39c59c515
parent 39 0cef3540b69f
child 73 390376b9e70f
equal deleted inserted replaced
43:a921669e913a 44:4fe39c59c515
     1 #!/usr/bin/env python3.2
     1 #!/usr/bin/env python3.2
     2 
     2 
     3 from pgtoolkit import pgstats, toolbase
     3 from pgtoolkit import pgstats, toolbase
     4 from pgtoolkit.highlight import highlight
     4 from pgtoolkit.highlight import highlight, YELLOW, BOLD
     5 
     5 
     6 
     6 
     7 class LongQueriesTool(toolbase.SimpleTool):
     7 class LongQueriesTool(toolbase.SimpleTool):
     8     def __init__(self):
     8     def __init__(self):
     9         toolbase.SimpleTool.__init__(self, name='longqueries', desc='List long queries.')
     9         toolbase.SimpleTool.__init__(self, name='longqueries', desc='List long queries.')
    11 
    11 
    12     def main(self):
    12     def main(self):
    13         stats = pgstats.PgStats(self.pgm.get_conn('target'))
    13         stats = pgstats.PgStats(self.pgm.get_conn('target'))
    14 
    14 
    15         for ln in stats.list_long_queries('1m'):
    15         for ln in stats.list_long_queries('1m'):
    16             print(highlight(1), 'backend PID: ', ln['procpid'], ', query_start: ', ln['query_start'].strftime('%F %T'), highlight(0), sep='')
    16             print(highlight(1),
       
    17                   'backend PID: ', ln['procpid'],
       
    18                   ', query_start: ', ln['query_start'].strftime('%F %T'),
       
    19                   ', client IP: ', ln['client_addr'],
       
    20                   ln['waiting'] and ', ' + highlight(1, YELLOW|BOLD) + 'waiting' or '',
       
    21                   highlight(0), sep='')
    17             print(ln['query'])
    22             print(ln['query'])
    18             print()
    23             print()
    19 
    24 
    20 
    25 
    21 tool = LongQueriesTool()
    26 tool = LongQueriesTool()