一.连接数据库
/opt/pg93/bin/psql -h Host -p port -d database -U user
二.查看数据库的大小
my_db=# select pg_size_pretty(pg_database_size('log_analysis'));
pg_size_pretty
----------------
1100 GB
(1 row)
三.按照表的大小降序排列
log_analysis=# select schemaname,relname,pg_size_pretty(pg_relation_size(relid))
log_analysis-# from pg_stat_user_tables
log_analysis-# order by pg_relation_size(relid) desc;
schemaname | relname | pg_size_pretty
-------------------+----------------------------------------------------------+----------------
dw | sp_product_view_history | 109 GB
dw | sp_sight_view_history | 42 GB
public | touch_ticket_channel_201504 | 41 GB
history | stat_sight_user_area_touch | 30 GB
public | touch_ticket_channel_201502 | 30 GB
history | stat_sight_user_area_www | 30 GB
public | touch_ticket_channel_201503 | 25 GB
dw | product_online | 25 GB
public | touch_ticket_channel_201501 | 19 GB
public | ticket_channel_data | 15 GB
四.查询某个表的行数
log_analysis=# SELECT reltuples log_analysis-# FROM pg_class r JOIN pg_namespace p on r.relnamespace = p.oid log_analysis-# WHERE relkind = 'r' and nspname='public' AND relname = 'touch_ticket_channel_201503'; reltuples ------------- 2.60146e+07 (1 row)