SELECT table_schema ||'.'|| table_name AS table_full_name, pg_size_pretty(pg_total_relation_size('"'|| table_schema ||'"."'|| table_name ||'"')) AS size FROM information_schema.tables ORDERBY pg_total_relation_size('"'|| table_schema ||'"."'|| table_name ||'"') DESC LIMIT 20
方法三:查出所有表按大小排序并分离 data 与 index
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
SELECT table_name, pg_size_pretty(table_size) AS table_size, pg_size_pretty(indexes_size) AS indexes_size, pg_size_pretty(total_size) AS total_size FROM ( SELECT table_name, pg_table_size(table_name) AS table_size, pg_indexes_size(table_name) AS indexes_size, pg_total_relation_size(table_name) AS total_size FROM ( SELECT ('"'|| table_schema ||'"."'|| table_name ||'"') AS table_name FROM information_schema.tables ) AS all_tables ORDERBY total_size DESC ) AS pretty_sizes
create role channel with login password 'channel123$' valid until 'infinity'; create database channel with encoding='UTF8' owner=channel connection limit=-1;
update npc_list set update_time = timezone('utc', now())
PostgreSQL 的进程列表
查找和终止挂起的查询:
1 2 3 4 5 6 7 8
-- 查看所有活动进程 SELECT*FROM pg_stat_activity WHERE state ='active';
-- 取消查询 SELECT pg_cancel_backend(PID);
-- 强制终止 SELECT pg_terminate_backend(PID);
CROSS JOIN 示例
1 2 3 4 5 6 7
select attribute->>'uid'as uid, attribute->>'day'asday, (building->>'lv')::integer* (building->>'num')::integer FROM public.user_daily_report CROSSJOIN jsonb_array_elements(metric->'building') as building WHERE attribute->>'day'='2019-09-01'