diff --git a/src/backend/distributed/shared_library_init.c b/src/backend/distributed/shared_library_init.c index 4e0f9d0de..c8927a1c4 100644 --- a/src/backend/distributed/shared_library_init.c +++ b/src/backend/distributed/shared_library_init.c @@ -2402,7 +2402,6 @@ RegisterCitusConfigVariables(void) PGC_POSTMASTER, GUC_STANDARD, NULL, NULL, NULL); - DefineCustomEnumVariable( "citus.stat_tenants_log_level", gettext_noop("Sets the level of citus_stat_tenants log messages"), @@ -2424,6 +2423,17 @@ RegisterCitusConfigVariables(void) GUC_STANDARD, NULL, NULL, NULL); + + DefineCustomIntVariable( + "citus.stat_tenants_sample_rate_for_new_tenants", + gettext_noop("Sampling rate for new tenants in citus_stat_tenants."), + NULL, + &StatTenantsSampleRateForNewTenants, + 100, 1, 100, + PGC_USERSET, + GUC_STANDARD, + NULL, NULL, NULL); + DefineCustomEnumVariable( "citus.stat_tenants_track", gettext_noop("Enables/Disables the stats collection for citus_stat_tenants."), diff --git a/src/backend/distributed/utils/citus_stat_tenants.c b/src/backend/distributed/utils/citus_stat_tenants.c index 380e5e116..d5578a4fd 100644 --- a/src/backend/distributed/utils/citus_stat_tenants.c +++ b/src/backend/distributed/utils/citus_stat_tenants.c @@ -77,7 +77,7 @@ int StatTenantsLogLevel = CITUS_LOG_LEVEL_OFF; int StatTenantsPeriod = (time_t) 60; int StatTenantsLimit = 100; int StatTenantsTrack = STAT_TENANTS_TRACK_NONE; - +int StatTenantsSampleRateForNewTenants = 100; PG_FUNCTION_INFO_V1(citus_stat_tenants_local); PG_FUNCTION_INFO_V1(citus_stat_tenants_local_reset); @@ -177,8 +177,6 @@ citus_stat_tenants_local(PG_FUNCTION_ARGS) } -#include "miscadmin.h" - /* * citus_stat_tenants_local_reset resets monitor for tenant statistics * on the local node. @@ -186,9 +184,6 @@ citus_stat_tenants_local(PG_FUNCTION_ARGS) Datum citus_stat_tenants_local_reset(PG_FUNCTION_ARGS) { - /* ereport(NOTICE, (errmsg("MyProcPid: %d", MyProcPid))); */ - /*sleep(10); */ - MultiTenantMonitor *monitor = GetMultiTenantMonitor(); /* if monitor is not created yet, there is nothing to reset */ @@ -268,6 +263,21 @@ AttributeTask(char *tenantId, int colocationId, CmdType commandType) return; } + TenantStatsHashKey key = { 0 }; + FillTenantStatsHashKey(&key, tenantId, colocationId); + + MultiTenantMonitor *monitor = GetMultiTenantMonitor(); + bool found = false; + hash_search(monitor->tenants, &key, HASH_FIND, NULL); + + int randomValue = rand() % 100; + + /* If the tenant is not found in the hash table, we will track the query with a probability of StatTenantsSampleRateForNewTenants.*/ + if (!(!found && randomValue < StatTenantsSampleRateForNewTenants)) + { + return; + } + AttributeToColocationGroupId = colocationId; strncpy_s(AttributeToTenant, MAX_TENANT_ATTRIBUTE_LENGTH, tenantId, MAX_TENANT_ATTRIBUTE_LENGTH - 1); diff --git a/src/include/distributed/utils/citus_stat_tenants.h b/src/include/distributed/utils/citus_stat_tenants.h index 6acbd5de7..8dac393a8 100644 --- a/src/include/distributed/utils/citus_stat_tenants.h +++ b/src/include/distributed/utils/citus_stat_tenants.h @@ -121,5 +121,6 @@ extern int StatTenantsLogLevel; extern int StatTenantsPeriod; extern int StatTenantsLimit; extern int StatTenantsTrack; +extern int StatTenantsSampleRateForNewTenants; #endif /*CITUS_ATTRIBUTE_H */