Skip to content

Commit

Permalink
[PG-644] Add option to disable application name tracking (#469)
Browse files Browse the repository at this point in the history
* Cache application name for every backed instance

* Improve pg_get_backend_status performance for PG16 and PG17

* Fix

* Make application_name tracking disabled by default

* Meke app name tracking opt-out

* Format newly added code with pgindent

* Fix build for PG17

* Fix
  • Loading branch information
artemgavrilov authored Jul 23, 2024
1 parent 16ec836 commit d7999f1
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 148 deletions.
13 changes: 13 additions & 0 deletions guc.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ bool pgsm_enable_query_plan;
bool pgsm_enable_overflow;
bool pgsm_normalized_query;
bool pgsm_track_utility;
bool pgsm_track_application_names;
bool pgsm_enable_pgsm_query_id;
int pgsm_track;
static int pgsm_overflow_target; /* Not used since 2.0 */
Expand Down Expand Up @@ -190,6 +191,18 @@ init_guc(void)
NULL /* show_hook */
);

DefineCustomBoolVariable("pg_stat_monitor.pgsm_track_application_names", /* name */
"Enable/Disable application names tracking.", /* short_desc */
NULL, /* long_desc */
&pgsm_track_application_names, /* value address */
true, /* boot value */
PGC_USERSET, /* context */
0, /* flags */
NULL, /* check_hook */
NULL, /* assign_hook */
NULL /* show_hook */
);

