Skip to content

Commit

Permalink
PG-607: Allow histogram to track queries in sub-ms time brackets (#384)
Browse files Browse the repository at this point in the history
* PG-607: Allow histogram to track queries in sub-ms time brackets

Updated the GUC configuration and the relevant histogram functionality
to track queries in lower cardinality than ms. This is done by saving
the GUC values for histogram min and max values in real (double) type.

All test cases except for the 030 tap test are passing. The test case
needs an update.

* Fixing regression issues for v12 and below because of histogram changes.
  • Loading branch information
codeforall authored Feb 23, 2023
1 parent 05ffcac commit fe23d31
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 73 deletions.
35 changes: 18 additions & 17 deletions guc.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ int pgsm_query_max_len;
int pgsm_bucket_time;
int pgsm_max_buckets;
int pgsm_histogram_buckets;
int pgsm_histogram_max;
int pgsm_histogram_min;
double pgsm_histogram_min;
double pgsm_histogram_max;
int pgsm_query_shared_buffer;
bool pgsm_track_planning;
bool pgsm_extract_comments;
Expand All @@ -38,8 +38,8 @@ int pgsm_track;
static int pgsm_overflow_target; /* Not used since 2.0 */

/* Check hooks to ensure histogram_min < histogram_max */
static bool check_histogram_min(int *newval, void **extra, GucSource source);
static bool check_histogram_max(int *newval, void **extra, GucSource source);
static bool check_histogram_min(double *newval, void **extra, GucSource source);
static bool check_histogram_max(double *newval, void **extra, GucSource source);
static bool check_overflow_targer(int *newval, void **extra, GucSource source);
/*
* Define (or redefine) custom GUC variables.
Expand Down Expand Up @@ -98,35 +98,35 @@ init_guc(void)
1, /* min value */
INT_MAX, /* max value */
PGC_POSTMASTER, /* context */
0, /* flags */
GUC_UNIT_S, /* flags */
NULL, /* check_hook */
NULL, /* assign_hook */
NULL /* show_hook */
);

