From 8d4e99a7afd68e90441f5fbff3b4499c8b14ce23 Mon Sep 17 00:00:00 2001 From: Hamid Akhtar Date: Thu, 16 Feb 2023 14:51:28 +0500 Subject: [PATCH] Reducing memory footprint of the PGSM entry by reducing relation size to match typlen of name type. Also, updated some gucs for better MAX and default values. --- guc.c | 8 ++--- pg_stat_monitor.c | 12 +++---- pg_stat_monitor.h | 4 ++- regression/expected/guc.out | 4 +-- regression/expected/guc_1.out | 4 +-- t/expected/001_settings_default.out | 8 ++--- t/expected/001_settings_default.out.12 | 8 ++--- t/expected/012_settings_pgsm_max_buckets.out | 34 ++++++++++---------- t/expected/016_settings_pgsm_max.out | 10 +++--- 9 files changed, 47 insertions(+), 45 deletions(-) mode change 100755 => 100644 t/expected/001_settings_default.out.12 diff --git a/guc.c b/guc.c index 6acbe0e..f7f8bb9 100644 --- a/guc.c +++ b/guc.c @@ -41,9 +41,9 @@ init_guc(void) { .guc_name = "pg_stat_monitor.pgsm_max", .guc_desc = "Sets the maximum size of shared memory in (MB) used for statement's metadata tracked by pg_stat_monitor.", - .guc_default = 100, - .guc_min = 1, - .guc_max = 1000, + .guc_default = 256, + .guc_min = 10, + .guc_max = 10240, .guc_restart = true, .guc_unit = GUC_UNIT_MB, .guc_value = &PGSM_MAX @@ -95,7 +95,7 @@ init_guc(void) .guc_desc = "Sets the maximum number of buckets.", .guc_default = 10, .guc_min = 1, - .guc_max = 10, + .guc_max = 20000, .guc_restart = true, .guc_unit = 0, .guc_value = &PGSM_MAX_BUCKETS diff --git a/pg_stat_monitor.c b/pg_stat_monitor.c index 07a9a46..3e95c5b 100644 --- a/pg_stat_monitor.c +++ b/pg_stat_monitor.c @@ -978,9 +978,12 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString, { Node *parsetree = pstmt->utilityStmt; uint64 queryId = 0; + +#if PG_VERSION_NUM < 140000 int len = strlen(queryString); -#if PG_VERSION_NUM >= 140000 + queryId = pgss_hash_string(queryString, len); +#else queryId = pstmt->queryId; /* @@ -994,8 +997,6 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString, */ if (PGSM_TRACK_UTILITY && pgsm_enabled(exec_nested_level)) pstmt->queryId = UINT64CONST(0); -#else - queryId = pgss_hash_string(queryString, len); #endif /* @@ -2140,8 +2141,8 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo, if (tmp.info.num_relations > 0) { int j; - char *text_str = palloc0(1024); - char *tmp_str = palloc0(1024); + char *text_str = palloc0(TOTAL_RELS_LENGTH); + char *tmp_str = palloc0(TOTAL_RELS_LENGTH); bool first = true; /* @@ -2315,7 +2316,6 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo, values[i++] = Int64GetDatumFast(tmp.jitinfo.jit_emission_count); values[i++] = Float8GetDatumFast(tmp.jitinfo.jit_emission_time); } - } values[i++] = BoolGetDatum(toplevel); values[i++] = BoolGetDatum(pg_atomic_read_u64(&pgss->current_wbucket) != bucketid); diff --git a/pg_stat_monitor.h b/pg_stat_monitor.h index 827d927..b5366b4 100644 --- a/pg_stat_monitor.h +++ b/pg_stat_monitor.h @@ -78,8 +78,9 @@ #define MAX_BUCKETS 10 #define TEXT_LEN 255 #define ERROR_MESSAGE_LEN 100 +#define REL_TYPENAME_LEN 64 #define REL_LST 10 -#define REL_LEN 1000 +#define REL_LEN 132 /* REL_TYPENAME_LEN * 2 (relname + schema) + 1 (for view indication) + 1 and dot and string terminator */ #define CMD_LST 10 #define CMD_LEN 20 #define APPLICATIONNAME_LEN 100 @@ -97,6 +98,7 @@ #define QUERY_MARGIN 100 #define MIN_QUERY_LEN 10 #define SQLCODE_LEN 20 +#define TOTAL_RELS_LENGTH (REL_LST * REL_LEN) #if PG_VERSION_NUM >= 130000 #define MAX_SETTINGS 15 diff --git a/regression/expected/guc.out b/regression/expected/guc.out index c0702a9..e62dc2d 100644 --- a/regression/expected/guc.out +++ b/regression/expected/guc.out @@ -24,8 +24,8 @@ COLLATE "C"; 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_max | 100 | MB | postmaster | integer | default | 1 | 1000 | | 100 | 100 | f - pg_stat_monitor.pgsm_max_buckets | 10 | | postmaster | integer | default | 1 | 10 | | 10 | 10 | 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 diff --git a/regression/expected/guc_1.out b/regression/expected/guc_1.out index e83f603..087cf5d 100644 --- a/regression/expected/guc_1.out +++ b/regression/expected/guc_1.out @@ -24,8 +24,8 @@ COLLATE "C"; 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_max | 100 | MB | postmaster | integer | default | 1 | 1000 | | 100 | 100 | f - pg_stat_monitor.pgsm_max_buckets | 10 | | postmaster | integer | default | 1 | 10 | | 10 | 10 | 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 diff --git a/t/expected/001_settings_default.out b/t/expected/001_settings_default.out index 498bb39..57ac4c4 100644 --- a/t/expected/001_settings_default.out +++ b/t/expected/001_settings_default.out @@ -14,8 +14,8 @@ SELECT name, setting, unit, context, vartype, source, min_val, max_val, enumvals 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_max | 100 | MB | postmaster | integer | default | 1 | 1000 | | 100 | 100 | f - pg_stat_monitor.pgsm_max_buckets | 10 | | postmaster | integer | default | 1 | 10 | | 10 | 10 | 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 @@ -41,8 +41,8 @@ SELECT name, setting, unit, context, vartype, source, min_val, max_val, enumvals 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_max | 100 | MB | postmaster | integer | default | 1 | 1000 | | 100 | 100 | f - pg_stat_monitor.pgsm_max_buckets | 10 | | postmaster | integer | default | 1 | 10 | | 10 | 10 | 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 diff --git a/t/expected/001_settings_default.out.12 b/t/expected/001_settings_default.out.12 old mode 100755 new mode 100644 index d77c7e4..157d687 --- a/t/expected/001_settings_default.out.12 +++ b/t/expected/001_settings_default.out.12 @@ -14,8 +14,8 @@ SELECT name, setting, unit, context, vartype, source, min_val, max_val, enumvals 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_max | 100 | MB | postmaster | integer | default | 1 | 1000 | | 100 | 100 | f - pg_stat_monitor.pgsm_max_buckets | 10 | | postmaster | integer | default | 1 | 10 | | 10 | 10 | 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 @@ -40,8 +40,8 @@ SELECT name, setting, unit, context, vartype, source, min_val, max_val, enumvals 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_max | 100 | MB | postmaster | integer | default | 1 | 1000 | | 100 | 100 | f - pg_stat_monitor.pgsm_max_buckets | 10 | | postmaster | integer | default | 1 | 10 | | 10 | 10 | 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 diff --git a/t/expected/012_settings_pgsm_max_buckets.out b/t/expected/012_settings_pgsm_max_buckets.out index 3942aae..2560281 100644 --- a/t/expected/012_settings_pgsm_max_buckets.out +++ b/t/expected/012_settings_pgsm_max_buckets.out @@ -8,7 +8,7 @@ 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='pg_stat_monitor.pgsm_max_buckets'; name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart ----------------------------------+---------+------+------------+---------+--------------------+---------+---------+----------+----------+-----------+----------------- - pg_stat_monitor.pgsm_max_buckets | 1 | | postmaster | integer | configuration file | 1 | 10 | | 10 | 1 | f + pg_stat_monitor.pgsm_max_buckets | 1 | | postmaster | integer | configuration file | 1 | 20000 | | 10 | 1 | f (1 row) SELECT pg_stat_monitor_reset(); @@ -20,7 +20,7 @@ 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='pg_stat_monitor.pgsm_max_buckets'; name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart ----------------------------------+---------+------+------------+---------+--------------------+---------+---------+----------+----------+-----------+----------------- - pg_stat_monitor.pgsm_max_buckets | 2 | | postmaster | integer | configuration file | 1 | 10 | | 10 | 2 | f + pg_stat_monitor.pgsm_max_buckets | 2 | | postmaster | integer | configuration file | 1 | 20000 | | 10 | 2 | f (1 row) SELECT pg_stat_monitor_reset(); @@ -32,7 +32,7 @@ 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='pg_stat_monitor.pgsm_max_buckets'; name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart ----------------------------------+---------+------+------------+---------+--------------------+---------+---------+----------+----------+-----------+----------------- - pg_stat_monitor.pgsm_max_buckets | 5 | | postmaster | integer | configuration file | 1 | 10 | | 10 | 5 | f + pg_stat_monitor.pgsm_max_buckets | 5 | | postmaster | integer | configuration file | 1 | 20000 | | 10 | 5 | f (1 row) SELECT pg_stat_monitor_reset(); @@ -44,7 +44,19 @@ 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='pg_stat_monitor.pgsm_max_buckets'; name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart ----------------------------------+---------+------+------------+---------+--------------------+---------+---------+----------+----------+-----------+----------------- - pg_stat_monitor.pgsm_max_buckets | 10 | | postmaster | integer | configuration file | 1 | 10 | | 10 | 10 | f + pg_stat_monitor.pgsm_max_buckets | 10 | | postmaster | integer | configuration file | 1 | 20000 | | 10 | 10 | f +(1 row) + +SELECT pg_stat_monitor_reset(); + pg_stat_monitor_reset +----------------------- + +(1 row) + +SELECT name, setting, unit, context, vartype, source, min_val, max_val, enumvals, boot_val, reset_val, pending_restart FROM pg_settings WHERE name='pg_stat_monitor.pgsm_max_buckets'; + name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart +----------------------------------+---------+------+------------+---------+--------------------+---------+---------+----------+----------+-----------+----------------- + pg_stat_monitor.pgsm_max_buckets | 11 | | postmaster | integer | configuration file | 1 | 20000 | | 10 | 11 | f (1 row) SELECT pg_stat_monitor_reset(); @@ -56,19 +68,7 @@ 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='pg_stat_monitor.pgsm_max_buckets'; name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart ----------------------------------+---------+------+------------+---------+---------+---------+---------+----------+----------+-----------+----------------- - pg_stat_monitor.pgsm_max_buckets | 10 | | postmaster | integer | default | 1 | 10 | | 10 | 10 | f -(1 row) - -SELECT pg_stat_monitor_reset(); - pg_stat_monitor_reset ------------------------ - -(1 row) - -SELECT name, setting, unit, context, vartype, source, min_val, max_val, enumvals, boot_val, reset_val, pending_restart FROM pg_settings WHERE name='pg_stat_monitor.pgsm_max_buckets'; - name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart -----------------------------------+---------+------+------------+---------+---------+---------+---------+----------+----------+-----------+----------------- - pg_stat_monitor.pgsm_max_buckets | 10 | | postmaster | integer | default | 1 | 10 | | 10 | 10 | f + pg_stat_monitor.pgsm_max_buckets | 10 | | postmaster | integer | default | 1 | 20000 | | 10 | 10 | f (1 row) DROP EXTENSION pg_stat_monitor; diff --git a/t/expected/016_settings_pgsm_max.out b/t/expected/016_settings_pgsm_max.out index 4f7e090..111ad5f 100644 --- a/t/expected/016_settings_pgsm_max.out +++ b/t/expected/016_settings_pgsm_max.out @@ -8,7 +8,7 @@ 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='pg_stat_monitor.pgsm_max'; name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart --------------------------+---------+------+------------+---------+--------------------+---------+---------+----------+----------+-----------+----------------- - pg_stat_monitor.pgsm_max | 1000 | MB | postmaster | integer | configuration file | 1 | 1000 | | 100 | 1000 | f + pg_stat_monitor.pgsm_max | 1000 | MB | postmaster | integer | configuration file | 10 | 10240 | | 256 | 1000 | f (1 row) SELECT pg_stat_monitor_reset(); @@ -20,7 +20,7 @@ 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='pg_stat_monitor.pgsm_max'; name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart --------------------------+---------+------+------------+---------+--------------------+---------+---------+----------+----------+-----------+----------------- - pg_stat_monitor.pgsm_max | 500 | MB | postmaster | integer | configuration file | 1 | 1000 | | 100 | 500 | f + pg_stat_monitor.pgsm_max | 500 | MB | postmaster | integer | configuration file | 10 | 10240 | | 256 | 500 | f (1 row) SELECT pg_stat_monitor_reset(); @@ -32,7 +32,7 @@ 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='pg_stat_monitor.pgsm_max'; name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart --------------------------+---------+------+------------+---------+--------------------+---------+---------+----------+----------+-----------+----------------- - pg_stat_monitor.pgsm_max | 100 | MB | postmaster | integer | configuration file | 1 | 1000 | | 100 | 100 | f + pg_stat_monitor.pgsm_max | 100 | MB | postmaster | integer | configuration file | 10 | 10240 | | 256 | 100 | f (1 row) SELECT pg_stat_monitor_reset(); @@ -44,7 +44,7 @@ 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='pg_stat_monitor.pgsm_max'; name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart --------------------------+---------+------+------------+---------+--------------------+---------+---------+----------+----------+-----------+----------------- - pg_stat_monitor.pgsm_max | 10 | MB | postmaster | integer | configuration file | 1 | 1000 | | 100 | 10 | f + pg_stat_monitor.pgsm_max | 10 | MB | postmaster | integer | configuration file | 10 | 10240 | | 256 | 10 | f (1 row) SELECT pg_stat_monitor_reset(); @@ -56,7 +56,7 @@ 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='pg_stat_monitor.pgsm_max'; name | setting | unit | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | pending_restart --------------------------+---------+------+------------+---------+---------+---------+---------+----------+----------+-----------+----------------- - pg_stat_monitor.pgsm_max | 100 | MB | postmaster | integer | default | 1 | 1000 | | 100 | 100 | f + pg_stat_monitor.pgsm_max | 256 | MB | postmaster | integer | default | 10 | 10240 | | 256 | 256 | f (1 row) DROP EXTENSION pg_stat_monitor;