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/sql/citus--11.2-1--11.3-1.sql b/src/backend/distributed/sql/citus--11.2-1--11.3-1.sql index 6fbeede70..73a2bf8a9 100644 --- a/src/backend/distributed/sql/citus--11.2-1--11.3-1.sql +++ b/src/backend/distributed/sql/citus--11.2-1--11.3-1.sql @@ -3,4 +3,3 @@ -- bump version to 11.3-1 #include "udfs/citus_stats_tenants/11.3-1.sql" -#include "udfs/citus_stats_tenants_storage/11.3-1.sql" diff --git a/src/backend/distributed/sql/downgrades/citus--11.3-1--11.2-1.sql b/src/backend/distributed/sql/downgrades/citus--11.3-1--11.2-1.sql index 855b4f9d1..4bf4c75ff 100644 --- a/src/backend/distributed/sql/downgrades/citus--11.3-1--11.2-1.sql +++ b/src/backend/distributed/sql/downgrades/citus--11.3-1--11.2-1.sql @@ -2,7 +2,4 @@ -- this is an empty downgrade path since citus--11.2-1--11.3-1.sql is empty for now DROP VIEW pg_catalog.citus_stats_tenants; -DROP VIEW pg_catalog.citus_stats_tenants_storage; - DROP FUNCTION pg_catalog.citus_stats_tenants(boolean); -DROP FUNCTION pg_catalog.citus_stats_tenants_storage; diff --git a/src/backend/distributed/sql/udfs/citus_stats_tenants_storage/11.3-1.sql b/src/backend/distributed/sql/udfs/citus_stats_tenants_storage/11.3-1.sql deleted file mode 100644 index c1604a7a1..000000000 --- a/src/backend/distributed/sql/udfs/citus_stats_tenants_storage/11.3-1.sql +++ /dev/null @@ -1,28 +0,0 @@ -CREATE OR REPLACE FUNCTION pg_catalog.citus_stats_tenants_storage ( - OUT colocation_id INT, - OUT tenant_attribute TEXT, - OUT storage_estimate INT -) -RETURNS SETOF record -LANGUAGE plpgsql -AS $function$ -DECLARE -tn TEXT; -dc TEXT; -ci INT; -BEGIN - FOR ci, tn, dc IN SELECT cts.colocation_id, cts.table_name, cts.distribution_column FROM citus_tables cts - LOOP - RETURN QUERY - EXECUTE 'SELECT ' || ci || '::int, ' || dc || '::text, sum(pg_column_size(' || tn || '.*))::int FROM ' || tn || ' GROUP BY ' || dc; - END LOOP; -END; -$function$; - -CREATE OR REPLACE VIEW citus.citus_stats_tenants_storage AS -SELECT colocation_id, tenant_attribute, sum(storage_estimate) total_storage FROM pg_catalog.citus_stats_tenants_storage() -GROUP BY colocation_id, tenant_attribute -ORDER BY total_storage DESC; - -ALTER VIEW citus.citus_stats_tenants_storage SET SCHEMA pg_catalog; -GRANT SELECT ON pg_catalog.citus_stats_tenants_storage TO PUBLIC; diff --git a/src/backend/distributed/sql/udfs/citus_stats_tenants_storage/latest.sql b/src/backend/distributed/sql/udfs/citus_stats_tenants_storage/latest.sql deleted file mode 100644 index c1604a7a1..000000000 --- a/src/backend/distributed/sql/udfs/citus_stats_tenants_storage/latest.sql +++ /dev/null @@ -1,28 +0,0 @@ -CREATE OR REPLACE FUNCTION pg_catalog.citus_stats_tenants_storage ( - OUT colocation_id INT, - OUT tenant_attribute TEXT, - OUT storage_estimate INT -) -RETURNS SETOF record -LANGUAGE plpgsql -AS $function$ -DECLARE -tn TEXT; -dc TEXT; -ci INT; -BEGIN - FOR ci, tn, dc IN SELECT cts.colocation_id, cts.table_name, cts.distribution_column FROM citus_tables cts - LOOP - RETURN QUERY - EXECUTE 'SELECT ' || ci || '::int, ' || dc || '::text, sum(pg_column_size(' || tn || '.*))::int FROM ' || tn || ' GROUP BY ' || dc; - END LOOP; -END; -$function$; - -CREATE OR REPLACE VIEW citus.citus_stats_tenants_storage AS -SELECT colocation_id, tenant_attribute, sum(storage_estimate) total_storage FROM pg_catalog.citus_stats_tenants_storage() -GROUP BY colocation_id, tenant_attribute -ORDER BY total_storage DESC; - -ALTER VIEW citus.citus_stats_tenants_storage SET SCHEMA pg_catalog; -GRANT SELECT ON pg_catalog.citus_stats_tenants_storage TO PUBLIC; diff --git a/src/backend/distributed/utils/attribute.c b/src/backend/distributed/utils/attribute.c index d95b87082..32d008848 100644 --- a/src/backend/distributed/utils/attribute.c +++ b/src/backend/distributed/utils/attribute.c @@ -72,6 +72,7 @@ int CitusStatsTenantsLimit = 10; PG_FUNCTION_INFO_V1(citus_stats_tenants); +PG_FUNCTION_INFO_V1(clean_citus_stats_tenants); /* @@ -132,12 +133,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); @@ -149,12 +150,28 @@ citus_stats_tenants(PG_FUNCTION_ARGS) } +/* + * clean_citus_stats_tenants cleans the citus_stats_tenants monitor. + */ +Datum +clean_citus_stats_tenants(PG_FUNCTION_ARGS) +{ + MultiTenantMonitor *monitor = GetMultiTenantMonitor(); + monitor->tenantCount = 0; + monitor->periodStart = time(0); + + PG_RETURN_VOID(); +} + + /* * AttributeQueryIfAnnotated assigns the attributes of tenant if the query is annotated. */ void AttributeQueryIfAnnotated(const char *query_string, CmdType commandType) { + strcpy_s(attributeToTenant, sizeof(attributeToTenant), ""); + attributeCommandType = commandType; if (query_string == NULL) @@ -313,15 +330,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); @@ -342,15 +361,15 @@ 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))); } } - /*attributeToTenant = NULL; */ + strcpy_s(attributeToTenant, sizeof(attributeToTenant), ""); } @@ -371,13 +390,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; } /* @@ -385,9 +404,9 @@ UpdatePeriodsIfNecessary(MultiTenantMonitor *monitor, TenantStats *tenantStats) */ if (tenantStats->lastQueryTime < monitor->periodStart - CitusStatsTenantsPeriod) { - tenantStats->insertsInLastPeriod = 0; + tenantStats->writesInLastPeriod = 0; - tenantStats->selectsInLastPeriod = 0; + tenantStats->readsInLastPeriod = 0; } } @@ -531,6 +550,8 @@ CreateTenantStats(MultiTenantMonitor *monitor) { int tenantIndex = monitor->tenantCount; + memset(&monitor->tenants[tenantIndex], 0, sizeof(monitor->tenants[tenantIndex])); + strcpy_s(monitor->tenants[tenantIndex].tenantAttribute, sizeof(monitor->tenants[tenantIndex].tenantAttribute), attributeToTenant); monitor->tenants[tenantIndex].colocationGroupId = colocationGroupId; diff --git a/src/include/distributed/utils/attribute.h b/src/include/distributed/utils/attribute.h index 7f7e14d50..c05ec950e 100644 --- a/src/include/distributed/utils/attribute.h +++ b/src/include/distributed/utils/attribute.h @@ -21,21 +21,20 @@ 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; long long score; time_t lastScoreReduction; - int rank; NamedLWLockTranche namedLockTranche; LWLock lock; diff --git a/src/test/regress/expected/citus_stats_tenants.out b/src/test/regress/expected/citus_stats_tenants.out new file mode 100644 index 000000000..5856bd819 --- /dev/null +++ b/src/test/regress/expected/citus_stats_tenants.out @@ -0,0 +1,298 @@ +CREATE SCHEMA citus_stats_tenants; +SET search_path TO citus_stats_tenants; +SET citus.next_shard_id TO 5797500; +SET citus.shard_replication_factor TO 1; +CREATE OR REPLACE FUNCTION pg_catalog.clean_citus_stats_tenants() +RETURNS VOID +LANGUAGE C +AS 'citus', $$clean_citus_stats_tenants$$; +SELECT result FROM run_command_on_all_nodes('SELECT clean_citus_stats_tenants()'); + result +--------------------------------------------------------------------- + + + +(3 rows) + +CREATE TABLE dist_tbl (a INT, b TEXT); +SELECT create_distributed_table('dist_tbl', 'a', shard_count:=4, colocate_with:='none'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +CREATE TABLE dist_tbl_2 (a INT, b INT); +SELECT create_distributed_table('dist_tbl_2', 'a', colocate_with:='dist_tbl'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +CREATE TABLE dist_tbl_text (a TEXT, b INT); +SELECT create_distributed_table('dist_tbl_text', 'a', shard_count:=4, colocate_with:='none'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +CREATE TABLE ref_tbl (a INT, b INT); +SELECT create_reference_table('ref_tbl'); + create_reference_table +--------------------------------------------------------------------- + +(1 row) + +INSERT INTO dist_tbl VALUES (1, 'abcd'); +INSERT INTO dist_tbl VALUES (2, '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 +--------------------------------------------------------------------- + 1 | 0 | 0 | 1 | 0 + 5 | 0 | 0 | 1 | 0 +(2 rows) + +\c - - - :worker_2_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 +--------------------------------------------------------------------- + 2 | 0 | 0 | 1 | 0 + 3 | 0 | 0 | 1 | 0 +(2 rows) + +\c - - - :master_port +SET search_path TO citus_stats_tenants; +SELECT result FROM run_command_on_all_nodes('ALTER SYSTEM SET citus.stats_tenants_period TO 3'); + result +--------------------------------------------------------------------- + ALTER SYSTEM + ALTER SYSTEM + ALTER SYSTEM +(3 rows) + +SELECT result FROM run_command_on_all_nodes('SELECT pg_reload_conf()'); + result +--------------------------------------------------------------------- + t + t + t +(3 rows) + +SELECT count(*)>=0 FROM dist_tbl WHERE a = 1; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +SELECT count(*)>=0 FROM dist_tbl WHERE a = 2; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +\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 +--------------------------------------------------------------------- + 1 | 1 | 0 | 2 | 0 + 5 | 0 | 0 | 1 | 0 +(2 rows) + +\c - - - :worker_2_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 +--------------------------------------------------------------------- + 2 | 1 | 0 | 2 | 0 + 3 | 0 | 0 | 1 | 0 +(2 rows) + +\c - - - :master_port +SELECT pg_sleep (3); + pg_sleep +--------------------------------------------------------------------- + +(1 row) + +\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 +--------------------------------------------------------------------- + 1 | 0 | 1 | 0 | 2 + 5 | 0 | 0 | 0 | 1 +(2 rows) + +\c - - - :worker_2_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 +--------------------------------------------------------------------- + 2 | 0 | 1 | 0 | 2 + 3 | 0 | 0 | 0 | 1 +(2 rows) + +\c - - - :master_port +SET search_path TO citus_stats_tenants; +SELECT result FROM run_command_on_all_nodes('ALTER SYSTEM SET citus.stats_tenants_period TO 60'); + result +--------------------------------------------------------------------- + ALTER SYSTEM + ALTER SYSTEM + ALTER SYSTEM +(3 rows) + +SELECT result FROM run_command_on_all_nodes('SELECT pg_reload_conf()'); + result +--------------------------------------------------------------------- + t + t + t +(3 rows) + +-- queries with multiple tenants should not be counted +SELECT count(*)>=0 FROM dist_tbl WHERE a IN (1, 5); + ?column? +--------------------------------------------------------------------- + t +(1 row) + +-- queries with reference tables should not be counted +SELECT count(*)>=0 FROM ref_tbl WHERE a = 1; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +\c - - - :worker_1_port +SELECT tenant_attribute, query_count_in_this_period FROM citus_stats_tenants ORDER BY tenant_attribute; + tenant_attribute | query_count_in_this_period +--------------------------------------------------------------------- + 1 | 0 + 5 | 0 +(2 rows) + +\c - - - :master_port +SET search_path TO citus_stats_tenants; +-- queries with multiple tables but one tenant should be counted +SELECT count(*)>=0 FROM dist_tbl, dist_tbl_2 WHERE dist_tbl.a = 1 AND dist_tbl_2.a = 1; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +SELECT count(*)>=0 FROM dist_tbl JOIN dist_tbl_2 ON dist_tbl.a = dist_tbl_2.a WHERE dist_tbl.a = 1; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +\c - - - :worker_1_port +SELECT tenant_attribute, query_count_in_this_period FROM citus_stats_tenants WHERE tenant_attribute = '1'; + tenant_attribute | query_count_in_this_period +--------------------------------------------------------------------- + 1 | 2 +(1 row) + +\c - - - :master_port +SET search_path TO citus_stats_tenants; +-- test scoring +-- all of these distribution column values are from second worker +SELECT count(*)>=0 FROM dist_tbl WHERE a = 2; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +SELECT count(*)>=0 FROM dist_tbl WHERE a = 3; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +SELECT count(*)>=0 FROM dist_tbl WHERE a = 4; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'abcd'; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +\c - - - :worker_2_port +SELECT tenant_attribute, query_count_in_this_period, score FROM citus_stats_tenants(true) ORDER BY score DESC; + tenant_attribute | query_count_in_this_period | score +--------------------------------------------------------------------- + 2 | 1 | 2000000000 + 3 | 1 | 1500000000 + 4 | 1 | 1500000000 + abcd | 1 | 1000000000 +(4 rows) + +\c - - - :master_port +SET search_path TO citus_stats_tenants; +SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'abcd'; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'abcd'; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'bcde'; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +\c - - - :worker_2_port +SELECT tenant_attribute, query_count_in_this_period, score FROM citus_stats_tenants(true) ORDER BY score DESC; + tenant_attribute | query_count_in_this_period | score +--------------------------------------------------------------------- + abcd | 3 | 3000000000 + 2 | 1 | 2000000000 + 3 | 1 | 1500000000 + 4 | 1 | 1500000000 + bcde | 1 | 1000000000 +(5 rows) + +\c - - - :master_port +SET search_path TO citus_stats_tenants; +SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'bcde'; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'bcde'; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'cdef'; + ?column? +--------------------------------------------------------------------- + t +(1 row) + +\c - - - :worker_2_port +SELECT tenant_attribute, query_count_in_this_period, score FROM citus_stats_tenants(true) ORDER BY score DESC; + tenant_attribute | query_count_in_this_period | score +--------------------------------------------------------------------- + abcd | 3 | 3000000000 + bcde | 3 | 3000000000 + 2 | 1 | 2000000000 + 3 | 1 | 1500000000 +(4 rows) + +\c - - - :master_port +SET client_min_messages TO ERROR; +DROP SCHEMA citus_stats_tenants CASCADE; diff --git a/src/test/regress/expected/multi_extension.out b/src/test/regress/expected/multi_extension.out index b475d2b6a..2b165bfeb 100644 --- a/src/test/regress/expected/multi_extension.out +++ b/src/test/regress/expected/multi_extension.out @@ -1326,10 +1326,8 @@ SELECT * FROM multi_extension.print_extension_changes(); previous_object | current_object --------------------------------------------------------------------- | function citus_stats_tenants(boolean) SETOF record - | function citus_stats_tenants_storage() SETOF record | view citus_stats_tenants - | view citus_stats_tenants_storage -(4 rows) +(2 rows) DROP TABLE multi_extension.prev_objects, multi_extension.extension_diff; -- show running version diff --git a/src/test/regress/expected/upgrade_list_citus_objects.out b/src/test/regress/expected/upgrade_list_citus_objects.out index 6733a28fc..8005c0c42 100644 --- a/src/test/regress/expected/upgrade_list_citus_objects.out +++ b/src/test/regress/expected/upgrade_list_citus_objects.out @@ -122,7 +122,6 @@ ORDER BY 1; function citus_stat_statements() function citus_stat_statements_reset() function citus_stats_tenants(boolean) - function citus_stats_tenants_storage() function citus_table_is_visible(oid) function citus_table_size(regclass) function citus_task_wait(bigint,citus_task_status) @@ -319,8 +318,7 @@ ORDER BY 1; view citus_stat_activity view citus_stat_statements view citus_stats_tenants - view citus_stats_tenants_storage view pg_dist_shard_placement view time_partitions -(314 rows) +(312 rows) diff --git a/src/test/regress/multi_1_schedule b/src/test/regress/multi_1_schedule index 5e2cd17c1..8bd234828 100644 --- a/src/test/regress/multi_1_schedule +++ b/src/test/regress/multi_1_schedule @@ -109,6 +109,11 @@ test: pg13_propagate_statistics # ---------- test: citus_update_table_statistics +# ---------- +# Test for tenant statistics +# ---------- +test: citus_stats_tenants + # ---------- # Parallel TPC-H tests to check our distributed execution behavior # ---------- diff --git a/src/test/regress/pg_regress_multi.pl b/src/test/regress/pg_regress_multi.pl index 119e6a758..b003e1663 100755 --- a/src/test/regress/pg_regress_multi.pl +++ b/src/test/regress/pg_regress_multi.pl @@ -485,6 +485,7 @@ push(@pgOptions, "citus.explain_analyze_sort_method='taskId'"); push(@pgOptions, "citus.enable_manual_changes_to_shards=on"); push(@pgOptions, "citus.allow_unsafe_locks_from_workers=on"); push(@pgOptions, "citus.stat_statements_track = 'all'"); +push(@pgOptions, "citus.stats_tenants_limit = 2"); # Some tests look at shards in pg_class, make sure we can usually see them: push(@pgOptions, "citus.show_shards_for_app_name_prefixes='pg_regress'"); diff --git a/src/test/regress/sql/citus_stats_tenants.sql b/src/test/regress/sql/citus_stats_tenants.sql new file mode 100644 index 000000000..7c7e1ed4c --- /dev/null +++ b/src/test/regress/sql/citus_stats_tenants.sql @@ -0,0 +1,112 @@ +CREATE SCHEMA citus_stats_tenants; +SET search_path TO citus_stats_tenants; +SET citus.next_shard_id TO 5797500; +SET citus.shard_replication_factor TO 1; + +CREATE OR REPLACE FUNCTION pg_catalog.clean_citus_stats_tenants() +RETURNS VOID +LANGUAGE C +AS 'citus', $$clean_citus_stats_tenants$$; + +SELECT result FROM run_command_on_all_nodes('SELECT clean_citus_stats_tenants()'); + +CREATE TABLE dist_tbl (a INT, b TEXT); +SELECT create_distributed_table('dist_tbl', 'a', shard_count:=4, colocate_with:='none'); + +CREATE TABLE dist_tbl_2 (a INT, b INT); +SELECT create_distributed_table('dist_tbl_2', 'a', colocate_with:='dist_tbl'); + +CREATE TABLE dist_tbl_text (a TEXT, b INT); +SELECT create_distributed_table('dist_tbl_text', 'a', shard_count:=4, colocate_with:='none'); + +CREATE TABLE ref_tbl (a INT, b INT); +SELECT create_reference_table('ref_tbl'); + +INSERT INTO dist_tbl VALUES (1, 'abcd'); +INSERT INTO dist_tbl VALUES (2, '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; +\c - - - :worker_2_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; +\c - - - :master_port +SET search_path TO citus_stats_tenants; + +SELECT result FROM run_command_on_all_nodes('ALTER SYSTEM SET citus.stats_tenants_period TO 3'); +SELECT result FROM run_command_on_all_nodes('SELECT pg_reload_conf()'); + +SELECT count(*)>=0 FROM dist_tbl WHERE a = 1; +SELECT count(*)>=0 FROM dist_tbl WHERE a = 2; + +\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; +\c - - - :worker_2_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; +\c - - - :master_port + +SELECT pg_sleep (3); + +\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; +\c - - - :worker_2_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; +\c - - - :master_port +SET search_path TO citus_stats_tenants; + +SELECT result FROM run_command_on_all_nodes('ALTER SYSTEM SET citus.stats_tenants_period TO 60'); +SELECT result FROM run_command_on_all_nodes('SELECT pg_reload_conf()'); + +-- queries with multiple tenants should not be counted +SELECT count(*)>=0 FROM dist_tbl WHERE a IN (1, 5); + +-- queries with reference tables should not be counted +SELECT count(*)>=0 FROM ref_tbl WHERE a = 1; + +\c - - - :worker_1_port +SELECT tenant_attribute, query_count_in_this_period FROM citus_stats_tenants ORDER BY tenant_attribute; +\c - - - :master_port +SET search_path TO citus_stats_tenants; + +-- queries with multiple tables but one tenant should be counted +SELECT count(*)>=0 FROM dist_tbl, dist_tbl_2 WHERE dist_tbl.a = 1 AND dist_tbl_2.a = 1; +SELECT count(*)>=0 FROM dist_tbl JOIN dist_tbl_2 ON dist_tbl.a = dist_tbl_2.a WHERE dist_tbl.a = 1; + +\c - - - :worker_1_port +SELECT tenant_attribute, query_count_in_this_period FROM citus_stats_tenants WHERE tenant_attribute = '1'; +\c - - - :master_port +SET search_path TO citus_stats_tenants; + +-- test scoring +-- all of these distribution column values are from second worker +SELECT count(*)>=0 FROM dist_tbl WHERE a = 2; +SELECT count(*)>=0 FROM dist_tbl WHERE a = 3; +SELECT count(*)>=0 FROM dist_tbl WHERE a = 4; +SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'abcd'; + +\c - - - :worker_2_port +SELECT tenant_attribute, query_count_in_this_period, score FROM citus_stats_tenants(true) ORDER BY score DESC; +\c - - - :master_port +SET search_path TO citus_stats_tenants; + +SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'abcd'; +SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'abcd'; +SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'bcde'; + +\c - - - :worker_2_port +SELECT tenant_attribute, query_count_in_this_period, score FROM citus_stats_tenants(true) ORDER BY score DESC; +\c - - - :master_port +SET search_path TO citus_stats_tenants; + +SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'bcde'; +SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'bcde'; +SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'cdef'; + +\c - - - :worker_2_port +SELECT tenant_attribute, query_count_in_this_period, score FROM citus_stats_tenants(true) ORDER BY score DESC; +\c - - - :master_port + +SET client_min_messages TO ERROR; +DROP SCHEMA citus_stats_tenants CASCADE;