120 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			120 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
```sql
 | 
						|
SELECT nspname || '.' || relname AS "relation",
 | 
						|
               pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
 | 
						|
        FROM pg_class C
 | 
						|
        LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
 | 
						|
        WHERE nspname NOT IN ('pg_catalog', 'information_schema')
 | 
						|
        AND C.relkind = 'r'
 | 
						|
        ORDER BY pg_total_relation_size(C.oid) DESC;
 | 
						|
```
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
```sql
 | 
						|
 SELECT schemaname || '.' || relname AS "relation",
 | 
						|
               pg_size_pretty(pg_total_relation_size(relid)) AS "total_size",
 | 
						|
               pg_size_pretty(pg_table_size(relid) - pg_indexes_size(relid)) AS "table_size",
 | 
						|
               pg_size_pretty(pg_indexes_size(relid)) AS "index_size",
 | 
						|
               n_dead_tup
 | 
						|
        FROM pg_stat_user_tables
 | 
						|
        WHERE n_dead_tup > 0
 | 
						|
        ORDER BY n_dead_tup DESC;
 | 
						|
        
 | 
						|
```
 | 
						|
 | 
						|
 | 
						|
 | 
						|
```sql
 | 
						|
 | 
						|
SELECT
 | 
						|
  table_schema || '.' || table_name AS 完整表名,
 | 
						|
  table_type AS 表类型,
 | 
						|
  table_catalog AS 数据库名,
 | 
						|
  -- 使用 format() 函数正确引用表名
 | 
						|
  pg_size_pretty(pg_relation_size(format('%I.%I', table_schema, table_name))) AS 数据大小,
 | 
						|
  pg_size_pretty(pg_indexes_size(format('%I.%I', table_schema, table_name))) AS 索引大小,
 | 
						|
  pg_size_pretty(pg_total_relation_size(format('%I.%I', table_schema, table_name))) AS 总大小,
 | 
						|
  pg_total_relation_size(format('%I.%I', table_schema, table_name)) AS 总字节数
 | 
						|
FROM
 | 
						|
  information_schema.tables
 | 
						|
WHERE
 | 
						|
  table_type IN ('BASE TABLE', 'PARTITIONED TABLE')
 | 
						|
  AND table_schema NOT IN ('pg_catalog', 'information_schema', 'edb_sys')
 | 
						|
ORDER BY
 | 
						|
  总字节数 DESC;
 | 
						|
 | 
						|
```
 | 
						|
 | 
						|
 | 
						|
 | 
						|
```sql
 | 
						|
 | 
						|
SELECT
 | 
						|
  pid,
 | 
						|
  relid::regclass AS table_name,
 | 
						|
  phase,
 | 
						|
  heap_blks_total,
 | 
						|
  heap_blks_scanned,
 | 
						|
  heap_blks_scanned/heap_blks_total rate,
 | 
						|
  heap_blks_vacuumed,
 | 
						|
  index_vacuum_count,
 | 
						|
  max_dead_tuples,
 | 
						|
  num_dead_tuples
 | 
						|
FROM
 | 
						|
  pg_stat_progress_vacuum;
 | 
						|
 | 
						|
SELECT
 | 
						|
  pid,
 | 
						|
  usename,
 | 
						|
  datname,
 | 
						|
  query,
 | 
						|
  state,
 | 
						|
  query_start,
 | 
						|
  now() - query_start AS running_time
 | 
						|
FROM
 | 
						|
  pg_stat_activity
 | 
						|
WHERE
 | 
						|
  query ILIKE '%VACUUM%'
 | 
						|
ORDER BY
 | 
						|
  query_start;
 | 
						|
 | 
						|
 | 
						|
SELECT
 | 
						|
  schemaname,
 | 
						|
  relname,
 | 
						|
  n_live_tup AS 活元组数量,
 | 
						|
  n_dead_tup AS 死元组数量,
 | 
						|
  round(100 * n_dead_tup / (n_live_tup + 1), 2) AS 死元组占比
 | 
						|
FROM pg_stat_user_tables
 | 
						|
WHERE n_live_tup > 0
 | 
						|
  AND round(100 * n_dead_tup / (n_live_tup + 1), 2) > 20  -- 死元组占比超20%
 | 
						|
ORDER BY 死元组占比 DESC;
 | 
						|
 | 
						|
 | 
						|
 | 
						|
SELECT
 | 
						|
  schemaname,
 | 
						|
  relname AS 索引名,
 | 
						|
  pg_size_pretty(pg_relation_size(relid)) AS 索引大小,
 | 
						|
  idx_scan AS 扫描次数,  -- EDB 中直接通过 pg_stat_user_indexes 的 idx_scan 字段获取扫描次数
 | 
						|
  -- 索引膨胀率(索引实际大小 / 有效数据大小,>1.5 表示碎片严重)
 | 
						|
  round(
 | 
						|
    pg_relation_size(relid)::numeric /
 | 
						|
    pg_indexes_size(relid::regclass)::numeric,  -- 修正类型转换,去掉多余的 text 转换
 | 
						|
    2
 | 
						|
  ) AS 膨胀率
 | 
						|
FROM pg_stat_user_indexes
 | 
						|
WHERE
 | 
						|
  pg_indexes_size(relid::regclass) > 0  -- 排除无效索引
 | 
						|
  AND round(
 | 
						|
    pg_relation_size(relid)::numeric /
 | 
						|
    pg_indexes_size(relid::regclass)::numeric,
 | 
						|
    2
 | 
						|
  ) > 1.5  -- 筛选膨胀率 >1.5 的索引
 | 
						|
ORDER BY 膨胀率 DESC;
 | 
						|
 | 
						|
``` |