diff --git a/guc.c b/guc.c index 52db4e3..9b1cbcb 100644 --- a/guc.c +++ b/guc.c @@ -20,8 +20,13 @@ GucVariable conf[MAX_SETTINGS]; static void DefineIntGUC(GucVariable *conf); +static void DefineIntGUCWithCheck(GucVariable *conf, GucIntCheckHook check); static void DefineBoolGUC(GucVariable *conf); +/* Check hooks to ensure histogram_min < histogram_max */ +static bool check_histogram_min(int *newval, void **extra, GucSource source); +static bool check_histogram_max(int *newval, void **extra, GucSource source); + /* * Define (or redefine) custom GUC variables. */ @@ -123,7 +128,7 @@ init_guc(void) .guc_unit = 0, .guc_value = &PGSM_HISTOGRAM_MIN }; - DefineIntGUC(&conf[i++]); + DefineIntGUCWithCheck(&conf[i++], check_histogram_min); conf[i] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_histogram_max", @@ -135,7 +140,7 @@ init_guc(void) .guc_unit = 0, .guc_value = &PGSM_HISTOGRAM_MAX }; - DefineIntGUC(&conf[i++]); + DefineIntGUCWithCheck(&conf[i++], check_histogram_max); conf[i] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_histogram_buckets", @@ -201,8 +206,7 @@ init_guc(void) #endif } -static void -DefineIntGUC(GucVariable *conf) +static void DefineIntGUCWithCheck(GucVariable *conf, GucIntCheckHook check) { DefineCustomIntVariable(conf->guc_name, conf->guc_desc, @@ -213,10 +217,17 @@ DefineIntGUC(GucVariable *conf) conf->guc_max, conf->guc_restart ? PGC_POSTMASTER : PGC_USERSET, conf->guc_unit, - NULL, + check, NULL, NULL); } + +static void +DefineIntGUC(GucVariable *conf) +{ + DefineIntGUCWithCheck(conf, NULL); +} + static void DefineBoolGUC(GucVariable *conf) { @@ -238,3 +249,16 @@ get_conf(int i) return &conf[i]; } +static bool check_histogram_min(int *newval, void **extra, GucSource source) +{ + /* + * During module initialization PGSM_HISTOGRAM_MIN is initialized before + * PGSM_HISTOGRAM_MAX, in this case PGSM_HISTOGRAM_MAX will be zero. + */ + return (PGSM_HISTOGRAM_MAX == 0 || *newval < PGSM_HISTOGRAM_MAX); +} + +static bool check_histogram_max(int *newval, void **extra, GucSource source) +{ + return (*newval > PGSM_HISTOGRAM_MIN); +} diff --git a/hash_query.c b/hash_query.c index 809c829..b33d226 100644 --- a/hash_query.c +++ b/hash_query.c @@ -135,13 +135,13 @@ hash_entry_alloc(pgssSharedState *pgss, pgssHashKey *key, int encoding) if (hash_get_num_entries(pgss_hash) >= MAX_BUCKET_ENTRIES) { - elog(DEBUG1, "%s", "pg_stat_monitor: out of memory"); + elog(DEBUG1, "pg_stat_monitor: out of memory"); return NULL; } /* Find or create an entry with desired hash code */ entry = (pgssEntry *) hash_search(pgss_hash, key, HASH_ENTER_NULL, &found); if (entry == NULL) - pgsm_log_error("hash_entry_alloc: OUT OF MEMORY"); + elog(DEBUG1, "hash_entry_alloc: OUT OF MEMORY"); else if (!found) { pgss->bucket_entry[pg_atomic_read_u64(&pgss->current_wbucket)]++; @@ -216,7 +216,7 @@ hash_entry_dealloc(int new_bucket_id, int old_bucket_id, unsigned char *query_bu pgssEntry *bkp_entry = malloc(sizeof(pgssEntry)); if (!bkp_entry) { - pgsm_log_error("hash_entry_dealloc: out of memory"); + elog(DEBUG1, "hash_entry_dealloc: out of memory"); /* * No memory, If the entry has calls > 1 then we change the state to finished, * as the pending query will likely finish execution during the new bucket diff --git a/pg_stat_monitor--1.0.13.sql.in b/pg_stat_monitor--1.0.13.sql.in index 864b617..4147536 100644 --- a/pg_stat_monitor--1.0.13.sql.in +++ b/pg_stat_monitor--1.0.13.sql.in @@ -119,13 +119,12 @@ LANGUAGE SQL PARALLEL SAFE; CREATE FUNCTION pg_stat_monitor_settings( OUT name text, - OUT value text, - OUT default_value text, + OUT value INTEGER, + OUT default_value INTEGER, OUT description text, OUT minimum INTEGER, OUT maximum INTEGER, - OUT options text, - OUT restart text + OUT restart INTEGER ) RETURNS SETOF record AS 'MODULE_PATHNAME', 'pg_stat_monitor_settings' @@ -138,7 +137,6 @@ CREATE VIEW pg_stat_monitor_settings AS SELECT description, minimum, maximum, - options, restart FROM pg_stat_monitor_settings(); @@ -259,40 +257,8 @@ $$ language plpgsql; -- 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(); -CREATE FUNCTION pg_stat_monitor_errors( - OUT severity int, - OUT message text, - OUT msgtime text, - OUT calls int8 -) -RETURNS SETOF record -AS 'MODULE_PATHNAME', 'pg_stat_monitor_errors' -LANGUAGE C STRICT VOLATILE PARALLEL SAFE; - -CREATE OR REPLACE FUNCTION pgsm_log_severity_as_text(severity int) RETURNS TEXT AS -$$ -SELECT - CASE - WHEN severity = 0 THEN 'INFO' - WHEN severity = 1 THEN 'WARNING' - WHEN severity = 2 THEN 'ERROR' - END -$$ -LANGUAGE SQL PARALLEL SAFE; - -CREATE VIEW pg_stat_monitor_errors AS SELECT - pgsm_log_severity_as_text(severity) as severity, message, msgtime, calls -FROM pg_stat_monitor_errors(); - -CREATE FUNCTION pg_stat_monitor_reset_errors() -RETURNS void -AS 'MODULE_PATHNAME' -LANGUAGE C PARALLEL SAFE; - GRANT SELECT ON pg_stat_monitor TO PUBLIC; GRANT SELECT ON pg_stat_monitor_settings TO PUBLIC; -GRANT SELECT ON pg_stat_monitor_errors TO PUBLIC; -- Don't want this to be available to non-superusers. -REVOKE ALL ON FUNCTION pg_stat_monitor_reset() FROM PUBLIC; -REVOKE ALL ON FUNCTION pg_stat_monitor_reset_errors() FROM PUBLIC; +REVOKE ALL ON FUNCTION pg_stat_monitor_reset() FROM PUBLIC; \ No newline at end of file diff --git a/pg_stat_monitor--1.0.14.sql.in b/pg_stat_monitor--1.0.14.sql.in index 08f13b9..f2bd4c8 100644 --- a/pg_stat_monitor--1.0.14.sql.in +++ b/pg_stat_monitor--1.0.14.sql.in @@ -119,13 +119,12 @@ LANGUAGE SQL PARALLEL SAFE; CREATE FUNCTION pg_stat_monitor_settings( OUT name text, - OUT value text, - OUT default_value text, + OUT value INTEGER, + OUT default_value INTEGER, OUT description text, OUT minimum INTEGER, OUT maximum INTEGER, - OUT options text, - OUT restart text + OUT restart INTEGER ) RETURNS SETOF record AS 'MODULE_PATHNAME', 'pg_stat_monitor_settings' @@ -138,7 +137,6 @@ CREATE VIEW pg_stat_monitor_settings AS SELECT description, minimum, maximum, - options, restart FROM pg_stat_monitor_settings(); @@ -260,40 +258,8 @@ $$ language plpgsql; -- 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(); -CREATE FUNCTION pg_stat_monitor_errors( - OUT severity int, - OUT message text, - OUT msgtime text, - OUT calls int8 -) -RETURNS SETOF record -AS 'MODULE_PATHNAME', 'pg_stat_monitor_errors' -LANGUAGE C STRICT VOLATILE PARALLEL SAFE; - -CREATE OR REPLACE FUNCTION pgsm_log_severity_as_text(severity int) RETURNS TEXT AS -$$ -SELECT - CASE - WHEN severity = 0 THEN 'INFO' - WHEN severity = 1 THEN 'WARNING' - WHEN severity = 2 THEN 'ERROR' - END -$$ -LANGUAGE SQL PARALLEL SAFE; - -CREATE VIEW pg_stat_monitor_errors AS SELECT - pgsm_log_severity_as_text(severity) as severity, message, msgtime, calls -FROM pg_stat_monitor_errors(); - -CREATE FUNCTION pg_stat_monitor_reset_errors() -RETURNS void -AS 'MODULE_PATHNAME' -LANGUAGE C PARALLEL SAFE; - GRANT SELECT ON pg_stat_monitor TO PUBLIC; GRANT SELECT ON pg_stat_monitor_settings TO PUBLIC; -GRANT SELECT ON pg_stat_monitor_errors TO PUBLIC; -- Don't want this to be available to non-superusers. -REVOKE ALL ON FUNCTION pg_stat_monitor_reset() FROM PUBLIC; -REVOKE ALL ON FUNCTION pg_stat_monitor_reset_errors() FROM PUBLIC; +REVOKE ALL ON FUNCTION pg_stat_monitor_reset() FROM PUBLIC; \ No newline at end of file diff --git a/pg_stat_monitor.c b/pg_stat_monitor.c index 52780ab..7de3dd3 100644 --- a/pg_stat_monitor.c +++ b/pg_stat_monitor.c @@ -485,7 +485,7 @@ static void pgss_ExecutorStart(QueryDesc *queryDesc, int eflags) { if (getrusage(RUSAGE_SELF, &rusage_start) != 0) - pgsm_log_error("pgss_ExecutorStart: failed to execute getrusage"); + elog(DEBUG1, "pgss_ExecutorStart: failed to execute getrusage"); if (prev_ExecutorStart) prev_ExecutorStart(queryDesc, eflags); @@ -980,6 +980,7 @@ static void pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString, WalUsage walusage_start = pgWalUsage; #endif INSTR_TIME_SET_CURRENT(start); + nested_level++; PG_TRY(); { #if PG_VERSION_NUM >= 140000 @@ -1018,12 +1019,12 @@ static void pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString, dest, completionTag); #endif + nested_level--; } PG_CATCH(); { nested_level--; PG_RE_THROW(); - } PG_END_TRY(); @@ -1513,7 +1514,7 @@ pgss_store(uint64 queryid, #if PG_VERSION_NUM < 140000 key.toplevel = 1; #else - key.toplevel = ((exec_nested_level + plan_nested_level) == 0); + key.toplevel = ((nested_level + plan_nested_level) == 0); #endif pgss_hash = pgsm_get_hash(); @@ -1552,7 +1553,7 @@ pgss_store(uint64 queryid, LWLockRelease(pgss->lock); if (norm_query) pfree(norm_query); - pgsm_log_error("pgss_store: out of memory (pgss_query_hash)."); + elog(DEBUG1, "pgss_store: out of memory (pgss_query_hash)."); return; } else if (!query_found) @@ -1578,7 +1579,7 @@ pgss_store(uint64 queryid, LWLockRelease(pgss->lock); if (norm_query) pfree(norm_query); - pgsm_log_error("pgss_store: insufficient shared space for query."); + elog(DEBUG1, "pgss_store: insufficient shared space for query."); return; } /* @@ -1624,7 +1625,6 @@ pgss_store(uint64 queryid, kind, /* kind */ app_name_ptr, app_name_len); - } LWLockRelease(pgss->lock); if (norm_query) @@ -1794,7 +1794,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo, { if (read_query(pgss_qbuf, tmp.info.parentid, parent_query_txt, 0) == 0) { - rc = read_query_buffer(bucketid, tmp.info.parentid, parent_query_txt, 0); + int rc = read_query_buffer(bucketid, tmp.info.parentid, parent_query_txt, 0); if (rc != 1) snprintf(parent_query_txt, 32, "%s", ""); } @@ -2088,7 +2088,7 @@ get_next_wbucket(pgssSharedState *pgss) * definitely make the while condition to fail, we can stop the loop as another * thread has already updated prev_bucket_usec. */ - while ((current_usec - current_bucket_usec) > (PGSM_BUCKET_TIME * 1000 * 1000)) + while ((current_usec - current_bucket_usec) > ((uint64)PGSM_BUCKET_TIME * 1000LU * 1000LU)) { if (pg_atomic_compare_exchange_u64(&pgss->prev_bucket_usec, ¤t_bucket_usec, current_usec)) { @@ -3215,7 +3215,7 @@ SaveQueryText(uint64 bucketid, if (pgss->overflow) { - pgsm_log_error("query buffer overflowed twice"); + elog(DEBUG1, "query buffer overflowed twice"); return false; } diff --git a/regression/expected/application_name_unique.out b/regression/expected/application_name_unique.out index 15ba62f..4598af4 100644 --- a/regression/expected/application_name_unique.out +++ b/regression/expected/application_name_unique.out @@ -20,14 +20,15 @@ SELECT 1 AS num; (1 row) SELECT query,application_name FROM pg_stat_monitor ORDER BY query, application_name COLLATE "C"; - query | application_name ---------------------------------+------------------------------------ - SELECT $1 AS num | naeem - SELECT $1 AS num | psql - SELECT pg_stat_monitor_reset() | pg_regress/application_name_unique - Set application_name = 'naeem' | naeem - Set application_name = 'psql' | psql -(5 rows) + query | application_name +-------------------------------------------------------------------------------------------------+------------------------------------ + SELECT $1 AS num | naeem + SELECT $1 AS num | psql + SELECT pg_stat_monitor_reset() | pg_regress/application_name_unique + SELECT query,application_name FROM pg_stat_monitor ORDER BY query, application_name COLLATE "C" | psql + Set application_name = 'naeem' | naeem + Set application_name = 'psql' | psql +(6 rows) SELECT pg_stat_monitor_reset(); pg_stat_monitor_reset diff --git a/regression/expected/counters.out b/regression/expected/counters.out index 40fed48..5aafb30 100644 --- a/regression/expected/counters.out +++ b/regression/expected/counters.out @@ -49,12 +49,6 @@ SELECT pg_stat_monitor_reset(); (1 row) -SELECT pg_stat_monitor_reset(); - pg_stat_monitor_reset ------------------------ - -(1 row) - do $$ declare n integer:= 1; @@ -83,12 +77,6 @@ SELECT query,calls FROM pg_stat_monitor ORDER BY query COLLATE "C"; end $$ | (4 rows) -SELECT pg_stat_monitor_reset(); - pg_stat_monitor_reset ------------------------ - -(1 row) - DROP TABLE t1; DROP TABLE t2; DROP TABLE t3; diff --git a/regression/expected/error_insert.out b/regression/expected/error_insert.out index 19a1d89..ca7ab1d 100644 --- a/regression/expected/error_insert.out +++ b/regression/expected/error_insert.out @@ -17,13 +17,14 @@ ERROR: duplicate key value violates unique constraint "company_pkey" DETAIL: Key (id)=(1) already exists. Drop Table if exists Company; SELECT query, elevel, sqlcode, message FROM pg_stat_monitor ORDER BY query COLLATE "C",elevel; - query | elevel | sqlcode | message --------------------------------------------------------+--------+---------+--------------------------------------------------------------- - Drop Table if exists Company | 0 | | - INSERT INTO Company(ID, Name) VALUES ($1, $2) | 0 | | - INSERT INTO Company(ID, Name) VALUES (1, 'Percona'); | 21 | 23505 | duplicate key value violates unique constraint "company_pkey" - SELECT pg_stat_monitor_reset() | 0 | | -(4 rows) + query | elevel | sqlcode | message +-----------------------------------------------------------------------------------------------+--------+---------+--------------------------------------------------------------- + Drop Table if exists Company | 0 | | + INSERT INTO Company(ID, Name) VALUES ($1, $2) | 0 | | + INSERT INTO Company(ID, Name) VALUES (1, 'Percona'); | 21 | 23505 | duplicate key value violates unique constraint "company_pkey" + SELECT pg_stat_monitor_reset() | 0 | | + SELECT query, elevel, sqlcode, message FROM pg_stat_monitor ORDER BY query COLLATE "C",elevel | 0 | | +(5 rows) SELECT pg_stat_monitor_reset(); pg_stat_monitor_reset diff --git a/regression/expected/error_insert_1.out b/regression/expected/error_insert_1.out index c706613..708dab3 100644 --- a/regression/expected/error_insert_1.out +++ b/regression/expected/error_insert_1.out @@ -17,13 +17,14 @@ ERROR: duplicate key value violates unique constraint "company_pkey" DETAIL: Key (id)=(1) already exists. Drop Table if exists Company; SELECT query, elevel, sqlcode, message FROM pg_stat_monitor ORDER BY query COLLATE "C",elevel; - query | elevel | sqlcode | message --------------------------------------------------------+--------+---------+--------------------------------------------------------------- - Drop Table if exists Company | 0 | | - INSERT INTO Company(ID, Name) VALUES ($1, $2) | 0 | | - INSERT INTO Company(ID, Name) VALUES (1, 'Percona'); | 20 | 23505 | duplicate key value violates unique constraint "company_pkey" - SELECT pg_stat_monitor_reset() | 0 | | -(4 rows) + query | elevel | sqlcode | message +-----------------------------------------------------------------------------------------------+--------+---------+--------------------------------------------------------------- + Drop Table if exists Company | 0 | | + INSERT INTO Company(ID, Name) VALUES ($1, $2) | 0 | | + INSERT INTO Company(ID, Name) VALUES (1, 'Percona'); | 20 | 23505 | duplicate key value violates unique constraint "company_pkey" + SELECT pg_stat_monitor_reset() | 0 | | + SELECT query, elevel, sqlcode, message FROM pg_stat_monitor ORDER BY query COLLATE "C",elevel | 0 | | +(5 rows) SELECT pg_stat_monitor_reset(); pg_stat_monitor_reset diff --git a/regression/expected/histogram.out b/regression/expected/histogram.out index 40e7fe5..b469b72 100644 --- a/regression/expected/histogram.out +++ b/regression/expected/histogram.out @@ -45,13 +45,14 @@ INFO: Sleep 5 seconds (1 row) SELECT substr(query, 0,50) as query, calls, resp_calls FROM pg_stat_monitor ORDER BY query COLLATE "C"; - query | calls | resp_calls ----------------------------------+-------+----------------------- - SELECT pg_sleep(i) | 5 | {0,0,0,0,0,0,3,2,0,0} - SELECT pg_stat_monitor_reset() | 1 | {1,0,0,0,0,0,0,0,0,0} - Set pg_stat_monitor.track='all' | 1 | {1,0,0,0,0,0,0,0,0,0} - select run_pg_sleep($1) | 1 | {0,0,0,0,0,0,0,0,1,0} -(4 rows) + query | calls | resp_calls +---------------------------------------------------+-------+----------------------- + SELECT pg_sleep(i) | 5 | {0,0,0,0,0,0,3,2,0,0} + SELECT pg_stat_monitor_reset() | 1 | {1,0,0,0,0,0,0,0,0,0} + SELECT substr(query, $1,$2) as query, calls, resp | 1 | {1,0,0,0,0,0,0,0,0,0} + Set pg_stat_monitor.track='all' | 1 | {1,0,0,0,0,0,0,0,0,0} + select run_pg_sleep($1) | 1 | {0,0,0,0,0,0,0,0,1,0} +(5 rows) select * from generate_histogram(); range | freq | bar diff --git a/regression/sql/counters.sql b/regression/sql/counters.sql index 8d5c461..51d4fbd 100644 --- a/regression/sql/counters.sql +++ b/regression/sql/counters.sql @@ -12,9 +12,9 @@ SELECT a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a; SELECT a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a; SELECT a,b,c,d FROM t1, t2, t3, t4 WHERE t1.a = t2.b AND t3.c = t4.d ORDER BY a; SELECT query,calls FROM pg_stat_monitor ORDER BY query COLLATE "C"; -SELECT pg_stat_monitor_reset(); SELECT pg_stat_monitor_reset(); + do $$ declare n integer:= 1; @@ -26,7 +26,6 @@ begin end loop; end $$; SELECT query,calls FROM pg_stat_monitor ORDER BY query COLLATE "C"; -SELECT pg_stat_monitor_reset(); DROP TABLE t1; DROP TABLE t2;