Skip to content

Commit e018546

Browse files
authored
Merge pull request #1 from postgrespro/master
Sync fork from original repository
2 parents 44dd7c5 + f8c6019 commit e018546

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ Metrics: PostgreSQL
220220
'PostgreSQL locks: Locks from application': pgsql.pg_locks[sharerowexclusive]
221221
'PostgreSQL locks: Locks from application or some operation on system catalogs': pgsql.pg_locks[exclusive]
222222
'PostgreSQL locks: ALTER TABLE, DROP TABLE, TRUNCATE, REINDEX, CLUSTER, VACUUM FULL, LOCK TABLE': pgsql.pg_locks[accessexclusive]
223-
'PostgreSQL oldest query running time': pgsql.oldest[query_time]
223+
'PostgreSQL oldest transaction running time': pgsql.oldest[transaction_time]
224224
'PostgreSQL age of oldest xid': pgsql.oldest[xid_age]
225225
'PostgreSQL waits: Lightweight locks': pgsql.all_lock[lwlock]
226226
'PostgreSQL waits: Heavyweight locks': pgsql.all_lock[hwlock]

mamonsu/plugins/pgsql/health.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def run(self, zbx):
1313

1414
start_time = time.time()
1515
Pooler.query('select 1 as health')
16-
zbx.send('pgsql.ping[]', (time.time() - start_time) * 100)
16+
zbx.send('pgsql.ping[]', (time.time() - start_time) * 1000)
1717

1818
result = Pooler.query("select \
1919
date_part('epoch', now() - pg_postmaster_start_time())")

mamonsu/plugins/pgsql/oldest.py

+20-12
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@ class Oldest(Plugin):
1717
"""
1818

1919
OldestQuerySql = """
20-
select
21-
extract(epoch from max(now() - xact_start))
22-
from pg_catalog.pg_stat_activity;
20+
SELECT
21+
CASE WHEN extract(epoch from max(now() - xact_start)) IS NOT null
22+
AND extract(epoch from max(now() - xact_start))>0
23+
THEN extract(epoch from max(now() - xact_start))
24+
ELSE 0
25+
END
26+
FROM pg_catalog.pg_stat_activity
27+
WHERE
28+
pid NOT IN(select pid from pg_stat_replication) AND
29+
pid <> pg_backend_pid() AND
30+
query NOT ilike '%%VACUUM%%';
2331
"""
2432

2533
OldestQuerySql_bootstrap = """
@@ -28,7 +36,7 @@ class Oldest(Plugin):
2836

2937
DEFAULT_CONFIG = {
3038
'max_xid_age': str(5000 * 60 * 60),
31-
'max_query_time': str(5 * 60 * 60)
39+
'max_transaction_time': str(5 * 60 * 60)
3240
}
3341

3442
def run(self, zbx):
@@ -40,13 +48,13 @@ def run(self, zbx):
4048
query = Pooler.query(self.OldestQuerySql)[0][0]
4149

4250
zbx.send('pgsql.oldest[xid_age]', xid)
43-
zbx.send('pgsql.oldest[query_time]', query)
51+
zbx.send('pgsql.oldest[transaction_time]', query)
4452

4553
def graphs(self, template):
4654
result = template.graph({
47-
'name': 'PostgreSQL oldest query running time',
55+
'name': 'PostgreSQL oldest transaction running time',
4856
'items': [{
49-
'key': 'pgsql.oldest[query_time]',
57+
'key': 'pgsql.oldest[transaction_time]',
5058
'color': '00CC00'
5159
}]
5260
})
@@ -65,8 +73,8 @@ def items(self, template):
6573
'name': 'PostgreSQL: age of oldest xid',
6674
'value_type': Plugin.VALUE_TYPE.numeric_unsigned
6775
}) + template.item({
68-
'key': 'pgsql.oldest[query_time]',
69-
'name': 'PostgreSQL: oldest query running time in sec',
76+
'key': 'pgsql.oldest[transaction_time]',
77+
'name': 'PostgreSQL: oldest transaction running time in sec',
7078
'units': Plugin.UNITS.s
7179
})
7280

@@ -76,7 +84,7 @@ def triggers(self, template):
7684
'expression': '{#TEMPLATE:pgsql.oldest[xid_age]'
7785
'.last()}&gt;' + self.plugin_config('max_xid_age')
7886
}) + template.trigger({
79-
'name': 'PostgreSQL query running is too old on {HOSTNAME}',
80-
'expression': '{#TEMPLATE:pgsql.oldest[query_time]'
81-
'.last()}&gt;' + self.plugin_config('max_query_time')
87+
'name': 'PostgreSQL transaction running is too old on {HOSTNAME}',
88+
'expression': '{#TEMPLATE:pgsql.oldest[transaction_time]'
89+
'.last()}&gt;' + self.plugin_config('max_transaction_time')
8290
})

mamonsu/tools/bootstrap/sql.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,17 @@
6767
6868
CREATE or REPLACE FUNCTION public.mamonsu_get_oldest_query()
6969
RETURNS DOUBLE PRECISION AS $$
70-
SELECT
71-
extract(epoch from max(now() - xact_start))
72-
FROM pg_catalog.pg_stat_activity
70+
SELECT
71+
CASE WHEN extract(epoch from max(now() - xact_start)) IS NOT null
72+
AND extract(epoch from max(now() - xact_start))>0
73+
THEN extract(epoch from max(now() - xact_start))
74+
ELSE 0
75+
END
76+
FROM pg_catalog.pg_stat_activity
77+
WHERE
78+
pid NOT IN(select pid from pg_stat_replication) AND
79+
pid <> pg_backend_pid() AND
80+
query NOT ilike '%%VACUUM%%'
7381
$$ LANGUAGE SQL SECURITY DEFINER;
7482
7583
CREATE OR REPLACE FUNCTION public.mamonsu_count_{3}_files()

0 commit comments

Comments
 (0)