From a6036b86ac1018547bd5891e44348c64c5bfa810 Mon Sep 17 00:00:00 2001 From: Ibrar Ahmed Date: Thu, 11 Feb 2021 12:02:04 +0000 Subject: [PATCH] PG-174: Code cleanup. --- Makefile | 2 +- expected/pg_stat_monitor.out | 31 ++--- guc.c | 251 +++++++++++++---------------------- hash_query.c | 16 +-- pg_stat_monitor--1.0.sql | 6 +- pg_stat_monitor.c | 196 ++++++++++++++------------- pg_stat_monitor.h | 10 +- 7 files changed, 208 insertions(+), 304 deletions(-) diff --git a/Makefile b/Makefile index dc1c41a..ab78ee1 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ PGFILEDESC = "pg_stat_monitor - execution statistics of SQL statements" LDFLAGS_SL += $(filter -lm, $(LIBS)) REGRESS_OPTS = --temp-config $(top_srcdir)/contrib/pg_stat_monitor/pg_stat_monitor.conf -REGRESS = basic pg_stat_monitor +REGRESS = guc basic pg_stat_monitor # Disabled because these tests require "shared_preload_libraries=pg_stat_statements", # which typical installcheck users do not have (e.g. buildfarm clients). diff --git a/expected/pg_stat_monitor.out b/expected/pg_stat_monitor.out index 3fbd635..84234e3 100644 --- a/expected/pg_stat_monitor.out +++ b/expected/pg_stat_monitor.out @@ -232,11 +232,12 @@ SELECT 1 + 1 AS "two"; (1 row) SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C"; - query | calls | rows -----------------+-------+------ - SELECT $1 | 0 | 0 - SELECT $1 + $2 | 0 | 0 -(2 rows) + query | calls | rows +--------------------------------+-------+------ + SELECT $1 | 1 | 1 + SELECT $1 + $2 | 1 | 1 + SELECT pg_stat_monitor_reset() | 1 | 1 +(3 rows) -- -- pg_stat_monitor.track = top @@ -295,8 +296,8 @@ SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C"; SELECT $1 +| 1 | 1 +| | AS "text" | | - SELECT (i + $2 + $3)::INTEGER | 0 | 0 - SELECT (i + $2)::INTEGER LIMIT $3 | 0 | 0 + SELECT (i + $2 + $3)::INTEGER | 2 | 2 + SELECT (i + $2)::INTEGER LIMIT $3 | 2 | 2 SELECT PLUS_ONE($1) | 2 | 2 SELECT PLUS_TWO($1) | 2 | 2 SELECT pg_stat_monitor_reset() | 1 | 1 @@ -389,16 +390,10 @@ NOTICE: table "test" does not exist, skipping NOTICE: function plus_one(pg_catalog.int4) does not exist, skipping DROP FUNCTION PLUS_TWO(INTEGER); SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C"; - query | calls | rows --------------------------------------------+-------+------ - CREATE INDEX test_b ON test(b) | 1 | 0 - DROP FUNCTION IF EXISTS PLUS_ONE(INTEGER) | 1 | 0 - DROP FUNCTION PLUS_ONE(INTEGER) | 1 | 0 - DROP FUNCTION PLUS_TWO(INTEGER) | 1 | 0 - DROP TABLE IF EXISTS test | 3 | 0 - DROP TABLE test | 1 | 0 - SELECT $1 | 1 | 1 - SELECT pg_stat_monitor_reset() | 1 | 1 -(8 rows) + query | calls | rows +--------------------------------+-------+------ + SELECT $1 | 1 | 1 + SELECT pg_stat_monitor_reset() | 1 | 1 +(2 rows) DROP EXTENSION pg_stat_monitor; diff --git a/guc.c b/guc.c index 6acacd1..2de9200 100644 --- a/guc.c +++ b/guc.c @@ -19,6 +19,8 @@ #include "pg_stat_monitor.h" GucVariable conf[13]; +static void DefineIntGUC(GucVariable *conf); +static void DefineBoolGUC(GucVariable *conf); /* * Define (or redefine) custom GUC variables. @@ -27,253 +29,178 @@ void init_guc(void) { int i = 0; - conf[i++] = (GucVariable) { + conf[i] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_max", .guc_desc = "Sets the maximum size of shared memory in (MB) used for statement's metadata tracked by pg_stat_monitor.", .guc_default = 100, .guc_min = 1, .guc_max = 1000, - .guc_restart = true + .guc_restart = true, + .guc_unit = GUC_UNIT_MB, + .guc_value = &PGSM_MAX }; + DefineIntGUC(&conf[i++]); - conf[i++] = (GucVariable) { + conf[i] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_query_max_len", .guc_desc = "Sets the maximum length of query.", .guc_default = 1024, .guc_min = 1024, .guc_max = INT_MAX, - .guc_restart = true + .guc_unit = 0, + .guc_restart = true, + .guc_value = &PGSM_QUERY_MAX_LEN }; - conf[i++] = (GucVariable) { + DefineIntGUC(&conf[i++]); + + conf[i] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_enable", .guc_desc = "Enable/Disable statistics collector.", .guc_default = 1, .guc_min = 0, .guc_max = 0, - .guc_restart = true + .guc_restart = false, + .guc_unit = 0, + .guc_value = &PGSM_ENABLED }; - conf[i++] = (GucVariable) { + DefineBoolGUC(&conf[i++]); + + conf[i] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_track_utility", .guc_desc = "Selects whether utility commands are tracked.", .guc_default = 0, .guc_min = 0, .guc_max = 0, - .guc_restart = false + .guc_restart = false, + .guc_unit = 0, + .guc_value = &PGSM_TRACK_UTILITY }; + DefineBoolGUC(&conf[i++]); - conf[i++] = (GucVariable) { + conf[i] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_normalized_query", .guc_desc = "Selects whether save query in normalized format.", .guc_default = 1, .guc_min = 0, .guc_max = 0, - .guc_restart = false + .guc_restart = false, + .guc_unit = 0, + .guc_value = &PGSM_NORMALIZED_QUERY }; - conf[i++] = (GucVariable) { + DefineBoolGUC(&conf[i++]); + + conf[i] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_max_buckets", .guc_desc = "Sets the maximum number of buckets.", .guc_default = 10, .guc_min = 1, .guc_max = 10, - .guc_restart = true + .guc_restart = true, + .guc_unit = 0, + .guc_value = &PGSM_MAX_BUCKETS }; - conf[i++] = (GucVariable) { + DefineIntGUC(&conf[i++]); + + conf[i] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_bucket_time", .guc_desc = "Sets the time in seconds per bucket.", .guc_default = 60, .guc_min = 1, .guc_max = INT_MAX, - .guc_restart = true + .guc_restart = true, + .guc_unit = 0, + .guc_value = &PGSM_BUCKET_TIME }; + DefineIntGUC(&conf[i++]); - conf[i++] = (GucVariable) { + conf[i] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_histogram_min", .guc_desc = "Sets the time in millisecond.", .guc_default = 0, .guc_min = 0, .guc_max = INT_MAX, - .guc_restart = true + .guc_restart = true, + .guc_unit = 0, + .guc_value = &PGSM_HISTOGRAM_MIN }; - conf[i++] = (GucVariable) { + DefineIntGUC(&conf[i++]); + + conf[i] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_histogram_max", .guc_desc = "Sets the time in millisecond.", .guc_default = 10, .guc_min = 10, .guc_max = INT_MAX, - .guc_restart = true + .guc_restart = true, + .guc_unit = 0, + .guc_value = &PGSM_HISTOGRAM_MAX }; + DefineIntGUC(&conf[i++]); - conf[i++] = (GucVariable) { + conf[i] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_histogram_buckets", .guc_desc = "Sets the maximum number of histogram buckets", .guc_default = 10, .guc_min = 2, .guc_max = INT_MAX, - .guc_restart = true + .guc_restart = true, + .guc_unit = 0, + .guc_value = &PGSM_HISTOGRAM_BUCKETS }; + DefineIntGUC(&conf[i++]); - conf[i++] = (GucVariable) { + conf[i] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_query_shared_buffer", .guc_desc = "Sets the maximum size of shared memory in (MB) used for query tracked by pg_stat_monitor.", .guc_default = 20, .guc_min = 1, .guc_max = 10000, - .guc_restart = true + .guc_restart = true, + .guc_unit = GUC_UNIT_MB, + .guc_value = &PGSM_QUERY_SHARED_BUFFER }; + DefineIntGUC(&conf[i++]); + #if PG_VERSION_NUM >= 130000 - conf[i++] = (GucVariable) { + conf[i] = (GucVariable) { .guc_name = "pg_stat_monitor.pgsm_track_planning", .guc_desc = "Selects whether planning statistics are tracked.", .guc_default = 1, .guc_min = 0, .guc_max = 0, - .guc_restart = false + .guc_restart = false, + .guc_unit = 0, + .guc_value = &PGSM_TRACK_PLANNING }; + DefineBoolGUC(&conf[i++]); #endif +} - DefineCustomIntVariable("pg_stat_monitor.pgsm_max", - "Sets the maximum size of shared memory in (MB) used for statement's metadata tracked by pg_stat_monitor.", +static void +DefineIntGUC(GucVariable *conf) +{ + DefineCustomIntVariable(conf->guc_name, + conf->guc_desc, NULL, - &PGSM_MAX, - 100, - 1, - 1000, - PGC_POSTMASTER, - GUC_UNIT_MB, + conf->guc_value, + conf->guc_default, + conf->guc_min, + conf->guc_max, + conf->guc_restart ? PGC_POSTMASTER : PGC_USERSET, + conf->guc_unit, NULL, NULL, NULL); - - DefineCustomIntVariable("pg_stat_monitor.pgsm_query_max_len", - "Sets the maximum length of query.", +} +static void +DefineBoolGUC(GucVariable *conf) +{ + DefineCustomBoolVariable(conf->guc_name, + conf->guc_desc, NULL, - &PGSM_QUERY_MAX_LEN, - 1024, - 1024, - INT_MAX, - PGC_POSTMASTER, - 0, - NULL, - NULL, - NULL); - - DefineCustomBoolVariable("pg_stat_monitor.pgsm_enable", - "Enable/Disable statistics collector.", - NULL, - (bool*)&PGSM_ENABLED, - true, - PGC_USERSET, - 0, - NULL, - NULL, - NULL); - - DefineCustomBoolVariable("pg_stat_monitor.pgsm_track_utility", - "Selects whether utility commands are tracked by pg_stat_monitor.", - NULL, - (bool*)&PGSM_TRACK_UTILITY, - true, - PGC_USERSET, - 0, - NULL, - NULL, - NULL); - - DefineCustomBoolVariable("pg_stat_monitor.pgsm_normalized_query", - "Selects whether save query in normalized format.", - NULL, - (bool*)&PGSM_NORMALIZED_QUERY, - true, - PGC_USERSET, - 0, - NULL, - NULL, - NULL); - - DefineCustomIntVariable("pg_stat_monitor.pgsm_max_buckets", - "Sets the maximum number of buckets.", - NULL, - &PGSM_MAX_BUCKETS, - 10, - 1, - 10, - PGC_POSTMASTER, - 0, - NULL, - NULL, - NULL); - - DefineCustomIntVariable("pg_stat_monitor.pgsm_bucket_time", - "Sets the time in seconds per bucket.", - NULL, - &PGSM_BUCKET_TIME, - 60, - 1, - INT_MAX, - PGC_POSTMASTER, - 0, - NULL, - NULL, - NULL); - - DefineCustomIntVariable("pg_stat_monitor.pgsm_histogram_min", - "Sets the time in millisecond.", - NULL, - &PGSM_HISTOGRAM_MIN, - 0, - 0, - INT_MAX, - PGC_POSTMASTER, - 0, - NULL, - NULL, - NULL); - - DefineCustomIntVariable("pg_stat_monitor.pgsm_histogram_max", - "Sets the time in millisecond.", - NULL, - &PGSM_HISTOGRAM_MAX, - 10, - 10, - INT_MAX, - PGC_POSTMASTER, - 0, - NULL, - NULL, - NULL); - - DefineCustomIntVariable("pg_stat_monitor.pgsm_histogram_buckets", - "Sets the total number of histogram buckets", - NULL, - &PGSM_HISTOGRAM_BUCKETS, - 10, - 2, - INT_MAX, - PGC_POSTMASTER, - 0, - NULL, - NULL, - NULL); - - DefineCustomIntVariable("pg_stat_monitor.pgsm_query_shared_buffer", - "Sets the maximum size of shared memory in (MB) used for query tracked by pg_stat_monitor.", - NULL, - &PGSM_QUERY_BUF_SIZE, - 20, - 1, - 10000, - PGC_POSTMASTER, - 0, - NULL, - NULL, - NULL); - - DefineCustomBoolVariable("pg_stat_monitor.pgsm_track_planning", - "Selects whether track planning statistics.", - NULL, - (bool*)&PGSM_TRACK_PLANNING, - true, - PGC_SUSET, + (bool*)conf->guc_value, + conf->guc_default, + conf->guc_restart ? PGC_POSTMASTER : PGC_USERSET, 0, NULL, NULL, diff --git a/hash_query.c b/hash_query.c index c204c4b..3c36fa0 100644 --- a/hash_query.c +++ b/hash_query.c @@ -83,12 +83,6 @@ pgss_startup(void) on_shmem_exit(pgss_shmem_shutdown, (Datum) 0); } -int -pgsm_get_bucket_size(void) -{ - return pgss->query_buf_size_bucket; -} - pgssSharedState* pgsm_get_ss(void) { @@ -101,12 +95,6 @@ pgsm_get_hash(void) return pgss_hash; } -HTAB* -pgsm_get_query_hash(void) -{ - return pgss_query_hash; -} - /* * shmem_shutdown hook: Dump statistics into file. * @@ -234,7 +222,7 @@ bool hash_create_query_entry(uint64 bucket_id, uint64 queryid) { pgssQueryHashKey key; - pgssQueryEntry *entry = NULL; + pgssQueryEntry *entry; bool found; key.queryid = queryid; @@ -249,7 +237,7 @@ bool hash_find_query_entry(uint64 bucket_id, uint64 queryid) { pgssQueryHashKey key; - pgssQueryEntry *entry = NULL; + pgssQueryEntry *entry; bool found; key.queryid = queryid; diff --git a/pg_stat_monitor--1.0.sql b/pg_stat_monitor--1.0.sql index f5ed116..6486360 100644 --- a/pg_stat_monitor--1.0.sql +++ b/pg_stat_monitor--1.0.sql @@ -71,9 +71,9 @@ CREATE FUNCTION pg_stat_monitor(IN showtext boolean, OUT resp_calls text, OUT cpu_user_time float8, OUT cpu_sys_time float8, - OUT wal_records int8, - OUT wal_fpi int8, - OUT wal_bytes numeric + OUT wal_records int8, + OUT wal_fpi int8, + OUT wal_bytes numeric ) RETURNS SETOF record AS 'MODULE_PATHNAME', 'pg_stat_monitor' diff --git a/pg_stat_monitor.c b/pg_stat_monitor.c index 947013d..544f664 100644 --- a/pg_stat_monitor.c +++ b/pg_stat_monitor.c @@ -21,7 +21,7 @@ PG_MODULE_MAGIC; #define BUILD_VERSION "0.7.0" -#define PG_STAT_STATEMENTS_COLS 45 /* maximum of above */ +#define PG_STAT_STATEMENTS_COLS 46 /* maximum of above */ #define PGSM_TEXT_FILE "/tmp/pg_stat_monitor_query" #define PGUNSIXBIT(val) (((val) & 0x3F) + '0') @@ -303,22 +303,23 @@ pgss_post_parse_analyze(ParseState *pstate, Query *query) if (query->queryId == UINT64CONST(0)) query->queryId = UINT64CONST(1); - if (PGSM_ENABLED == 1) - if (jstate.clocations_count > 0) - pgss_store(query->queryId, - pstate->p_sourcetext, - query->commandType, - 0, /* error elevel */ - "", /* error sqlcode */ - NULL, /* error message */ - query->stmt_location, - query->stmt_len, - PGSS_INVALID, - 0, - 0, - NULL, + if (jstate.clocations_count <= 0) + return; + + pgss_store(query->queryId, + pstate->p_sourcetext, + query->commandType, + 0, /* error elevel */ + "", /* error sqlcode */ + NULL, /* error message */ + query->stmt_location, + query->stmt_len, + PGSS_INVALID, + 0, + 0, + NULL, #if PG_VERSION_NUM >= 130000 - NULL, + NULL, #endif &jstate, 0.0, @@ -333,7 +334,7 @@ pgss_ExecutorStart(QueryDesc *queryDesc, int eflags) { if(getrusage(RUSAGE_SELF, &rusage_start) != 0) elog(WARNING, "pg_stat_monitor: failed to execute getrusage"); - + if (prev_ExecutorStart) prev_ExecutorStart(queryDesc, eflags); else @@ -415,13 +416,13 @@ pgss_ExecutorFinish(QueryDesc *queryDesc) static void pgss_ExecutorEnd(QueryDesc *queryDesc) { - float utime; - float stime; uint64 queryId = queryDesc->plannedstmt->queryId; pgssSharedState *pgss = pgsm_get_ss(); if (queryId != UINT64CONST(0) && queryDesc->totaltime) { + float utime; + float stime; /* * Make sure stats accumulation is done. (Note: it's okay if several * levels of hook all do this.) @@ -432,25 +433,24 @@ pgss_ExecutorEnd(QueryDesc *queryDesc) utime = time_diff(rusage_end.ru_utime, rusage_start.ru_utime); stime = time_diff(rusage_end.ru_stime, rusage_start.ru_stime); - if (PGSM_ENABLED == 1) - pgss_store(queryId, - queryDesc->sourceText, - queryDesc->operation, - 0, /* error elevel */ - "", /* error sqlcode */ - NULL, /* error message */ - queryDesc->plannedstmt->stmt_location, - queryDesc->plannedstmt->stmt_len, - PGSS_EXEC, - queryDesc->totaltime->total * 1000.0, /* convert to msec */ - queryDesc->estate->es_processed, - &queryDesc->totaltime->bufusage, + pgss_store(queryId, + queryDesc->sourceText, + queryDesc->operation, + 0, /* error elevel */ + "", /* error sqlcode */ + NULL, /* error message */ + queryDesc->plannedstmt->stmt_location, + queryDesc->plannedstmt->stmt_len, + PGSS_EXEC, + queryDesc->totaltime->total * 1000.0, /* convert to msec */ + queryDesc->estate->es_processed, + &queryDesc->totaltime->bufusage, #if PG_VERSION_NUM >= 130000 - &queryDesc->totaltime->walusage, + &queryDesc->totaltime->walusage, #endif - NULL, - utime, - stime); + NULL, + utime, + stime); } if (prev_ExecutorEnd) @@ -463,7 +463,7 @@ pgss_ExecutorEnd(QueryDesc *queryDesc) static bool pgss_ExecutorCheckPerms(List *rt, bool abort) { - ListCell *lr; + ListCell *lr = NULL; pgssSharedState *pgss = pgsm_get_ss(); int i = 0; int j = 0; @@ -473,13 +473,13 @@ pgss_ExecutorCheckPerms(List *rt, bool abort) foreach(lr, rt) { - bool found = false; RangeTblEntry *rte = lfirst(lr); if (rte->rtekind != RTE_RELATION) continue; if (i < REL_LST) { + bool found = false; for(j = 0; j < i; j++) { if (pgss->relations[j] == rte->relid) @@ -609,25 +609,24 @@ static void pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString, /* calc differences of buffer counters. */ memset(&bufusage, 0, sizeof(BufferUsage)); BufferUsageAccumDiff(&bufusage, &pgBufferUsage, &bufusage_start); - if (PGSM_ENABLED == 1) - pgss_store(0, /* query id, passing 0 to signal that it's a utility stmt */ - queryString, /* query text */ - 0, - 0, /* error elevel */ - "", /* error sqlcode */ - NULL, /* error message */ - pstmt->stmt_location, - pstmt->stmt_len, - PGSS_EXEC, - INSTR_TIME_GET_MILLISEC(duration), - rows, - &bufusage, + pgss_store(0, /* query id, passing 0 to signal that it's a utility stmt */ + queryString, /* query text */ + 0, + 0, /* error elevel */ + "", /* error sqlcode */ + NULL, /* error message */ + pstmt->stmt_location, + pstmt->stmt_len, + PGSS_EXEC, + INSTR_TIME_GET_MILLISEC(duration), + rows, + &bufusage, #if PG_VERSION_NUM >= 130000 - &walusage, + &walusage, #endif - NULL, - 0, - 0); + NULL, + 0, + 0); } else { @@ -791,15 +790,16 @@ static void pgss_store(uint64 queryId, int application_name_len; int sqlcode_len = strlen(sqlcode); - Assert(query != NULL); - Assert(PGSM_ENABLED); + /* Monitoring is disabled */ + if (!PGSM_ENABLED) + return; + Assert(query != NULL); application_name_len = pg_get_application_name(application_name); /* Safety check... */ if (!IsSystemInitialized() || !pgss_qbuf[pgss->current_wbucket]) return; - /* * Confine our attention to the relevant part of the string, if the query @@ -995,6 +995,10 @@ static void pgss_store(uint64 queryId, e->counters.walusage.wal_fpi = 0; e->counters.walusage.wal_bytes = 0; } +#else + e->counters.walusage.wal_records = 0; + e->counters.walusage.wal_fpi = 0; + e->counters.walusage.wal_bytes = 0; #endif SpinLockRelease(&e->mutex); } @@ -1044,7 +1048,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo, MemoryContext per_query_ctx; MemoryContext oldcontext; Oid userid = GetUserId(); - bool is_allowed_role = false; + bool is_allowed_role; HASH_SEQ_STATUS hash_seq; pgssEntry *entry; char *query_txt; @@ -1096,7 +1100,8 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo, { Datum values[PG_STAT_STATEMENTS_COLS]; bool nulls[PG_STAT_STATEMENTS_COLS]; - int i = 0,j; + int i = 0; + int j = 0; int len = 0; int kind; Counters tmp; @@ -1154,9 +1159,9 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo, values[i++] = CStringGetTextDatum(queryid_txt); if (showtext) { - char *enc; if (query_txt) { + char *enc; enc = pg_any_to_server(query_txt, strlen(query_txt), entry->encoding); values[i++] = CStringGetTextDatum(enc); if (enc != query_txt) @@ -1184,7 +1189,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo, else nulls[i++] = true; } - if (strlen(tmp.info.application_name) <= 0) + if (strlen(tmp.info.application_name) == 0) nulls[i++] = true; else values[i++] = CStringGetTextDatum(tmp.info.application_name); @@ -1201,7 +1206,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo, values[i++] = Int64GetDatumFast(tmp.info.cmd_type); values[i++] = Int64GetDatumFast(tmp.error.elevel); - if (strlen(tmp.error.sqlcode) <= 0) + if (strlen(tmp.error.sqlcode) == 0) values[i++] = CStringGetTextDatum("0"); else values[i++] = CStringGetTextDatum(tmp.error.sqlcode); @@ -1247,7 +1252,6 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo, values[i++] = IntArrayGetTextDatum(tmp.resp_calls, MAX_RESPONSE_BUCKET); values[i++] = Float8GetDatumFast(tmp.sysinfo.utime); values[i++] = Float8GetDatumFast(tmp.sysinfo.stime); -#if PG_VERSION_NUM >= 130000 { char buf[256]; Datum wal_bytes; @@ -1264,11 +1268,6 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo, Int32GetDatum(-1)); values[i++] = wal_bytes; } -#else - nulls[i++] = true; - nulls[i++] = true; - nulls[i++] = true; -#endif tuplestore_putvalues(tupstore, tupdesc, values, nulls); } free(query_txt); @@ -1285,19 +1284,18 @@ get_next_wbucket(pgssSharedState *pgss) struct timeval tv; uint64 current_usec; uint64 bucket_id; - char file_name[1024]; struct tm *lt; - int sec = 0; gettimeofday(&tv,NULL); - current_usec = tv.tv_usec; - current_usec = (TimestampTz) tv.tv_sec - ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY); current_usec = (current_usec * USECS_PER_SEC) + tv.tv_usec; if ((current_usec - pgss->prev_bucket_usec) > (PGSM_BUCKET_TIME * 1000 * 1000)) { unsigned char *buf; + char file_name[1024]; + int sec = 0; + bucket_id = (tv.tv_sec / PGSM_BUCKET_TIME) % PGSM_MAX_BUCKETS; LWLockAcquire(pgss->lock, LW_EXCLUSIVE); buf = pgss_qbuf[bucket_id]; @@ -1408,7 +1406,7 @@ JumbleQuery(pgssJumbleState *jstate, Query *query) static void JumbleRangeTable(pgssJumbleState *jstate, List *rtable) { - ListCell *lc; + ListCell *lc = NULL; foreach(lc, rtable) { @@ -2242,7 +2240,7 @@ textarray_get_datum(char **arr, int arr_len, int str_len) /* Need to calculate the actual size, and avoid unnessary memory usage */ for (j = 0; j < arr_len; j++) { - if (arr[j] == NULL || strlen(arr[j]) <= 0) + if (arr[j] == NULL || strlen(arr[j]) == 0) continue; if (first) { @@ -2421,23 +2419,22 @@ static PlannedStmt *pgss_planner_hook(Query *parse, int opt, ParamListInfo param /* calc differences of WAL counters. */ memset(&walusage, 0, sizeof(WalUsage)); WalUsageAccumDiff(&walusage, &pgWalUsage, &walusage_start); - if (PGSM_ENABLED == 1) - pgss_store(parse->queryId, /* query id */ - query_string, /* query text */ - parse->commandType, - 0, /* error elevel */ - "", /* error sqlcode */ - NULL, /* error message */ - parse->stmt_location, - parse->stmt_len, - PGSS_PLAN, - INSTR_TIME_GET_MILLISEC(duration), - 0, - &bufusage, - &walusage, - NULL, - 0, - 0); + pgss_store(parse->queryId, /* query id */ + query_string, /* query text */ + parse->commandType, + 0, /* error elevel */ + "", /* error sqlcode */ + NULL, /* error message */ + parse->stmt_location, + parse->stmt_len, + PGSS_PLAN, + INSTR_TIME_GET_MILLISEC(duration), + 0, + &bufusage, + &walusage, + NULL, + 0, + 0); } else { @@ -2545,8 +2542,7 @@ pgsm_emit_log_hook(ErrorData *edata) WalUsage walusage; #endif - if (PGSM_ENABLED == 1 && - IsSystemInitialized() && + if (IsSystemInitialized() && (edata->elevel == ERROR || edata->elevel == WARNING || edata->elevel == INFO || edata->elevel == DEBUG1) ) { uint64 queryid = 0; @@ -2671,19 +2667,19 @@ char * unpack_sql_state(int sql_state) { static char buf[12]; - int i; + int i; - for (i = 0; i < 5; i++) - { + for (i = 0; i < 5; i++) + { buf[i] = PGUNSIXBIT(sql_state); sql_state >>= 6; - } + } buf[i] = '\0'; return buf; } -static int +static int get_histogram_bucket(double q_time) { double q_min = PGSM_HISTOGRAM_MIN; @@ -2726,7 +2722,7 @@ get_histogram_timings(PG_FUNCTION_ARGS) double b_min; double bucket_size; char range[50][1024] = {0}; - + b_max = log(q_max - q_min); b_min = 0; bucket_size = (b_max - b_min) / (double)b_count; diff --git a/pg_stat_monitor.h b/pg_stat_monitor.h index 384a578..4b4e254 100644 --- a/pg_stat_monitor.h +++ b/pg_stat_monitor.h @@ -83,10 +83,9 @@ /* the assumption of query max nested level */ #define DEFAULT_MAX_NESTED_LEVEL 10 -#define MAX_QUERY_BUF (PGSM_QUERY_BUF_SIZE * 1024 * 1024) +#define MAX_QUERY_BUF (PGSM_QUERY_SHARED_BUFFER * 1024 * 1024) #define MAX_BUCKETS_MEM (PGSM_MAX * 1024 * 1024) #define BUCKETS_MEM_OVERFLOW() ((hash_get_num_entries(pgss_hash) * sizeof(pgssEntry)) >= MAX_BUCKETS_MEM) -//#define MAX_QUERY_BUFFER_BUCKET 200 #define MAX_QUERY_BUFFER_BUCKET MAX_QUERY_BUF / PGSM_MAX_BUCKETS #define MAX_BUCKET_ENTRIES (MAX_BUCKETS_MEM / sizeof(pgssEntry)) #define QUERY_BUFFER_OVERFLOW(x,y) ((x + y + sizeof(uint64) + sizeof(uint64)) > MAX_QUERY_BUFFER_BUCKET) @@ -101,6 +100,8 @@ typedef struct GucVariables int guc_default; int guc_min; int guc_max; + int guc_unit; + int *guc_value; bool guc_restart; } GucVariable; @@ -275,9 +276,6 @@ do { \ memset(&x->bucket_entry, 0, MAX_BUCKETS * sizeof(uint64)); \ } while(0) - - - /* * Struct for tracking locations/lengths of constants during normalization */ @@ -352,7 +350,7 @@ void pgss_startup(void); #define PGSM_HISTOGRAM_MIN get_conf(7)->guc_variable #define PGSM_HISTOGRAM_MAX get_conf(8)->guc_variable #define PGSM_HISTOGRAM_BUCKETS get_conf(9)->guc_variable -#define PGSM_QUERY_BUF_SIZE get_conf(10)->guc_variable +#define PGSM_QUERY_SHARED_BUFFER get_conf(10)->guc_variable #define PGSM_TRACK_PLANNING get_conf(11)->guc_variable #endif