Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #433

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
bb6f2fa
PG-564: Added a column to display bind variables when used with the n…
kayform Jan 9, 2023
ae7bbc3
PG-564: Added a column to display bind variables when used with the n…
kayform Jan 16, 2023
18bd18f
PG-564: fix the memory overflow
kayform Jan 20, 2023
e5669ff
PG-564: Added TAP test for bind parameter. update pg_stat_monitor-1.0…
kayform Feb 7, 2023
2437997
PG-564: Change the delimiter from ',' to '|' between variable and var…
Mar 14, 2023
84c9f46
resolve conflict with upstream/main
kayform Aug 11, 2023
70728c8
Add files via upload
ksjj97inzent Nov 28, 2023
d868bd4
Add files via upload
ksjj97inzent Nov 28, 2023
0295a53
Update 001_settings_default.out
ksjj97inzent Nov 28, 2023
214d137
Update 018_column_names.pl
ksjj97inzent Nov 28, 2023
1e1df9d
Update guc.out
ksjj97inzent Dec 21, 2023
4c072fc
Update guc_1.out
ksjj97inzent Dec 21, 2023
bdedf9f
Update guc_2.out
ksjj97inzent Dec 21, 2023
6568237
Update guc_1.out
ksjj97inzent Dec 21, 2023
ace7038
Update guc.out
ksjj97inzent Dec 21, 2023
ba60e25
Update guc_1.out
ksjj97inzent Dec 21, 2023
8ecdd12
Update guc_2.out
ksjj97inzent Dec 21, 2023
2359bf6
Merge branch 'percona:main' into PG-564
ksjj97inzent Dec 21, 2023
9bdd0d1
Merge branch 'percona:main' into main
ksjj97inzent Dec 21, 2023
17e1a7e
Update guc_2.out
ksjj97inzent Dec 21, 2023
add00ae
Update guc_1.out
ksjj97inzent Dec 21, 2023
d25adb3
Update guc_2.out
ksjj97inzent Dec 21, 2023
7193d4d
Update guc_2.out
ksjj97inzent Dec 21, 2023
3050d5d
Update guc_2.out
ksjj97inzent Dec 21, 2023
71311b6
Update guc_1.out
ksjj97inzent Dec 21, 2023
6ad4c2d
Update guc_2.out
ksjj97inzent Dec 22, 2023
dfb31ec
Update guc_1.out
ksjj97inzent Dec 22, 2023
2e72e54
Update guc_1.out
ksjj97inzent Dec 22, 2023
71832f3
Update 001_settings_default.out.12
ksjj97inzent Dec 22, 2023
a711aa7
Merge branch 'main' into PG-564
ksjj97inzent Jan 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ PGFILEDESC = "pg_stat_monitor - execution statistics of SQL statements"

LDFLAGS_SL += $(filter -lm, $(LIBS))

PG_CPPFLAGS += -std=c99 -g -ggdb

TAP_TESTS = 1
REGRESS_OPTS = --temp-config $(top_srcdir)/contrib/pg_stat_monitor/pg_stat_monitor.conf --inputdir=regression
REGRESS = basic version guc pgsm_query_id functions counters relations database error_insert application_name application_name_unique top_query cmd_type error rows tags user
Expand Down
62 changes: 62 additions & 0 deletions guc.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ bool pgsm_track_utility;
bool pgsm_enable_pgsm_query_id;
int pgsm_track;
static int pgsm_overflow_target; /* Not used since 2.0 */
/*custum */
bool pgsm_extract_bind_variables;

/* Check hooks to ensure histogram_min < histogram_max */
static bool check_histogram_min(double *newval, void **extra, GucSource source);
Expand Down Expand Up @@ -213,6 +215,19 @@ init_guc(void)
NULL, /* assign_hook */
NULL /* show_hook */
);

/* custum */
DefineCustomBoolVariable("pg_stat_monitor.pgsm_extract_bind_variables", /* name */
"Selects whether extracting bind variables from queries.", /* short_desc */
NULL, /* long_desc */
&pgsm_extract_bind_variables, /* value address */
false, /* boot value */
PGC_USERSET, /* context */
0, /* flags */
NULL, /* check_hook */
NULL, /* assign_hook */
NULL /* show_hook */
);

