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$$; CREATE OR REPLACE FUNCTION pg_catalog.sleep_until_next_period() RETURNS VOID LANGUAGE C AS 'citus', $$sleep_until_next_period$$; SELECT result FROM run_command_on_all_nodes('SELECT clean_citus_stats_tenants()'); result --------------------------------------------------------------------- (3 rows) -- set period to a high number to prevent stats from being reset SELECT result FROM run_command_on_all_nodes('ALTER SYSTEM SET citus.stats_tenants_period TO 1000000000'); 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) 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; 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(true) 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 2 | 0 | 0 | 1 | 0 3 | 0 | 0 | 1 | 0 4 | 0 | 0 | 1 | 0 5 | 0 | 0 | 1 | 0 (5 rows) SELECT result FROM run_command_on_all_nodes('SELECT clean_citus_stats_tenants()'); result --------------------------------------------------------------------- (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) SELECT tenant_attribute, query_count_in_this_period FROM citus_stats_tenants(true) ORDER BY tenant_attribute; tenant_attribute | query_count_in_this_period --------------------------------------------------------------------- (0 rows) -- 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) SELECT tenant_attribute, query_count_in_this_period FROM citus_stats_tenants(true) WHERE tenant_attribute = '1'; tenant_attribute | query_count_in_this_period --------------------------------------------------------------------- 1 | 2 (1 row) -- test scoring -- all of these distribution column values are from second worker SELECT nodeid AS worker_2_nodeid FROM pg_dist_node WHERE nodeport = :worker_2_port \gset 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) SELECT tenant_attribute, query_count_in_this_period, score FROM citus_stats_tenants(true) WHERE nodeid = :worker_2_nodeid ORDER BY score DESC, tenant_attribute; tenant_attribute | query_count_in_this_period | score --------------------------------------------------------------------- 2 | 1 | 1000000000 3 | 1 | 1000000000 4 | 1 | 1000000000 abcd | 1 | 1000000000 (4 rows) 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) SELECT count(*)>=0 FROM dist_tbl_text WHERE a = 'cdef'; ?column? --------------------------------------------------------------------- t (1 row) SELECT tenant_attribute, query_count_in_this_period, score FROM citus_stats_tenants(true) WHERE nodeid = :worker_2_nodeid ORDER BY score DESC, tenant_attribute; tenant_attribute | query_count_in_this_period | score --------------------------------------------------------------------- abcd | 3 | 3000000000 2 | 1 | 1000000000 3 | 1 | 1000000000 4 | 1 | 1000000000 bcde | 1 | 1000000000 cdef | 1 | 1000000000 (6 rows) 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 = 'defg'; ?column? --------------------------------------------------------------------- t (1 row) SELECT tenant_attribute, query_count_in_this_period, score FROM citus_stats_tenants(true) WHERE nodeid = :worker_2_nodeid ORDER BY score DESC, tenant_attribute; tenant_attribute | query_count_in_this_period | score --------------------------------------------------------------------- abcd | 3 | 3000000000 bcde | 3 | 3000000000 2 | 1 | 1000000000 3 | 1 | 1000000000 defg | 1 | 1000000000 (5 rows) -- test period passing SELECT result FROM run_command_on_all_nodes('SELECT clean_citus_stats_tenants()'); result --------------------------------------------------------------------- (3 rows) SELECT count(*)>=0 FROM dist_tbl WHERE a = 1; ?column? --------------------------------------------------------------------- t (1 row) INSERT INTO dist_tbl VALUES (5, 'abcd'); \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_local 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 | 1 | 0 5 | 0 | 0 | 1 | 0 (2 rows) -- simulate passing the period SET citus.stats_tenants_period TO 2; SELECT sleep_until_next_period(); sleep_until_next_period --------------------------------------------------------------------- (1 row) 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_local 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 | 1 5 | 0 | 0 | 0 | 1 (2 rows) \c - - - :master_port SET search_path TO citus_stats_tenants; SET client_min_messages TO ERROR; DROP SCHEMA citus_stats_tenants CASCADE;