longqueries.py
changeset 101 2a2d0d5df03b
parent 100 d6088dba8fea
child 102 fda45bdfd68d
equal deleted inserted replaced
100:d6088dba8fea 101:2a2d0d5df03b
     1 #!/usr/bin/env python3
       
     2 
       
     3 from pgtoolkit import pgstats, toolbase
       
     4 from pycolib.ansicolor import highlight, YELLOW, BOLD
       
     5 
       
     6 
       
     7 class LongQueriesTool(toolbase.SimpleTool):
       
     8     def __init__(self):
       
     9         toolbase.SimpleTool.__init__(self, name='longqueries', desc='List long queries.')
       
    10         self.parser.add_argument('--age', default='1m', help='How long must be the query running to be listed.')
       
    11         self.init()
       
    12 
       
    13     def main(self):
       
    14         stats = pgstats.PgStats(self.pgm.get_conn('target'))
       
    15 
       
    16         for ln in stats.list_long_queries(self.args.age):
       
    17             print(highlight(1),
       
    18                   'backend PID: ', ln['procpid'],
       
    19                   ', query_start: ', ln['query_start'].strftime('%F %T'),
       
    20                   ', client IP: ', ln['client_addr'],
       
    21                   ln['waiting'] and ', ' + highlight(1, YELLOW|BOLD) + 'waiting' or '',
       
    22                   highlight(0), sep='')
       
    23             print(ln['query'])
       
    24             print()
       
    25 
       
    26 
       
    27 tool = LongQueriesTool()
       
    28 tool.main()
       
    29