Skip to content

Commit

Permalink
Make lib/ as deprecated.
Browse files Browse the repository at this point in the history
  • Loading branch information
lesovsky committed Nov 7, 2020
1 parent 174ea15 commit 4842aa9
Show file tree
Hide file tree
Showing 42 changed files with 1,037 additions and 990 deletions.
17 changes: 17 additions & 0 deletions internal/math/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package math

// Min returns minimum of two integers
func Min(a, b int) int {
if a > b {
return b
}
return a
}

// Max returns maximum of two integers
func Max(a, b int) int {
if a > b {
return a
}
return b
}
5 changes: 5 additions & 0 deletions internal/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,8 @@ func (db *DB) Close() {
fmt.Printf("close connection failed: %s; ignore", err)
}
}

func (db *DB) PQstatus() error {
var s string
return db.QueryRow("SELECT 1").Scan(&s)
}
84 changes: 84 additions & 0 deletions internal/query/activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package query

const (
// PgStatActivityQueryDefault is the default query for getting stats from pg_stat_activity view
// { Name: "pg_stat_activity", Query: common.PgStatActivityQueryDefault, DiffIntvl: [2]int{99,99}, Ncols: 14, OrderKey: 0, OrderDesc: true }
// regexp_replace() removes extra spaces, tabs and newlines from queries
PgStatActivityQueryDefault = `SELECT
pid,
client_addr AS cl_addr,
client_port AS cl_port,
datname,
usename,
left(application_name, 16) AS appname,
backend_type,
wait_event_type AS wait_etype,
wait_event,
state,
date_trunc('seconds', clock_timestamp() - xact_start)::text AS xact_age,
date_trunc('seconds', clock_timestamp() - query_start)::text AS query_age,
date_trunc('seconds', clock_timestamp() - state_change)::text AS change_age,
regexp_replace(
regexp_replace(query,
E'( |\t)+', ' ', 'g'),
E'\n', '', 'g') AS query
FROM pg_stat_activity
{{ if .ShowNoIdle }}
WHERE ((clock_timestamp() - xact_start) > '{{.QueryAgeThresh}}'::interval OR (clock_timestamp() - query_start) > '{{.QueryAgeThresh}}'::interval)
AND state != 'idle'
{{ end }}
ORDER BY pid DESC`

// PgStatActivityQuery96 queries for getting stats from pg_stat_activity view for versions prior 9.6
// { Name: "pg_stat_activity", Query: common.PgStatActivityQuery96, DiffIntvl: [2]int{99,99}, Ncols: 13, OrderKey: 0, OrderDesc: true }
// regexp_replace() removes extra spaces, tabs and newlines from queries
PgStatActivityQuery96 = `SELECT
pid,
client_addr AS cl_addr,
client_port AS cl_port,
datname,
usename,
left(application_name, 16) AS appname,
wait_event_type AS wait_etype,
wait_event,
state,
date_trunc('seconds', clock_timestamp() - xact_start)::text AS xact_age,
date_trunc('seconds', clock_timestamp() - query_start)::text AS query_age,
date_trunc('seconds', clock_timestamp() - state_change)::text AS change_age,
regexp_replace(
regexp_replace(query,
E'( |\t)+', ' ', 'g'),
E'\n', '', 'g') AS query
FROM pg_stat_activity
{{ if .ShowNoIdle }}
WHERE ((clock_timestamp() - xact_start) > '{{.QueryAgeThresh}}'::interval OR (clock_timestamp() - query_start) > '{{.QueryAgeThresh}}'::interval)
AND state != 'idle'
{{ end }}
ORDER BY pid DESC`

// PgStatActivityQuery95 queries activity stats from pg_stat_activity view from versions prior 9.5
// { Name: "pg_stat_activity", Query: common.PgStatActivityQuery95, DiffIntvl: [2]int{99,99}, Ncols: 12, OrderKey: 0, OrderDesc: true }
// regexp_replace() removes extra spaces, tabs and newlines from queries
PgStatActivityQuery95 = `SELECT
pid,
client_addr AS cl_addr,
client_port AS cl_port,
datname,
usename,
left(application_name, 16) AS appname,
waiting,
state,
date_trunc('seconds', clock_timestamp() - xact_start)::text AS xact_age,
date_trunc('seconds', clock_timestamp() - query_start)::text AS query_age,
date_trunc('seconds', clock_timestamp() - state_change)::text AS change_age,
regexp_replace(
regexp_replace(query,
E'( |\t)+', ' ', 'g'),
E'\n', '', 'g') AS query
FROM pg_stat_activity
{{ if .ShowNoIdle }}
WHERE ((clock_timestamp() - xact_start) > '{{.QueryAgeThresh}}'::interval OR (clock_timestamp() - query_start) > '{{.QueryAgeThresh}}'::interval)
AND state != 'idle'
{{ end }}
ORDER BY pid DESC`
)
50 changes: 50 additions & 0 deletions internal/query/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package query

