PG-174: Code cleanup.

pull/73/head
Ibrar Ahmed 2021-02-11 12:02:04 +00:00
parent bd8c54476f
commit a6036b86ac
7 changed files with 208 additions and 304 deletions

View File

@ -11,7 +11,7 @@ PGFILEDESC = "pg_stat_monitor - execution statistics of SQL statements"
LDFLAGS_SL += $(filter -lm, $(LIBS)) LDFLAGS_SL += $(filter -lm, $(LIBS))
REGRESS_OPTS = --temp-config $(top_srcdir)/contrib/pg_stat_monitor/pg_stat_monitor.conf 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", # Disabled because these tests require "shared_preload_libraries=pg_stat_statements",
# which typical installcheck users do not have (e.g. buildfarm clients). # which typical installcheck users do not have (e.g. buildfarm clients).

View File

@ -232,11 +232,12 @@ SELECT 1 + 1 AS "two";
(1 row) (1 row)
SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C"; SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls | rows query | calls | rows
----------------+-------+------ --------------------------------+-------+------
SELECT $1 | 0 | 0 SELECT $1 | 1 | 1
SELECT $1 + $2 | 0 | 0 SELECT $1 + $2 | 1 | 1
(2 rows) SELECT pg_stat_monitor_reset() | 1 | 1
(3 rows)
-- --
-- pg_stat_monitor.track = top -- 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 SELECT $1 +| 1 | 1
+| | +| |
AS "text" | | AS "text" | |
SELECT (i + $2 + $3)::INTEGER | 0 | 0 SELECT (i + $2 + $3)::INTEGER | 2 | 2
SELECT (i + $2)::INTEGER LIMIT $3 | 0 | 0 SELECT (i + $2)::INTEGER LIMIT $3 | 2 | 2
SELECT PLUS_ONE($1) | 2 | 2 SELECT PLUS_ONE($1) | 2 | 2
SELECT PLUS_TWO($1) | 2 | 2 SELECT PLUS_TWO($1) | 2 | 2
SELECT pg_stat_monitor_reset() | 1 | 1 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 NOTICE: function plus_one(pg_catalog.int4) does not exist, skipping
DROP FUNCTION PLUS_TWO(INTEGER); DROP FUNCTION PLUS_TWO(INTEGER);
SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C"; SELECT query, calls, rows FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls | rows query | calls | rows
-------------------------------------------+-------+------ --------------------------------+-------+------
CREATE INDEX test_b ON test(b) | 1 | 0 SELECT $1 | 1 | 1
DROP FUNCTION IF EXISTS PLUS_ONE(INTEGER) | 1 | 0 SELECT pg_stat_monitor_reset() | 1 | 1
DROP FUNCTION PLUS_ONE(INTEGER) | 1 | 0 (2 rows)
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)
DROP EXTENSION pg_stat_monitor; DROP EXTENSION pg_stat_monitor;

251
guc.c
View File

