Skip to content

Commit 70728c8

Browse files
authored
Add files via upload
add bind_variables ver 2.03
1 parent 4863020 commit 70728c8

File tree

5 files changed

+86
-18
lines changed

5 files changed

+86
-18
lines changed

guc.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ bool pgsm_track_utility;
3636
bool pgsm_enable_pgsm_query_id;
3737
int pgsm_track;
3838
static int pgsm_overflow_target; /* Not used since 2.0 */
39+
/*custum */
40+
bool pgsm_extract_bind_variables;
3941

4042
/* Check hooks to ensure histogram_min < histogram_max */
4143
static bool check_histogram_min(double *newval, void **extra, GucSource source);
@@ -213,6 +215,19 @@ init_guc(void)
213215
NULL, /* assign_hook */
214216
NULL /* show_hook */
215217
);
218+
219+
/* custum */
220+
DefineCustomBoolVariable("pg_stat_monitor.pgsm_extract_bind_variables", /* name */
221+
"Selects whether extracting bind variables from queries.", /* short_desc */
222+
NULL, /* long_desc */
223+
&pgsm_extract_bind_variables, /* value address */
224+
false, /* boot value */
225+
PGC_USERSET, /* context */
226+
0, /* flags */
227+
NULL, /* check_hook */
228+
NULL, /* assign_hook */
229+
NULL /* show_hook */
230+
);
216231

