Daily Shaarli
December 23, 2025
Find your widest tables (by column count)
SELECT
n.nspname AS schema,
c.relname AS table,
count(*) FILTER (WHERE a.attnum > 0 AND NOT a.attisdropped) AS live_columns,
count(*) FILTER (WHERE a.attisdropped) AS dropped_columns,
count(*) FILTER (WHERE a.attnum > 0) AS total_attnums
FROM pg_attribute a
JOIN pg_class c ON c.oid = a.attrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','p') -- r=table, p=partitioned table
AND n.nspname NOT IN ('pg_catalog','information_schema')
GROUP BY 1,2
ORDER BY total_attnums DESC
LIMIT 50;
Identify “mostly NULL” columns (classic wide-table driver)
SELECT
attname,
null_frac,
avg_width
FROM pg_stats
WHERE schemaname = 'public'
AND tablename = 'your_table'
ORDER BY null_frac DESC, avg_width DESC
LIMIT 30;
Get a rough “row width” estimate (planner stats)
SELECT
relname,
n_live_tup,
n_dead_tup,
pg_size_pretty(pg_relation_size(relid)) AS heap_size
FROM pg_stat_user_tables
ORDER BY pg_relation_size(relid) DESC
LIMIT 20;