PostgreSQL 磁盘使用大小监控

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PostgreSQL 磁盘使用大小监控相关的知识,希望对你有一定的参考价值。

表大小信息

postgres=# SELECT *, pg_size_pretty(total_bytes) AS total
postgres-# , pg_size_pretty(index_bytes) AS INDEX
postgres-# , pg_size_pretty(toast_bytes) AS toast
postgres-# , pg_size_pretty(table_bytes) AS TABLE
postgres-# FROM (
postgres(# SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes FROM (
postgres(# SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
postgres(# , c.reltuples AS row_estimate
postgres(# , pg_total_relation_size(c.oid) AS total_bytes
postgres(# , pg_indexes_size(c.oid) AS index_bytes
postgres(# , pg_total_relation_size(reltoastrelid) AS toast_bytes
postgres(# FROM pg_class c
postgres(# LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
postgres(# WHERE relkind = ‘r‘
postgres(# ) a
postgres(# ) a;
oid | table_schema | table_name | row_estimate | total_bytes | index_bytes | toast_bytes | table_bytes | total
| index | toast | table
-------+--------------------+-------------------------+--------------+-------------+-------------+-------------+-------------+--------
----+------------+------------+------------
24933 | public | pgbench_tellers | 1280 | 237568 | 73728 | | 163840 | 232 kB
| 72 kB | | 160 kB
24939 | public | pgbench_branches | 128 | 65536 | 16384 | | 49152 | 64 kB
| 16 kB | | 48 kB
24936 | public | pgbench_accounts | 1.28e+07 | 2009858048 | 287531008 | | 1722327040 | 1917 MB
| 274 MB | | 1643 MB
24930 | public | pgbench_history | 20826 | 1261568 | 0 | | 1261568 | 1232 kB
| 0 bytes | | 1232 kB
2619 | pg_catalog | pg_statistic | 402 | 286720 | 32768 | 73728 | 180224 | 280 kB
| 32 kB | 72 kB | 176 kB
1247 | pg_catalog | pg_type | 354 | 163840 | 57344 | | 106496 | 160 kB
| 56 kB | | 104 kB
1260 | pg_catalog | pg_authid | 1 | 73728 | 32768 | | 40960 | 72 kB
| 32 kB | | 40 kB
1418 | pg_catalog | pg_user_mapping | 0 | 16384 | 16384 | | 0 | 16 kB
| 16 kB | | 0 bytes
2613 | pg_catalog | pg_largeobject | 0 | 8192 | 8192 | | 0 | 8192 by
tes | 8192 bytes | | 0 bytes
1249 | pg_catalog | pg_attribute | 2399 | 598016 | 188416 | | 409600 | 584 kB
| 184 kB | | 400 kB
1255 | pg_catalog | pg_proc | 2821 | 958464 | 327680 | 8192 | 622592 | 936 kB

最大的数据库

postgres=# SELECT d.datname AS Name, pg_catalog.pg_get_userbyid(d.datdba) AS Owner,
postgres-# CASE WHEN pg_catalog.has_database_privilege(d.datname, ‘CONNECT‘)
postgres-# THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))
postgres-# ELSE ‘No Access‘
postgres-# END AS SIZE
postgres-# FROM pg_catalog.pg_database d
postgres-# ORDER BY
postgres-# CASE WHEN pg_catalog.has_database_privilege(d.datname, ‘CONNECT‘)
postgres-# THEN pg_catalog.pg_database_size(d.datname)
postgres-# ELSE NULL
postgres-# END DESC -- nulls first
postgres-# LIMIT 20
postgres-# ;
name | owner | size
--------------+--------------+---------
benchmarksql | benchmarksql | 10 GB
postgres | postgres | 1925 MB
pg_monitor | postgres | 7816 kB
scott | scott | 7632 kB
cott | cott | 7496 kB
template1 | postgres | 7496 kB
mgt | mgt | 7496 kB
template0 | postgres | 7129 kB
(8 rows)

最大的表

postgres=# SELECT nspname || ‘.‘ || relname AS "relation",
postgres-# pg_size_pretty(pg_relation_size(C.oid)) AS "size"
postgres-# FROM pg_class C
postgres-# LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
postgres-# WHERE nspname NOT IN (‘pg_catalog‘, ‘information_schema‘)
postgres-# ORDER BY pg_relation_size(C.oid) DESC
postgres-# LIMIT 20;
relation | size
------------------------------+------------
public.pgbench_accounts | 1642 MB
public.pgbench_accounts_pkey | 274 MB
public.pgbench_history | 1208 kB
pg_toast.pg_toast_2618 | 376 kB
public.pgbench_tellers | 128 kB
public.pgbench_tellers_pkey | 72 kB
pg_toast.pg_toast_2619 | 24 kB
pg_toast.pg_toast_2618_index | 16 kB
public.pgbench_branches_pkey | 16 kB
public.pgbench_branches | 16 kB
pg_toast.pg_toast_2619_index | 16 kB
pg_toast.pg_toast_2396_index | 8192 bytes
pg_toast.pg_toast_2606_index | 8192 bytes
pg_toast.pg_toast_2609_index | 8192 bytes
pg_toast.pg_toast_2964_index | 8192 bytes
pg_toast.pg_toast_2620_index | 8192 bytes
pg_toast.pg_toast_2604_index | 8192 bytes
pg_toast.pg_toast_1255_index | 8192 bytes
pg_toast.pg_toast_3596_index | 8192 bytes
pg_toast.pg_toast_3592_index | 8192 bytes
(20 rows)

表的总大小

postgres=# SELECT nspname || ‘.‘ || relname AS "relation",
postgres-# pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
postgres-# FROM pg_class C
postgres-# LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
postgres-# WHERE nspname NOT IN (‘pg_catalog‘, ‘information_schema‘)
postgres-# AND C.relkind <> ‘i‘
postgres-# AND nspname !~ ‘^pg_toast‘
postgres-# ORDER BY pg_total_relation_size(C.oid) DESC
postgres-# LIMIT 20;
relation | total_size
---------------------------+------------
public.pgbench_accounts | 1917 MB
public.pgbench_history | 1232 kB
public.pgbench_tellers | 232 kB
public.pgbench_branches | 64 kB
public.pg_stat_statements | 0 bytes
public.pg_buffercache | 0 bytes
(6 rows)

ref:https://wiki.postgresql.org/wiki/Disk_Usage

以上是关于PostgreSQL 磁盘使用大小监控的主要内容,如果未能解决你的问题,请参考以下文章

PostgreSQL 锁监控

Zabbix监控PostgreSQL

postgresql查看数据库占用的物理存储空间大小

PostgreSQL监控之pgwatch2

使用非常大的结果集查询 Postgresql

Postgresql相关数据库表占用磁盘大小统计