217232
DefineCustomBoolVariable("pg_stat_monitor.pgsm_enable_overflow", /* name */
218233
"Enable/Disable pg_stat_monitor to grow beyond shared memory into swap space.", /* short_desc */

pg_stat_monitor--1.0--2.0.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ CREATE FUNCTION pg_stat_monitor_internal(
2525
OUT top_queryid int8,
2626
OUT top_query text,
2727
OUT application_name text,
28+
OUT bind_variables text,
2829

2930
OUT relations text, -- 11
3031
OUT cmd_type int,
@@ -128,6 +129,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
128129
query_plan,
129130
top_query,
130131
application_name,
132+
bind_variables,
131133
string_to_array(relations, ',') AS relations,
132134
cmd_type,
133135
get_cmd_type(cmd_type) AS cmd_type_text,
@@ -185,6 +187,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
185187
query_plan,
186188
top_query,
187189
application_name,
190+
bind_variables,
188191
string_to_array(relations, ',') AS relations,
189192
cmd_type,
190193
get_cmd_type(cmd_type) AS cmd_type_text,
@@ -251,6 +254,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
251254
query_plan,
252255
top_query,
253256
application_name,
257+
bind_variables,
254258
string_to_array(relations, ',') AS relations,
255259
cmd_type,
256260
get_cmd_type(cmd_type) AS cmd_type_text,
@@ -317,6 +321,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
317321
query_plan,
318322
top_query,
319323
application_name,
324+
bind_variables,
320325
string_to_array(relations, ',') AS relations,
321326
cmd_type,
322327
get_cmd_type(cmd_type) AS cmd_type_text,

pg_stat_monitor--2.0.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ CREATE FUNCTION pg_stat_monitor_internal(
9797
OUT top_queryid int8,
9898
OUT top_query text,
9999
OUT application_name text,
100+
OUT bind_variables text,
100101

101102
OUT relations text, -- 11
102103
OUT cmd_type int,
@@ -184,6 +185,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
184185
query_plan,
185186
top_query,
186187
application_name,
188+
bind_variables,
187189
string_to_array(relations, ',') AS relations,
188190
cmd_type,
189191
get_cmd_type(cmd_type) AS cmd_type_text,
@@ -241,6 +243,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
241243
query_plan,
242244
top_query,
243245
application_name,
246+
bind_variables,
244247
string_to_array(relations, ',') AS relations,
245248
cmd_type,
246249
get_cmd_type(cmd_type) AS cmd_type_text,
@@ -307,6 +310,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
307310
query_plan,
308311
top_query,
309312
application_name,
313+
bind_variables,
310314
string_to_array(relations, ',') AS relations,
311315
cmd_type,
312316
get_cmd_type(cmd_type) AS cmd_type_text,
@@ -373,6 +377,7 @@ CREATE VIEW pg_stat_monitor AS SELECT
373377
query_plan,
374378
top_query,
375379
application_name,
380+
bind_variables,
376381
string_to_array(relations, ',') AS relations,
377382
cmd_type,
378383
get_cmd_type(cmd_type) AS cmd_type_text,

pg_stat_monitor.c

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ PG_MODULE_MAGIC;
4040

4141
/* Number of output arguments (columns) for various API versions */
4242
#define PG_STAT_MONITOR_COLS_V1_0 52
43-
#define PG_STAT_MONITOR_COLS_V2_0 64
43+
#define PG_STAT_MONITOR_COLS_V2_0 65
4444
#define PG_STAT_MONITOR_COLS PG_STAT_MONITOR_COLS_V2_0 /* maximum of above */
4545

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

258258
static char *generate_normalized_query(JumbleState *jstate, const char *query,
259-
int query_loc, int *query_len_p, int encoding);
259+
int query_loc, int *query_len_p, int encoding,
260+
char* bind_variables, int *bind_var_len_p);
260261
static void fill_in_constant_lengths(JumbleState *jstate, const char *query, int query_loc);
261262
static int comp_location(const void *a, const void *b);
262263

@@ -407,6 +408,10 @@ pgsm_post_parse_analyze_internal(ParseState *pstate, Query *query, JumbleState *
407408
int norm_query_len;
408409
int location;
409410
int query_len;
411+
/* custum bind_variables */
412+
char bind_variables[VAR_LEN] = "";
413+
int bind_var_len = 0;
414+
410415

411416
/* Safety check... */
412417
if (!IsSystemInitialized())
@@ -477,8 +482,9 @@ pgsm_post_parse_analyze_internal(ParseState *pstate, Query *query, JumbleState *
477482
query_text, /* query */
478483
location, /* query location */
479484
&norm_query_len,
480-
GetDatabaseEncoding());
481-
485+
GetDatabaseEncoding(),
486+
&bind_variables[0],
487+
&bind_var_len);
482488
Assert(norm_query);
483489
}
484490

@@ -496,6 +502,10 @@ pgsm_post_parse_analyze_internal(ParseState *pstate, Query *query, JumbleState *
496502
*/
497503
entry->pgsm_query_id = get_pgsm_query_id_hash(norm_query ? norm_query : query_text, norm_query_len);
498504
entry->counters.info.cmd_type = query->commandType;
505+
506+
if(bind_var_len > 0){
507+
_snprintf(entry->counters.info.bind_variables, bind_variables, bind_var_len + 1, VAR_LEN);
508+
}
499509

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

17771787
pgsm = pgsm_get_ss();
17781788

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

@@ -1878,11 +1892,6 @@ pgsm_store(pgsmEntry * entry)
18781892

18791893
if (shared_hash_entry == NULL)
18801894
{
1881-
LWLockRelease(pgsm->lock);
1882-
1883-
if (DsaPointerIsValid(dsa_query_pointer))
1884-
dsa_free(query_dsa_area, dsa_query_pointer);
1885-
18861895
/*
18871896
* Out of memory; report only if the state has changed now.
18881897
* Otherwise we risk filling up the log file with these message.
@@ -1891,16 +1900,18 @@ pgsm_store(pgsmEntry * entry)
18911900
{
18921901
pgsm->pgsm_oom = true;
18931902

1894-
PGSM_DISABLE_ERROR_CAPUTRE();
1895-
{
1896-
ereport(WARNING,
1897-
(errcode(ERRCODE_OUT_OF_MEMORY),
1898-
errmsg("[pg_stat_monitor] pgsm_store: Hash table is out of memory and can no longer store queries!"),
1899-
errdetail("You may reset the view or when the buckets are deallocated, pg_stat_monitor will resume saving " \
1900-
"queries. Alternatively, try increasing the value of pg_stat_monitor.pgsm_max.")));
1901-
} PGSM_END_DISABLE_ERROR_CAPTURE();
1903+
ereport(WARNING,
1904+
(errcode(ERRCODE_OUT_OF_MEMORY),
1905+
errmsg("[pg_stat_monitor] pgsm_store: Hash table is out of memory and can no longer store queries!"),
1906+
errdetail("You may reset the view or when the buckets are deallocated, pg_stat_monitor will resume saving " \
1907+
"queries. Alternatively, try increasing the value of pg_stat_monitor.pgsm_max.")));
19021908
}
19031909

1910+
LWLockRelease(pgsm->lock);
1911+
1912+
if (DsaPointerIsValid(dsa_query_pointer))
1913+
dsa_free(query_dsa_area, dsa_query_pointer);
1914+
19041915
return;
19051916
}
19061917
else
@@ -1923,6 +1934,9 @@ pgsm_store(pgsmEntry * entry)
19231934
snprintf(shared_hash_entry->username, sizeof(shared_hash_entry->username), "%s", entry->username);
19241935
}
19251936

1937+
if(strlen(entry->counters.info.bind_variables) > 0)
1938+
strncpy(shared_hash_entry->counters.info.bind_variables, entry->counters.info.bind_variables, VAR_LEN);
1939+
19261940
pgsm_update_entry(shared_hash_entry, /* entry */
19271941
query, /* query */
19281942
comments, /* comments */
@@ -2232,6 +2246,12 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
22322246
values[i++] = CStringGetTextDatum(tmp.info.application_name);
22332247
else
22342248
nulls[i++] = true;
2249+
2250+
/* bind_variables at column number 48 */
2251+
if (strlen(tmp.info.bind_variables) > 0)
2252+
values[i++] = CStringGetTextDatum(tmp.info.bind_variables);
2253+
else
2254+
nulls[i++] = true;
22352255

22362256
/* relations at column number 10 */
22372257
if (tmp.info.num_relations > 0)
@@ -3303,15 +3323,18 @@ CleanQuerytext(const char *query, int *location, int *len)
33033323
*/
33043324
static char *
33053325
generate_normalized_query(JumbleState *jstate, const char *query,
3306-
int query_loc, int *query_len_p, int encoding)
3326+
int query_loc, int *query_len_p, int encoding,
3327+
char* bind_variables, int *bind_var_len_p)
33073328
{
33083329
char *norm_query;
33093330
int query_len = *query_len_p;
3331+
int bind_var_len;
33103332
int i,
33113333
norm_query_buflen, /* Space allowed for norm_query */
33123334
len_to_wrt, /* Length (in bytes) to write */
33133335
quer_loc = 0, /* Source query byte location */
33143336
n_quer_loc = 0, /* Normalized query byte location */
3337+
n_var_loc = 0, /* bind_variables byte location */
33153338
last_off = 0, /* Offset from start for previous tok */
33163339
last_tok_len = 0; /* Length (in bytes) of that tok */
33173340

@@ -3362,6 +3385,16 @@ generate_normalized_query(JumbleState *jstate, const char *query,
33623385
quer_loc = off + tok_len;
33633386
last_off = off;
33643387
last_tok_len = tok_len;
3388+
3389+
3390+
if (pgsm_extract_bind_variables){
3391+
if (n_var_loc+tok_len + 1 < VAR_LEN-1){
3392+
memcpy(bind_variables + n_var_loc, query + quer_loc - tok_len, tok_len);
3393+
n_var_loc += tok_len;
3394+
memcpy(bind_variables + n_var_loc , "|", 1);
3395+
n_var_loc ++;
3396+
}
3397+
}
33653398
}
33663399

33673400
/*
@@ -3378,6 +3411,13 @@ generate_normalized_query(JumbleState *jstate, const char *query,
33783411
norm_query[n_quer_loc] = '\0';
33793412

33803413
*query_len_p = n_quer_loc;
3414+
3415+
if (pgsm_extract_bind_variables){
3416+
bind_var_len = n_var_loc-1;
3417+
bind_variables[bind_var_len] = '\0';
3418+
*bind_var_len_p = bind_var_len;
3419+
}
3420+
33813421
return norm_query;
33823422
}
33833423

pg_stat_monitor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
#define CMD_LEN 20
8989
#define APPLICATIONNAME_LEN NAMEDATALEN
9090
#define COMMENTS_LEN 256
91+
#define VAR_LEN 512
9192
#define PGSM_OVER_FLOW_MAX 10
9293
#define PLAN_TEXT_LEN 1024
9394
/* the assumption of query max nested level */
@@ -252,6 +253,7 @@ typedef struct QueryInfo
252253
char comments[COMMENTS_LEN];
253254
char relations[REL_LST][REL_LEN]; /* List of relation involved
254255
* in the query */
256+
char bind_variables[VAR_LEN];
255257
int num_relations; /* Number of relation in the query */
256258
CmdType cmd_type; /* query command type
257259
* SELECT/UPDATE/DELETE/INSERT */
@@ -514,6 +516,7 @@ extern bool pgsm_enable_overflow;
514516
extern bool pgsm_normalized_query;
515517
extern bool pgsm_track_utility;
516518
extern bool pgsm_enable_pgsm_query_id;
519+
extern bool pgsm_extract_bind_variables;
517520
extern int pgsm_track;
518521

519522
#define DECLARE_HOOK(hook, ...) \

0 commit comments

Comments
 (0)