From 5e9e67e5bf5e9d7cd637a563bb4b4d3efb50b316 Mon Sep 17 00:00:00 2001 From: Halil Ozan Akgul Date: Tue, 14 Mar 2023 15:52:36 +0300 Subject: [PATCH] Include update and delete queries --- .../planner/multi_router_planner.c | 16 ++++++- src/backend/distributed/utils/attribute.c | 48 ++++++++++--------- src/include/distributed/utils/attribute.h | 16 +++---- .../regress/expected/citus_stats_tenants.out | 6 +-- src/test/regress/sql/citus_stats_tenants.sql | 6 +-- 5 files changed, 54 insertions(+), 38 deletions(-) diff --git a/src/backend/distributed/planner/multi_router_planner.c b/src/backend/distributed/planner/multi_router_planner.c index 7e596bfe5..e2776cc9a 100644 --- a/src/backend/distributed/planner/multi_router_planner.c +++ b/src/backend/distributed/planner/multi_router_planner.c @@ -1997,11 +1997,25 @@ GenerateSingleShardRouterTaskList(Job *job, List *relationShardList, } else { + Datum partitionColumnValue; + Oid partitionColumnType = 0; + char *partitionColumnString = NULL; + if (job->partitionKeyValue != NULL) + { + partitionColumnValue = job->partitionKeyValue->constvalue; + partitionColumnType = job->partitionKeyValue->consttype; + partitionColumnString = DatumToString(partitionColumnValue, + partitionColumnType); + } + + SetJobColocationId(job); + job->taskList = SingleShardTaskList(originalQuery, job->jobId, relationShardList, placementList, shardId, job->parametersInJobQueryResolved, - isLocalTableModification, "", -1); + isLocalTableModification, + partitionColumnString, job->colocationId); } } diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index 3bedf4ec4..cb6f93807 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -127,12 +127,12 @@ citus_stats_tenants(PG_FUNCTION_ARGS) values[0] = Int32GetDatum(tenantStats->colocationGroupId); values[1] = PointerGetDatum(cstring_to_text(tenantStats->tenantAttribute)); - values[2] = Int32GetDatum(tenantStats->selectsInThisPeriod); - values[3] = Int32GetDatum(tenantStats->selectsInLastPeriod); - values[4] = Int32GetDatum(tenantStats->selectsInThisPeriod + - tenantStats->insertsInThisPeriod); - values[5] = Int32GetDatum(tenantStats->selectsInLastPeriod + - tenantStats->insertsInLastPeriod); + values[2] = Int32GetDatum(tenantStats->readsInThisPeriod); + values[3] = Int32GetDatum(tenantStats->readsInLastPeriod); + values[4] = Int32GetDatum(tenantStats->readsInThisPeriod + + tenantStats->writesInThisPeriod); + values[5] = Int32GetDatum(tenantStats->readsInLastPeriod + + tenantStats->writesInLastPeriod); values[6] = Int64GetDatum(tenantStats->score); tuplestore_putvalues(tupleStore, tupleDescriptor, values, isNulls); @@ -347,15 +347,17 @@ AttributeMetricsIfApplicable() if (attributeCommandType == CMD_SELECT) { - tenantStats->selectCount++; - tenantStats->selectsInThisPeriod++; - tenantStats->totalSelectTime += cpu_time_used; + tenantStats->readCount++; + tenantStats->readsInThisPeriod++; + tenantStats->totalReadTime += cpu_time_used; } - else if (attributeCommandType == CMD_INSERT) + else if (attributeCommandType == CMD_UPDATE || + attributeCommandType == CMD_INSERT || + attributeCommandType == CMD_DELETE) { - tenantStats->insertCount++; - tenantStats->insertsInThisPeriod++; - tenantStats->totalInsertTime += cpu_time_used; + tenantStats->writeCount++; + tenantStats->writesInThisPeriod++; + tenantStats->totalWriteTime += cpu_time_used; } LWLockRelease(&monitor->lock); @@ -376,10 +378,10 @@ AttributeMetricsIfApplicable() if (MultiTenantMonitoringLogLevel != CITUS_LOG_LEVEL_OFF) { - ereport(NOTICE, (errmsg("total select count = %d, total CPU time = %f " + ereport(NOTICE, (errmsg("total read count = %d, total read CPU time = %f " "to tenant: %s", - tenantStats->selectCount, - tenantStats->totalSelectTime, + tenantStats->readCount, + tenantStats->totalReadTime, tenantStats->tenantAttribute))); } } @@ -405,13 +407,13 @@ UpdatePeriodsIfNecessary(MultiTenantMonitor *monitor, TenantStats *tenantStats) * but there are some query count for this period we move them to the last period. */ if (tenantStats->lastQueryTime < monitor->periodStart && - (tenantStats->insertsInThisPeriod || tenantStats->selectsInThisPeriod)) + (tenantStats->writesInThisPeriod || tenantStats->readsInThisPeriod)) { - tenantStats->insertsInLastPeriod = tenantStats->insertsInThisPeriod; - tenantStats->insertsInThisPeriod = 0; + tenantStats->writesInLastPeriod = tenantStats->writesInThisPeriod; + tenantStats->writesInThisPeriod = 0; - tenantStats->selectsInLastPeriod = tenantStats->selectsInThisPeriod; - tenantStats->selectsInThisPeriod = 0; + tenantStats->readsInLastPeriod = tenantStats->readsInThisPeriod; + tenantStats->readsInThisPeriod = 0; } /* @@ -419,9 +421,9 @@ UpdatePeriodsIfNecessary(MultiTenantMonitor *monitor, TenantStats *tenantStats) */ if (tenantStats->lastQueryTime < monitor->periodStart - CitusStatsTenantsPeriod) { - tenantStats->insertsInLastPeriod = 0; + tenantStats->writesInLastPeriod = 0; - tenantStats->selectsInLastPeriod = 0; + tenantStats->readsInLastPeriod = 0; } } diff --git a/src/include/distributed/utils/attribute.h b/src/include/distributed/utils/attribute.h index b196158b0..c05ec950e 100644 --- a/src/include/distributed/utils/attribute.h +++ b/src/include/distributed/utils/attribute.h @@ -21,15 +21,15 @@ typedef struct TenantStats int colocationGroupId; - int selectCount; - double totalSelectTime; - int selectsInLastPeriod; - int selectsInThisPeriod; + int readCount; + double totalReadTime; + int readsInLastPeriod; + int readsInThisPeriod; - int insertCount; - double totalInsertTime; - int insertsInLastPeriod; - int insertsInThisPeriod; + int writeCount; + double totalWriteTime; + int writesInLastPeriod; + int writesInThisPeriod; time_t lastQueryTime; diff --git a/src/test/regress/expected/citus_stats_tenants.out b/src/test/regress/expected/citus_stats_tenants.out index 0186bbe16..5856bd819 100644 --- a/src/test/regress/expected/citus_stats_tenants.out +++ b/src/test/regress/expected/citus_stats_tenants.out @@ -44,9 +44,9 @@ SELECT create_reference_table('ref_tbl'); INSERT INTO dist_tbl VALUES (1, 'abcd'); INSERT INTO dist_tbl VALUES (2, 'abcd'); -INSERT INTO dist_tbl VALUES (3, 'abcd'); -INSERT INTO dist_tbl VALUES (4, 'abcd'); -INSERT INTO dist_tbl VALUES (5, 'abcd'); +UPDATE dist_tbl SET b = a + 1 WHERE a = 3; +UPDATE dist_tbl SET b = a + 1 WHERE a = 4; +DELETE FROM dist_tbl WHERE a = 5; \c - - - :worker_1_port SELECT tenant_attribute, read_count_in_this_period, read_count_in_last_period, query_count_in_this_period, query_count_in_last_period FROM citus_stats_tenants ORDER BY tenant_attribute; tenant_attribute | read_count_in_this_period | read_count_in_last_period | query_count_in_this_period | query_count_in_last_period diff --git a/src/test/regress/sql/citus_stats_tenants.sql b/src/test/regress/sql/citus_stats_tenants.sql index c6046984f..7c7e1ed4c 100644 --- a/src/test/regress/sql/citus_stats_tenants.sql +++ b/src/test/regress/sql/citus_stats_tenants.sql @@ -24,9 +24,9 @@ SELECT create_reference_table('ref_tbl'); INSERT INTO dist_tbl VALUES (1, 'abcd'); INSERT INTO dist_tbl VALUES (2, 'abcd'); -INSERT INTO dist_tbl VALUES (3, 'abcd'); -INSERT INTO dist_tbl VALUES (4, 'abcd'); -INSERT INTO dist_tbl VALUES (5, 'abcd'); +UPDATE dist_tbl SET b = a + 1 WHERE a = 3; +UPDATE dist_tbl SET b = a + 1 WHERE a = 4; +DELETE FROM dist_tbl WHERE a = 5; \c - - - :worker_1_port SELECT tenant_attribute, read_count_in_this_period, read_count_in_last_period, query_count_in_this_period, query_count_in_last_period FROM citus_stats_tenants ORDER BY tenant_attribute;