const (
// PgStatDatabaseQueryDefault is the default query for getting databases' stats from pg_stat_database view
// { Name: "pg_stat_database", Query: common.PgStatDatabaseQueryDefault, DiffIntvl: [2]int{1,16}, Ncols: 18, OrderKey: 0, OrderDesc: true }
PgStatDatabaseQueryDefault = `SELECT
datname,
coalesce(xact_commit, 0) AS commits,
coalesce(xact_rollback, 0) AS rollbacks,
coalesce(blks_read * (SELECT current_setting('block_size')::int / 1024), 0) AS reads,
coalesce(blks_hit, 0) AS hits,
coalesce(tup_returned, 0) AS returned,
coalesce(tup_fetched, 0) AS fetched,
coalesce(tup_inserted, 0) AS inserts,
coalesce(tup_updated, 0) AS updates,
coalesce(tup_deleted, 0) AS deletes,
coalesce(conflicts, 0) AS conflicts,
coalesce(deadlocks, 0) AS deadlocks,
coalesce(checksum_failures, 0) AS csum_fails,
coalesce(temp_files, 0) AS temp_files,
coalesce(temp_bytes, 0) AS temp_bytes,
coalesce(blk_read_time, 0)::numeric(20,2) AS read_t,
coalesce(blk_write_time, 0)::numeric(20,2) AS write_t,
date_trunc('seconds', now() - stats_reset)::text AS stats_age
FROM pg_stat_database
ORDER BY datname DESC`

// PgStatDatabaseQuery11 is the query for getting databases' stats from pg_stat_database view for versions prior 12
// { Name: "pg_stat_database", Query: common.PgStatDatabaseQuery11, DiffIntvl: [2]int{1,15}, Ncols: 17, OrderKey: 0, OrderDesc: true }
PgStatDatabaseQuery11 = `SELECT
datname,
coalesce(xact_commit, 0) AS commits,
coalesce(xact_rollback, 0) AS rollbacks,
coalesce(blks_read * (SELECT current_setting('block_size')::int / 1024), 0) AS reads,
coalesce(blks_hit, 0) AS hits,
coalesce(tup_returned, 0) AS returned,
coalesce(tup_fetched, 0) AS fetched,
coalesce(tup_inserted, 0) AS inserts,
coalesce(tup_updated, 0) AS updates,
coalesce(tup_deleted, 0) AS deletes,
coalesce(conflicts, 0) AS conflicts,
coalesce(deadlocks, 0) AS deadlocks,
coalesce(temp_files, 0) AS temp_files,
coalesce(temp_bytes, 0) AS temp_bytes,
coalesce(blk_read_time, 0)::numeric(20,2) AS read_t,
coalesce(blk_write_time, 0)::numeric(20,2) AS write_t,
date_trunc('seconds', now() - stats_reset)::text AS stats_age
FROM pg_stat_database
ORDER BY datname DESC`
)
17 changes: 17 additions & 0 deletions internal/query/functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package query

const (
// PgStatFunctionsQueryDefault is the default query for getting stats from pg_stat_user_functions view
// { Name: "pg_stat_functions", Query: common.PgStatFunctionsQueryDefault, DiffIntvl: [2]int{3,3}, Ncols: 8, OrderKey: 0, OrderDesc: true }
PgStatFunctionsQueryDefault = `SELECT
funcid,
schemaname ||'.'||funcname AS function,
calls AS total_calls,
calls AS calls,
date_trunc('seconds', total_time / 1000 * '1 second'::interval)::text AS total_t,
date_trunc('seconds', self_time / 1000 * '1 second'::interval)::text AS self_t,
round((total_time / greatest(calls, 1))::numeric(20,2), 4) AS avg_t,
round((self_time / greatest(calls, 1))::numeric(20,2), 4) AS avg_self_t
FROM pg_stat_user_functions
ORDER BY funcid DESC`
)
16 changes: 16 additions & 0 deletions internal/query/indexes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package query

