From f01421d2f6bfc19eb07b86c1f817748e682e3bcd Mon Sep 17 00:00:00 2001 From: xking Date: Wed, 30 Jul 2025 16:39:18 +0800 Subject: [PATCH] update --- postgresql_and_edb/edb表空间查询.md | 93 ++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/postgresql_and_edb/edb表空间查询.md b/postgresql_and_edb/edb表空间查询.md index 40e2b39..36b914d 100644 --- a/postgresql_and_edb/edb表空间查询.md +++ b/postgresql_and_edb/edb表空间查询.md @@ -23,5 +23,98 @@ SELECT nspname || '.' || relname AS "relation", 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; + +``` \ No newline at end of file