DefineCustomBoolVariable("pg_stat_monitor.pgsm_enable_pgsm_query_id", /* name */
"Enable/disable PGSM specific query id calculation which is very useful in comparing same query across databases and clusters..", /* short_desc */
NULL, /* long_desc */
Expand Down
31 changes: 19 additions & 12 deletions pg_stat_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ PG_MODULE_MAGIC;
#define PG_STAT_MONITOR_COLS_V1_0 52
#define PG_STAT_MONITOR_COLS_V2_0 64
#define PG_STAT_MONITOR_COLS_V2_1 70
#define PG_STAT_MONITOR_COLS PG_STAT_MONITOR_COLS_V2_0 /* maximum of above */
#define PG_STAT_MONITOR_COLS PG_STAT_MONITOR_COLS_V2_1 /* maximum of above */

#define PGSM_TEXT_FILE PGSTAT_STAT_PERMANENT_DIRECTORY "pg_stat_monitor_query"

Expand Down Expand Up @@ -106,8 +106,8 @@ static struct rusage rusage_start;
static struct rusage rusage_end;

/* Application name and length; set each time when an entry is created locally */
char app_name[APPLICATIONNAME_LEN];
int app_name_len;
static char app_name[APPLICATIONNAME_LEN];
static int app_name_len;


/* Query buffer, store queries' text. */
Expand Down Expand Up @@ -1334,18 +1334,21 @@ pgsm_hash_string(const char *str, int len)
static PgBackendStatus *
pg_get_backend_status(void)
{

#if PG_VERSION_NUM >= 170000
return pgstat_get_beentry_by_proc_number(MyProcPid);
#elif PG_VERSION_NUM >= 160000
return &(pgstat_get_local_beentry_by_backend_id(MyBackendId)->backendStatus);
#else
LocalPgBackendStatus *local_beentry;
int num_backends = pgstat_fetch_stat_numbackends();
int i;

for (i = 1; i <= num_backends; i++)
{
PgBackendStatus *beentry;
#if PG_VERSION_NUM < 160000

local_beentry = pgstat_fetch_stat_local_beentry(i);
#else
local_beentry = pgstat_get_local_beentry_by_index(i);
#endif
if (!local_beentry)
continue;

Expand All @@ -1355,6 +1358,8 @@ pg_get_backend_status(void)
return beentry;
}
return NULL;

#endif
}

/*
Expand Down Expand Up @@ -1531,7 +1536,7 @@ pgsm_update_entry(pgsmEntry * entry,
/* Only should process this once when storing the data */
if (kind == PGSM_STORE)
{
if (app_name_len > 0 && !e->counters.info.application_name[0])
if (pgsm_track_application_names && app_name_len > 0 && !e->counters.info.application_name[0])
_snprintf(e->counters.info.application_name, app_name, app_name_len + 1, APPLICATIONNAME_LEN);

e->counters.info.num_relations = num_relations;
Expand Down Expand Up @@ -1787,7 +1792,6 @@ pgsm_create_hash_entry(uint64 bucket_id, uint64 queryid, PlanInfo * plan_info)
pgsmEntry *entry;
int sec_ctx;
bool found_client_addr = false;
char *app_name_ptr = app_name;
MemoryContext oldctx;
char *datname = NULL;
char *username = NULL;
Expand All @@ -1802,9 +1806,12 @@ pgsm_create_hash_entry(uint64 bucket_id, uint64 queryid, PlanInfo * plan_info)
*/
GetUserIdAndSecContext((Oid *) &entry->key.userid, &sec_ctx);

/* Get the application name and set appid */
app_name_len = pg_get_application_name(app_name, APPLICATIONNAME_LEN);
entry->key.appid = pgsm_hash_string((const char *) app_name_ptr, app_name_len);
if (pgsm_track_application_names)
{
/* Get the application name and set appid */
app_name_len = pg_get_application_name(app_name, APPLICATIONNAME_LEN);
entry->key.appid = pgsm_hash_string((const char *) app_name, app_name_len);
}

/* client address */
if (!pgsm_client_ip_is_valid())
Expand Down
1 change: 1 addition & 0 deletions pg_stat_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ extern bool pgsm_enable_query_plan;
extern bool pgsm_enable_overflow;
extern bool pgsm_normalized_query;
extern bool pgsm_track_utility;
extern bool pgsm_track_application_names;
extern bool pgsm_enable_pgsm_query_id;
extern int pgsm_track;

Expand Down
28 changes: 28 additions & 0 deletions regression/expected/application_name.out
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,32 @@ SELECT pg_stat_monitor_reset();

(1 row)

SELECT 1 AS num;
num
-----
1
(1 row)

SET pg_stat_monitor.pgsm_track_application_names='no';
SELECT 1 AS num;
num
-----
1
(1 row)

SELECT query,application_name FROM pg_stat_monitor ORDER BY query, application_name COLLATE "C";
query | application_name
-------------------------------------------------------+-----------------------------
SELECT 1 AS num | pg_regress/application_name
SELECT 1 AS num |
SELECT pg_stat_monitor_reset() | pg_regress/application_name
SET pg_stat_monitor.pgsm_track_application_names='no' |
(4 rows)

SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------

(1 row)

DROP EXTENSION pg_stat_monitor;
41 changes: 21 additions & 20 deletions regression/expected/guc.out
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@ WHERE name LIKE 'pg_stat_monitor.%'
ORDER
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 | 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 | 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
pg_stat_monitor.pgsm_overflow_target | 1 | | postmaster | integer | default | 0 | 1 | | 1 | 1 | f
pg_stat_monitor.pgsm_query_max_len | 2048 | | postmaster | integer | default | 1024 | 2147483647 | | 2048 | 2048 | f
pg_stat_monitor.pgsm_query_shared_buffer | 20 | MB | postmaster | integer | default | 1 | 10000 | | 20 | 20 | f
pg_stat_monitor.pgsm_track | top | | user | enum | default | | | {none,top,all} | top | top | f
pg_stat_monitor.pgsm_track_planning | off | | user | bool | default | | | | off | off | f
pg_stat_monitor.pgsm_track_utility | on | | user | bool | default | | | | on | on | f
(17 rows)
name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart
----------------------------------------------+---------+------+------------+---------+---------+---------+------------+----------------+----------+-----------+-----------------
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 | 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
pg_stat_monitor.pgsm_overflow_target | 1 | | postmaster | integer | default | 0 | 1 | | 1 | 1 | f
pg_stat_monitor.pgsm_query_max_len | 2048 | | postmaster | integer | default | 1024 | 2147483647 | | 2048 | 2048 | f
pg_stat_monitor.pgsm_query_shared_buffer | 20 | MB | postmaster | integer | default | 1 | 10000 | | 20 | 20 | f
pg_stat_monitor.pgsm_track | top | | user | enum | default | | | {none,top,all} | top | top | f
pg_stat_monitor.pgsm_track_application_names | on | | user | bool | default | | | | on | on | f
pg_stat_monitor.pgsm_track_planning | off | | user | bool | default | | | | off | off | f
pg_stat_monitor.pgsm_track_utility | on | | user | bool | default | | | | on | on | f
(18 rows)

DROP EXTENSION pg_stat_monitor;
39 changes: 20 additions & 19 deletions regression/expected/guc_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,25 @@ WHERE name LIKE 'pg_stat_monitor.%'
ORDER
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 | 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 | 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
pg_stat_monitor.pgsm_overflow_target | 1 | | postmaster | integer | default | 0 | 1 | | 1 | 1 | f
pg_stat_monitor.pgsm_query_max_len | 2048 | | postmaster | integer | default | 1024 | 2147483647 | | 2048 | 2048 | f
pg_stat_monitor.pgsm_query_shared_buffer | 20 | MB | postmaster | integer | default | 1 | 10000 | | 20 | 20 | f
pg_stat_monitor.pgsm_track | top | | user | enum | default | | | {none,top,all} | top | top | f
pg_stat_monitor.pgsm_track_utility | on | | user | bool | default | | | | on | on | f
(16 rows)
name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart
----------------------------------------------+---------+------+------------+---------+---------+---------+------------+----------------+----------+-----------+-----------------
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 | 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
pg_stat_monitor.pgsm_overflow_target | 1 | | postmaster | integer | default | 0 | 1 | | 1 | 1 | f
pg_stat_monitor.pgsm_query_max_len | 2048 | | postmaster | integer | default | 1024 | 2147483647 | | 2048 | 2048 | f
pg_stat_monitor.pgsm_query_shared_buffer | 20 | MB | postmaster | integer | default | 1 | 10000 | | 20 | 20 | f
pg_stat_monitor.pgsm_track | top | | user | enum | default | | | {none,top,all} | top | top | f
pg_stat_monitor.pgsm_track_application_names | on | | user | bool | default | | | | on | on | f
pg_stat_monitor.pgsm_track_utility | on | | user | bool | default | | | | on | on | f
(17 rows)

DROP EXTENSION pg_stat_monitor;
Loading

0 comments on commit d7999f1

Please sign in to comment.