DefineCustomBoolVariable("pg_stat_monitor.pgsm_enable_overflow", /* name */
"Enable/Disable pg_stat_monitor to grow beyond shared memory into swap space.", /* short_desc */
Expand All @@ -226,6 +241,53 @@ init_guc(void)
NULL /* show_hook */
);

<<<<<<< HEAD
#if PG_VERSION_NUM >= 130000
conf[i] = (GucVariable)
{
.guc_name = "pg_stat_monitor.pgsm_track_planning",
.guc_desc = "Selects whether planning statistics are tracked.",
.guc_default = 0,
.guc_min = 0,
.guc_max = 0,
.guc_restart = false,
.guc_unit = 0,
.guc_value = &PGSM_TRACK_PLANNING
};
DefineBoolGUC(&conf[i++]);

conf[i] = (GucVariable)
{
.guc_name = "pg_stat_monitor.pgsm_extract_bind_variables",
.guc_desc = "Selects whether extracting bind variables from queries.",
.guc_default = 0,
.guc_min = 0,
.guc_max = 0,
.guc_restart = false,
.guc_unit = 0,
.guc_value = &PGSM_EXTRACT_VARIABLES
};
DefineBoolGUC(&conf[i++]);
#endif
}

static void
DefineIntGUCWithCheck(GucVariable * conf, GucIntCheckHook check)
{
conf->type = PGC_INT;
DefineCustomIntVariable(conf->guc_name,
conf->guc_desc,
NULL,
conf->guc_value,
conf->guc_default,
conf->guc_min,
conf->guc_max,
conf->guc_restart ? PGC_POSTMASTER : PGC_USERSET,
conf->guc_unit,
check,
NULL,
NULL);
}
DefineCustomBoolVariable("pg_stat_monitor.pgsm_enable_query_plan", /* name */
"Enable/Disable query plan monitoring.", /* short_desc */
NULL, /* long_desc */
Expand Down
5 changes: 5 additions & 0 deletions pg_stat_monitor--1.0--2.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CREATE FUNCTION pg_stat_monitor_internal(
OUT top_queryid int8,
OUT top_query text,
OUT application_name text,
OUT bind_variables text,

OUT relations text, -- 11
OUT cmd_type int,
Expand Down Expand Up @@ -128,6 +129,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
query_plan,
top_query,
application_name,
bind_variables,
string_to_array(relations, ',') AS relations,
cmd_type,
get_cmd_type(cmd_type) AS cmd_type_text,
Expand Down Expand Up @@ -185,6 +187,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
query_plan,
top_query,
application_name,
bind_variables,
string_to_array(relations, ',') AS relations,
cmd_type,
get_cmd_type(cmd_type) AS cmd_type_text,
Expand Down Expand Up @@ -251,6 +254,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
query_plan,
top_query,
application_name,
bind_variables,
string_to_array(relations, ',') AS relations,
cmd_type,
get_cmd_type(cmd_type) AS cmd_type_text,
Expand Down Expand Up @@ -317,6 +321,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
query_plan,
top_query,
application_name,
bind_variables,
string_to_array(relations, ',') AS relations,
cmd_type,
get_cmd_type(cmd_type) AS cmd_type_text,
Expand Down
6 changes: 5 additions & 1 deletion pg_stat_monitor--2.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ CREATE FUNCTION pg_stat_monitor_internal(
OUT top_queryid int8,
OUT top_query text,
OUT application_name text,

OUT bind_variables text,
OUT relations text, -- 11
OUT cmd_type int,
OUT elevel int,
Expand Down Expand Up @@ -184,6 +184,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
query_plan,
top_query,
application_name,
bind_variables,
string_to_array(relations, ',') AS relations,
cmd_type,
get_cmd_type(cmd_type) AS cmd_type_text,
Expand Down Expand Up @@ -241,6 +242,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
query_plan,
top_query,
application_name,
bind_variables,
string_to_array(relations, ',') AS relations,
cmd_type,
get_cmd_type(cmd_type) AS cmd_type_text,
Expand Down Expand Up @@ -307,6 +309,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
query_plan,
top_query,
application_name,
bind_variables,
string_to_array(relations, ',') AS relations,
cmd_type,
get_cmd_type(cmd_type) AS cmd_type_text,
Expand Down Expand Up @@ -373,6 +376,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
query_plan,
top_query,
application_name,
bind_variables,
string_to_array(relations, ',') AS relations,
cmd_type,
get_cmd_type(cmd_type) AS cmd_type_text,
Expand Down
79 changes: 61 additions & 18 deletions pg_stat_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ PG_MODULE_MAGIC;

/* Number of output arguments (columns) for various API versions */
#define PG_STAT_MONITOR_COLS_V1_0 52
#define PG_STAT_MONITOR_COLS_V2_0 64
#define PG_STAT_MONITOR_COLS_V2_0 65
#define PG_STAT_MONITOR_COLS PG_STAT_MONITOR_COLS_V2_0 /* maximum of above */

#define PGSM_TEXT_FILE PGSTAT_STAT_PERMANENT_DIRECTORY "pg_stat_monitor_query"
Expand Down Expand Up @@ -256,7 +256,8 @@ static uint64 get_query_id(JumbleState *jstate, Query *query);
#endif

static char *generate_normalized_query(JumbleState *jstate, const char *query,
int query_loc, int *query_len_p, int encoding);
int query_loc, int *query_len_p, int encoding,
char* bind_variables, int *bind_var_len_p);
static void fill_in_constant_lengths(JumbleState *jstate, const char *query, int query_loc);
static int comp_location(const void *a, const void *b);

Expand Down Expand Up @@ -407,6 +408,10 @@ pgsm_post_parse_analyze_internal(ParseState *pstate, Query *query, JumbleState *
int norm_query_len;
int location;
int query_len;
/* custum bind_variables */
char bind_variables[VAR_LEN] = "";
int bind_var_len = 0;


/* Safety check... */
if (!IsSystemInitialized())
Expand Down Expand Up @@ -477,8 +482,9 @@ pgsm_post_parse_analyze_internal(ParseState *pstate, Query *query, JumbleState *
query_text, /* query */
location, /* query location */
&norm_query_len,
GetDatabaseEncoding());

GetDatabaseEncoding(),
&bind_variables[0],
&bind_var_len);
Assert(norm_query);
}

Expand All @@ -496,6 +502,10 @@ pgsm_post_parse_analyze_internal(ParseState *pstate, Query *query, JumbleState *
*/
entry->pgsm_query_id = get_pgsm_query_id_hash(norm_query ? norm_query : query_text, norm_query_len);
entry->counters.info.cmd_type = query->commandType;

if(bind_var_len > 0){
_snprintf(entry->counters.info.bind_variables, bind_variables, bind_var_len + 1, VAR_LEN);
}

/*
* Add the query text and entry to the local list.
Expand Down Expand Up @@ -1776,6 +1786,10 @@ pgsm_store(pgsmEntry * entry)

pgsm = pgsm_get_ss();

/*
* We should lock the hash table here what if the bucket is removed; e.g.
* reset is called - HAMID
*/
prev_bucket_id = pg_atomic_read_u64(&pgsm->current_wbucket);
bucketid = get_next_wbucket(pgsm);

Expand Down Expand Up @@ -1852,6 +1866,7 @@ pgsm_store(pgsmEntry * entry)
* Get the memory address from DSA pointer and copy the query text in
* local variable
*/

query_buff = dsa_get_address(query_dsa_area, dsa_query_pointer);
memcpy(query_buff, query, query_len);

Expand All @@ -1878,11 +1893,6 @@ pgsm_store(pgsmEntry * entry)

if (shared_hash_entry == NULL)
{
LWLockRelease(pgsm->lock);

if (DsaPointerIsValid(dsa_query_pointer))
dsa_free(query_dsa_area, dsa_query_pointer);

/*
* Out of memory; report only if the state has changed now.
* Otherwise we risk filling up the log file with these message.
Expand All @@ -1891,16 +1901,18 @@ pgsm_store(pgsmEntry * entry)
{
pgsm->pgsm_oom = true;

PGSM_DISABLE_ERROR_CAPUTRE();
{
ereport(WARNING,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("[pg_stat_monitor] pgsm_store: Hash table is out of memory and can no longer store queries!"),
errdetail("You may reset the view or when the buckets are deallocated, pg_stat_monitor will resume saving " \
"queries. Alternatively, try increasing the value of pg_stat_monitor.pgsm_max.")));
} PGSM_END_DISABLE_ERROR_CAPTURE();
ereport(WARNING,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("[pg_stat_monitor] pgsm_store: Hash table is out of memory and can no longer store queries!"),
errdetail("You may reset the view or when the buckets are deallocated, pg_stat_monitor will resume saving " \
"queries. Alternatively, try increasing the value of pg_stat_monitor.pgsm_max.")));
}

LWLockRelease(pgsm->lock);

if (DsaPointerIsValid(dsa_query_pointer))
dsa_free(query_dsa_area, dsa_query_pointer);

return;
}
else
Expand All @@ -1923,6 +1935,9 @@ pgsm_store(pgsmEntry * entry)
snprintf(shared_hash_entry->username, sizeof(shared_hash_entry->username), "%s", entry->username);
}

if(strlen(entry->counters.info.bind_variables) > 0)
strncpy(shared_hash_entry->counters.info.bind_variables, entry->counters.info.bind_variables, VAR_LEN);

pgsm_update_entry(shared_hash_entry, /* entry */
query, /* query */
comments, /* comments */
Expand Down Expand Up @@ -2232,6 +2247,18 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
values[i++] = CStringGetTextDatum(tmp.info.application_name);
else
nulls[i++] = true;

/* bind_variables at column number 48 */
if (strlen(tmp.info.bind_variables) > 0)
values[i++] = CStringGetTextDatum(tmp.info.bind_variables);
else
nulls[i++] = true;

/* bind_variables at column number 48 */
if (strlen(tmp.info.bind_variables) > 0)
values[i++] = CStringGetTextDatum(tmp.info.bind_variables);
else
nulls[i++] = true;

/* relations at column number 10 */
if (tmp.info.num_relations > 0)
Expand Down Expand Up @@ -3303,15 +3330,18 @@ CleanQuerytext(const char *query, int *location, int *len)
*/
static char *
generate_normalized_query(JumbleState *jstate, const char *query,
int query_loc, int *query_len_p, int encoding)
int query_loc, int *query_len_p, int encoding,
char* bind_variables, int *bind_var_len_p)
{
char *norm_query;
int query_len = *query_len_p;
int bind_var_len;
int i,
norm_query_buflen, /* Space allowed for norm_query */
len_to_wrt, /* Length (in bytes) to write */
quer_loc = 0, /* Source query byte location */
n_quer_loc = 0, /* Normalized query byte location */
n_var_loc = 0, /* bind_variables byte location */
last_off = 0, /* Offset from start for previous tok */
last_tok_len = 0; /* Length (in bytes) of that tok */

Expand Down Expand Up @@ -3362,6 +3392,14 @@ generate_normalized_query(JumbleState *jstate, const char *query,
quer_loc = off + tok_len;
last_off = off;
last_tok_len = tok_len;
if (pgsm_extract_bind_variables){
if (n_var_loc+tok_len + 1 < VAR_LEN-1){
memcpy(bind_variables + n_var_loc, query + quer_loc - tok_len, tok_len);
n_var_loc += tok_len;
memcpy(bind_variables + n_var_loc , "|", 1);
n_var_loc ++;
}
}
}

/*
Expand All @@ -3378,6 +3416,11 @@ generate_normalized_query(JumbleState *jstate, const char *query,
norm_query[n_quer_loc] = '\0';

*query_len_p = n_quer_loc;
if (pgsm_extract_bind_variables){
bind_var_len = n_var_loc-1;
bind_variables[bind_var_len] = '\0';
*bind_var_len_p = bind_var_len;
}
return norm_query;
}

Expand Down
Loading