Skip to content

Commit 1f3050f

Browse files
store: Add table_stats materialzed view
Signed-off-by: Maksim Dimitrov <dimitrov.maksim@gmail.com>
1 parent 405370c commit 1f3050f

File tree

2 files changed

+36
-0
lines changed
  • store/postgres/migrations/2025-10-30-094807-0000_table_stats_view

2 files changed

+36
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP MATERIALIZED VIEW IF EXISTS info.table_stats;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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;

0 commit comments

Comments
 (0)