Merge pull request #25 from ibrarahmad/master

Major refactoring to support multiple new features.
pull/36/head
Ibrar Ahmed 2020-02-28 01:21:16 +05:00 committed by GitHub
commit 76015097f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 933 additions and 1634 deletions

View File

@ -4,7 +4,6 @@ MODULE_big = pg_stat_monitor
OBJS = pg_stat_monitor.o $(WIN32RES)
EXTENSION = pg_stat_monitor
DATA = pg_stat_monitor--1.0.sql
PGFILEDESC = "pg_stat_monitor - execution statistics of SQL statements"

View File

@ -18,11 +18,11 @@ There are two ways to install pg_stat_monitor. The first is by downloading the p
##### Download and compile
The latest release of pg_stat_monitor can be downloaded from this GitHub page:
https://github.com/Percona-Lab/pg_stat_monitor/releases
https://github.com/Percona/pg_stat_monitor/releases
or it can be downloaded using the git:
git clone git://github.com/Percona-Lab/pg_stat_monitor.git
git clone git://github.com/Percona/pg_stat_monitor.git
After downloading the code, set the path for the [PostgreSQL][1] binary:
@ -57,13 +57,15 @@ There are four views, and complete statistics can be accessed using these views.
##### pg_stat_monitor
This is the main view which stores per query-based statistics, similar to pg_stat_statment with some additional columns.
\d pg_stat_monitor;
postgres=# \d pg_stat_monitor;
View "public.pg_stat_monitor"
Column | Type | Collation | Nullable | Default
---------------------+------------------+-----------+----------+---------
---------------------+--------------------------+-----------+----------+---------
bucket | oid | | |
bucket_start_time | timestamp with time zone | | |
userid | oid | | |
dbid | oid | | |
queryid | bigint | | |
queryid | text | | |
query | text | | |
calls | bigint | | |
total_time | double precision | | |
@ -71,7 +73,7 @@ This is the main view which stores per query-based statistics, similar to pg_sta
max_time | double precision | | |
mean_time | double precision | | |
stddev_time | double precision | | |
rows | bigint | | |
int8 | bigint | | |
shared_blks_hit | bigint | | |
shared_blks_read | bigint | | |
shared_blks_dirtied | bigint | | |
@ -84,19 +86,17 @@ This is the main view which stores per query-based statistics, similar to pg_sta
temp_blks_written | bigint | | |
blk_read_time | double precision | | |
blk_write_time | double precision | | |
host | integer | | |
hist_calls | text | | |
hist_min_time | text | | |
hist_max_time | text | | |
hist_mean_time | text | | |
slow_query | text | | |
host | bigint | | |
client_ip | inet | | |
resp_calls | text[] | | |
cpu_user_time | double precision | | |
cpu_sys_time | double precision | | |
tables_names | text[] | | |
These are new column added to have more detail about the query.
host: Client IP or Hostname
client_ip: Client IP or Hostname
hist_calls: Hourly based 24 hours calls of query histogram
hist_min_time: Hourly based 24 hours min time of query histogram
Hist_max_time: Hourly based 24 hours max time of query histogram
@ -106,84 +106,72 @@ These are new column added to have more detail about the query.
cpu_sys_time: CPU System time for that query.
regression=# \d pg_stat_agg_database
postgres=# \d pg_stat_agg_database
View "public.pg_stat_agg_database"
Column | Type | Collation | Nullable | Default
----------------+--------------------------+-----------+----------+---------
queryid | bigint | | |
---------------+------------------+-----------+----------+---------
bucket | oid | | |
queryid | text | | |
dbid | bigint | | |
userid | oid | | |
host | inet | | |
client_ip | inet | | |
total_calls | integer | | |
min_time | double precision | | |
max_time | double precision | | |
mean_time | double precision | | |
hist_calls | text[] | | |
hist_min_time | text[] | | |
hist_max_time | text[] | | |
hist_mean_time | text[] | | |
first_log_time | timestamp with time zone | | |
last_log_time | timestamp with time zone | | |
resp_calls | text[] | | |
cpu_user_time | double precision | | |
cpu_sys_time | double precision | | |
query | text | | |
slow_query | text | | |
tables_names | text[] | | |
# \d pg_stat_agg_user
postgres=# \d pg_stat_agg_user
View "public.pg_stat_agg_user"
Column | Type | Collation | Nullable | Default
----------------+--------------------------+-----------+----------+---------
queryid | bigint | | |
---------------+------------------+-----------+----------+---------
bucket | oid | | |
queryid | text | | |
dbid | bigint | | |
userid | oid | | |
host | inet | | |
client_ip | inet | | |
total_calls | integer | | |
min_time | double precision | | |
max_time | double precision | | |
mean_time | double precision | | |
hist_calls | text[] | | |
hist_min_time | text[] | | |
hist_max_time | text[] | | |
hist_mean_time | text[] | | |
first_log_time | timestamp with time zone | | |
last_log_time | timestamp with time zone | | |
resp_calls | text[] | | |
cpu_user_time | double precision | | |
cpu_sys_time | double precision | | |
query | text | | |
slow_query | text | | |
tables_names | text[] | | |
# \d pg_stat_agg_host
View "public.pg_stat_agg_host"
postgres=# \d pg_stat_agg_ip
View "public.pg_stat_agg_ip"
Column | Type | Collation | Nullable | Default
----------------+--------------------------+-----------+----------+---------
queryid | bigint | | |
---------------+------------------+-----------+----------+---------
bucket | oid | | |
queryid | text | | |
dbid | bigint | | |
userid | oid | | |
host | inet | | |
client_ip | inet | | |
host | bigint | | |
total_calls | integer | | |
min_time | double precision | | |
max_time | double precision | | |
mean_time | double precision | | |
hist_calls | text[] | | |
hist_min_time | text[] | | |
hist_max_time | text[] | | |
hist_mean_time | text[] | | |
first_log_time | timestamp with time zone | | |
last_log_time | timestamp with time zone | | |
resp_calls | text[] | | |
cpu_user_time | double precision | | |
cpu_sys_time | double precision | | |
query | text | | |
slow_query | text | | |
tables_names | text[] | | |
Examples
1 - In this query we are getting the exact value of f1 which is '05:06:07-07' in the case of slow queries.
1 - In this query we are getting the exact value of f1 which is '05:06:07-07' (special setting is required for this).
# select userid, queryid, query, slow_query, max_time, total_calls from pg_stat_agg_user;
# select userid, queryid, query, max_time, total_calls from pg_stat_agg_user;
-[ RECORD 1 ]----------------------------------------------------------------------------------------
userid | 10
queryid | -203926152419851453
query | SELECT f1 FROM TIMETZ_TBL WHERE f1 < $1
slow_query | SELECT f1 FROM TIMETZ_TBL WHERE f1 < '05:06:07-07';
query | SELECT f1 FROM TIMETZ_TBL WHERE f1 < '05:06:07-07';
max_time | 1.237875
total_calls | 8
@ -206,6 +194,6 @@ Copyright (c) 2006 - 2019, Percona LLC.
See [`LICENSE`][2] for full details.
[1]: https://www.postgresql.org/
[2]: https://github.com/Percona-Lab/pg_stat_monitor/blob/master/LICENSE
[3]: https://github.com/Percona-Lab/pg_stat_monitor/issues/new
[2]: https://github.com/Percona/pg_stat_monitor/blob/master/LICENSE
[3]: https://github.com/Percona/pg_stat_monitor/issues/new
[4]: CONTRIBUTING.md

