commit
d7e8a0ae79
20
Makefile
20
Makefile
|
@ -4,7 +4,7 @@ MODULE_big = pg_stat_monitor
|
|||
OBJS = hash_query.o guc.o pg_stat_monitor.o $(WIN32RES)
|
||||
|
||||
EXTENSION = pg_stat_monitor
|
||||
DATA = pg_stat_monitor--1.0.sql
|
||||
DATA = pg_stat_monitor--1.0.sql pg_stat_monitor--2.0.sql pg_stat_monitor--1.0--2.0.sql
|
||||
|
||||
PGFILEDESC = "pg_stat_monitor - execution statistics of SQL statements"
|
||||
|
||||
|
@ -19,7 +19,6 @@ REGRESS = basic version guc counters relations database error_insert application
|
|||
# NO_INSTALLCHECK = 1
|
||||
|
||||
PG_CONFIG = pg_config
|
||||
PGSM_INPUT_SQL_VERSION := 1.0
|
||||
|
||||
ifdef USE_PGXS
|
||||
MAJORVERSION := $(shell pg_config --version | awk {'print $$2'} | cut -f1 -d".")
|
||||
|
@ -31,20 +30,3 @@ top_builddir = ../..
|
|||
include $(top_builddir)/src/Makefile.global
|
||||
include $(top_srcdir)/contrib/contrib-global.mk
|
||||
endif
|
||||
|
||||
ifeq ($(shell test $(MAJORVERSION) -gt 12; echo $$?),0)
|
||||
PGSM_INPUT_SQL_VERSION := ${PGSM_INPUT_SQL_VERSION}.${MAJORVERSION}
|
||||
endif
|
||||
|
||||
$(info Using pg_stat_monitor--${PGSM_INPUT_SQL_VERSION}.sql.in file to generate sql filea.)
|
||||
|
||||
ifneq (,$(wildcard ../pg_stat_monitor--${PGSM_INPUT_SQL_VERSION}.sql.in))
|
||||
CP := $(shell cp -v ../pg_stat_monitor--${PGSM_INPUT_SQL_VERSION}.sql.in ../pg_stat_monitor--1.0.sql)
|
||||
endif
|
||||
ifneq (,$(wildcard pg_stat_monitor--${PGSM_INPUT_SQL_VERSION}.sql.in))
|
||||
CP := $(shell cp -v pg_stat_monitor--${PGSM_INPUT_SQL_VERSION}.sql.in pg_stat_monitor--1.0.sql)
|
||||
endif
|
||||
|
||||
clean:
|
||||
rm -rf ${DATA}
|
||||
rm -rf t/results
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
/* contrib/pg_stat_monitor/pg_stat_monitor--1.0--2.0.sql */
|
||||
|
||||
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
||||
\echo Use "ALTER EXTENSION pg_stat_monitor" to load this file. \quit
|
||||
|
|
@ -1,266 +0,0 @@
|
|||
/* contrib/pg_stat_monitor/pg_stat_monitor--1.0.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
|
||||
|
||||
-- Register functions.
|
||||
CREATE FUNCTION pg_stat_monitor_reset()
|
||||
RETURNS void
|
||||
AS 'MODULE_PATHNAME'
|
||||
LANGUAGE C PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION pg_stat_monitor_version()
|
||||
RETURNS text
|
||||
AS 'MODULE_PATHNAME'
|
||||
LANGUAGE C PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION get_histogram_timings()
|
||||
RETURNS text
|
||||
AS 'MODULE_PATHNAME'
|
||||
LANGUAGE C PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION range()
|
||||
RETURNS text[] AS $$
|
||||
SELECT string_to_array(get_histogram_timings(), ',');
|
||||
$$ LANGUAGE SQL;
|
||||
|
||||
CREATE FUNCTION pg_stat_monitor_internal(IN showtext boolean,
|
||||
OUT bucket int8, -- 0
|
||||
OUT userid oid,
|
||||
OUT dbid oid,
|
||||
OUT client_ip int8,
|
||||
|
||||
OUT queryid text, -- 4
|
||||
OUT planid text,
|
||||
OUT query text,
|
||||
OUT query_plan text,
|
||||
OUT state_code int8,
|
||||
OUT top_queryid text,
|
||||
OUT top_query text,
|
||||
OUT application_name text,
|
||||
|
||||
OUT relations text, -- 11
|
||||
OUT cmd_type int,
|
||||
OUT elevel int,
|
||||
OUT sqlcode TEXT,
|
||||
OUT message text,
|
||||
OUT bucket_start_time text,
|
||||
|
||||
OUT calls int8, -- 16
|
||||
|
||||
OUT total_exec_time float8,
|
||||
OUT min_exec_time float8,
|
||||
OUT max_exec_time float8,
|
||||
OUT mean_exec_time float8,
|
||||
OUT stddev_exec_time float8,
|
||||
|
||||
OUT rows_retrieved int8,
|
||||
|
||||
OUT plans_calls int8, -- 23
|
||||
|
||||
OUT total_plan_time float8,
|
||||
OUT min_plan_time float8,
|
||||
OUT max_plan_time float8,
|
||||
OUT mean_plan_time float8,
|
||||
OUT stddev_plan_time float8,
|
||||
|
||||
OUT shared_blks_hit int8, -- 29
|
||||
OUT shared_blks_read int8,
|
||||
OUT shared_blks_dirtied int8,
|
||||
OUT shared_blks_written int8,
|
||||
OUT local_blks_hit int8,
|
||||
OUT local_blks_read int8,
|
||||
OUT local_blks_dirtied int8,
|
||||
OUT local_blks_written int8,
|
||||
OUT temp_blks_read int8,
|
||||
OUT temp_blks_written int8,
|
||||
OUT blk_read_time float8,
|
||||
OUT blk_write_time float8,
|
||||
OUT resp_calls text, -- 41
|
||||
OUT cpu_user_time float8,
|
||||
OUT cpu_sys_time float8,
|
||||
OUT wal_records int8,
|
||||
OUT wal_fpi int8,
|
||||
OUT wal_bytes numeric,
|
||||
OUT comments TEXT,
|
||||
OUT toplevel BOOLEAN
|
||||
)
|
||||
RETURNS SETOF record
|
||||
AS 'MODULE_PATHNAME', 'pg_stat_monitor'
|
||||
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION get_state(state_code int8) RETURNS TEXT AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN state_code = 0 THEN 'PARSING'
|
||||
WHEN state_code = 1 THEN 'PLANNING'
|
||||
WHEN state_code = 2 THEN 'ACTIVE'
|
||||
WHEN state_code = 3 THEN 'FINISHED'
|
||||
WHEN state_code = 4 THEN 'FINISHED WITH ERROR'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION get_cmd_type (cmd_type INTEGER) RETURNS TEXT AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN cmd_type = 0 THEN ''
|
||||
WHEN cmd_type = 1 THEN 'SELECT'
|
||||
WHEN cmd_type = 2 THEN 'UPDATE'
|
||||
WHEN cmd_type = 3 THEN 'INSERT'
|
||||
WHEN cmd_type = 4 THEN 'DELETE'
|
||||
WHEN cmd_type = 5 THEN 'UTILITY'
|
||||
WHEN cmd_type = 6 THEN 'NOTHING'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION pg_stat_monitor_settings(
|
||||
OUT name text,
|
||||
OUT value text,
|
||||
OUT default_value text,
|
||||
OUT description text,
|
||||
OUT minimum INTEGER,
|
||||
OUT maximum INTEGER,
|
||||
OUT options text,
|
||||
OUT restart text
|
||||
)
|
||||
RETURNS SETOF record
|
||||
AS 'MODULE_PATHNAME', 'pg_stat_monitor_settings'
|
||||
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
|
||||
|
||||
CREATE VIEW pg_stat_monitor_settings AS SELECT
|
||||
name,
|
||||
value,
|
||||
default_value,
|
||||
description,
|
||||
minimum,
|
||||
maximum,
|
||||
options,
|
||||
restart
|
||||
FROM pg_stat_monitor_settings();
|
||||
|
||||
-- Register a view on the function for ease of use.
|
||||
CREATE VIEW pg_stat_monitor AS SELECT
|
||||
bucket,
|
||||
bucket_start_time AS bucket_start_time,
|
||||
userid::regrole,
|
||||
datname,
|
||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
||||
queryid,
|
||||
top_queryid,
|
||||
query,
|
||||
comments,
|
||||
planid,
|
||||
query_plan,
|
||||
top_query,
|
||||
application_name,
|
||||
string_to_array(relations, ',') AS relations,
|
||||
cmd_type,
|
||||
get_cmd_type(cmd_type) AS cmd_type_text,
|
||||
elevel,
|
||||
sqlcode,
|
||||
message,
|
||||
calls,
|
||||
total_exec_time,
|
||||
min_exec_time,
|
||||
max_exec_time,
|
||||
mean_exec_time,
|
||||
stddev_exec_time,
|
||||
|
||||
rows_retrieved,
|
||||
|
||||
plans_calls,
|
||||
|
||||
total_plan_time,
|
||||
min_plan_time,
|
||||
max_plan_time,
|
||||
mean_plan_time,
|
||||
stddev_plan_time,
|
||||
|
||||
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,
|
||||
(string_to_array(resp_calls, ',')) resp_calls,
|
||||
cpu_user_time,
|
||||
cpu_sys_time,
|
||||
wal_records,
|
||||
wal_fpi,
|
||||
wal_bytes,
|
||||
state_code,
|
||||
get_state(state_code) as state
|
||||
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
|
||||
ORDER BY bucket_start_time;
|
||||
|
||||
CREATE FUNCTION decode_error_level(elevel int)
|
||||
RETURNS text
|
||||
AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN elevel = 0 THEN ''
|
||||
WHEN elevel = 10 THEN 'DEBUG5'
|
||||
WHEN elevel = 11 THEN 'DEBUG4'
|
||||
WHEN elevel = 12 THEN 'DEBUG3'
|
||||
WHEN elevel = 13 THEN 'DEBUG2'
|
||||
WHEN elevel = 14 THEN 'DEBUG1'
|
||||
WHEN elevel = 15 THEN 'LOG'
|
||||
WHEN elevel = 16 THEN 'LOG_SERVER_ONLY'
|
||||
WHEN elevel = 17 THEN 'INFO'
|
||||
WHEN elevel = 18 THEN 'NOTICE'
|
||||
WHEN elevel = 19 THEN 'WARNING'
|
||||
WHEN elevel = 20 THEN 'ERROR'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION histogram(_bucket int, _quryid text)
|
||||
RETURNS SETOF RECORD AS $$
|
||||
DECLARE
|
||||
rec record;
|
||||
BEGIN
|
||||
for rec in
|
||||
with stat as (select queryid, bucket, unnest(range()) as range, unnest(resp_calls)::int freq from pg_stat_monitor) select range, freq, repeat('■', (freq::float / max(freq) over() * 30)::int) as bar from stat where queryid = _quryid and bucket = _bucket
|
||||
loop
|
||||
return next rec;
|
||||
end loop;
|
||||
END
|
||||
$$ language plpgsql;
|
||||
|
||||
--CREATE FUNCTION pg_stat_monitor_hook_stats(
|
||||
-- OUT hook text,
|
||||
-- OUT min_time float8,
|
||||
-- OUT max_time float8,
|
||||
-- OUT total_time float8,
|
||||
-- OUT ncalls int8
|
||||
--)
|
||||
--RETURNS SETOF record
|
||||
--AS 'MODULE_PATHNAME', 'pg_stat_monitor_hook_stats'
|
||||
--LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
|
||||
|
||||
--CREATE VIEW pg_stat_monitor_hook_stats AS SELECT
|
||||
-- hook,
|
||||
-- min_time,
|
||||
-- max_time,
|
||||
-- total_time,
|
||||
-- total_time / greatest(ncalls, 1) as avg_time,
|
||||
-- ncalls,
|
||||
-- ROUND(CAST(total_time / greatest(sum(total_time) OVER(), 0.00000001) * 100 as numeric), 2)::text || '%' as load_comparison
|
||||
-- FROM pg_stat_monitor_hook_stats();
|
||||
|
||||
GRANT SELECT ON pg_stat_monitor TO PUBLIC;
|
||||
GRANT SELECT ON pg_stat_monitor_settings TO PUBLIC;
|
||||
|
||||
-- Don't want this to be available to non-superusers.
|
||||
REVOKE ALL ON FUNCTION pg_stat_monitor_reset() FROM PUBLIC;
|
|
@ -24,7 +24,102 @@ RETURNS text[] AS $$
|
|||
SELECT string_to_array(get_histogram_timings(), ',');
|
||||
$$ LANGUAGE SQL;
|
||||
|
||||
CREATE FUNCTION pg_stat_monitor_internal(IN showtext boolean,
|
||||
-- Some generic utility function used internally.
|
||||
|
||||
CREATE FUNCTION get_state(state_code int8) RETURNS TEXT AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN state_code = 0 THEN 'PARSING'
|
||||
WHEN state_code = 1 THEN 'PLANNING'
|
||||
WHEN state_code = 2 THEN 'ACTIVE'
|
||||
WHEN state_code = 3 THEN 'FINISHED'
|
||||
WHEN state_code = 4 THEN 'FINISHED WITH ERROR'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION get_cmd_type (cmd_type INTEGER) RETURNS TEXT AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN cmd_type = 0 THEN ''
|
||||
WHEN cmd_type = 1 THEN 'SELECT'
|
||||
WHEN cmd_type = 2 THEN 'UPDATE'
|
||||
WHEN cmd_type = 3 THEN 'INSERT'
|
||||
WHEN cmd_type = 4 THEN 'DELETE'
|
||||
WHEN cmd_type = 5 THEN 'UTILITY'
|
||||
WHEN cmd_type = 6 THEN 'NOTHING'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION pg_stat_monitor_settings(
|
||||
OUT name text,
|
||||
OUT value text,
|
||||
OUT default_value text,
|
||||
OUT description text,
|
||||
OUT minimum INTEGER,
|
||||
OUT maximum INTEGER,
|
||||
OUT options text,
|
||||
OUT restart text
|
||||
)
|
||||
RETURNS SETOF record
|
||||
AS 'MODULE_PATHNAME', 'pg_stat_monitor_settings'
|
||||
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
|
||||
|
||||
CREATE VIEW pg_stat_monitor_settings AS SELECT
|
||||
name,
|
||||
value,
|
||||
default_value,
|
||||
description,
|
||||
minimum,
|
||||
maximum,
|
||||
options,
|
||||
restart
|
||||
FROM pg_stat_monitor_settings();
|
||||
|
||||
CREATE FUNCTION decode_error_level(elevel int)
|
||||
RETURNS text
|
||||
AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN elevel = 0 THEN ''
|
||||
WHEN elevel = 10 THEN 'DEBUG5'
|
||||
WHEN elevel = 11 THEN 'DEBUG4'
|
||||
WHEN elevel = 12 THEN 'DEBUG3'
|
||||
WHEN elevel = 13 THEN 'DEBUG2'
|
||||
WHEN elevel = 14 THEN 'DEBUG1'
|
||||
WHEN elevel = 15 THEN 'LOG'
|
||||
WHEN elevel = 16 THEN 'LOG_SERVER_ONLY'
|
||||
WHEN elevel = 17 THEN 'INFO'
|
||||
WHEN elevel = 18 THEN 'NOTICE'
|
||||
WHEN elevel = 19 THEN 'WARNING'
|
||||
WHEN elevel = 20 THEN 'ERROR'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION histogram(_bucket int, _quryid text)
|
||||
RETURNS SETOF RECORD AS $$
|
||||
DECLARE
|
||||
rec record;
|
||||
BEGIN
|
||||
FOR rec IN
|
||||
WITH stat AS (select queryid, bucket, unnest(range()) AS range,
|
||||
unnest(resp_calls)::int freq FROM pg_stat_monitor) select range,
|
||||
freq, repeat('■', (freq::float / max(freq) over() * 30)::int) AS bar
|
||||
FROM stat WHERE queryid = _quryid and bucket = _bucket
|
||||
LOOP
|
||||
RETURN next rec;
|
||||
END loop;
|
||||
END
|
||||
$$ language plpgsql;
|
||||
|
||||
-- pg_stat_monitor internal function, must not call outside from this file.
|
||||
CREATE FUNCTION pg_stat_monitor_internal(
|
||||
IN showtext boolean,
|
||||
OUT bucket int8, -- 0
|
||||
OUT userid oid,
|
||||
OUT dbid oid,
|
||||
|
@ -89,60 +184,10 @@ RETURNS SETOF record
|
|||
AS 'MODULE_PATHNAME', 'pg_stat_monitor'
|
||||
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION get_state(state_code int8) RETURNS TEXT AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN state_code = 0 THEN 'PARSING'
|
||||
WHEN state_code = 1 THEN 'PLANNING'
|
||||
WHEN state_code = 2 THEN 'ACTIVE'
|
||||
WHEN state_code = 3 THEN 'FINISHED'
|
||||
WHEN state_code = 4 THEN 'FINISHED WITH ERROR'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION get_cmd_type (cmd_type INTEGER) RETURNS TEXT AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN cmd_type = 0 THEN ''
|
||||
WHEN cmd_type = 1 THEN 'SELECT'
|
||||
WHEN cmd_type = 2 THEN 'UPDATE'
|
||||
WHEN cmd_type = 3 THEN 'INSERT'
|
||||
WHEN cmd_type = 4 THEN 'DELETE'
|
||||
WHEN cmd_type = 5 THEN 'UTILITY'
|
||||
WHEN cmd_type = 6 THEN 'NOTHING'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION pg_stat_monitor_settings(
|
||||
OUT name text,
|
||||
OUT value text,
|
||||
OUT default_value text,
|
||||
OUT description text,
|
||||
OUT minimum INTEGER,
|
||||
OUT maximum INTEGER,
|
||||
OUT options text,
|
||||
OUT restart text
|
||||
)
|
||||
RETURNS SETOF record
|
||||
AS 'MODULE_PATHNAME', 'pg_stat_monitor_settings'
|
||||
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
|
||||
|
||||
CREATE VIEW pg_stat_monitor_settings AS SELECT
|
||||
name,
|
||||
value,
|
||||
default_value,
|
||||
description,
|
||||
minimum,
|
||||
maximum,
|
||||
options,
|
||||
restart
|
||||
FROM pg_stat_monitor_settings();
|
||||
|
||||
-- Register a view on the function for ease of use.
|
||||
CREATE FUNCTION pgsm_create_11_view() RETURNS INT AS
|
||||
$$
|
||||
BEGIN
|
||||
CREATE VIEW pg_stat_monitor AS SELECT
|
||||
bucket,
|
||||
bucket_start_time AS bucket_start_time,
|
||||
|
@ -170,17 +215,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
|||
max_exec_time,
|
||||
mean_exec_time,
|
||||
stddev_exec_time,
|
||||
|
||||
rows_retrieved,
|
||||
|
||||
plans_calls,
|
||||
|
||||
total_plan_time,
|
||||
min_plan_time,
|
||||
max_plan_time,
|
||||
mean_plan_time,
|
||||
stddev_plan_time,
|
||||
|
||||
shared_blks_hit,
|
||||
shared_blks_read,
|
||||
shared_blks_dirtied,
|
||||
|
@ -203,65 +238,155 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
|||
get_state(state_code) as state
|
||||
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
|
||||
ORDER BY bucket_start_time;
|
||||
RETURN 0;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE FUNCTION decode_error_level(elevel int)
|
||||
RETURNS text
|
||||
AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN elevel = 0 THEN ''
|
||||
WHEN elevel = 10 THEN 'DEBUG5'
|
||||
WHEN elevel = 11 THEN 'DEBUG4'
|
||||
WHEN elevel = 12 THEN 'DEBUG3'
|
||||
WHEN elevel = 13 THEN 'DEBUG2'
|
||||
WHEN elevel = 14 THEN 'DEBUG1'
|
||||
WHEN elevel = 15 THEN 'LOG'
|
||||
WHEN elevel = 16 THEN 'LOG_SERVER_ONLY'
|
||||
WHEN elevel = 17 THEN 'INFO'
|
||||
WHEN elevel = 18 THEN 'NOTICE'
|
||||
WHEN elevel = 19 THEN 'WARNING'
|
||||
WHEN elevel = 20 THEN 'ERROR'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION histogram(_bucket int, _quryid text)
|
||||
RETURNS SETOF RECORD AS $$
|
||||
DECLARE
|
||||
rec record;
|
||||
CREATE FUNCTION pgsm_create_13_view() RETURNS INT AS
|
||||
$$
|
||||
BEGIN
|
||||
for rec in
|
||||
with stat as (select queryid, bucket, unnest(range()) as range, unnest(resp_calls)::int freq from pg_stat_monitor) select range, freq, repeat('■', (freq::float / max(freq) over() * 30)::int) as bar from stat where queryid = _quryid and bucket = _bucket
|
||||
loop
|
||||
return next rec;
|
||||
end loop;
|
||||
END
|
||||
$$ language plpgsql;
|
||||
CREATE VIEW pg_stat_monitor AS SELECT
|
||||
bucket,
|
||||
bucket_start_time AS bucket_start_time,
|
||||
userid::regrole,
|
||||
datname,
|
||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
||||
queryid,
|
||||
toplevel,
|
||||
top_queryid,
|
||||
query,
|
||||
comments,
|
||||
planid,
|
||||
query_plan,
|
||||
top_query,
|
||||
application_name,
|
||||
string_to_array(relations, ',') AS relations,
|
||||
cmd_type,
|
||||
get_cmd_type(cmd_type) AS cmd_type_text,
|
||||
elevel,
|
||||
sqlcode,
|
||||
message,
|
||||
calls,
|
||||
total_exec_time,
|
||||
min_exec_time,
|
||||
max_exec_time,
|
||||
mean_exec_time,
|
||||
stddev_exec_time,
|
||||
rows_retrieved,
|
||||
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,
|
||||
(string_to_array(resp_calls, ',')) resp_calls,
|
||||
cpu_user_time,
|
||||
cpu_sys_time,
|
||||
wal_records,
|
||||
wal_fpi,
|
||||
wal_bytes,
|
||||
state_code,
|
||||
get_state(state_code) as state,
|
||||
|
||||
--CREATE FUNCTION pg_stat_monitor_hook_stats(
|
||||
-- OUT hook text,
|
||||
-- OUT min_time float8,
|
||||
-- OUT max_time float8,
|
||||
-- OUT total_time float8,
|
||||
-- OUT ncalls int8
|
||||
--)
|
||||
--RETURNS SETOF record
|
||||
--AS 'MODULE_PATHNAME', 'pg_stat_monitor_hook_stats'
|
||||
--LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
|
||||
--
|
||||
--CREATE VIEW pg_stat_monitor_hook_stats AS SELECT
|
||||
-- hook,
|
||||
-- min_time,
|
||||
-- max_time,
|
||||
-- total_time,
|
||||
-- total_time / greatest(ncalls, 1) as avg_time,
|
||||
-- ncalls,
|
||||
-- ROUND(CAST(total_time / greatest(sum(total_time) OVER(), 0.00000001) * 100 as numeric), 2)::text || '%' as load_comparison
|
||||
-- FROM pg_stat_monitor_hook_stats();
|
||||
-- PostgreSQL-13 Specific Coulumns
|
||||
plans_calls
|
||||
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
|
||||
ORDER BY bucket_start_time;
|
||||
RETURN 0;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE FUNCTION pgsm_create_14_view() RETURNS INT AS
|
||||
$$
|
||||
BEGIN
|
||||
CREATE VIEW pg_stat_monitor AS SELECT
|
||||
bucket,
|
||||
bucket_start_time AS bucket_start_time,
|
||||
userid::regrole,
|
||||
datname,
|
||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
||||
queryid,
|
||||
toplevel,
|
||||
top_queryid,
|
||||
query,
|
||||
comments,
|
||||
planid,
|
||||
query_plan,
|
||||
top_query,
|
||||
application_name,
|
||||
string_to_array(relations, ',') AS relations,
|
||||
cmd_type,
|
||||
get_cmd_type(cmd_type) AS cmd_type_text,
|
||||
elevel,
|
||||
sqlcode,
|
||||
message,
|
||||
calls,
|
||||
total_exec_time,
|
||||
min_exec_time,
|
||||
max_exec_time,
|
||||
mean_exec_time,
|
||||
stddev_exec_time,
|
||||
rows_retrieved,
|
||||
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,
|
||||
(string_to_array(resp_calls, ',')) resp_calls,
|
||||
cpu_user_time,
|
||||
cpu_sys_time,
|
||||
wal_records,
|
||||
wal_fpi,
|
||||
wal_bytes,
|
||||
state_code,
|
||||
get_state(state_code) as state,
|
||||
|
||||
-- PostgreSQL-14 Specific Columns
|
||||
plans_calls,
|
||||
total_plan_time,
|
||||
min_plan_time,
|
||||
max_plan_time,
|
||||
mean_plan_time,
|
||||
stddev_plan_time
|
||||
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
|
||||
ORDER BY bucket_start_time;
|
||||
RETURN 0;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE FUNCTION pgsm_create_view() RETURNS INT AS
|
||||
$$
|
||||
DECLARE ver integer;
|
||||
BEGIN
|
||||
SELECT current_setting('server_version_num') INTO ver;
|
||||
IF (ver >= 14000) THEN
|
||||
return pgsm_create_14_view();
|
||||
END IF;
|
||||
IF (ver >= 13000) THEN
|
||||
return pgsm_create_13_view();
|
||||
END IF;
|
||||
IF (ver >= 11000) THEN
|
||||
return pgsm_create_11_view();
|
||||
END IF;
|
||||
RETURN 0;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
SELECT pgsm_create_view();
|
||||
|
||||
GRANT SELECT ON pg_stat_monitor TO PUBLIC;
|
||||
GRANT SELECT ON pg_stat_monitor_settings TO PUBLIC;
|
||||
|
||||
-- Don't want this to be available to non-superusers.
|
||||
REVOKE ALL ON FUNCTION pg_stat_monitor_reset() FROM PUBLIC;
|
|
@ -1,255 +0,0 @@
|
|||
/* contrib/pg_stat_monitor/pg_stat_monitor--1.0.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
|
||||
|
||||
-- Register functions.
|
||||
CREATE FUNCTION pg_stat_monitor_reset()
|
||||
RETURNS void
|
||||
AS 'MODULE_PATHNAME'
|
||||
LANGUAGE C PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION pg_stat_monitor_version()
|
||||
RETURNS text
|
||||
AS 'MODULE_PATHNAME'
|
||||
LANGUAGE C PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION get_histogram_timings()
|
||||
RETURNS text
|
||||
AS 'MODULE_PATHNAME'
|
||||
LANGUAGE C PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION range()
|
||||
RETURNS text[] AS $$
|
||||
SELECT string_to_array(get_histogram_timings(), ',');
|
||||
$$ LANGUAGE SQL;
|
||||
|
||||
CREATE FUNCTION pg_stat_monitor_internal(IN showtext boolean,
|
||||
OUT bucket int8, -- 0
|
||||
OUT userid oid,
|
||||
OUT dbid oid,
|
||||
OUT client_ip int8,
|
||||
|
||||
OUT queryid text, -- 4
|
||||
OUT planid text,
|
||||
OUT query text,
|
||||
OUT query_plan text,
|
||||
OUT state_code int8,
|
||||
OUT top_queryid text,
|
||||
OUT top_query text,
|
||||
OUT application_name text,
|
||||
|
||||
OUT relations text, -- 11
|
||||
OUT cmd_type int,
|
||||
OUT elevel int,
|
||||
OUT sqlcode TEXT,
|
||||
OUT message text,
|
||||
OUT bucket_start_time text,
|
||||
|
||||
OUT calls int8, -- 16
|
||||
OUT total_time float8,
|
||||
OUT min_time float8,
|
||||
OUT max_time float8,
|
||||
OUT mean_time float8,
|
||||
OUT stddev_time float8,
|
||||
OUT rows_retrieved int8,
|
||||
|
||||
OUT plans_calls int8, -- 23
|
||||
OUT plan_total_time float8,
|
||||
OUT plan_min_time float8,
|
||||
OUT plan_max_time float8,
|
||||
OUT plan_mean_time float8,
|
||||
OUT plan_stddev_time float8,
|
||||
|
||||
OUT shared_blks_hit int8, -- 29
|
||||
OUT shared_blks_read int8,
|
||||
OUT shared_blks_dirtied int8,
|
||||
OUT shared_blks_written int8,
|
||||
OUT local_blks_hit int8,
|
||||
OUT local_blks_read int8,
|
||||
OUT local_blks_dirtied int8,
|
||||
OUT local_blks_written int8,
|
||||
OUT temp_blks_read int8,
|
||||
OUT temp_blks_written int8,
|
||||
OUT blk_read_time float8,
|
||||
OUT blk_write_time float8,
|
||||
OUT resp_calls text, -- 41
|
||||
OUT cpu_user_time float8,
|
||||
OUT cpu_sys_time float8,
|
||||
OUT wal_records int8,
|
||||
OUT wal_fpi int8,
|
||||
OUT wal_bytes numeric,
|
||||
OUT comments TEXT,
|
||||
OUT toplevel BOOLEAN
|
||||
)
|
||||
RETURNS SETOF record
|
||||
AS 'MODULE_PATHNAME', 'pg_stat_monitor'
|
||||
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION get_state(state_code int8) RETURNS TEXT AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN state_code = 0 THEN 'PARSING'
|
||||
WHEN state_code = 1 THEN 'PLANNING'
|
||||
WHEN state_code = 2 THEN 'ACTIVE'
|
||||
WHEN state_code = 3 THEN 'FINISHED'
|
||||
WHEN state_code = 4 THEN 'FINISHED WITH ERROR'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION get_cmd_type (cmd_type INTEGER) RETURNS TEXT AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN cmd_type = 0 THEN ''
|
||||
WHEN cmd_type = 1 THEN 'SELECT'
|
||||
WHEN cmd_type = 2 THEN 'UPDATE'
|
||||
WHEN cmd_type = 3 THEN 'INSERT'
|
||||
WHEN cmd_type = 4 THEN 'DELETE'
|
||||
WHEN cmd_type = 5 THEN 'UTILITY'
|
||||
WHEN cmd_type = 6 THEN 'NOTHING'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION pg_stat_monitor_settings(
|
||||
OUT name text,
|
||||
OUT value text,
|
||||
OUT default_value text,
|
||||
OUT description text,
|
||||
OUT minimum INTEGER,
|
||||
OUT maximum INTEGER,
|
||||
OUT options text,
|
||||
OUT restart text
|
||||
)
|
||||
RETURNS SETOF record
|
||||
AS 'MODULE_PATHNAME', 'pg_stat_monitor_settings'
|
||||
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
|
||||
|
||||
CREATE VIEW pg_stat_monitor_settings AS SELECT
|
||||
name,
|
||||
value,
|
||||
default_value,
|
||||
description,
|
||||
minimum,
|
||||
maximum,
|
||||
options,
|
||||
restart
|
||||
FROM pg_stat_monitor_settings();
|
||||
|
||||
-- Register a view on the function for ease of use.
|
||||
CREATE VIEW pg_stat_monitor AS SELECT
|
||||
bucket,
|
||||
bucket_start_time AS bucket_start_time,
|
||||
userid::regrole,
|
||||
datname,
|
||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
||||
queryid,
|
||||
top_queryid,
|
||||
query,
|
||||
comments,
|
||||
planid,
|
||||
query_plan,
|
||||
top_query,
|
||||
application_name,
|
||||
string_to_array(relations, ',') AS relations,
|
||||
cmd_type,
|
||||
get_cmd_type(cmd_type) AS cmd_type_text,
|
||||
elevel,
|
||||
sqlcode,
|
||||
message,
|
||||
calls,
|
||||
total_time,
|
||||
min_time,
|
||||
max_time,
|
||||
mean_time,
|
||||
stddev_time,
|
||||
rows_retrieved,
|
||||
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,
|
||||
(string_to_array(resp_calls, ',')) resp_calls,
|
||||
cpu_user_time,
|
||||
cpu_sys_time,
|
||||
wal_records,
|
||||
wal_fpi,
|
||||
wal_bytes,
|
||||
state_code,
|
||||
get_state(state_code) as state
|
||||
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
|
||||
ORDER BY bucket_start_time;
|
||||
|
||||
CREATE FUNCTION decode_error_level(elevel int)
|
||||
RETURNS text
|
||||
AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN elevel = 0 THEN ''
|
||||
WHEN elevel = 10 THEN 'DEBUG5'
|
||||
WHEN elevel = 11 THEN 'DEBUG4'
|
||||
WHEN elevel = 12 THEN 'DEBUG3'
|
||||
WHEN elevel = 13 THEN 'DEBUG2'
|
||||
WHEN elevel = 14 THEN 'DEBUG1'
|
||||
WHEN elevel = 15 THEN 'LOG'
|
||||
WHEN elevel = 16 THEN 'LOG_SERVER_ONLY'
|
||||
WHEN elevel = 17 THEN 'INFO'
|
||||
WHEN elevel = 18 THEN 'NOTICE'
|
||||
WHEN elevel = 19 THEN 'WARNING'
|
||||
WHEN elevel = 20 THEN 'ERROR'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION histogram(_bucket int, _quryid text)
|
||||
RETURNS SETOF RECORD AS $$
|
||||
DECLARE
|
||||
rec record;
|
||||
BEGIN
|
||||
for rec in
|
||||
with stat as (select queryid, bucket, unnest(range()) as range, unnest(resp_calls)::int freq from pg_stat_monitor) select range, freq, repeat('■', (freq::float / max(freq) over() * 30)::int) as bar from stat where queryid = _quryid and bucket = _bucket
|
||||
loop
|
||||
return next rec;
|
||||
end loop;
|
||||
END
|
||||
$$ language plpgsql;
|
||||
|
||||
-- CREATE FUNCTION pg_stat_monitor_hook_stats(
|
||||
-- OUT hook text,
|
||||
-- OUT min_time float8,
|
||||
-- OUT max_time float8,
|
||||
-- OUT total_time float8,
|
||||
-- OUT ncalls int8
|
||||
--)
|
||||
--RETURNS SETOF record
|
||||
--AS 'MODULE_PATHNAME', 'pg_stat_monitor_hook_stats'
|
||||
--LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
|
||||
|
||||
--CREATE VIEW pg_stat_monitor_hook_stats AS SELECT
|
||||
-- hook,
|
||||
-- min_time,
|
||||
-- max_time,
|
||||
-- total_time,
|
||||
-- total_time / greatest(ncalls, 1) as avg_time,
|
||||
-- ncalls,
|
||||
-- ROUND(CAST(total_time / greatest(sum(total_time) OVER(), 0.00000001) * 100 as numeric), 2)::text || '%' as load_comparison
|
||||
-- FROM pg_stat_monitor_hook_stats();
|
||||
|
||||
GRANT SELECT ON pg_stat_monitor TO PUBLIC;
|
||||
GRANT SELECT ON pg_stat_monitor_settings TO PUBLIC;
|
||||
|
||||
-- Don't want this to be available to non-superusers.
|
||||
REVOKE ALL ON FUNCTION pg_stat_monitor_reset() FROM PUBLIC;
|
||||
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
/* contrib/pg_stat_monitor/pg_stat_monitor--1.0.sql */
|
||||
/* contrib/pg_stat_monitor/pg_stat_monitor--2.0.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
|
||||
|
@ -24,7 +24,102 @@ RETURNS text[] AS $$
|
|||
SELECT string_to_array(get_histogram_timings(), ',');
|
||||
$$ LANGUAGE SQL;
|
||||
|
||||
CREATE FUNCTION pg_stat_monitor_internal(IN showtext boolean,
|
||||
-- Some generic utility function used internally.
|
||||
|
||||
CREATE FUNCTION get_state(state_code int8) RETURNS TEXT AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN state_code = 0 THEN 'PARSING'
|
||||
WHEN state_code = 1 THEN 'PLANNING'
|
||||
WHEN state_code = 2 THEN 'ACTIVE'
|
||||
WHEN state_code = 3 THEN 'FINISHED'
|
||||
WHEN state_code = 4 THEN 'FINISHED WITH ERROR'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION get_cmd_type (cmd_type INTEGER) RETURNS TEXT AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN cmd_type = 0 THEN ''
|
||||
WHEN cmd_type = 1 THEN 'SELECT'
|
||||
WHEN cmd_type = 2 THEN 'UPDATE'
|
||||
WHEN cmd_type = 3 THEN 'INSERT'
|
||||
WHEN cmd_type = 4 THEN 'DELETE'
|
||||
WHEN cmd_type = 5 THEN 'UTILITY'
|
||||
WHEN cmd_type = 6 THEN 'NOTHING'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION pg_stat_monitor_settings(
|
||||
OUT name text,
|
||||
OUT value text,
|
||||
OUT default_value text,
|
||||
OUT description text,
|
||||
OUT minimum INTEGER,
|
||||
OUT maximum INTEGER,
|
||||
OUT options text,
|
||||
OUT restart text
|
||||
)
|
||||
RETURNS SETOF record
|
||||
AS 'MODULE_PATHNAME', 'pg_stat_monitor_settings'
|
||||
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
|
||||
|
||||
CREATE VIEW pg_stat_monitor_settings AS SELECT
|
||||
name,
|
||||
value,
|
||||
default_value,
|
||||
description,
|
||||
minimum,
|
||||
maximum,
|
||||
options,
|
||||
restart
|
||||
FROM pg_stat_monitor_settings();
|
||||
|
||||
CREATE FUNCTION decode_error_level(elevel int)
|
||||
RETURNS text
|
||||
AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN elevel = 0 THEN ''
|
||||
WHEN elevel = 10 THEN 'DEBUG5'
|
||||
WHEN elevel = 11 THEN 'DEBUG4'
|
||||
WHEN elevel = 12 THEN 'DEBUG3'
|
||||
WHEN elevel = 13 THEN 'DEBUG2'
|
||||
WHEN elevel = 14 THEN 'DEBUG1'
|
||||
WHEN elevel = 15 THEN 'LOG'
|
||||
WHEN elevel = 16 THEN 'LOG_SERVER_ONLY'
|
||||
WHEN elevel = 17 THEN 'INFO'
|
||||
WHEN elevel = 18 THEN 'NOTICE'
|
||||
WHEN elevel = 19 THEN 'WARNING'
|
||||
WHEN elevel = 20 THEN 'ERROR'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION histogram(_bucket int, _quryid text)
|
||||
RETURNS SETOF RECORD AS $$
|
||||
DECLARE
|
||||
rec record;
|
||||
BEGIN
|
||||
FOR rec IN
|
||||
WITH stat AS (select queryid, bucket, unnest(range()) AS range,
|
||||
unnest(resp_calls)::int freq FROM pg_stat_monitor) select range,
|
||||
freq, repeat('■', (freq::float / max(freq) over() * 30)::int) AS bar
|
||||
FROM stat WHERE queryid = _quryid and bucket = _bucket
|
||||
LOOP
|
||||
RETURN next rec;
|
||||
END loop;
|
||||
END
|
||||
$$ language plpgsql;
|
||||
|
||||
-- pg_stat_monitor internal function, must not call outside from this file.
|
||||
CREATE FUNCTION pg_stat_monitor_internal(
|
||||
IN showtext boolean,
|
||||
OUT bucket int8, -- 0
|
||||
OUT userid oid,
|
||||
OUT dbid oid,
|
||||
|
@ -89,60 +184,10 @@ RETURNS SETOF record
|
|||
AS 'MODULE_PATHNAME', 'pg_stat_monitor'
|
||||
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION get_state(state_code int8) RETURNS TEXT AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN state_code = 0 THEN 'PARSING'
|
||||
WHEN state_code = 1 THEN 'PLANNING'
|
||||
WHEN state_code = 2 THEN 'ACTIVE'
|
||||
WHEN state_code = 3 THEN 'FINISHED'
|
||||
WHEN state_code = 4 THEN 'FINISHED WITH ERROR'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION get_cmd_type (cmd_type INTEGER) RETURNS TEXT AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN cmd_type = 0 THEN ''
|
||||
WHEN cmd_type = 1 THEN 'SELECT'
|
||||
WHEN cmd_type = 2 THEN 'UPDATE'
|
||||
WHEN cmd_type = 3 THEN 'INSERT'
|
||||
WHEN cmd_type = 4 THEN 'DELETE'
|
||||
WHEN cmd_type = 5 THEN 'UTILITY'
|
||||
WHEN cmd_type = 6 THEN 'NOTHING'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION pg_stat_monitor_settings(
|
||||
OUT name text,
|
||||
OUT value text,
|
||||
OUT default_value text,
|
||||
OUT description text,
|
||||
OUT minimum INTEGER,
|
||||
OUT maximum INTEGER,
|
||||
OUT options text,
|
||||
OUT restart text
|
||||
)
|
||||
RETURNS SETOF record
|
||||
AS 'MODULE_PATHNAME', 'pg_stat_monitor_settings'
|
||||
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
|
||||
|
||||
CREATE VIEW pg_stat_monitor_settings AS SELECT
|
||||
name,
|
||||
value,
|
||||
default_value,
|
||||
description,
|
||||
minimum,
|
||||
maximum,
|
||||
options,
|
||||
restart
|
||||
FROM pg_stat_monitor_settings();
|
||||
|
||||
-- Register a view on the function for ease of use.
|
||||
CREATE FUNCTION pgsm_create_11_view() RETURNS INT AS
|
||||
$$
|
||||
BEGIN
|
||||
CREATE VIEW pg_stat_monitor AS SELECT
|
||||
bucket,
|
||||
bucket_start_time AS bucket_start_time,
|
||||
|
@ -170,17 +215,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
|||
max_exec_time,
|
||||
mean_exec_time,
|
||||
stddev_exec_time,
|
||||
|
||||
rows_retrieved,
|
||||
|
||||
plans_calls,
|
||||
|
||||
total_plan_time,
|
||||
min_plan_time,
|
||||
max_plan_time,
|
||||
mean_plan_time,
|
||||
stddev_plan_time,
|
||||
|
||||
shared_blks_hit,
|
||||
shared_blks_read,
|
||||
shared_blks_dirtied,
|
||||
|
@ -203,65 +238,155 @@ CREATE VIEW pg_stat_monitor AS SELECT
|
|||
get_state(state_code) as state
|
||||
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
|
||||
ORDER BY bucket_start_time;
|
||||
RETURN 0;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE FUNCTION decode_error_level(elevel int)
|
||||
RETURNS text
|
||||
AS
|
||||
$$
|
||||
SELECT
|
||||
CASE
|
||||
WHEN elevel = 0 THEN ''
|
||||
WHEN elevel = 10 THEN 'DEBUG5'
|
||||
WHEN elevel = 11 THEN 'DEBUG4'
|
||||
WHEN elevel = 12 THEN 'DEBUG3'
|
||||
WHEN elevel = 13 THEN 'DEBUG2'
|
||||
WHEN elevel = 14 THEN 'DEBUG1'
|
||||
WHEN elevel = 15 THEN 'LOG'
|
||||
WHEN elevel = 16 THEN 'LOG_SERVER_ONLY'
|
||||
WHEN elevel = 17 THEN 'INFO'
|
||||
WHEN elevel = 18 THEN 'NOTICE'
|
||||
WHEN elevel = 19 THEN 'WARNING'
|
||||
WHEN elevel = 20 THEN 'ERROR'
|
||||
END
|
||||
$$
|
||||
LANGUAGE SQL PARALLEL SAFE;
|
||||
|
||||
CREATE FUNCTION histogram(_bucket int, _quryid text)
|
||||
RETURNS SETOF RECORD AS $$
|
||||
DECLARE
|
||||
rec record;
|
||||
CREATE FUNCTION pgsm_create_13_view() RETURNS INT AS
|
||||
$$
|
||||
BEGIN
|
||||
for rec in
|
||||
with stat as (select queryid, bucket, unnest(range()) as range, unnest(resp_calls)::int freq from pg_stat_monitor) select range, freq, repeat('■', (freq::float / max(freq) over() * 30)::int) as bar from stat where queryid = _quryid and bucket = _bucket
|
||||
loop
|
||||
return next rec;
|
||||
end loop;
|
||||
END
|
||||
$$ language plpgsql;
|
||||
CREATE VIEW pg_stat_monitor AS SELECT
|
||||
bucket,
|
||||
bucket_start_time AS bucket_start_time,
|
||||
userid::regrole,
|
||||
datname,
|
||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
||||
queryid,
|
||||
toplevel,
|
||||
top_queryid,
|
||||
query,
|
||||
comments,
|
||||
planid,
|
||||
query_plan,
|
||||
top_query,
|
||||
application_name,
|
||||
string_to_array(relations, ',') AS relations,
|
||||
cmd_type,
|
||||
get_cmd_type(cmd_type) AS cmd_type_text,
|
||||
elevel,
|
||||
sqlcode,
|
||||
message,
|
||||
calls,
|
||||
total_exec_time,
|
||||
min_exec_time,
|
||||
max_exec_time,
|
||||
mean_exec_time,
|
||||
stddev_exec_time,
|
||||
rows_retrieved,
|
||||
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,
|
||||
(string_to_array(resp_calls, ',')) resp_calls,
|
||||
cpu_user_time,
|
||||
cpu_sys_time,
|
||||
wal_records,
|
||||
wal_fpi,
|
||||
wal_bytes,
|
||||
state_code,
|
||||
get_state(state_code) as state,
|
||||
|
||||
--CREATE FUNCTION pg_stat_monitor_hook_stats(
|
||||
-- OUT hook text,
|
||||
-- OUT min_time float8,
|
||||
-- OUT max_time float8,
|
||||
-- OUT total_time float8,
|
||||
-- OUT ncalls int8
|
||||
--)
|
||||
--RETURNS SETOF record
|
||||
--AS 'MODULE_PATHNAME', 'pg_stat_monitor_hook_stats'
|
||||
--LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
|
||||
--
|
||||
--CREATE VIEW pg_stat_monitor_hook_stats AS SELECT
|
||||
-- hook,
|
||||
-- min_time,
|
||||
-- max_time,
|
||||
-- total_time,
|
||||
-- total_time / greatest(ncalls, 1) as avg_time,
|
||||
-- ncalls,
|
||||
-- ROUND(CAST(total_time / greatest(sum(total_time) OVER(), 0.00000001) * 100 as numeric), 2)::text || '%' as load_comparison
|
||||
-- FROM pg_stat_monitor_hook_stats();
|
||||
-- PostgreSQL-13 Specific Coulumns
|
||||
plans_calls
|
||||
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
|
||||
ORDER BY bucket_start_time;
|
||||
RETURN 0;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE FUNCTION pgsm_create_14_view() RETURNS INT AS
|
||||
$$
|
||||
BEGIN
|
||||
CREATE VIEW pg_stat_monitor AS SELECT
|
||||
bucket,
|
||||
bucket_start_time AS bucket_start_time,
|
||||
userid::regrole,
|
||||
datname,
|
||||
'0.0.0.0'::inet + client_ip AS client_ip,
|
||||
queryid,
|
||||
toplevel,
|
||||
top_queryid,
|
||||
query,
|
||||
comments,
|
||||
planid,
|
||||
query_plan,
|
||||
top_query,
|
||||
application_name,
|
||||
string_to_array(relations, ',') AS relations,
|
||||
cmd_type,
|
||||
get_cmd_type(cmd_type) AS cmd_type_text,
|
||||
elevel,
|
||||
sqlcode,
|
||||
message,
|
||||
calls,
|
||||
total_exec_time,
|
||||
min_exec_time,
|
||||
max_exec_time,
|
||||
mean_exec_time,
|
||||
stddev_exec_time,
|
||||
rows_retrieved,
|
||||
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,
|
||||
(string_to_array(resp_calls, ',')) resp_calls,
|
||||
cpu_user_time,
|
||||
cpu_sys_time,
|
||||
wal_records,
|
||||
wal_fpi,
|
||||
wal_bytes,
|
||||
state_code,
|
||||
get_state(state_code) as state,
|
||||
|
||||
-- PostgreSQL-14 Specific Columns
|
||||
plans_calls,
|
||||
total_plan_time,
|
||||
min_plan_time,
|
||||
max_plan_time,
|
||||
mean_plan_time,
|
||||
stddev_plan_time
|
||||
FROM pg_stat_monitor_internal(TRUE) p, pg_database d WHERE dbid = oid
|
||||
ORDER BY bucket_start_time;
|
||||
RETURN 0;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE FUNCTION pgsm_create_view() RETURNS INT AS
|
||||
$$
|
||||
DECLARE ver integer;
|
||||
BEGIN
|
||||
SELECT current_setting('server_version_num') INTO ver;
|
||||
IF (ver >= 14000) THEN
|
||||
return pgsm_create_14_view();
|
||||
END IF;
|
||||
IF (ver >= 13000) THEN
|
||||
return pgsm_create_13_view();
|
||||
END IF;
|
||||
IF (ver >= 11000) THEN
|
||||
return pgsm_create_11_view();
|
||||
END IF;
|
||||
RETURN 0;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
SELECT pgsm_create_view();
|
||||
|
||||
GRANT SELECT ON pg_stat_monitor TO PUBLIC;
|
||||
GRANT SELECT ON pg_stat_monitor_settings TO PUBLIC;
|
||||
|
||||
-- Don't want this to be available to non-superusers.
|
||||
REVOKE ALL ON FUNCTION pg_stat_monitor_reset() FROM PUBLIC;
|
|
@ -19,9 +19,6 @@
|
|||
#include "access/parallel.h"
|
||||
#include "utils/guc.h"
|
||||
#include <regex.h>
|
||||
#ifdef BENCHMARK
|
||||
#include <time.h> /* clock() */
|
||||
#endif
|
||||
#include "pgstat.h"
|
||||
#include "commands/explain.h"
|
||||
#include "pg_stat_monitor.h"
|
||||
|
@ -78,9 +75,6 @@ static struct rusage rusage_end;
|
|||
/* Query buffer, store queries' text. */
|
||||
static unsigned char *pgss_qbuf = NULL;
|
||||
static char *pgss_explain(QueryDesc *queryDesc);
|
||||
#ifdef BENCHMARK
|
||||
static struct pg_hook_stats_t *pg_hook_stats;
|
||||
#endif
|
||||
|
||||
static void extract_query_comments(const char *query, char *comments, size_t max_len);
|
||||
static int get_histogram_bucket(double q_time);
|
||||
|
@ -349,18 +343,6 @@ pg_stat_monitor_version(PG_FUNCTION_ARGS)
|
|||
}
|
||||
|
||||
#if PG_VERSION_NUM >= 140000
|
||||
#ifdef BENCHMARK
|
||||
static void
|
||||
pgss_post_parse_analyze_benchmark(ParseState *pstate, Query *query, JumbleState *jstate)
|
||||
{
|
||||
double start_time = (double) clock();
|
||||
|
||||
pgss_post_parse_analyze(pstate, query, jstate);
|
||||
double elapsed = ((double) clock() - start_time) / CLOCKS_PER_SEC;
|
||||
|
||||
update_hook_stats(STATS_PGSS_POST_PARSE_ANALYZE, elapsed);
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* Post-parse-analysis hook: mark query with a queryId
|
||||
*/
|
||||
|
@ -412,20 +394,8 @@ pgss_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
|
|||
jstate, /* JumbleState */
|
||||
PGSS_PARSE); /* pgssStoreKind */
|
||||
}
|
||||
#else
|
||||
#elif
|
||||
|
||||
#ifdef BENCHMARK
|
||||
static void
|
||||
pgss_post_parse_analyze_benchmark(ParseState *pstate, Query *query)
|
||||
{
|
||||
double start_time = (double) clock();
|
||||
|
||||
pgss_post_parse_analyze(pstate, query);
|
||||
double elapsed = ((double) clock() - start_time) / CLOCKS_PER_SEC;
|
||||
|
||||
update_hook_stats(STATS_PGSS_POST_PARSE_ANALYZE, elapsed);
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* Post-parse-analysis hook: mark query with a queryId
|
||||
*/
|
||||
|
@ -484,18 +454,6 @@ pgss_post_parse_analyze(ParseState *pstate, Query *query)
|
|||
}
|
||||
#endif
|
||||
|
||||
#ifdef BENCHMARK
|
||||
static void
|
||||
pgss_ExecutorStart_benchmark(QueryDesc *queryDesc, int eflags)
|
||||
{
|
||||
double start_time = (double) clock();
|
||||
|
||||
pgss_ExecutorStart(queryDesc, eflags);
|
||||
double elapsed = ((double) clock() - start_time) / CLOCKS_PER_SEC;
|
||||
|
||||
update_hook_stats(STATS_PGSS_EXECUTORSTART, elapsed);
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
* ExecutorStart hook: start up tracking if needed
|
||||
*/
|
||||
|
@ -552,19 +510,6 @@ pgss_ExecutorStart(QueryDesc *queryDesc, int eflags)
|
|||
}
|
||||
}
|
||||
|
||||
#ifdef BENCHMARK
|
||||
static void
|
||||
pgss_ExecutorRun_benchmark(QueryDesc *queryDesc, ScanDirection direction, uint64 count,
|
||||
bool execute_once)
|
||||
{
|
||||
double start_time = (double) clock();
|
||||
|
||||
pgss_ExecutorRun(queryDesc, direction, count, execute_once);
|
||||
double elapsed = ((double) clock() - start_time) / CLOCKS_PER_SEC;
|
||||
|
||||
update_hook_stats(STATS_PGSS_EXECUTORUN, elapsed);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* ExecutorRun hook: all we need do is track nesting depth
|
||||
|
@ -596,19 +541,6 @@ pgss_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count,
|
|||
PG_END_TRY();
|
||||
}
|
||||
|
||||
#ifdef BENCHMARK
|
||||
static void
|
||||
pgss_ExecutorFinish_benchmark(QueryDesc *queryDesc)
|
||||
{
|
||||
double start_time = (double) clock();
|
||||
|
||||
pgss_ExecutorFinish(queryDesc);
|
||||
double elapsed = ((double) clock() - start_time) / CLOCKS_PER_SEC;
|
||||
|
||||
update_hook_stats(STATS_PGSS_EXECUTORFINISH, elapsed);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* ExecutorFinish hook: all we need do is track nesting depth
|
||||
*/
|
||||
|
@ -652,19 +584,6 @@ pgss_explain(QueryDesc *queryDesc)
|
|||
return es->str->data;
|
||||
}
|
||||
|
||||
#ifdef BENCHMARK
|
||||
static void
|
||||
pgss_ExecutorEnd_benchmark(QueryDesc *queryDesc)
|
||||
{
|
||||
double start_time = (double) clock();
|
||||
|
||||
pgss_ExecutorEnd(queryDesc);
|
||||
double elapsed = ((double) clock() - start_time) / CLOCKS_PER_SEC;
|
||||
|
||||
update_hook_stats(STATS_PGSS_EXECUTOREND, elapsed);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* ExecutorEnd hook: store results if needed
|
||||
*/
|
||||
|
@ -726,21 +645,6 @@ pgss_ExecutorEnd(QueryDesc *queryDesc)
|
|||
num_relations = 0;
|
||||
}
|
||||
|
||||
#ifdef BENCHMARK
|
||||
static bool
|
||||
pgss_ExecutorCheckPerms_benchmark(List *rt, bool abort)
|
||||
{
|
||||
bool ret;
|
||||
double start_time = (double) clock();
|
||||
|
||||
ret = pgss_ExecutorCheckPerms(rt, abort);
|
||||
double elapsed = ((double) clock() - start_time) / CLOCKS_PER_SEC;
|
||||
|
||||
update_hook_stats(STATS_PGSS_EXECUTORCHECKPERMS, elapsed);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool
|
||||
pgss_ExecutorCheckPerms(List *rt, bool abort)
|
||||
{
|
||||
|
@ -792,20 +696,6 @@ pgss_ExecutorCheckPerms(List *rt, bool abort)
|
|||
}
|
||||
|
||||
#if PG_VERSION_NUM >= 130000
|
||||
#ifdef BENCHMARK
|
||||
static PlannedStmt *
|
||||
pgss_planner_hook_benchmark(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams)
|
||||
{
|
||||
PlannedStmt *ret;
|
||||
double start_time = (double) clock();
|
||||
|
||||
ret = pgss_planner_hook(parse, query_string, cursorOptions, boundParams);
|
||||
double elapsed = ((double) clock() - start_time) / CLOCKS_PER_SEC;
|
||||
|
||||
update_hook_stats(STATS_PGSS_PLANNER_HOOK, elapsed);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
static PlannedStmt *
|
||||
pgss_planner_hook(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams)
|
||||
{
|
||||
|
@ -912,23 +802,6 @@ pgss_planner_hook(Query *parse, const char *query_string, int cursorOptions, Par
|
|||
* ProcessUtility hook
|
||||
*/
|
||||
#if PG_VERSION_NUM >= 140000
|
||||
#ifdef BENCHMARK
|
||||
static void
|
||||
pgss_ProcessUtility_benchmark(PlannedStmt *pstmt, const char *queryString,
|
||||
bool readOnlyTree,
|
||||
ProcessUtilityContext context,
|
||||
ParamListInfo params, QueryEnvironment *queryEnv,
|
||||
DestReceiver *dest,
|
||||
QueryCompletion *qc)
|
||||
{
|
||||
double start_time = (double) clock();
|
||||
|
||||
pgss_ProcessUtility(pstmt, queryString, readOnlyTree, context, params, queryEnv, dest, qc);
|
||||
double elapsed = ((double) clock() - start_time) / CLOCKS_PER_SEC;
|
||||
|
||||
update_hook_stats(STATS_PGSS_PROCESSUTILITY, elapsed);
|
||||
}
|
||||
#endif
|
||||
static void
|
||||
pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
|
||||
bool readOnlyTree,
|
||||
|
@ -938,22 +811,6 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
|
|||
QueryCompletion *qc)
|
||||
|
||||
#elif PG_VERSION_NUM >= 130000
|
||||
#ifdef BENCHMARK
|
||||
static void
|
||||
pgss_ProcessUtility_benchmark(PlannedStmt *pstmt, const char *queryString,
|
||||
ProcessUtilityContext context,
|
||||
ParamListInfo params, QueryEnvironment *queryEnv,
|
||||
DestReceiver *dest,
|
||||
QueryCompletion *qc)
|
||||
{
|
||||
double start_time = (double) clock();
|
||||
|
||||
pgss_ProcessUtility(pstmt, queryString, context, params, queryEnv, dest, qc);
|
||||
double elapsed = ((double) clock() - start_time) / CLOCKS_PER_SEC;
|
||||
|
||||
update_hook_stats(STATS_PGSS_PROCESSUTILITY, elapsed);
|
||||
}
|
||||
#endif
|
||||
static void
|
||||
pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
|
||||
ProcessUtilityContext context,
|
||||
|
@ -962,22 +819,6 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
|
|||
QueryCompletion *qc)
|
||||
|
||||
#else
|
||||
#ifdef BENCHMARK
|
||||
static void
|
||||
pgss_ProcessUtility_benchmark(PlannedStmt *pstmt, const char *queryString,
|
||||
ProcessUtilityContext context, ParamListInfo params,
|
||||
QueryEnvironment *queryEnv,
|
||||
DestReceiver *dest,
|
||||
char *completionTag)
|
||||
{
|
||||
double start_time = (double) clock();
|
||||
|
||||
pgss_ProcessUtility(pstmt, queryString, context, params, queryEnv, dest, completionTag);
|
||||
double elapsed = ((double) clock() - start_time) / CLOCKS_PER_SEC;
|
||||
|
||||
update_hook_stats(STATS_PGSS_PROCESSUTILITY, elapsed);
|
||||
}
|
||||
#endif
|
||||
static void
|
||||
pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
|
||||
ProcessUtilityContext context, ParamListInfo params,
|
||||
|
@ -1714,19 +1555,6 @@ pg_stat_monitor_reset(PG_FUNCTION_ARGS)
|
|||
/* Reset query buffer. */
|
||||
*(uint64 *) pgss_qbuf = 0;
|
||||
|
||||
#ifdef BENCHMARK
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = STATS_START; i < STATS_END; ++i)
|
||||
{
|
||||
pg_hook_stats[i].min_time = 0;
|
||||
pg_hook_stats[i].max_time = 0;
|
||||
pg_hook_stats[i].total_time = 0;
|
||||
pg_hook_stats[i].ncalls = 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
LWLockRelease(pgss->lock);
|
||||
PG_RETURN_VOID();
|
||||
}
|
||||
|
@ -3497,63 +3325,6 @@ pg_stat_monitor_settings(PG_FUNCTION_ARGS)
|
|||
Datum
|
||||
pg_stat_monitor_hook_stats(PG_FUNCTION_ARGS)
|
||||
{
|
||||
#ifdef BENCHMARK
|
||||
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
|
||||
TupleDesc tupdesc;
|
||||
Tuplestorestate *tupstore;
|
||||
MemoryContext per_query_ctx;
|
||||
MemoryContext oldcontext;
|
||||
enum pg_hook_stats_id hook_id;
|
||||
|
||||
/* Safety check... */
|
||||
if (!IsSystemInitialized())
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
||||
errmsg("pg_stat_monitor: must be loaded via shared_preload_libraries")));
|
||||
|
||||
/* check to see if caller supports us returning a tuplestore */
|
||||
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||
errmsg("pg_stat_monitor: set-valued function called in context that cannot accept a set")));
|
||||
|
||||
/* Switch into long-lived context to construct returned data structures */
|
||||
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
|
||||
oldcontext = MemoryContextSwitchTo(per_query_ctx);
|
||||
|
||||
/* Build a tuple descriptor for our result type */
|
||||
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
||||
elog(ERROR, "pg_stat_monitor: return type must be a row type");
|
||||
|
||||
if (tupdesc->natts != 5)
|
||||
elog(ERROR, "pg_stat_monitor: incorrect number of output arguments, required %d", tupdesc->natts);
|
||||
|
||||
tupstore = tuplestore_begin_heap(true, false, work_mem);
|
||||
rsinfo->returnMode = SFRM_Materialize;
|
||||
rsinfo->setResult = tupstore;
|
||||
rsinfo->setDesc = tupdesc;
|
||||
|
||||
MemoryContextSwitchTo(oldcontext);
|
||||
|
||||
for (hook_id = 0; hook_id < STATS_END; hook_id++)
|
||||
{
|
||||
Datum values[5];
|
||||
bool nulls[5];
|
||||
int j = 0;
|
||||
|
||||
memset(values, 0, sizeof(values));
|
||||
memset(nulls, 0, sizeof(nulls));
|
||||
|
||||
values[j++] = CStringGetTextDatum(pg_hook_stats[hook_id].hook_name);
|
||||
values[j++] = Float8GetDatumFast(pg_hook_stats[hook_id].min_time);
|
||||
values[j++] = Float8GetDatumFast(pg_hook_stats[hook_id].max_time);
|
||||
values[j++] = Float8GetDatumFast(pg_hook_stats[hook_id].total_time);
|
||||
values[j++] = Int64GetDatumFast(pg_hook_stats[hook_id].ncalls);
|
||||
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
|
||||
}
|
||||
/* clean up and return the tuplestore */
|
||||
tuplestore_donestoring(tupstore);
|
||||
#endif /* #ifdef BENCHMARK */
|
||||
return (Datum) 0;
|
||||
}
|
||||
|
||||
|
@ -3564,18 +3335,6 @@ set_qbuf(unsigned char *buf)
|
|||
*(uint64 *) pgss_qbuf = 0;
|
||||
}
|
||||
|
||||
#ifdef BENCHMARK
|
||||
static void
|
||||
pgsm_emit_log_hook_benchmark(ErrorData *edata)
|
||||
{
|
||||
double start_time = (double) clock();
|
||||
|
||||
pgsm_emit_log_hook(edata);
|
||||
double elapsed = ((double) clock() - start_time) / CLOCKS_PER_SEC;
|
||||
|
||||
update_hook_stats(STATS_PGSM_EMIT_LOG_HOOK, elapsed);
|
||||
}
|
||||
#endif
|
||||
void
|
||||
pgsm_emit_log_hook(ErrorData *edata)
|
||||
{
|
||||
|
@ -3941,49 +3700,3 @@ djb2_hash_str(unsigned char *str, int *out_len)
|
|||
|
||||
return hash;
|
||||
}
|
||||
|
||||
#ifdef BENCHMARK
|
||||
void
|
||||
init_hook_stats(void)
|
||||
{
|
||||
bool found = false;
|
||||
|
||||
pg_hook_stats = ShmemInitStruct("pg_stat_monitor_hook_stats", HOOK_STATS_SIZE, &found);
|
||||
if (!found)
|
||||
{
|
||||
memset(pg_hook_stats, 0, HOOK_STATS_SIZE);
|
||||
|
||||
#define SET_HOOK_NAME(hook, name) \
|
||||
snprintf(pg_hook_stats[hook].hook_name, sizeof(pg_hook_stats->hook_name), name);
|
||||
|
||||
SET_HOOK_NAME(STATS_PGSS_POST_PARSE_ANALYZE, "pgss_post_parse_analyze");
|
||||
SET_HOOK_NAME(STATS_PGSS_EXECUTORSTART, "pgss_ExecutorStart");
|
||||
SET_HOOK_NAME(STATS_PGSS_EXECUTORUN, "pgss_ExecutorRun");
|
||||
SET_HOOK_NAME(STATS_PGSS_EXECUTORFINISH, "pgss_ExecutorFinish");
|
||||
SET_HOOK_NAME(STATS_PGSS_EXECUTOREND, "pgss_ExecutorEnd");
|
||||
SET_HOOK_NAME(STATS_PGSS_PROCESSUTILITY, "pgss_ProcessUtility");
|
||||
#if PG_VERSION_NUM >= 130000
|
||||
SET_HOOK_NAME(STATS_PGSS_PLANNER_HOOK, "pgss_planner_hook");
|
||||
#endif
|
||||
SET_HOOK_NAME(STATS_PGSM_EMIT_LOG_HOOK, "pgsm_emit_log_hook");
|
||||
SET_HOOK_NAME(STATS_PGSS_EXECUTORCHECKPERMS, "pgss_ExecutorCheckPerms");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
update_hook_stats(enum pg_hook_stats_id hook_id, double time_elapsed)
|
||||
{
|
||||
Assert(hook_id > STATS_START && hook_id < STATS_END);
|
||||
|
||||
struct pg_hook_stats_t *p = &pg_hook_stats[hook_id];
|
||||
|
||||
if (time_elapsed < p->min_time)
|
||||
p->min_time = time_elapsed;
|
||||
|
||||
if (time_elapsed > p->max_time)
|
||||
p->max_time = time_elapsed;
|
||||
|
||||
p->total_time += time_elapsed;
|
||||
p->ncalls++;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# pg_stat_monitor extension
|
||||
comment = 'The pg_stat_monitor is a PostgreSQL Query Performance Monitoring tool, based on PostgreSQL contrib module pg_stat_statements. pg_stat_monitor provides aggregated statistics, client information, plan details including plan, and histogram information.'
|
||||
default_version = '1.0'
|
||||
default_version = '2.0'
|
||||
module_pathname = '$libdir/pg_stat_monitor'
|
||||
relocatable = true
|
||||
|
|
|
@ -455,79 +455,9 @@ static const struct config_enum_entry track_options[] =
|
|||
#define PGSM_EXTRACT_COMMENTS get_conf(13)->guc_variable
|
||||
#define PGSM_TRACK_PLANNING get_conf(14)->guc_variable
|
||||
|
||||
|
||||
/*---- Benchmarking ----*/
|
||||
#ifdef BENCHMARK
|
||||
/*
|
||||
* These enumerator values are used as index in the hook stats array.
|
||||
* STATS_START and STATS_END are used only to delimit the range.
|
||||
* STATS_END is also the length of the valid items in the enum.
|
||||
*/
|
||||
enum pg_hook_stats_id
|
||||
{
|
||||
STATS_START = -1,
|
||||
STATS_PGSS_POST_PARSE_ANALYZE,
|
||||
STATS_PGSS_EXECUTORSTART,
|
||||
STATS_PGSS_EXECUTORUN,
|
||||
STATS_PGSS_EXECUTORFINISH,
|
||||
STATS_PGSS_EXECUTOREND,
|
||||
STATS_PGSS_PROCESSUTILITY,
|
||||
#if PG_VERSION_NUM >= 130000
|
||||
STATS_PGSS_PLANNER_HOOK,
|
||||
#endif
|
||||
STATS_PGSM_EMIT_LOG_HOOK,
|
||||
STATS_PGSS_EXECUTORCHECKPERMS,
|
||||
STATS_END
|
||||
};
|
||||
|
||||
/* Hold time to execute statistics for a hook. */
|
||||
struct pg_hook_stats_t
|
||||
{
|
||||
char hook_name[64];
|
||||
double min_time;
|
||||
double max_time;
|
||||
double total_time;
|
||||
uint64 ncalls;
|
||||
};
|
||||
|
||||
#define HOOK_STATS_SIZE MAXALIGN((size_t)STATS_END * sizeof(struct pg_hook_stats_t))
|
||||
|
||||
/* Allocate a pg_hook_stats_t array of size HOOK_STATS_SIZE on shared memory. */
|
||||
void init_hook_stats(void);
|
||||
|
||||
/* Update hook time execution statistics. */
|
||||
void update_hook_stats(enum pg_hook_stats_id hook_id, double time_elapsed);
|
||||
|
||||
/*
|
||||
* Macro used to declare a hook function:
|
||||
* Example:
|
||||
* DECLARE_HOOK(void my_hook, const char *query, size_t length);
|
||||
* Will expand to:
|
||||
* static void my_hook(const char *query, size_t length);
|
||||
* static void my_hook_benchmark(const char *query, size_t length);
|
||||
*/
|
||||
#define DECLARE_HOOK(hook, ...) \
|
||||
static hook(__VA_ARGS__); \
|
||||
static hook##_benchmark(__VA_ARGS__);
|
||||
|
||||
/*
|
||||
* Macro used to wrap a hook when pg_stat_monitor is compiled with -DBENCHMARK.
|
||||
*
|
||||
* It is intended to be used as follows in _PG_init():
|
||||
* pg_hook_function = HOOK(my_hook_function);
|
||||
* Then, if pg_stat_monitor is compiled with -DBENCHMARK this will expand to:
|
||||
* pg_hook_name = my_hook_function_benchmark;
|
||||
* Otherwise it will simple expand to:
|
||||
* pg_hook_name = my_hook_function;
|
||||
*/
|
||||
#define HOOK(name) name##_benchmark
|
||||
|
||||
#else /* #ifdef BENCHMARK */
|
||||
|
||||
#define DECLARE_HOOK(hook, ...) \
|
||||
static hook(__VA_ARGS__);
|
||||
#define HOOK(name) name
|
||||
#define HOOK_STATS_SIZE 0
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
228
pgsm_errors.c
228
pgsm_errors.c
|
@ -1,228 +0,0 @@
|
|||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* pgsm_errors.c
|
||||
* Track pg_stat_monitor internal error messages.
|
||||
*
|
||||
* Copyright © 2021, Percona LLC and/or its affiliates
|
||||
*
|
||||
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
||||
*
|
||||
* Portions Copyright (c) 1994, The Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* contrib/pg_stat_monitor/pgsm_errors.c
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <postgres.h>
|
||||
#include <access/hash.h>
|
||||
#include <storage/shmem.h>
|
||||
#include <utils/hsearch.h>
|
||||
|
||||
#include "pg_stat_monitor.h"
|
||||
#include "pgsm_errors.h"
|
||||
|
||||
|
||||
PG_FUNCTION_INFO_V1(pg_stat_monitor_errors);
|
||||
PG_FUNCTION_INFO_V1(pg_stat_monitor_reset_errors);
|
||||
|
||||
|
||||
/*
|
||||
* Maximum number of error messages tracked.
|
||||
* This should be set to a sensible value in order to track
|
||||
* the different type of errors that pg_stat_monitor may
|
||||
* report, e.g. out of memory.
|
||||
*/
|
||||
#define PSGM_ERRORS_MAX 128
|
||||
|
||||
static HTAB *pgsm_errors_ht = NULL;
|
||||
|
||||
void
|
||||
psgm_errors_init(void)
|
||||
{
|
||||
HASHCTL info;
|
||||
#if PG_VERSION_NUM >= 140000
|
||||
int flags = HASH_ELEM | HASH_STRINGS;
|
||||
#else
|
||||
int flags = HASH_ELEM | HASH_BLOBS;
|
||||
#endif
|
||||
|
||||
|
||||
memset(&info, 0, sizeof(info));
|
||||
info.keysize = ERROR_MSG_MAX_LEN;
|
||||
info.entrysize = sizeof(ErrorEntry);
|
||||
pgsm_errors_ht = ShmemInitHash("pg_stat_monitor: errors hashtable",
|
||||
PSGM_ERRORS_MAX, /* initial size */
|
||||
PSGM_ERRORS_MAX, /* maximum size */
|
||||
&info,
|
||||
flags);
|
||||
}
|
||||
|
||||
size_t
|
||||
pgsm_errors_size(void)
|
||||
{
|
||||
return hash_estimate_size(PSGM_ERRORS_MAX, sizeof(ErrorEntry));
|
||||
}
|
||||
|
||||
void
|
||||
pgsm_log(PgsmLogSeverity severity, const char *format,...)
|
||||
{
|
||||
char key[ERROR_MSG_MAX_LEN];
|
||||
ErrorEntry *entry;
|
||||
bool found = false;
|
||||
va_list ap;
|
||||
int n;
|
||||
struct timeval tv;
|
||||
struct tm *lt;
|
||||
pgssSharedState *pgss;
|
||||
|
||||
va_start(ap, format);
|
||||
n = vsnprintf(key, ERROR_MSG_MAX_LEN, format, ap);
|
||||
va_end(ap);
|
||||
|
||||
if (n < 0)
|
||||
return;
|
||||
|
||||
pgss = pgsm_get_ss();
|
||||
LWLockAcquire(pgss->errors_lock, LW_EXCLUSIVE);
|
||||
|
||||
entry = (ErrorEntry *) hash_search(pgsm_errors_ht, key, HASH_ENTER_NULL, &found);
|
||||
if (!entry)
|
||||
{
|
||||
LWLockRelease(pgss->errors_lock);
|
||||
|
||||
/*
|
||||
* We're out of memory, can't track this error message.
|
||||
*/
|
||||
return;
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
entry->severity = severity;
|
||||
entry->calls = 0;
|
||||
}
|
||||
|
||||
/* Update message timestamp. */
|
||||
gettimeofday(&tv, NULL);
|
||||
lt = localtime(&tv.tv_sec);
|
||||
snprintf(entry->time, sizeof(entry->time),
|
||||
"%04d-%02d-%02d %02d:%02d:%02d",
|
||||
lt->tm_year + 1900,
|
||||
lt->tm_mon + 1,
|
||||
lt->tm_mday,
|
||||
lt->tm_hour,
|
||||
lt->tm_min,
|
||||
lt->tm_sec);
|
||||
|
||||
entry->calls++;
|
||||
|
||||
LWLockRelease(pgss->errors_lock);
|
||||
}
|
||||
|
||||
/*
|
||||
* Clear all entries from the hash table.
|
||||
*/
|
||||
Datum
|
||||
pg_stat_monitor_reset_errors(PG_FUNCTION_ARGS)
|
||||
{
|
||||
HASH_SEQ_STATUS hash_seq;
|
||||
ErrorEntry *entry;
|
||||
pgssSharedState *pgss = pgsm_get_ss();
|
||||
|
||||
/* Safety check... */
|
||||
if (!IsSystemInitialized())
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
||||
errmsg("pg_stat_monitor: must be loaded via shared_preload_libraries")));
|
||||
|
||||
LWLockAcquire(pgss->errors_lock, LW_EXCLUSIVE);
|
||||
|
||||
hash_seq_init(&hash_seq, pgsm_errors_ht);
|
||||
while ((entry = hash_seq_search(&hash_seq)) != NULL)
|
||||
entry = hash_search(pgsm_errors_ht, &entry->message, HASH_REMOVE, NULL);
|
||||
|
||||
LWLockRelease(pgss->errors_lock);
|
||||
PG_RETURN_VOID();
|
||||
}
|
||||
|
||||
/*
|
||||
* Invoked when users query the view pg_stat_monitor_errors.
|
||||
* This function creates tuples with error messages from data present in
|
||||
* the hash table, then return the dataset to the caller.
|
||||
*/
|
||||
Datum
|
||||
pg_stat_monitor_errors(PG_FUNCTION_ARGS)
|
||||
{
|
||||
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
|
||||
TupleDesc tupdesc;
|
||||
Tuplestorestate *tupstore;
|
||||
MemoryContext per_query_ctx;
|
||||
MemoryContext oldcontext;
|
||||
HASH_SEQ_STATUS hash_seq;
|
||||
ErrorEntry *error_entry;
|
||||
pgssSharedState *pgss = pgsm_get_ss();
|
||||
|
||||
/* Safety check... */
|
||||
if (!IsSystemInitialized())
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
||||
errmsg("pg_stat_monitor: must be loaded via shared_preload_libraries")));
|
||||
|
||||
/* check to see if caller supports us returning a tuplestore */
|
||||
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||
errmsg("pg_stat_monitor: set-valued function called in context that cannot accept a set")));
|
||||
|
||||
/* Switch into long-lived context to construct returned data structures */
|
||||
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
|
||||
oldcontext = MemoryContextSwitchTo(per_query_ctx);
|
||||
|
||||
/* Build a tuple descriptor for our result type */
|
||||
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
||||
elog(ERROR, "pg_stat_monitor: return type must be a row type");
|
||||
|
||||
if (tupdesc->natts != 4)
|
||||
elog(ERROR, "pg_stat_monitor: incorrect number of output arguments, required 3, found %d", tupdesc->natts);
|
||||
|
||||
tupstore = tuplestore_begin_heap(true, false, work_mem);
|
||||
rsinfo->returnMode = SFRM_Materialize;
|
||||
rsinfo->setResult = tupstore;
|
||||
rsinfo->setDesc = tupdesc;
|
||||
|
||||
MemoryContextSwitchTo(oldcontext);
|
||||
|
||||
LWLockAcquire(pgss->errors_lock, LW_SHARED);
|
||||
|
||||
hash_seq_init(&hash_seq, pgsm_errors_ht);
|
||||
while ((error_entry = hash_seq_search(&hash_seq)) != NULL)
|
||||
{
|
||||
Datum values[4];
|
||||
bool nulls[4];
|
||||
int i = 0;
|
||||
|
||||
memset(values, 0, sizeof(values));
|
||||
memset(nulls, 0, sizeof(nulls));
|
||||
|
||||
values[i++] = Int64GetDatumFast(error_entry->severity);
|
||||
values[i++] = CStringGetTextDatum(error_entry->message);
|
||||
values[i++] = CStringGetTextDatum(error_entry->time);
|
||||
values[i++] = Int64GetDatumFast(error_entry->calls);
|
||||
|
||||
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
|
||||
}
|
||||
|
||||
LWLockRelease(pgss->errors_lock);
|
||||
/* clean up and return the tuplestore */
|
||||
tuplestore_donestoring(tupstore);
|
||||
|
||||
return (Datum) 0;
|
||||
}
|
Loading…
Reference in New Issue