pgtoolkit/tools/longqueries.py
changeset 104 d8ff52a0390f
parent 103 24e94a3da209
child 105 10551741f61f
equal deleted inserted replaced
103:24e94a3da209 104:d8ff52a0390f
     1 from pgtoolkit.toolbase import SimpleTool
       
     2 from pgtoolkit import pgstats
       
     3 from pycolib.ansicolor import highlight, YELLOW, BOLD
       
     4 
       
     5 
       
     6 class LongQueriesTool(SimpleTool):
       
     7 
       
     8     """
       
     9     List long running queries.
       
    10     """
       
    11 
       
    12     def __init__(self):
       
    13         SimpleTool.__init__(self, name='longqueries')
       
    14 
       
    15     def specify_args(self):
       
    16         SimpleTool.specify_args(self)
       
    17         self.parser.add_argument('--age', default='1m', help='How long must be the query running to be listed.')
       
    18 
       
    19     def main(self):
       
    20         stats = pgstats.PgStats(self.pgm.get_conn('target'))
       
    21 
       
    22         for ln in stats.list_long_queries(self.args.age):
       
    23             print(highlight(1),
       
    24                   'backend PID: ', ln['procpid'],
       
    25                   ', query_start: ', ln['query_start'].strftime('%F %T'),
       
    26                   ', client IP: ', ln['client_addr'],
       
    27                   ln['waiting'] and ', ' + highlight(1, YELLOW|BOLD) + 'waiting' or '',
       
    28                   highlight(0), sep='')
       
    29             print(ln['query'])
       
    30             print()
       
    31 
       
    32 
       
    33 cls = LongQueriesTool
       
    34