You've already forked postgres_exporter
mirror of
https://github.com/prometheus-community/postgres_exporter.git
synced 2025-08-09 15:42:47 +03:00
118 lines
3.3 KiB
Go
118 lines
3.3 KiB
Go
package builtin
|
|
|
|
import (
|
|
"github.com/blang/semver"
|
|
. "github.com/wrouesnel/postgres_exporter/pkg/queries/metricmaps"
|
|
)
|
|
|
|
// Overriding queries for namespaces above.
|
|
// TODO: validate this is a closed set in tests, and there are no overlaps
|
|
var queryOverrides = map[string][]OverrideQuery{
|
|
"pg_locks": {
|
|
{
|
|
semver.MustParseRange(">0.0.0"),
|
|
`SELECT pg_database.datname,tmp.mode,COALESCE(count,0) as count
|
|
FROM
|
|
(
|
|
VALUES ('accesssharelock'),
|
|
('rowsharelock'),
|
|
('rowexclusivelock'),
|
|
('shareupdateexclusivelock'),
|
|
('sharelock'),
|
|
('sharerowexclusivelock'),
|
|
('exclusivelock'),
|
|
('accessexclusivelock')
|
|
) AS tmp(mode) CROSS JOIN pg_database
|
|
LEFT JOIN
|
|
(SELECT database, lower(mode) AS mode,count(*) AS count
|
|
FROM pg_locks WHERE database IS NOT NULL
|
|
GROUP BY database, lower(mode)
|
|
) AS tmp2
|
|
ON tmp.mode=tmp2.mode and pg_database.oid = tmp2.database ORDER BY 1`,
|
|
},
|
|
},
|
|
|
|
"pg_stat_replication": {
|
|
{
|
|
semver.MustParseRange(">=10.0.0"),
|
|
`
|
|
SELECT *,
|
|
(case pg_is_in_recovery() when 't' then null else pg_current_wal_lsn() end) AS pg_current_wal_lsn,
|
|
(case pg_is_in_recovery() when 't' then null else pg_wal_lsn_diff(pg_current_wal_lsn(), pg_lsn('0/0'))::float end) AS pg_current_wal_lsn_bytes,
|
|
(case pg_is_in_recovery() when 't' then null else pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn)::float end) AS pg_wal_lsn_diff
|
|
FROM pg_stat_replication
|
|
`,
|
|
},
|
|
{
|
|
semver.MustParseRange(">=9.2.0 <10.0.0"),
|
|
`
|
|
SELECT *,
|
|
(case pg_is_in_recovery() when 't' then null else pg_current_xlog_location() end) AS pg_current_xlog_location,
|
|
(case pg_is_in_recovery() when 't' then null else pg_xlog_location_diff(pg_current_xlog_location(), replay_location)::float end) AS pg_xlog_location_diff
|
|
FROM pg_stat_replication
|
|
`,
|
|
},
|
|
{
|
|
semver.MustParseRange("<9.2.0"),
|
|
`
|
|
SELECT *,
|
|
(case pg_is_in_recovery() when 't' then null else pg_current_xlog_location() end) AS pg_current_xlog_location
|
|
FROM pg_stat_replication
|
|
`,
|
|
},
|
|
},
|
|
|
|
"pg_stat_archiver": {
|
|
{
|
|
semver.MustParseRange(">=0.0.0"),
|
|
`
|
|
SELECT *,
|
|
extract(epoch from now() - last_archived_time) AS last_archive_age
|
|
FROM pg_stat_archiver
|
|
`,
|
|
},
|
|
},
|
|
|
|
"pg_stat_activity": {
|
|
// This query only works
|
|
{
|
|
semver.MustParseRange(">=9.2.0"),
|
|
`
|
|
SELECT
|
|
pg_database.datname,
|
|
tmp.state,
|
|
COALESCE(count,0) as count,
|
|
COALESCE(max_tx_duration,0) as max_tx_duration
|
|
FROM
|
|
(
|
|
VALUES ('active'),
|
|
('idle'),
|
|
('idle in transaction'),
|
|
('idle in transaction (aborted)'),
|
|
('fastpath function call'),
|
|
('disabled')
|
|
) AS tmp(state) CROSS JOIN pg_database
|
|
LEFT JOIN
|
|
(
|
|
SELECT
|
|
datname,
|
|
state,
|
|
count(*) AS count,
|
|
MAX(EXTRACT(EPOCH FROM now() - xact_start))::float AS max_tx_duration
|
|
FROM pg_stat_activity GROUP BY datname,state) AS tmp2
|
|
ON tmp.state = tmp2.state AND pg_database.datname = tmp2.datname
|
|
`,
|
|
},
|
|
{
|
|
semver.MustParseRange("<9.2.0"),
|
|
`
|
|
SELECT
|
|
datname,
|
|
'unknown' AS state,
|
|
COALESCE(count(*),0) AS count,
|
|
COALESCE(MAX(EXTRACT(EPOCH FROM now() - xact_start))::float,0) AS max_tx_duration
|
|
FROM pg_stat_activity GROUP BY datname
|
|
`,
|
|
},
|
|
},
|
|
} |