const (
// PgStatIndexesQueryDefault is the default query for getting indexes' stats from pg_stat_all_indexes and pg_statio_all_indexes views
// { Name: "pg_stat_indexes", Query: common.PgStatIndexesQueryDefault, DiffIntvl: [2]int{1,5}, Ncols: 6, OrderKey: 0, OrderDesc: true }
PgStatIndexesQueryDefault = `SELECT
s.schemaname ||'.'|| s.relname ||'.'|| s.indexrelname AS index,
coalesce(s.idx_scan, 0) AS idx_scan,
coalesce(s.idx_tup_read, 0) AS idx_tup_read,
coalesce(s.idx_tup_fetch, 0) AS idx_tup_fetch,
coalesce(i.idx_blks_read * (SELECT current_setting('block_size')::int / 1024), 0) AS idx_read,
coalesce(i.idx_blks_hit, 0) AS idx_hit
FROM pg_stat_{{.ViewType}}_indexes s, pg_statio_{{.ViewType}}_indexes i
WHERE s.indexrelid = i.indexrelid
ORDER BY (s.schemaname ||'.'|| s.relname ||'.'|| s.indexrelname) DESC`
)
24 changes: 24 additions & 0 deletions internal/query/progress_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package query

const (
// PgStatProgressClusterQueryDefault is the default query for getting stats from pg_stat_progress_cluster view
// { Name: "pg_stat_progress_cluster", Query: common.PgStatProgressClusterQueryDefault, DiffIntvl: [2]int{10,11}, Ncols: 13, OrderKey: 0, OrderDesc: true }
PgStatProgressClusterQueryDefault = `SELECT
a.pid,
date_trunc('seconds', clock_timestamp() - xact_start)::text AS xact_age,
p.datname,
p.relid::regclass AS relation,
p.cluster_index_relid::regclass AS index,
a.state,
coalesce((a.wait_event_type ||'.'|| a.wait_event), 'f') AS waiting,
p.phase,
p.heap_blks_total * (SELECT current_setting('block_size')::int / 1024) AS t_size,
round(100 * p.heap_blks_scanned / greatest(p.heap_blks_total,1), 2) AS "scanned_%",
coalesce(p.heap_tuples_scanned, 0) AS tup_scanned,
coalesce(p.heap_tuples_written, 0) AS tup_written,
a.query
FROM pg_stat_progress_cluster p
INNER JOIN pg_stat_activity a ON p.pid = a.pid
WHERE a.pid <> pg_backend_pid()
ORDER BY a.pid DESC`
)
25 changes: 25 additions & 0 deletions internal/query/progress_create_index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package query

const (
// PgStatProgressCreateIndexQueryDefault is the default query for getting stats from pg_stat_progress_cluster view
// { Name: "pg_stat_progress_create_index", Query: common.PgStatProgressCreateIndexQueryDefault, DiffIntvl: [2]int{99,99}, Ncols: 14, OrderKey: 0, OrderDesc: true }
PgStatProgressCreateIndexQueryDefault = `SELECT
a.pid,
date_trunc('seconds', clock_timestamp() - xact_start)::text AS xact_age,
p.datname,
p.relid::regclass AS relation,
p.index_relid::regclass AS index,
a.state,
coalesce((a.wait_event_type ||'.'|| a.wait_event), 'f') AS waiting,
p.phase,
current_locker_pid AS locker_pid,
lockers_total ||'/'|| lockers_done AS lockers,
p.blocks_total * (SELECT current_setting('block_size')::int / 1024) ||'/'|| round(100 * p.blocks_done / greatest(p.blocks_total, 1), 2) AS "size_total/done_%",
p.tuples_total ||'/'|| round(100 * p.tuples_done / greatest(p.tuples_total, 1), 2) AS "tup_total/done_%",
p.partitions_total ||'/'|| round(100 * p.partitions_done / greatest(p.partitions_total, 1), 2) AS "parts_total/done_%",
a.query
FROM pg_stat_progress_create_index p
INNER JOIN pg_stat_activity a ON p.pid = a.pid
WHERE a.pid <> pg_backend_pid()
ORDER BY a.pid DESC`
)
24 changes: 24 additions & 0 deletions internal/query/progress_vacuum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package query

