PostgreSQL监控

(1)查询缓存

create extension pg_buffercache;
SELECT c.relname, count(*) AS buffers       FROM pg_buffercache b INNER JOIN pg_class c       ON b.relfilenode = pg_relation_filenode(c.oid) AND          b.reldatabase IN (0, (SELECT oid FROM pg_database       WHERE datname = current_database()))       GROUP BY c.relname ORDER BY 2 DESC LIMIT 5;

(2)查询表记录行数
SELECT reltuples FROM pg_class r WHERE relkind = 'r' AND relname = 'bird';
(3)查询表大小(\dt+、\di+)
select pg_size_pretty(pg_relation_size('bird'));
3.3.3 确定PostgreSQL任务瓶颈
(1)找出慢查询
SELECT query, calls, total_time, rows, 100.0 * shared_blks_hit /nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent FROM pg_stat_statements ORDER BY total_time DESC LIMIT 5;
(2)思考慢查询是IO密集型还是CPU密集型。
(3)查看慢查询进程的CPU消耗占比。
(4)确认是否物理内存不够用,确认是否有swap产生。
(5)如果(3)中CPU占比较高,CPU密级型。如果(4)中内存不够,且发生大量swap,IO密级型。

参考资料

  • PostgreSQL查看数据库,索引,表,表空间大小
  • PostgreSQL表的行数统计
  • postgresql安装pg_stat_statements 模块监控运行状态

你可能感兴趣的:(PostgreSQL监控)