@ -19,6 +19,8 @@
#include "pg_stat_monitor.h" #include "pg_stat_monitor.h"
GucVariable conf[13]; GucVariable conf[13];
static void DefineIntGUC(GucVariable *conf);
static void DefineBoolGUC(GucVariable *conf);
/* /*
* Define (or redefine) custom GUC variables. * Define (or redefine) custom GUC variables.
@ -27,253 +29,178 @@ void
init_guc(void) init_guc(void)
{ {
int i = 0; int i = 0;
conf[i++] = (GucVariable) { conf[i] = (GucVariable) {
.guc_name = "pg_stat_monitor.pgsm_max", .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_desc = "Sets the maximum size of shared memory in (MB) used for statement's metadata tracked by pg_stat_monitor.",
.guc_default = 100, .guc_default = 100,
.guc_min = 1, .guc_min = 1,
.guc_max = 1000, .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_name = "pg_stat_monitor.pgsm_query_max_len",
.guc_desc = "Sets the maximum length of query.", .guc_desc = "Sets the maximum length of query.",
.guc_default = 1024, .guc_default = 1024,
.guc_min = 1024, .guc_min = 1024,
.guc_max = INT_MAX, .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_name = "pg_stat_monitor.pgsm_enable",
.guc_desc = "Enable/Disable statistics collector.", .guc_desc = "Enable/Disable statistics collector.",
.guc_default = 1, .guc_default = 1,
.guc_min = 0, .guc_min = 0,
.guc_max = 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_name = "pg_stat_monitor.pgsm_track_utility",
.guc_desc = "Selects whether utility commands are tracked.", .guc_desc = "Selects whether utility commands are tracked.",
.guc_default = 0, .guc_default = 0,
.guc_min = 0, .guc_min = 0,
.guc_max = 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_name = "pg_stat_monitor.pgsm_normalized_query",
.guc_desc = "Selects whether save query in normalized format.", .guc_desc = "Selects whether save query in normalized format.",
.guc_default = 1, .guc_default = 1,
.guc_min = 0, .guc_min = 0,
.guc_max = 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_name = "pg_stat_monitor.pgsm_max_buckets",
.guc_desc = "Sets the maximum number of buckets.", .guc_desc = "Sets the maximum number of buckets.",
.guc_default = 10, .guc_default = 10,
.guc_min = 1, .guc_min = 1,
.guc_max = 10, .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_name = "pg_stat_monitor.pgsm_bucket_time",
.guc_desc = "Sets the time in seconds per bucket.", .guc_desc = "Sets the time in seconds per bucket.",
.guc_default = 60, .guc_default = 60,
.guc_min = 1, .guc_min = 1,
.guc_max = INT_MAX, .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_name = "pg_stat_monitor.pgsm_histogram_min",
.guc_desc = "Sets the time in millisecond.", .guc_desc = "Sets the time in millisecond.",
.guc_default = 0, .guc_default = 0,
.guc_min = 0, .guc_min = 0,
.guc_max = INT_MAX, .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_name = "pg_stat_monitor.pgsm_histogram_max",
.guc_desc = "Sets the time in millisecond.", .guc_desc = "Sets the time in millisecond.",
.guc_default = 10, .guc_default = 10,
.guc_min = 10, .guc_min = 10,
.guc_max = INT_MAX, .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_name = "pg_stat_monitor.pgsm_histogram_buckets",
.guc_desc = "Sets the maximum number of histogram buckets", .guc_desc = "Sets the maximum number of histogram buckets",
.guc_default = 10, .guc_default = 10,
.guc_min = 2, .guc_min = 2,
.guc_max = INT_MAX, .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_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_desc = "Sets the maximum size of shared memory in (MB) used for query tracked by pg_stat_monitor.",
.guc_default = 20, .guc_default = 20,
.guc_min = 1, .guc_min = 1,
.guc_max = 10000, .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 #if PG_VERSION_NUM >= 130000
conf[i++] = (GucVariable) { conf[i] = (GucVariable) {
.guc_name = "pg_stat_monitor.pgsm_track_planning", .guc_name = "pg_stat_monitor.pgsm_track_planning",
.guc_desc = "Selects whether planning statistics are tracked.", .guc_desc = "Selects whether planning statistics are tracked.",
.guc_default = 1, .guc_default = 1,
.guc_min = 0, .guc_min = 0,
.guc_max = 0, .guc_max = 0,
.guc_restart = false .guc_restart = false,
.guc_unit = 0,
.guc_value = &PGSM_TRACK_PLANNING
}; };
DefineBoolGUC(&conf[i++]);
#endif #endif
}
DefineCustomIntVariable("pg_stat_monitor.pgsm_max", static void
"Sets the maximum size of shared memory in (MB) used for statement's metadata tracked by pg_stat_monitor.", DefineIntGUC(GucVariable *conf)
{
DefineCustomIntVariable(conf->guc_name,
conf->guc_desc,
NULL, NULL,
&PGSM_MAX, conf->guc_value,
100, conf->guc_default,
1, conf->guc_min,
1000, conf->guc_max,
PGC_POSTMASTER, conf->guc_restart ? PGC_POSTMASTER : PGC_USERSET,
GUC_UNIT_MB, conf->guc_unit,
NULL, NULL,
NULL, NULL,
NULL); NULL);
}
DefineCustomIntVariable("pg_stat_monitor.pgsm_query_max_len", static void
"Sets the maximum length of query.", DefineBoolGUC(GucVariable *conf)
{
DefineCustomBoolVariable(conf->guc_name,
conf->guc_desc,
NULL, NULL,
&PGSM_QUERY_MAX_LEN, (bool*)conf->guc_value,
1024, conf->guc_default,
1024, conf->guc_restart ? PGC_POSTMASTER : PGC_USERSET,
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,
0, 0,
NULL, NULL,
NULL, NULL,

View File

@ -83,12 +83,6 @@ pgss_startup(void)
on_shmem_exit(pgss_shmem_shutdown, (Datum) 0); on_shmem_exit(pgss_shmem_shutdown, (Datum) 0);
} }
int
pgsm_get_bucket_size(void)
{
return pgss->query_buf_size_bucket;
}
pgssSharedState* pgssSharedState*
pgsm_get_ss(void) pgsm_get_ss(void)
{ {
@ -101,12 +95,6 @@ pgsm_get_hash(void)
return pgss_hash; return pgss_hash;
} }
HTAB*
pgsm_get_query_hash(void)
{
return pgss_query_hash;
}
/* /*
* shmem_shutdown hook: Dump statistics into file. * shmem_shutdown hook: Dump statistics into file.
* *
@ -234,7 +222,7 @@ bool
hash_create_query_entry(uint64 bucket_id, uint64 queryid) hash_create_query_entry(uint64 bucket_id, uint64 queryid)
{ {
pgssQueryHashKey key; pgssQueryHashKey key;
pgssQueryEntry *entry = NULL; pgssQueryEntry *entry;
bool found; bool found;
key.queryid = queryid; key.queryid = queryid;
@ -249,7 +237,7 @@ bool
hash_find_query_entry(uint64 bucket_id, uint64 queryid) hash_find_query_entry(uint64 bucket_id, uint64 queryid)
{ {
pgssQueryHashKey key; pgssQueryHashKey key;
pgssQueryEntry *entry = NULL; pgssQueryEntry *entry;
bool found; bool found;
key.queryid = queryid; key.queryid = queryid;

View File

@ -71,9 +71,9 @@ CREATE FUNCTION pg_stat_monitor(IN showtext boolean,
OUT resp_calls text, OUT resp_calls text,
OUT cpu_user_time float8, OUT cpu_user_time float8,
OUT cpu_sys_time float8, OUT cpu_sys_time float8,
OUT wal_records int8, OUT wal_records int8,
OUT wal_fpi int8, OUT wal_fpi int8,
OUT wal_bytes numeric OUT wal_bytes numeric
) )
RETURNS SETOF record RETURNS SETOF record
AS 'MODULE_PATHNAME', 'pg_stat_monitor' AS 'MODULE_PATHNAME', 'pg_stat_monitor'

View File

@ -21,7 +21,7 @@
PG_MODULE_MAGIC; PG_MODULE_MAGIC;
#define BUILD_VERSION "0.7.0" #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 PGSM_TEXT_FILE "/tmp/pg_stat_monitor_query"
#define PGUNSIXBIT(val) (((val) & 0x3F) + '0') #define PGUNSIXBIT(val) (((val) & 0x3F) + '0')
@ -303,22 +303,23 @@ pgss_post_parse_analyze(ParseState *pstate, Query *query)
if (query->queryId == UINT64CONST(0)) if (query->queryId == UINT64CONST(0))
query->queryId = UINT64CONST(1); query->queryId = UINT64CONST(1);
if (PGSM_ENABLED == 1) if (jstate.clocations_count <= 0)
if (jstate.clocations_count > 0) return;
pgss_store(query->queryId,
pstate->p_sourcetext, pgss_store(query->queryId,
query->commandType, pstate->p_sourcetext,
0, /* error elevel */ query->commandType,
"", /* error sqlcode */ 0, /* error elevel */
NULL, /* error message */ "", /* error sqlcode */
query->stmt_location, NULL, /* error message */
query->stmt_len, query->stmt_location,
PGSS_INVALID, query->stmt_len,
0, PGSS_INVALID,
0, 0,
NULL, 0,
NULL,
#if PG_VERSION_NUM >= 130000 #if PG_VERSION_NUM >= 130000
NULL, NULL,
#endif #endif
&jstate, &jstate,
0.0, 0.0,
@ -333,7 +334,7 @@ pgss_ExecutorStart(QueryDesc *queryDesc, int eflags)
{ {
if(getrusage(RUSAGE_SELF, &rusage_start) != 0) if(getrusage(RUSAGE_SELF, &rusage_start) != 0)
elog(WARNING, "pg_stat_monitor: failed to execute getrusage"); elog(WARNING, "pg_stat_monitor: failed to execute getrusage");
if (prev_ExecutorStart) if (prev_ExecutorStart)
prev_ExecutorStart(queryDesc, eflags); prev_ExecutorStart(queryDesc, eflags);
else else
@ -415,13 +416,13 @@ pgss_ExecutorFinish(QueryDesc *queryDesc)
static void static void
pgss_ExecutorEnd(QueryDesc *queryDesc) pgss_ExecutorEnd(QueryDesc *queryDesc)
{ {
float utime;
float stime;
uint64 queryId = queryDesc->plannedstmt->queryId; uint64 queryId = queryDesc->plannedstmt->queryId;
pgssSharedState *pgss = pgsm_get_ss(); pgssSharedState *pgss = pgsm_get_ss();
if (queryId != UINT64CONST(0) && queryDesc->totaltime) if (queryId != UINT64CONST(0) && queryDesc->totaltime)
{ {
float utime;
float stime;
/* /*
* Make sure stats accumulation is done. (Note: it's okay if several * Make sure stats accumulation is done. (Note: it's okay if several
* levels of hook all do this.) * 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); utime = time_diff(rusage_end.ru_utime, rusage_start.ru_utime);
stime = time_diff(rusage_end.ru_stime, rusage_start.ru_stime); stime = time_diff(rusage_end.ru_stime, rusage_start.ru_stime);
if (PGSM_ENABLED == 1) pgss_store(queryId,
pgss_store(queryId, queryDesc->sourceText,
queryDesc->sourceText, queryDesc->operation,
queryDesc->operation, 0, /* error elevel */
0, /* error elevel */ "", /* error sqlcode */
"", /* error sqlcode */ NULL, /* error message */
NULL, /* error message */ queryDesc->plannedstmt->stmt_location,
queryDesc->plannedstmt->stmt_location, queryDesc->plannedstmt->stmt_len,
queryDesc->plannedstmt->stmt_len, PGSS_EXEC,
PGSS_EXEC, queryDesc->totaltime->total * 1000.0, /* convert to msec */
queryDesc->totaltime->total * 1000.0, /* convert to msec */ queryDesc->estate->es_processed,
queryDesc->estate->es_processed, &queryDesc->totaltime->bufusage,
&queryDesc->totaltime->bufusage,
#if PG_VERSION_NUM >= 130000 #if PG_VERSION_NUM >= 130000
&queryDesc->totaltime->walusage, &queryDesc->totaltime->walusage,
#endif #endif
NULL, NULL,
utime, utime,
stime); stime);
} }
if (prev_ExecutorEnd) if (prev_ExecutorEnd)
@ -463,7 +463,7 @@ pgss_ExecutorEnd(QueryDesc *queryDesc)
static bool static bool
pgss_ExecutorCheckPerms(List *rt, bool abort) pgss_ExecutorCheckPerms(List *rt, bool abort)
{ {
ListCell *lr; ListCell *lr = NULL;
pgssSharedState *pgss = pgsm_get_ss(); pgssSharedState *pgss = pgsm_get_ss();
int i = 0; int i = 0;
int j = 0; int j = 0;
@ -473,13 +473,13 @@ pgss_ExecutorCheckPerms(List *rt, bool abort)
foreach(lr, rt) foreach(lr, rt)
{ {
bool found = false;
RangeTblEntry *rte = lfirst(lr); RangeTblEntry *rte = lfirst(lr);
if (rte->rtekind != RTE_RELATION) if (rte->rtekind != RTE_RELATION)
continue; continue;
if (i < REL_LST) if (i < REL_LST)
{ {
bool found = false;
for(j = 0; j < i; j++) for(j = 0; j < i; j++)
{ {
if (pgss->relations[j] == rte->relid) if (pgss->relations[j] == rte->relid)
@ -609,25 +609,24 @@ static void pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
/* calc differences of buffer counters. */ /* calc differences of buffer counters. */
memset(&bufusage, 0, sizeof(BufferUsage)); memset(&bufusage, 0, sizeof(BufferUsage));
BufferUsageAccumDiff(&bufusage, &pgBufferUsage, &bufusage_start); BufferUsageAccumDiff(&bufusage, &pgBufferUsage, &bufusage_start);
if (PGSM_ENABLED == 1) pgss_store(0, /* query id, passing 0 to signal that it's a utility stmt */
pgss_store(0, /* query id, passing 0 to signal that it's a utility stmt */ queryString, /* query text */
queryString, /* query text */ 0,
0, 0, /* error elevel */
0, /* error elevel */ "", /* error sqlcode */
"", /* error sqlcode */ NULL, /* error message */
NULL, /* error message */ pstmt->stmt_location,
pstmt->stmt_location, pstmt->stmt_len,
pstmt->stmt_len, PGSS_EXEC,
PGSS_EXEC, INSTR_TIME_GET_MILLISEC(duration),
INSTR_TIME_GET_MILLISEC(duration), rows,
rows, &bufusage,
&bufusage,
#if PG_VERSION_NUM >= 130000 #if PG_VERSION_NUM >= 130000
&walusage, &walusage,
#endif #endif
NULL, NULL,
0, 0,
0); 0);
} }
else else
{ {
@ -791,15 +790,16 @@ static void pgss_store(uint64 queryId,
int application_name_len; int application_name_len;
int sqlcode_len = strlen(sqlcode); int sqlcode_len = strlen(sqlcode);
Assert(query != NULL); /* Monitoring is disabled */
Assert(PGSM_ENABLED); if (!PGSM_ENABLED)
return;
Assert(query != NULL);
application_name_len = pg_get_application_name(application_name); application_name_len = pg_get_application_name(application_name);
/* Safety check... */ /* Safety check... */
if (!IsSystemInitialized() || !pgss_qbuf[pgss->current_wbucket]) if (!IsSystemInitialized() || !pgss_qbuf[pgss->current_wbucket])
return; return;
/* /*
* Confine our attention to the relevant part of the string, if the query * 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_fpi = 0;
e->counters.walusage.wal_bytes = 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 #endif
SpinLockRelease(&e->mutex); SpinLockRelease(&e->mutex);
} }
@ -1044,7 +1048,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
MemoryContext per_query_ctx; MemoryContext per_query_ctx;
MemoryContext oldcontext; MemoryContext oldcontext;
Oid userid = GetUserId(); Oid userid = GetUserId();
bool is_allowed_role = false; bool is_allowed_role;
HASH_SEQ_STATUS hash_seq; HASH_SEQ_STATUS hash_seq;
pgssEntry *entry; pgssEntry *entry;
char *query_txt; char *query_txt;
@ -1096,7 +1100,8 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
{ {
Datum values[PG_STAT_STATEMENTS_COLS]; Datum values[PG_STAT_STATEMENTS_COLS];
bool nulls[PG_STAT_STATEMENTS_COLS]; bool nulls[PG_STAT_STATEMENTS_COLS];
int i = 0,j; int i = 0;
int j = 0;
int len = 0; int len = 0;
int kind; int kind;
Counters tmp; Counters tmp;
@ -1154,9 +1159,9 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
values[i++] = CStringGetTextDatum(queryid_txt); values[i++] = CStringGetTextDatum(queryid_txt);
if (showtext) if (showtext)
{ {
char *enc;
if (query_txt) if (query_txt)
{ {
char *enc;
enc = pg_any_to_server(query_txt, strlen(query_txt), entry->encoding); enc = pg_any_to_server(query_txt, strlen(query_txt), entry->encoding);
values[i++] = CStringGetTextDatum(enc); values[i++] = CStringGetTextDatum(enc);
if (enc != query_txt) if (enc != query_txt)
@ -1184,7 +1189,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
else else
nulls[i++] = true; nulls[i++] = true;
} }
if (strlen(tmp.info.application_name) <= 0) if (strlen(tmp.info.application_name) == 0)
nulls[i++] = true; nulls[i++] = true;
else else
values[i++] = CStringGetTextDatum(tmp.info.application_name); 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.info.cmd_type);
values[i++] = Int64GetDatumFast(tmp.error.elevel); values[i++] = Int64GetDatumFast(tmp.error.elevel);
if (strlen(tmp.error.sqlcode) <= 0) if (strlen(tmp.error.sqlcode) == 0)
values[i++] = CStringGetTextDatum("0"); values[i++] = CStringGetTextDatum("0");
else else
values[i++] = CStringGetTextDatum(tmp.error.sqlcode); 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++] = IntArrayGetTextDatum(tmp.resp_calls, MAX_RESPONSE_BUCKET);
values[i++] = Float8GetDatumFast(tmp.sysinfo.utime); values[i++] = Float8GetDatumFast(tmp.sysinfo.utime);
values[i++] = Float8GetDatumFast(tmp.sysinfo.stime); values[i++] = Float8GetDatumFast(tmp.sysinfo.stime);
#if PG_VERSION_NUM >= 130000
{ {
char buf[256]; char buf[256];
Datum wal_bytes; Datum wal_bytes;
@ -1264,11 +1268,6 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
Int32GetDatum(-1)); Int32GetDatum(-1));
values[i++] = wal_bytes; values[i++] = wal_bytes;
} }
#else
nulls[i++] = true;
nulls[i++] = true;
nulls[i++] = true;
#endif
tuplestore_putvalues(tupstore, tupdesc, values, nulls); tuplestore_putvalues(tupstore, tupdesc, values, nulls);
} }
free(query_txt); free(query_txt);
@ -1285,19 +1284,18 @@ get_next_wbucket(pgssSharedState *pgss)
struct timeval tv; struct timeval tv;
uint64 current_usec; uint64 current_usec;
uint64 bucket_id; uint64 bucket_id;
char file_name[1024];
struct tm *lt; struct tm *lt;
int sec = 0;
gettimeofday(&tv,NULL); 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 = (TimestampTz) tv.tv_sec - ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY);
current_usec = (current_usec * USECS_PER_SEC) + tv.tv_usec; current_usec = (current_usec * USECS_PER_SEC) + tv.tv_usec;
if ((current_usec - pgss->prev_bucket_usec) > (PGSM_BUCKET_TIME * 1000 * 1000)) if ((current_usec - pgss->prev_bucket_usec) > (PGSM_BUCKET_TIME * 1000 * 1000))
{ {
unsigned char *buf; unsigned char *buf;
char file_name[1024];
int sec = 0;
bucket_id = (tv.tv_sec / PGSM_BUCKET_TIME) % PGSM_MAX_BUCKETS; bucket_id = (tv.tv_sec / PGSM_BUCKET_TIME) % PGSM_MAX_BUCKETS;
LWLockAcquire(pgss->lock, LW_EXCLUSIVE); LWLockAcquire(pgss->lock, LW_EXCLUSIVE);
buf = pgss_qbuf[bucket_id]; buf = pgss_qbuf[bucket_id];
@ -1408,7 +1406,7 @@ JumbleQuery(pgssJumbleState *jstate, Query *query)
static void static void
JumbleRangeTable(pgssJumbleState *jstate, List *rtable) JumbleRangeTable(pgssJumbleState *jstate, List *rtable)
{ {
ListCell *lc; ListCell *lc = NULL;
foreach(lc, rtable) 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 */ /* Need to calculate the actual size, and avoid unnessary memory usage */
for (j = 0; j < arr_len; j++) for (j = 0; j < arr_len; j++)
{ {
if (arr[j] == NULL || strlen(arr[j]) <= 0) if (arr[j] == NULL || strlen(arr[j]) == 0)
continue; continue;
if (first) if (first)
{ {
@ -2421,23 +2419,22 @@ static PlannedStmt *pgss_planner_hook(Query *parse, int opt, ParamListInfo param
/* calc differences of WAL counters. */ /* calc differences of WAL counters. */
memset(&walusage, 0, sizeof(WalUsage)); memset(&walusage, 0, sizeof(WalUsage));
WalUsageAccumDiff(&walusage, &pgWalUsage, &walusage_start); WalUsageAccumDiff(&walusage, &pgWalUsage, &walusage_start);
if (PGSM_ENABLED == 1) pgss_store(parse->queryId, /* query id */
pgss_store(parse->queryId, /* query id */ query_string, /* query text */
query_string, /* query text */ parse->commandType,
parse->commandType, 0, /* error elevel */
0, /* error elevel */ "", /* error sqlcode */
"", /* error sqlcode */ NULL, /* error message */
NULL, /* error message */ parse->stmt_location,
parse->stmt_location, parse->stmt_len,
parse->stmt_len, PGSS_PLAN,
PGSS_PLAN, INSTR_TIME_GET_MILLISEC(duration),
INSTR_TIME_GET_MILLISEC(duration), 0,
0, &bufusage,
&bufusage, &walusage,
&walusage, NULL,
NULL, 0,
0, 0);
0);
} }
else else
{ {
@ -2545,8 +2542,7 @@ pgsm_emit_log_hook(ErrorData *edata)
WalUsage walusage; WalUsage walusage;
#endif #endif
if (PGSM_ENABLED == 1 && if (IsSystemInitialized() &&
IsSystemInitialized() &&
(edata->elevel == ERROR || edata->elevel == WARNING || edata->elevel == INFO || edata->elevel == DEBUG1) ) (edata->elevel == ERROR || edata->elevel == WARNING || edata->elevel == INFO || edata->elevel == DEBUG1) )
{ {
uint64 queryid = 0; uint64 queryid = 0;
@ -2671,19 +2667,19 @@ char *
unpack_sql_state(int sql_state) unpack_sql_state(int sql_state)
{ {
static char buf[12]; 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); buf[i] = PGUNSIXBIT(sql_state);
sql_state >>= 6; sql_state >>= 6;
} }
buf[i] = '\0'; buf[i] = '\0';
return buf; return buf;
} }
static int static int
get_histogram_bucket(double q_time) get_histogram_bucket(double q_time)
{ {
double q_min = PGSM_HISTOGRAM_MIN; double q_min = PGSM_HISTOGRAM_MIN;
@ -2726,7 +2722,7 @@ get_histogram_timings(PG_FUNCTION_ARGS)
double b_min; double b_min;
double bucket_size; double bucket_size;
char range[50][1024] = {0}; char range[50][1024] = {0};
b_max = log(q_max - q_min); b_max = log(q_max - q_min);
b_min = 0; b_min = 0;
bucket_size = (b_max - b_min) / (double)b_count; bucket_size = (b_max - b_min) / (double)b_count;

View File

@ -83,10 +83,9 @@
/* the assumption of query max nested level */ /* the assumption of query max nested level */
#define DEFAULT_MAX_NESTED_LEVEL 10 #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 MAX_BUCKETS_MEM (PGSM_MAX * 1024 * 1024)
#define BUCKETS_MEM_OVERFLOW() ((hash_get_num_entries(pgss_hash) * sizeof(pgssEntry)) >= MAX_BUCKETS_MEM) #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_QUERY_BUFFER_BUCKET MAX_QUERY_BUF / PGSM_MAX_BUCKETS
#define MAX_BUCKET_ENTRIES (MAX_BUCKETS_MEM / sizeof(pgssEntry)) #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) #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_default;
int guc_min; int guc_min;
int guc_max; int guc_max;
int guc_unit;
int *guc_value;
bool guc_restart; bool guc_restart;
} GucVariable; } GucVariable;
@ -275,9 +276,6 @@ do { \
memset(&x->bucket_entry, 0, MAX_BUCKETS * sizeof(uint64)); \ memset(&x->bucket_entry, 0, MAX_BUCKETS * sizeof(uint64)); \
} while(0) } while(0)
/* /*
* Struct for tracking locations/lengths of constants during normalization * 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_MIN get_conf(7)->guc_variable
#define PGSM_HISTOGRAM_MAX get_conf(8)->guc_variable #define PGSM_HISTOGRAM_MAX get_conf(8)->guc_variable
#define PGSM_HISTOGRAM_BUCKETS get_conf(9)->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 #define PGSM_TRACK_PLANNING get_conf(11)->guc_variable
#endif #endif