DefineCustomIntVariable("pg_stat_monitor.pgsm_histogram_min", /* name */
DefineCustomRealVariable("pg_stat_monitor.pgsm_histogram_min", /* name */
"Sets the time in millisecond.", /* short_desc */
NULL, /* long_desc */
&pgsm_histogram_min, /* value address */
1, /* boot value */
0, /* min value */
INT_MAX, /* max value */
HISTOGRAM_MAX_TIME, /* max value */
PGC_POSTMASTER, /* context */
0, /* flags */
GUC_UNIT_MS, /* flags */
check_histogram_min, /* check_hook */
NULL, /* assign_hook */
NULL /* show_hook */
);

DefineCustomIntVariable("pg_stat_monitor.pgsm_histogram_max", /* name */
DefineCustomRealVariable("pg_stat_monitor.pgsm_histogram_max", /* name */
"Sets the time in millisecond.", /* short_desc */
NULL, /* long_desc */
&pgsm_histogram_max, /* value address */
100000, /* boot value */
10, /* min value */
INT_MAX, /* max value */
100000.0, /* boot value */
10.0, /* min value */
HISTOGRAM_MAX_TIME, /* max value */
PGC_POSTMASTER, /* context */
0, /* flags */
GUC_UNIT_MS, /* flags */
check_histogram_max, /* check_hook */
NULL, /* assign_hook */
NULL /* show_hook */
Expand Down Expand Up @@ -276,20 +276,21 @@ init_guc(void)

}

/* Maximum value must be greater or equal to minimum + 1.0 */
static bool
check_histogram_min(int *newval, void **extra, GucSource source)
check_histogram_min(double *newval, void **extra, GucSource source)
{
/*
* During module initialization PGSM_HISTOGRAM_MIN is initialized before
* PGSM_HISTOGRAM_MAX, in this case PGSM_HISTOGRAM_MAX will be zero.
*/
return (pgsm_histogram_max == 0 || *newval < pgsm_histogram_max);
return (pgsm_histogram_max == 0 || (*newval + 1.0) <= pgsm_histogram_max);
}

static bool
check_histogram_max(int *newval, void **extra, GucSource source)
check_histogram_max(double *newval, void **extra, GucSource source)
{
return (*newval > pgsm_histogram_min);
return (*newval >= (pgsm_histogram_min + 1.0));
}

static bool
Expand Down
22 changes: 11 additions & 11 deletions pg_stat_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ static int plan_nested_level = 0;
/* Histogram bucket variables */
static double hist_bucket_min;
static double hist_bucket_max;
static double hist_bucket_timings[MAX_RESPONSE_BUCKET + 2][2]; /* Start and end timings */
static int hist_bucket_count_user;
static int hist_bucket_count_total;
int64 hist_bucket_timings[MAX_RESPONSE_BUCKET + 2][2]; /* Start and end timings */

static uint32 pgsm_client_ip = PGSM_INVALID_IP_MASK;

Expand Down Expand Up @@ -112,7 +112,7 @@ static char *pgsm_explain(QueryDesc *queryDesc);

static void extract_query_comments(const char *query, char *comments, size_t max_len);
static void set_histogram_bucket_timings(void);
static void histogram_bucket_timings(int index, int64 *b_start, int64 *b_end);
static void histogram_bucket_timings(int index, double *b_start, double *b_end);
static int get_histogram_bucket(double q_time);

static bool IsSystemInitialized(void);
Expand Down Expand Up @@ -3545,8 +3545,8 @@ unpack_sql_state(int sql_state)
static void
set_histogram_bucket_timings(void)
{
int64 b2_start;
int64 b2_end;
double b2_start;
double b2_end;
int b_count;

hist_bucket_min = pgsm_histogram_min;
Expand Down Expand Up @@ -3596,7 +3596,7 @@ set_histogram_bucket_timings(void)
* Given an index, return the histogram start and end times.
*/
static void
histogram_bucket_timings(int index, int64 *b_start, int64 *b_end)
histogram_bucket_timings(int index, double *b_start, double *b_end)
{
double q_min = hist_bucket_min;
double q_max = hist_bucket_max;
Expand Down Expand Up @@ -3640,7 +3640,7 @@ static int
get_histogram_bucket(double q_time)
{
int index = 0;
int64 exec_time = (int64)q_time;
double exec_time = q_time;

for (index = 0; index < hist_bucket_count_total; index++)
{
Expand All @@ -3662,8 +3662,8 @@ get_histogram_bucket(double q_time)
Datum
get_histogram_timings(PG_FUNCTION_ARGS)
{
int64 b_start;
int64 b_end;
double b_start;
double b_end;
int b_count = hist_bucket_count_total;
int index = 0;
char *tmp_str = palloc0(MAX_STRING_LEN);
Expand All @@ -3675,16 +3675,16 @@ get_histogram_timings(PG_FUNCTION_ARGS)

if (index == 0)
{
snprintf(text_str, MAX_STRING_LEN, "{{%ld - %ld}", b_start, b_end);
snprintf(text_str, MAX_STRING_LEN, "{{%.3lf - %.3lf}", b_start, b_end);
}
else if (index == (b_count - 1))
{
snprintf(tmp_str, MAX_STRING_LEN, "%s, (%ld - ...}}", text_str, b_start);
snprintf(tmp_str, MAX_STRING_LEN, "%s, (%.3lf - ...}}", text_str, b_start);
snprintf(text_str, MAX_STRING_LEN, "%s", tmp_str);
}
else
{
snprintf(tmp_str, MAX_STRING_LEN, "%s, (%ld - %ld}", text_str, b_start, b_end);
snprintf(tmp_str, MAX_STRING_LEN, "%s, (%.3lf - %.3lf}", text_str, b_start, b_end);
snprintf(text_str, MAX_STRING_LEN, "%s", tmp_str);
}
}
Expand Down
6 changes: 3 additions & 3 deletions pg_stat_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@

#define JUMBLE_SIZE 1024 /* query serialization buffer size */

#define HISTOGRAM_MAX_TIME INT_MAX
#define HISTOGRAM_MAX_TIME 50000000
#define MAX_RESPONSE_BUCKET 50
#define INVALID_BUCKET_ID -1
#define MAX_BUCKETS 10
Expand Down Expand Up @@ -506,8 +506,8 @@ extern int pgsm_query_max_len;
extern int pgsm_bucket_time;
extern int pgsm_max_buckets;
extern int pgsm_histogram_buckets;
extern int pgsm_histogram_max;
extern int pgsm_histogram_min;
extern double pgsm_histogram_min;
extern double pgsm_histogram_max;
extern int pgsm_query_shared_buffer;
extern bool pgsm_track_planning;
extern bool pgsm_extract_comments;
Expand Down
6 changes: 3 additions & 3 deletions regression/expected/guc.out
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ BY name
COLLATE "C";
name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart
-------------------------------------------+---------+------+------------+---------+---------+---------+------------+----------------+----------+-----------+-----------------
pg_stat_monitor.pgsm_bucket_time | 60 | | postmaster | integer | default | 1 | 2147483647 | | 60 | 60 | f
pg_stat_monitor.pgsm_bucket_time | 60 | s | postmaster | integer | default | 1 | 2147483647 | | 60 | 60 | f
pg_stat_monitor.pgsm_enable_overflow | on | | postmaster | bool | default | | | | on | on | f
pg_stat_monitor.pgsm_enable_pgsm_query_id | on | | user | bool | default | | | | on | on | f
pg_stat_monitor.pgsm_enable_query_plan | 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 | integer | default | 10 | 2147483647 | | 100000 | 100000 | f
pg_stat_monitor.pgsm_histogram_min | 1 | | postmaster | integer | default | 0 | 2147483647 | | 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
6 changes: 3 additions & 3 deletions regression/expected/guc_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ BY name
COLLATE "C";
name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart
-------------------------------------------+---------+------+------------+---------+---------+---------+------------+----------------+----------+-----------+-----------------
pg_stat_monitor.pgsm_bucket_time | 60 | | postmaster | integer | default | 1 | 2147483647 | | 60 | 60 | f
pg_stat_monitor.pgsm_bucket_time | 60 | s | postmaster | integer | default | 1 | 2147483647 | | 60 | 60 | f
pg_stat_monitor.pgsm_enable_overflow | on | | postmaster | bool | default | | | | on | on | f
pg_stat_monitor.pgsm_enable_pgsm_query_id | on | | user | bool | default | | | | on | on | f
pg_stat_monitor.pgsm_enable_query_plan | 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 | integer | default | 10 | 2147483647 | | 100000 | 100000 | f
pg_stat_monitor.pgsm_histogram_min | 1 | | postmaster | integer | default | 0 | 2147483647 | | 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
12 changes: 6 additions & 6 deletions t/expected/001_settings_default.out
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ SELECT pg_stat_monitor_reset();
SELECT name, setting, unit, context, vartype, source, min_val, max_val, enumvals, boot_val, reset_val, pending_restart FROM pg_settings WHERE name LIKE '%pg_stat_monitor%';
name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart
-------------------------------------------+---------+------+------------+---------+---------+---------+------------+----------------+----------+-----------+-----------------
pg_stat_monitor.pgsm_bucket_time | 60 | | postmaster | integer | default | 1 | 2147483647 | | 60 | 60 | f
pg_stat_monitor.pgsm_bucket_time | 60 | s | postmaster | integer | default | 1 | 2147483647 | | 60 | 60 | f
pg_stat_monitor.pgsm_enable_overflow | on | | postmaster | bool | default | | | | on | on | f
pg_stat_monitor.pgsm_enable_pgsm_query_id | on | | user | bool | default | | | | on | on | f
pg_stat_monitor.pgsm_enable_query_plan | 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 | integer | default | 10 | 2147483647 | | 100000 | 100000 | f
pg_stat_monitor.pgsm_histogram_min | 1 | | postmaster | integer | default | 0 | 2147483647 | | 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 All @@ -37,14 +37,14 @@ SELECT datname, substr(query,0,100) AS query, calls FROM pg_stat_monitor ORDER B
SELECT name, setting, unit, context, vartype, source, min_val, max_val, enumvals, boot_val, reset_val, pending_restart FROM pg_settings WHERE name LIKE '%pg_stat_monitor%';
name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart
-------------------------------------------+---------+------+------------+---------+---------+---------+------------+----------------+----------+-----------+-----------------
pg_stat_monitor.pgsm_bucket_time | 60 | | postmaster | integer | default | 1 | 2147483647 | | 60 | 60 | f
pg_stat_monitor.pgsm_bucket_time | 60 | s | postmaster | integer | default | 1 | 2147483647 | | 60 | 60 | f
pg_stat_monitor.pgsm_enable_overflow | on | | postmaster | bool | default | | | | on | on | f
pg_stat_monitor.pgsm_enable_pgsm_query_id | on | | user | bool | default | | | | on | on | f
pg_stat_monitor.pgsm_enable_query_plan | 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 | integer | default | 10 | 2147483647 | | 100000 | 100000 | f
pg_stat_monitor.pgsm_histogram_min | 1 | | postmaster | integer | default | 0 | 2147483647 | | 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
Loading

0 comments on commit fe23d31

Please sign in to comment.