Skip to content

Commit

Permalink
Merge pull request #21 from zalando/feature/actions_for_aux_processes
Browse files Browse the repository at this point in the history
Feature/actions for aux processes
  • Loading branch information
Oleksii Kliukin committed Jun 24, 2015
2 parents 1ccad16 + 8a2402d commit f298663
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pg_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,18 +1132,20 @@ def ident(self):
def _get_psinfo(cmdline):
""" gets PostgreSQL process type from the command-line."""
pstype = 'unknown'
action = None
if cmdline is not None and len(cmdline) > 0:
# postgres: stats collector process
m = re.match(r'postgres:\s+(.*)\s+process\s*(.*)$', cmdline)
if m:
pstype = m.group(1)
action = m.group(2)
else:
if re.match(r'postgres:.*', cmdline):
# assume it's a backend process
pstype = 'backend'
if pstype == 'autovacuum worker':
pstype = 'autovacuum'
return pstype
return (pstype, action)

@staticmethod
def _is_auxiliary_process(pstype):
Expand Down Expand Up @@ -1227,7 +1229,9 @@ def _read_proc(self, pid, is_backend, is_active):
# generated columns
result['cmdline'] = raw_result.get('cmd', None)
if not is_backend:
result['type'] = self._get_psinfo(result['cmdline'])
result['type'], action = self._get_psinfo(result['cmdline'])
if action:
result['query'] = action
else:
result['type'] = 'backend'
if is_active or not is_backend:
Expand Down Expand Up @@ -1410,6 +1414,10 @@ def ncurses_produce_prefix(self):
self.active_connections, self.max_connections,
self.recovery_status)

@staticmethod
def process_sort_key(process):
return process['age'] if process['age'] is not None else maxsize

def diff(self):
""" we only diff backend processes if new one is not idle and use pid to identify processes """

Expand Down Expand Up @@ -1443,7 +1451,7 @@ def diff(self):
# order the result rows by the start time value
if len(self.blocked_diffs) == 0:
self.rows_diff = self.running_diffs
self.rows_diff.sort(key=lambda process: (process['age'] or maxsize), reverse=True)
self.rows_diff.sort(key=self.process_sort_key, reverse=True)
else:
blocked_temp = []
# we traverse the tree of blocked processes in a depth-first order, building a list
Expand All @@ -1452,10 +1460,10 @@ def diff(self):
# by the current one from the plain list of process information rows, that's why
# we use a dictionary of lists of blocked processes with a blocker pid as a key
# and effectively build a separate tree for each blocker.
self.running_diffs.sort(key=lambda process: (process['age'] or maxsize), reverse=True)
self.running_diffs.sort(key=self.process_sort_key, reverse=True)
# sort elements in the blocked lists, so that they still appear in the latest to earliest order
for key in self.blocked_diffs:
self.blocked_diffs[key].sort(key=lambda process: (process['age'] or maxsize))
self.blocked_diffs[key].sort(key=self.process_sort_key)
for parent_row in self.running_diffs:
self.rows_diff.append(parent_row)
# if no processes blocked by this one - just skip to the next row
Expand Down

0 comments on commit f298663

Please sign in to comment.