@@ -6,7 +6,7 @@ CREATE OR REPLACE VIEW citus.citus_stats AS
66WITH most_common_vals_double_json AS (
77 SELECT ( SELECT json_agg(row_to_json(f)) FROM ( SELECT * FROM run_command_on_shards(logicalrelid,
88 $$ SELECT json_agg(row_to_json(shard_stats)) FROM (
9- SELECT ' $$ || logicalrelid || $$' AS dist_table , attname, s .null_frac ,
9+ SELECT ' $$ || logicalrelid || $$' AS citus_table , attname, s .null_frac ,
1010 most_common_vals, most_common_freqs, c .reltuples AS reltuples
1111 -- join on tablename is enough here, no need to join with pg_namespace
1212 -- since shards have unique ids in their names, hence two shard names
@@ -23,41 +23,42 @@ most_common_vals_json AS (
2323table_reltuples_json AS (
2424 SELECT distinct (shardid),
2525 (json_array_elements(result::json)- >> ' reltuples' )::bigint AS shard_reltuples,
26- (json_array_elements(result::json)- >> ' dist_table ' )::regclass AS dist_table
26+ (json_array_elements(result::json)- >> ' citus_table ' )::regclass AS citus_table
2727 FROM most_common_vals_json),
2828
2929table_reltuples AS (
30- SELECT dist_table , sum (shard_reltuples) AS table_reltuples
31- FROM table_reltuples_json GROUP BY dist_table ),
30+ SELECT citus_table , sum (shard_reltuples) AS table_reltuples
31+ FROM table_reltuples_json GROUP BY 1 ORDER BY 1 ),
3232
3333null_frac_json AS (
34- SELECT (json_array_elements(result::json)- >> ' dist_table ' )::regclass AS dist_table ,
34+ SELECT (json_array_elements(result::json)- >> ' citus_table ' )::regclass AS citus_table ,
3535 (json_array_elements(result::json)- >> ' reltuples' )::bigint AS shard_reltuples,
3636 (json_array_elements(result::json)- >> ' null_frac' )::float4 AS null_frac,
3737 (json_array_elements(result::json)- >> ' attname' )::text AS attname
3838 FROM most_common_vals_json
3939),
4040
4141null_occurrences AS (
42- SELECT dist_table , attname, sum (null_frac * shard_reltuples)::bigint AS null_occurrences
42+ SELECT citus_table , attname, sum (null_frac * shard_reltuples)::bigint AS null_occurrences
4343 FROM null_frac_json
44- GROUP BY dist_table, attname
44+ GROUP BY 1 , 2
45+ ORDER BY 1 , 2
4546),
4647
4748most_common_vals AS (
48- SELECT (json_array_elements(result::json)- >> ' dist_table ' )::regclass AS dist_table ,
49+ SELECT (json_array_elements(result::json)- >> ' citus_table ' )::regclass AS citus_table ,
4950 (json_array_elements(result::json)- >> ' attname' )::text AS attname,
5051 json_array_elements_text((json_array_elements(result::json)- >> ' most_common_vals' )::json)::text AS common_val,
5152 json_array_elements_text((json_array_elements(result::json)- >> ' most_common_freqs' )::json)::float4 AS common_freq,
5253 (json_array_elements(result::json)- >> ' reltuples' )::bigint AS shard_reltuples
5354 FROM most_common_vals_json),
5455
5556common_val_occurrence AS (
56- SELECT dist_table , m .attname , common_val,
57+ SELECT citus_table , m .attname , common_val,
5758 sum (common_freq * shard_reltuples)::bigint AS occurrence
5859 FROM most_common_vals m
59- GROUP BY dist_table , m .attname , common_val
60- ORDER BY m . attname , occurrence DESC , common_val )
60+ GROUP BY citus_table , m .attname , common_val
61+ ORDER BY 1 , 2 , occurrence DESC )
6162
6263SELECT nsp .nspname AS schemaname, p .relname AS tablename, c .attname ,
6364
@@ -70,10 +71,10 @@ SELECT nsp.nspname AS schemaname, p.relname AS tablename, c.attname,
7071 ELSE ARRAY_agg((occurrence/ t .table_reltuples )::float4) END AS most_common_freqs
7172
7273FROM common_val_occurrence c, table_reltuples t, null_occurrences n, pg_class p, pg_namespace nsp
73- WHERE c .dist_table = t .dist_table
74- AND c .dist_table = n .dist_table AND c .attname = n .attname
75- AND c .dist_table ::regclass::oid = p .oid AND p .relnamespace = nsp .oid
76- GROUP BY nsp .nspname , c .dist_table , p .relname , c .attname ;
74+ WHERE c .citus_table = t .citus_table
75+ AND c .citus_table = n .citus_table AND c .attname = n .attname
76+ AND c .citus_table ::regclass::oid = p .oid AND p .relnamespace = nsp .oid
77+ GROUP BY nsp .nspname , c .citus_table , p .relname , c .attname ;
7778
7879ALTER VIEW citus .citus_stats SET SCHEMA pg_catalog;
7980GRANT SELECT ON pg_catalog .citus_stats TO PUBLIC;
0 commit comments