View File

@ -1,4 +1,4 @@
/* contrib/pg_stat_monitor/pg_stat_monitor--1.4.sql */
/* contrib/pg_stat_monitor/pg_stat_monitor--1.1.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pg_stat_monitor" to load this file. \quit
@ -10,10 +10,13 @@ AS 'MODULE_PATHNAME'
LANGUAGE C PARALLEL SAFE;
CREATE FUNCTION pg_stat_monitor(IN showtext boolean,
OUT bucket oid,
OUT userid oid,
OUT dbid oid,
OUT queryid bigint,
OUT queryid text,
OUT query text,
OUT bucket_start_time timestamptz,
OUT calls int8,
OUT total_time float8,
OUT min_time float8,
@ -33,109 +36,126 @@ CREATE FUNCTION pg_stat_monitor(IN showtext boolean,
OUT temp_blks_written int8,
OUT blk_read_time float8,
OUT blk_write_time float8,
OUT host bigint,
OUT hist_calls text,
OUT hist_min_time text,
OUT hist_max_time text,
OUT hist_mean_time text,
OUT slow_query text,
OUT client_ip bigint,
OUT resp_calls text,
OUT cpu_user_time float8,
OUT cpu_sys_time float8
OUT cpu_sys_time float8,
OUT tables_names text
)
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'pg_stat_monitor_1_3'
AS 'MODULE_PATHNAME', 'pg_stat_monitor'
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
CREATE FUNCTION pg_stat_agg(
OUT queryid bigint,
OUT queryid text,
OUT id bigint,
OUT type bigint,
OUT total_calls int,
OUT first_call_time timestamptz,
OUT last_call_time timestamptz)
OUT total_calls int)
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'pg_stat_agg'
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
-- Register a view on the function for ease of use.
CREATE VIEW pg_stat_monitor AS
SELECT * FROM pg_stat_monitor(true);
CREATE VIEW pg_stat_monitor AS SELECT
bucket,
bucket_start_time,
userid,
dbid,
queryid,
query,
calls,
total_time,
min_time,
max_time,
mean_time,
stddev_time,
rows int8,
shared_blks_hit,
shared_blks_read,
shared_blks_dirtied,
shared_blks_written,
local_blks_hit,
local_blks_read,
local_blks_dirtied,
local_blks_written,
temp_blks_read,
temp_blks_written,
blk_read_time,
blk_write_time,
client_ip as host,
'0.0.0.0'::inet + client_ip AS client_ip,
(string_to_array(resp_calls, ',')) resp_calls,
cpu_user_time,
cpu_sys_time,
(string_to_array(tables_names, ',')) tables_names
FROM pg_stat_monitor(true);
GRANT SELECT ON pg_stat_monitor TO PUBLIC;
CREATE VIEW pg_stat_agg_database AS
SELECT
ss.bucket,
agg.queryid,
agg.id AS dbid,
ss.userid,
'0.0.0.0'::inet + ss.host AS host,
client_ip,
agg.total_calls,
ss.min_time,
ss.max_time,
ss.mean_time,
(string_to_array(hist_calls, ',')) hist_calls,
(string_to_array(hist_min_time, ',')) hist_min_time,
(string_to_array(hist_max_time, ',')) hist_max_time,
(string_to_array(hist_mean_time, ',')) hist_mean_time,
agg.first_call_time AS first_log_time,
agg.last_call_time AS last_log_time,
ss.resp_calls,
ss.cpu_user_time,
ss.cpu_sys_time,
ss.query,
ss.slow_query
ss.tables_names
FROM pg_stat_agg() agg
INNER JOIN (SELECT DISTINCT queryid, dbid, userid, query, host, min_time, max_time, mean_time, hist_calls, hist_min_time, hist_max_time,hist_mean_time,slow_query,cpu_user_time,cpu_sys_time
INNER JOIN (SELECT DISTINCT bucket, queryid, dbid, userid, query, client_ip, min_time, max_time, mean_time, resp_calls, tables_names, cpu_user_time,cpu_sys_time
FROM pg_stat_monitor) ss
ON agg.queryid = ss.queryid AND agg.type = 0 AND id = dbid;
CREATE VIEW pg_stat_agg_user AS
SELECT
ss.bucket,
agg.queryid,
agg.id AS dbid,
ss.userid,
'0.0.0.0'::inet + ss.host AS host,
client_ip,
agg.total_calls,
ss.min_time,
ss.max_time,
ss.mean_time,
(string_to_array(hist_calls, ',')) hist_calls,
(string_to_array(hist_min_time, ',')) hist_min_time,
(string_to_array(hist_max_time, ',')) hist_max_time,
(string_to_array(hist_mean_time, ',')) hist_mean_time,
agg.first_call_time AS first_log_time,
agg.last_call_time AS last_log_time,
ss.resp_calls,
ss.cpu_user_time,
ss.cpu_sys_time,
ss.query,
ss.slow_query
ss.tables_names
FROM pg_stat_agg() agg
INNER JOIN (SELECT DISTINCT queryid, userid, query, host, min_time, max_time, mean_time, hist_calls, hist_min_time, hist_max_time,hist_mean_time,slow_query,cpu_user_time,cpu_sys_time FROM pg_stat_monitor) ss
INNER JOIN (SELECT DISTINCT bucket, queryid, userid, query, client_ip, min_time, max_time, mean_time, resp_calls, tables_names, cpu_user_time,cpu_sys_time FROM pg_stat_monitor) ss
ON agg.queryid = ss.queryid AND agg.type = 1 AND id = userid;
CREATE VIEW pg_stat_agg_host AS
CREATE VIEW pg_stat_agg_ip AS
SELECT
ss.bucket,
agg.queryid,
agg.id AS dbid,
ss.userid,
'0.0.0.0'::inet + ss.host AS host,
ss.client_ip,
ss.host,
agg.total_calls,
ss.min_time,
ss.max_time,
ss.mean_time,
(string_to_array(hist_calls, ',')) hist_calls,
(string_to_array(hist_min_time, ',')) hist_min_time,
(string_to_array(hist_max_time, ',')) hist_max_time,
(string_to_array(hist_mean_time, ',')) hist_mean_time,
agg.first_call_time AS first_log_time,
agg.last_call_time AS last_log_time,
ss.resp_calls,
ss.cpu_user_time,
ss.cpu_sys_time,
ss.query,
ss.slow_query
ss.tables_names
FROM pg_stat_agg() agg
INNER JOIN (SELECT DISTINCT queryid, userid, query, host, min_time, max_time, mean_time, hist_calls, hist_min_time, hist_max_time,hist_mean_time,slow_query,cpu_user_time,cpu_sys_time FROM pg_stat_monitor) ss
INNER JOIN (SELECT DISTINCT bucket, queryid, userid, query, client_ip, host, min_time, max_time, mean_time, resp_calls, tables_names, cpu_user_time,cpu_sys_time FROM pg_stat_monitor) ss
ON agg.queryid = ss.queryid AND agg.type = 2 AND id = host;
GRANT SELECT ON pg_stat_agg_user TO PUBLIC;
GRANT SELECT ON pg_stat_agg_ip TO PUBLIC;
GRANT SELECT ON pg_stat_agg_database TO PUBLIC;
-- Don't want this to be available to non-superusers.

File diff suppressed because it is too large Load Diff