Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
add bind_variables ver 2.03
  • Loading branch information
ksjj97inzent committed Nov 28, 2023
1 parent 4863020 commit 70728c8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 18 deletions.
15 changes: 15 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 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
5 changes: 5 additions & 0 deletions pg_stat_monitor--2.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +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,
Expand Down Expand Up @@ -184,6 +185,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 +243,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 +310,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 +377,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
76 changes: 58 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 @@ -1878,11 +1892,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 +1900,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 +1934,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 +2246,12 @@ 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;

/* relations at column number 10 */
if (tmp.info.num_relations > 0)
Expand Down Expand Up @@ -3303,15 +3323,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 +3385,16 @@ 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 +3411,13 @@ 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
3 changes: 3 additions & 0 deletions pg_stat_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
#define CMD_LEN 20
#define APPLICATIONNAME_LEN NAMEDATALEN
#define COMMENTS_LEN 256
#define VAR_LEN 512
#define PGSM_OVER_FLOW_MAX 10
#define PLAN_TEXT_LEN 1024
/* the assumption of query max nested level */
Expand Down Expand Up @@ -252,6 +253,7 @@ typedef struct QueryInfo
char comments[COMMENTS_LEN];
char relations[REL_LST][REL_LEN]; /* List of relation involved
* in the query */
char bind_variables[VAR_LEN];
int num_relations; /* Number of relation in the query */
CmdType cmd_type; /* query command type
* SELECT/UPDATE/DELETE/INSERT */
Expand Down Expand Up @@ -514,6 +516,7 @@ extern bool pgsm_enable_overflow;
extern bool pgsm_normalized_query;
extern bool pgsm_track_utility;
extern bool pgsm_enable_pgsm_query_id;
extern bool pgsm_extract_bind_variables;
extern int pgsm_track;

#define DECLARE_HOOK(hook, ...) \
Expand Down

1 comment on commit 70728c8

@ksjj97inzent
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bind_variables add

Please sign in to comment.