Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
3 changes: 2 additions & 1 deletion pg_stat_monitor--1.0--2.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ CREATE FUNCTION pg_stat_monitor_internal(
OUT top_queryid int8,
OUT top_query text,
OUT application_name text,
OUT bind_variables text,
OUT bind_variables text,

OUT relations text, -- 11
OUT cmd_type int,
OUT elevel int,
Expand Down
2 changes: 1 addition & 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 bind_variables text,
OUT relations text, -- 11
OUT cmd_type int,
OUT elevel int,
Expand Down
58 changes: 37 additions & 21 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 @@ -408,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 @@ -478,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 @@ -497,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 @@ -1777,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 @@ -1880,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 @@ -1893,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 @@ -1925,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 @@ -2234,6 +2247,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;

/* bind_variables at column number 48 */
if (strlen(tmp.info.bind_variables) > 0)
Expand Down Expand Up @@ -3373,15 +3392,13 @@ generate_normalized_query(JumbleState *jstate, const char *query,
quer_loc = off + tok_len;
last_off = off;
last_tok_len = tok_len;

if (PGSM_EXTRACT_VARIABLES){
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 @@ -3399,8 +3416,7 @@ generate_normalized_query(JumbleState *jstate, const char *query,
norm_query[n_quer_loc] = '\0';

*query_len_p = n_quer_loc;

if (PGSM_EXTRACT_VARIABLES){
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;
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
#define VAR_LEN COMMENTS_LEN
Expand Down Expand Up @@ -254,6 +255,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 @@ -516,6 +518,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
4 changes: 2 additions & 2 deletions regression/expected/guc_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ COLLATE "C";
pg_stat_monitor.pgsm_extract_bind_variables | off | | user | bool | default | | | | off | off | f
pg_stat_monitor.pgsm_extract_comments | off | | user | bool | default | | | | off | off | f
pg_stat_monitor.pgsm_histogram_buckets | 20 | | postmaster | integer | default | 2 | 50 | | 20 | 20 | f
pg_stat_monitor.pgsm_histogram_max | 100000 | | postmaster | real | default | 10 | 5e+07 | | 100000 | 100000 | f
pg_stat_monitor.pgsm_histogram_min | 1 | | postmaster | real | default | 0 | 5e+07 | | 1 | 1 | f
pg_stat_monitor.pgsm_histogram_max | 100000 | ms | postmaster | real | default | 10 | 5e+07 | | 100000 | 100000 | f
pg_stat_monitor.pgsm_histogram_min | 1 | ms | postmaster | real | default | 0 | 5e+07 | | 1 | 1 | f
pg_stat_monitor.pgsm_max | 256 | MB | postmaster | integer | default | 10 | 10240 | | 256 | 256 | f
pg_stat_monitor.pgsm_max_buckets | 10 | | postmaster | integer | default | 1 | 20000 | | 10 | 10 | f
pg_stat_monitor.pgsm_normalized_query | off | | user | bool | default | | | | off | off | f
Expand Down
10 changes: 5 additions & 5 deletions t/018_column_names.pl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
close $conf;

# Dictionary for expected PGSM columns names on different PG server versions
my %pg_versions_pgsm_columns = ( 16 => "application_name,blk_read_time," .
my %pg_versions_pgsm_columns = ( 16 => "application_name,bind_variables,blk_read_time," .
"blk_write_time,bucket,bucket_done,bucket_start_time,calls," .
"client_ip,cmd_type,cmd_type_text,comments,cpu_sys_time,cpu_user_time," .
"datname,dbid,elevel,jit_emission_count,jit_emission_time,jit_functions," .
Expand All @@ -37,7 +37,7 @@
"temp_blk_read_time,temp_blk_write_time,temp_blks_read,temp_blks_written," .
"top_query,top_queryid,toplevel,total_exec_time,total_plan_time," .
"userid,username,wal_bytes,wal_fpi,wal_records",
15 => "application_name,blk_read_time," .
15 => "application_name,bind_variables,blk_read_time," .
"blk_write_time,bucket,bucket_done,bucket_start_time,calls," .
"client_ip,cmd_type,cmd_type_text,comments,cpu_sys_time,cpu_user_time," .
"datname,dbid,elevel,jit_emission_count,jit_emission_time,jit_functions," .
Expand All @@ -52,7 +52,7 @@
"temp_blk_read_time,temp_blk_write_time,temp_blks_read,temp_blks_written," .
"top_query,top_queryid,toplevel,total_exec_time,total_plan_time," .
"userid,username,wal_bytes,wal_fpi,wal_records",
14 => "application_name,blk_read_time," .
14 => "application_name,bind_variables,blk_read_time," .
"blk_write_time,bucket,bucket_done,bucket_start_time,calls," .
"client_ip,cmd_type,cmd_type_text,comments,cpu_sys_time,cpu_user_time," .
"datname,dbid,elevel,local_blks_dirtied,local_blks_hit,local_blks_read," .
Expand All @@ -63,7 +63,7 @@
"shared_blks_written,sqlcode,stddev_exec_time,stddev_plan_time," .
"temp_blks_read,temp_blks_written,top_query,top_queryid,toplevel," .
"total_exec_time,total_plan_time,userid,username,wal_bytes,wal_fpi,wal_records",
13 => "application_name,blk_read_time," .
13 => "application_name,bind_variables,blk_read_time," .
"blk_write_time,bucket,bucket_done,bucket_start_time,calls," .
"client_ip,cmd_type,cmd_type_text,comments,cpu_sys_time,cpu_user_time," .
"datname,dbid,elevel,local_blks_dirtied,local_blks_hit,local_blks_read," .
Expand All @@ -74,7 +74,7 @@
"shared_blks_written,sqlcode,stddev_exec_time,stddev_plan_time," .
"temp_blks_read,temp_blks_written,top_query,top_queryid,toplevel," .
"total_exec_time,total_plan_time,userid,username,wal_bytes,wal_fpi,wal_records",
12 => "application_name,blk_read_time,blk_write_time,bucket,bucket_done," .
12 => "application_name,bind_variables,blk_read_time,blk_write_time,bucket,bucket_done," .
"bucket_start_time,calls,client_ip,cmd_type,cmd_type_text,comments," .
"cpu_sys_time,cpu_user_time,datname,dbid,elevel,local_blks_dirtied," .
"local_blks_hit,local_blks_read,local_blks_written,max_time,mean_time," .
Expand Down
Loading