File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
store/postgres/migrations/2025-10-30-094807-0000_table_stats_view Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ DROP MATERIALIZED VIEW IF EXISTS info .table_stats ;
Original file line number Diff line number Diff line change 1+ CREATE MATERIALIZED VIEW info .table_stats AS
2+ WITH table_info AS (
3+ SELECT
4+ s .schemaname AS schema_name,
5+ sd .id AS deployment_id,
6+ sd .subgraph AS subgraph,
7+ s .tablename AS table_name,
8+ c .reltuples AS total_row_count,
9+ s .n_distinct AS n_distinct
10+ FROM pg_stats s
11+ JOIN pg_namespace n ON n .nspname = s .schemaname
12+ JOIN pg_class c ON c .relnamespace = n .oid AND c .relname = s .tablename
13+ JOIN subgraphs .deployment sd ON sd .id ::text = substring (s .schemaname , 4 )
14+ WHERE
15+ s .attname = ' id'
16+ AND s .schemaname LIKE ' sgd%'
17+ AND c .relname NOT IN (' poi2$' , ' data_sources$' )
18+ )
19+ SELECT
20+ schema_name,
21+ deployment_id AS deployment,
22+ subgraph,
23+ table_name,
24+ CASE
25+ WHEN n_distinct < 0 THEN (- n_distinct) * total_row_count
26+ ELSE n_distinct
27+ END::bigint AS entities,
28+ total_row_count::bigint AS versions,
29+ CASE
30+ WHEN total_row_count = 0 THEN 0 ::float8
31+ WHEN n_distinct < 0 THEN (- n_distinct)::float8
32+ ELSE n_distinct::numeric / total_row_count::numeric
33+ END AS ratio
34+ FROM table_info
35+ WITH NO DATA;
You can’t perform that action at this time.
0 commit comments