Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Use pg_locks.waitstart for counting waiting time. (#32)
Browse files Browse the repository at this point in the history
* Use pg_locks.waitstart for counting waiting time.
* Add new memstats metrics.

Co-authored-by: Lesovsky Aleksey <[email protected]>
  • Loading branch information
lesovsky authored Apr 9, 2022
1 parent da4120c commit 9ed2fef
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
3 changes: 2 additions & 1 deletion internal/collector/linux_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func TestMeminfoCollector_Update(t *testing.T) {
"node_vmstat_balloon_migrate", "node_vmstat_swap_ra", "node_vmstat_swap_ra_hit", "node_vmstat_nr_foll_pin_acquired",
"node_vmstat_pgsteal_anon", "node_vmstat_pgsteal_file", "node_vmstat_pgscan_file", "node_vmstat_pgscan_anon",
"node_vmstat_thp_file_fallback_charge", "node_vmstat_nr_foll_pin_released", "node_vmstat_thp_file_fallback",
"node_vmstat_thp_fault_fallback_charge",
"node_vmstat_thp_fault_fallback_charge", "node_vmstat_nr_swapcached", "node_vmstat_direct_map_level2_splits",
"node_vmstat_direct_map_level3_splits",
},
collector: NewMeminfoCollector,
}
Expand Down
29 changes: 21 additions & 8 deletions internal/collector/postgres_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,37 @@ const (
postgresActivityQuery95 = "SELECT " +
"coalesce(usename, 'system') AS user, datname AS database, state, waiting, " +
"coalesce(extract(epoch FROM clock_timestamp() - xact_start), 0) AS active_seconds, " +
"case when waiting = 't' THEN extract(epoch FROM clock_timestamp() - state_change) ELSE 0 END AS waiting_seconds, " +
"left(query, 32) as query " +
"CASE WHEN waiting = 't' THEN extract(epoch FROM clock_timestamp() - state_change) ELSE 0 END AS waiting_seconds, " +
"left(query, 32) AS query " +
"FROM pg_stat_activity"

// postgresActivityQuery96 defines activity query for 9.6 and older.
// postgresActivityQuery96 defines activity query for 9.6.
// Postgres 9.6 doesn't have 'backend_type' attribute.
postgresActivityQuery96 = "SELECT " +
"coalesce(usename, 'system') AS user, datname AS database, state, wait_event_type, wait_event, " +
"coalesce(extract(epoch FROM clock_timestamp() - xact_start), 0) AS active_seconds, " +
"case when wait_event_type = 'Lock' THEN extract(epoch FROM clock_timestamp() - state_change) ELSE 0 END AS waiting_seconds, " +
"left(query, 32) as query " +
"CASE WHEN wait_event_type = 'Lock' THEN extract(epoch FROM clock_timestamp() - state_change) ELSE 0 END AS waiting_seconds, " +
"left(query, 32) AS query " +
"FROM pg_stat_activity"

// postgresActivityQuery13 defines activity query for versions from 10 to 13.
postgresActivityQuery13 = "SELECT " +
"coalesce(usename, backend_type) AS user, datname AS database, state, wait_event_type, wait_event, " +
"coalesce(extract(epoch FROM clock_timestamp() - xact_start), 0) AS active_seconds, " +
"CASE WHEN wait_event_type = 'Lock' THEN extract(epoch FROM clock_timestamp() - state_change) ELSE 0 END AS waiting_seconds, " +
"left(query, 32) AS query " +
"FROM pg_stat_activity"

// postgresActivityQueryLatest defines activity query for recent versions.
// Postgres 14 has pg_locks.waitstart which is better for taking sessions waiting time.
postgresActivityQueryLatest = "SELECT " +
"coalesce(usename, backend_type) AS user, datname AS database, state, wait_event_type, wait_event, " +
"coalesce(extract(epoch FROM clock_timestamp() - xact_start), 0) AS active_seconds, " +
"case when wait_event_type = 'Lock' THEN extract(epoch FROM clock_timestamp() - state_change) ELSE 0 END AS waiting_seconds, " +
"left(query, 32) as query " +
"FROM pg_stat_activity"
"CASE WHEN wait_event_type = 'Lock' " +
"THEN (SELECT extract(epoch FROM clock_timestamp() - max(waitstart)) FROM pg_locks l WHERE l.pid = a.pid) " +
"ELSE 0 END AS waiting_seconds, " +
"left(query, 32) AS query " +
"FROM pg_stat_activity a"

postgresPreparedXactQuery = "SELECT count(*) AS total FROM pg_prepared_xacts"

Expand Down Expand Up @@ -641,6 +652,8 @@ func selectActivityQuery(version int) string {
return postgresActivityQuery95
case version < PostgresV10:
return postgresActivityQuery96
case version < PostgresV14:
return postgresActivityQuery13
default:
return postgresActivityQueryLatest
}
Expand Down
6 changes: 5 additions & 1 deletion internal/collector/postgres_activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ func Test_selectActivityQuery(t *testing.T) {
}{
{version: PostgresV95, want: postgresActivityQuery95},
{version: PostgresV96, want: postgresActivityQuery96},
{version: PostgresV10, want: postgresActivityQueryLatest},
{version: PostgresV10, want: postgresActivityQuery13},
{version: PostgresV11, want: postgresActivityQuery13},
{version: PostgresV12, want: postgresActivityQuery13},
{version: PostgresV13, want: postgresActivityQuery13},
{version: PostgresV14, want: postgresActivityQueryLatest},
}

for _, tc := range testcases {
Expand Down
1 change: 1 addition & 0 deletions internal/collector/postgres_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
PostgresV95 = 90500
PostgresV96 = 90600
PostgresV10 = 100000
PostgresV11 = 110000
PostgresV12 = 120000
PostgresV13 = 130000
PostgresV14 = 140000
Expand Down

0 comments on commit 9ed2fef

Please sign in to comment.