const (
// PgStatProgressVacuumQueryDefault is the default query for getting stats from pg_stat_progress_vacuum view
// { Name: "pg_stat_vacuum", Query: common.PgStatVacuumQueryDefault, DiffIntvl: [2]int{10,11}, Ncols: 13, OrderKey: 0, OrderDesc: true }
PgStatProgressVacuumQueryDefault = `SELECT
a.pid,
date_trunc('seconds', clock_timestamp() - xact_start)::text AS xact_age,
v.datname,
v.relid::regclass AS relation,
a.state,
coalesce((a.wait_event_type ||'.'|| a.wait_event), 'f') AS waiting,
v.phase,
v.heap_blks_total * (SELECT current_setting('block_size')::int / 1024) AS t_size,
round(100 * v.heap_blks_scanned / v.heap_blks_total, 2) AS "t_scanned_%",
round(100 * v.heap_blks_vacuumed / v.heap_blks_total, 2) AS "t_vacuumed_%",
coalesce(v.heap_blks_scanned * (SELECT current_setting('block_size')::int / 1024), 0) AS scanned,
coalesce(v.heap_blks_vacuumed * (SELECT current_setting('block_size')::int / 1024), 0) AS vacuumed,
a.query
FROM pg_stat_progress_vacuum v
RIGHT JOIN pg_stat_activity a ON v.pid = a.pid
WHERE (a.query ~* '^autovacuum:' OR a.query ~* '^vacuum') AND a.pid <> pg_backend_pid()
ORDER BY a.pid DESC`
)
85 changes: 85 additions & 0 deletions internal/query/replication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package query

const (
// PgStatReplicationQueryDefault is the default query for getting replication stats from pg_stat_replication view
// { Name: "pg_stat_replication", Query: common.PgStatReplicationQueryDefault, DiffIntvl: [2]int{6,6}, Ncols: 15, OrderKey: 0, OrderDesc: true }
PgStatReplicationQueryDefault = `SELECT
pid AS pid,
client_addr AS client,
usename AS user,
application_name AS name,
state,
sync_state AS mode,
({{.WalFunction1}}({{.WalFunction2}}(),'0/0') / 1024)::bigint AS wal,
({{.WalFunction1}}({{.WalFunction2}}(),sent_lsn) / 1024)::bigint AS pending,
({{.WalFunction1}}(sent_lsn,write_lsn) / 1024)::bigint AS write,
({{.WalFunction1}}(write_lsn,flush_lsn) / 1024)::bigint AS flush,
({{.WalFunction1}}(flush_lsn,replay_lsn) / 1024)::bigint AS replay,
({{.WalFunction1}}({{.WalFunction2}}(),replay_lsn))::bigint / 1024 AS total_lag,
coalesce(date_trunc('seconds', write_lag), '0 seconds'::interval) AS write_lag,
coalesce(date_trunc('seconds', flush_lag), '0 seconds'::interval) AS flush_lag,
coalesce(date_trunc('seconds', replay_lag), '0 seconds'::interval) AS replay_lag
FROM pg_stat_replication
ORDER BY pid DESC`

// PgStatReplicationQueryExtended is the extended query for getting replication stats from pg_stat_replication view
// { Name: "pg_stat_replication", Query: common.PgStatReplicationQueryExtended, DiffIntvl: [2]int{6,6}, Ncols: 17, OrderKey: 0, OrderDesc: true }
PgStatReplicationQueryExtended = `SELECT
pid AS pid,
client_addr AS client,
usename AS user,
application_name AS name,
state,
sync_state AS mode,
({{.WalFunction1}}({{.WalFunction2}}(),'0/0') / 1024)::bigint AS wal,
({{.WalFunction1}}({{.WalFunction2}}(),sent_lsn) / 1024)::bigint AS pending,
({{.WalFunction1}}(sent_lsn,write_lsn) / 1024)::bigint AS write,
({{.WalFunction1}}(write_lsn,flush_lsn) / 1024)::bigint AS flush,
({{.WalFunction1}}(flush_lsn,replay_lsn) / 1024)::bigint AS replay,
({{.WalFunction1}}({{.WalFunction2}}(),replay_lsn) / 1024)::bigint AS total_lag,
coalesce(date_trunc('seconds', write_lag), '0 seconds'::interval) AS write_lag,
coalesce(date_trunc('seconds', flush_lag), '0 seconds'::interval) AS flush_lag,
coalesce(date_trunc('seconds', replay_lag), '0 seconds'::interval) AS replay_lag,
(pg_last_committed_xact()).xid::text::bigint - backend_xmin::text::bigint as xact_age,
date_trunc('seconds', (pg_last_committed_xact()).timestamp - pg_xact_commit_timestamp(backend_xmin)) as time_age
FROM pg_stat_replication
ORDER BY pid DESC`

// PgStatReplicationQuery96 is the query for getting replication stats from versions prior 9.6
// { Name: "pg_stat_replication", Query: common.PgStatReplicationQuery96, DiffIntvl: [2]int{6,6}, Ncols: 12, OrderKey: 0, OrderDesc: true }
PgStatReplicationQuery96 = `SELECT
pid AS pid,
client_addr AS client,
usename AS user,
application_name AS name,
state,
sync_state AS mode,
({{.WalFunction1}}({{.WalFunction2}}(),'0/0') / 1024)::bigint AS wal,
({{.WalFunction1}}({{.WalFunction2}}(),sent_location) / 1024)::bigint AS pending,
({{.WalFunction1}}(sent_location,write_location) / 1024)::bigint AS write,
({{.WalFunction1}}(write_location,flush_location) / 1024)::bigint AS flush,
({{.WalFunction1}}(flush_location,replay_location) / 1024)::bigint AS replay,
({{.WalFunction1}}({{.WalFunction2}}(),replay_location))::bigint / 1024 AS total_lag
FROM pg_stat_replication
ORDER BY pid DESC`

// PgStatReplicationQuery96Extended is the extended query for getting replication stats from versions prior 9.6
// { Name: "pg_stat_replication", Query: common.PgStatReplicationQuery96Extended, DiffIntvl: [2]int{6,6}, Ncols: 14, OrderKey: 0, OrderDesc: true }
PgStatReplicationQuery96Extended = `SELECT
pid AS pid,
client_addr AS client,
usename AS user,
application_name AS name,
state,
sync_state AS mode,
({{.WalFunction1}}({{.WalFunction2}}(),'0/0') / 1024)::bigint AS wal,
({{.WalFunction1}}({{.WalFunction2}}(),sent_location) / 1024)::bigint AS pending,
({{.WalFunction1}}(sent_location,write_location) / 1024)::bigint AS write,
({{.WalFunction1}}(write_location,flush_location) / 1024)::bigint AS flush,
({{.WalFunction1}}(flush_location,replay_location) / 1024)::bigint AS replay,
({{.WalFunction1}}({{.WalFunction2}}(),replay_location))::bigint / 1024 AS total_lag,
(pg_last_committed_xact()).xid::text::bigint - backend_xmin::text::bigint as xact_age,
date_trunc('seconds', (pg_last_committed_xact()).timestamp - pg_xact_commit_timestamp(backend_xmin)) as time_age
FROM pg_stat_replication
ORDER BY pid DESC`
)
19 changes: 19 additions & 0 deletions internal/query/sizes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package query

