From 695f60e84990547da62ac8f521c33f61e04039e9 Mon Sep 17 00:00:00 2001 From: Halil Ozan Akgul Date: Wed, 15 Mar 2023 09:55:25 +0300 Subject: [PATCH] Add logs to citus_stats_tenants --- .../sql/udfs/citus_stats_tenants/11.3-1.sql | 8 ++++ .../sql/udfs/citus_stats_tenants/latest.sql | 8 ++++ src/backend/distributed/utils/attribute.c | 18 --------- .../regress/expected/citus_stats_tenants.out | 40 +++++++++++++++++++ src/test/regress/sql/citus_stats_tenants.sql | 12 ++++++ 5 files changed, 68 insertions(+), 18 deletions(-) diff --git a/src/backend/distributed/sql/udfs/citus_stats_tenants/11.3-1.sql b/src/backend/distributed/sql/udfs/citus_stats_tenants/11.3-1.sql index d85f90d66d6..be42cc51291 100644 --- a/src/backend/distributed/sql/udfs/citus_stats_tenants/11.3-1.sql +++ b/src/backend/distributed/sql/udfs/citus_stats_tenants/11.3-1.sql @@ -14,6 +14,14 @@ CREATE OR REPLACE FUNCTION pg_catalog.citus_stats_tenants ( LANGUAGE plpgsql AS $function$ BEGIN + IF + array_position(enumvals, 'log') >= array_position(enumvals, setting) + AND setting != 'off' + FROM pg_settings + WHERE name = 'citus.multi_tenant_monitoring_log_level' + THEN + RAISE LOG 'Generating citus_stats_tenants'; + END IF; RETURN QUERY SELECT * FROM jsonb_to_recordset(( diff --git a/src/backend/distributed/sql/udfs/citus_stats_tenants/latest.sql b/src/backend/distributed/sql/udfs/citus_stats_tenants/latest.sql index d85f90d66d6..be42cc51291 100644 --- a/src/backend/distributed/sql/udfs/citus_stats_tenants/latest.sql +++ b/src/backend/distributed/sql/udfs/citus_stats_tenants/latest.sql @@ -14,6 +14,14 @@ CREATE OR REPLACE FUNCTION pg_catalog.citus_stats_tenants ( LANGUAGE plpgsql AS $function$ BEGIN + IF + array_position(enumvals, 'log') >= array_position(enumvals, setting) + AND setting != 'off' + FROM pg_settings + WHERE name = 'citus.multi_tenant_monitoring_log_level' + THEN + RAISE LOG 'Generating citus_stats_tenants'; + END IF; RETURN QUERY SELECT * FROM jsonb_to_recordset(( diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index 19d290cfdbc..5c482bcd982 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -40,7 +40,6 @@ ExecutorEnd_hook_type prev_ExecutorEnd = NULL; char attributeToTenant[MAX_TENANT_ATTRIBUTE_LENGTH] = ""; CmdType attributeCommandType = CMD_UNKNOWN; int colocationGroupId = -1; -clock_t attributeToTenantStart = { 0 }; const char *SharedMemoryNameForMultiTenantMonitor = "Shared memory for multi tenant monitor"; @@ -238,14 +237,7 @@ AttributeQueryIfAnnotated(const char *query_string, CmdType commandType) tenantId = pstrdup(tenantId); *tenantEnd = tenantEndTmp; - if (MultiTenantMonitoringLogLevel != CITUS_LOG_LEVEL_OFF) - { - ereport(NOTICE, (errmsg("attributing query to tenant: %s", - quote_literal_cstr(tenantId)))); - } - strcpy_s(attributeToTenant, sizeof(attributeToTenant), tenantId); - attributeToTenantStart = clock(); } else { @@ -330,17 +322,7 @@ AttributeMetricsIfApplicable() { if (strcmp(attributeToTenant, "") != 0) { - clock_t end = { 0 }; - - end = clock(); time_t queryTime = time(0); - double cpu_time_used = ((double) (end - attributeToTenantStart)) / CLOCKS_PER_SEC; - - if (MultiTenantMonitoringLogLevel != CITUS_LOG_LEVEL_OFF) - { - ereport(NOTICE, (errmsg("attribute cpu counter (%f) to tenant: %s", - cpu_time_used, attributeToTenant))); - } MultiTenantMonitor *monitor = GetMultiTenantMonitor(); diff --git a/src/test/regress/expected/citus_stats_tenants.out b/src/test/regress/expected/citus_stats_tenants.out index 783f38240c8..e4705eb241d 100644 --- a/src/test/regress/expected/citus_stats_tenants.out +++ b/src/test/regress/expected/citus_stats_tenants.out @@ -264,5 +264,45 @@ SELECT tenant_attribute, read_count_in_this_period, read_count_in_last_period, q \c - - - :master_port SET search_path TO citus_stats_tenants; +-- test logs +SET client_min_messages TO LOG; +SELECT count(*)>=0 FROM citus_stats_tenants; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +SET citus.multi_tenant_monitoring_log_level TO ERROR; +SELECT count(*)>=0 FROM citus_stats_tenants; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +SET citus.multi_tenant_monitoring_log_level TO OFF; +SELECT count(*)>=0 FROM citus_stats_tenants; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +SET citus.multi_tenant_monitoring_log_level TO LOG; +SELECT count(*)>=0 FROM citus_stats_tenants; +LOG: Generating citus_stats_tenants +CONTEXT: PL/pgSQL function citus_stats_tenants(boolean) line XX at RAISE + ?column? +--------------------------------------------------------------------- + t +(1 row) + +SET citus.multi_tenant_monitoring_log_level TO DEBUG; +SELECT count(*)>=0 FROM citus_stats_tenants; +LOG: Generating citus_stats_tenants +CONTEXT: PL/pgSQL function citus_stats_tenants(boolean) line XX at RAISE + ?column? +--------------------------------------------------------------------- + t +(1 row) + SET client_min_messages TO ERROR; DROP SCHEMA citus_stats_tenants CASCADE; diff --git a/src/test/regress/sql/citus_stats_tenants.sql b/src/test/regress/sql/citus_stats_tenants.sql index 981533a6e58..b895a842368 100644 --- a/src/test/regress/sql/citus_stats_tenants.sql +++ b/src/test/regress/sql/citus_stats_tenants.sql @@ -96,5 +96,17 @@ SELECT tenant_attribute, read_count_in_this_period, read_count_in_last_period, q \c - - - :master_port SET search_path TO citus_stats_tenants; +-- test logs +SET client_min_messages TO LOG; +SELECT count(*)>=0 FROM citus_stats_tenants; +SET citus.multi_tenant_monitoring_log_level TO ERROR; +SELECT count(*)>=0 FROM citus_stats_tenants; +SET citus.multi_tenant_monitoring_log_level TO OFF; +SELECT count(*)>=0 FROM citus_stats_tenants; +SET citus.multi_tenant_monitoring_log_level TO LOG; +SELECT count(*)>=0 FROM citus_stats_tenants; +SET citus.multi_tenant_monitoring_log_level TO DEBUG; +SELECT count(*)>=0 FROM citus_stats_tenants; + SET client_min_messages TO ERROR; DROP SCHEMA citus_stats_tenants CASCADE;