const (
// PgTablesSizesQueryDefault is the defaulr query for getting stats related to tables' sizes
// { Name: "pg_tables_sizes", Query: common.PgTablesSizesQueryDefault, DiffIntvl: [2]int{4,6}, Ncols: 7, OrderKey: 0, OrderDesc: true }
PgTablesSizesQueryDefault = `SELECT
s.schemaname ||'.'|| s.relname AS relation,
pg_total_relation_size((s.schemaname ||'.'|| s.relname)::regclass) / 1024 AS total_size,
pg_relation_size((s.schemaname ||'.'|| s.relname)::regclass) / 1024 AS rel_size,
(pg_total_relation_size((s.schemaname ||'.'|| s.relname)::regclass) / 1024) -
(pg_relation_size((s.schemaname ||'.'|| s.relname)::regclass) / 1024) AS idx_size,
pg_total_relation_size((s.schemaname ||'.'|| s.relname)::regclass) / 1024 AS total_change,
pg_relation_size((s.schemaname ||'.'|| s.relname)::regclass) / 1024 AS rel_change,
(pg_total_relation_size((s.schemaname ||'.'|| s.relname)::regclass) / 1024) -
(pg_relation_size((s.schemaname ||'.'|| s.relname)::regclass) / 1024) AS idx_change
FROM pg_stat_{{.ViewType}}_tables s, pg_class c
WHERE s.relid = c.oid AND NOT EXISTS (SELECT 1 FROM pg_locks WHERE relation = s.relid AND mode = 'AccessExclusiveLock' and granted)
ORDER BY (s.schemaname || '.' || s.relname) DESC`
)
Loading

0 comments on commit 4842aa9

Please sign in to comment.