查找 PostgreSQL 表及其索引的磁盘大小

本文将讨论如何找到 PostgreSQL 表及其索引的磁盘大小。

使用 PSQL 查找 PostgreSQL 表和数据库的磁盘大小

你可以使用 \l+ 查看数据库大小,使用 \d+ 显示表大小。但在此之前,你需要登录数据库执行查询。

以下是在 Postgres 中显示表和数据库大小的命令和输出:

postgres=#\l+

输出:

数据库大小

postgres-#\d+ListofrelationsSchema|Name|Type|Owner|Persistence|Accessmethod|Size|Description--------+------------------+----------+----------+-------------+---------------+------------+-------------
public|book_lends|table|postgres|permanent|heap|16kB|public|books|table|postgres|permanent|heap|16kB|public|employee|table|postgres|permanent|heap|16kB|public|employee_id_seq|sequence|postgres|permanent||8192bytes|public|events|table|postgres|permanent|heap|16kB|public|mock_data|table|postgres|permanent|heap|48kB|public|product|table|postgres|permanent|heap|16kB|public|product_id_seq|sequence|postgres|permanent||8192bytes|public|products|table|postgres|permanent|heap|16kB|public|products_id_seq|sequence|postgres|permanent||8192bytes|public|prroducts|table|postgres|permanent|heap|8192bytes|public|prroducts_id_seq|sequence|postgres|permanent||8192bytes|public|stores|table|postgres|permanent|heap|16kB|public|stores_id_seq|sequence|postgres|permanent||8192bytes|public|users|table|postgres|permanent|heap|16kB|(15rows)

这里它显示了我们在 Postgres 数据库中的所有表及其名称、类型、所有者、大小、访问方法等。

查找数据库中最大表的大小

这是 Postgres 官方编写的代码片段,用于按降序显示表格大小。

SELECTnspname||'.'||relnameAS"relation",pg_size_pretty(pg_total_relation_size(C.oid))AS"total_size"FROMpg_classCLEFTJOINpg_namespaceNON(N.oid=C.relnamespace)WHEREnspnameNOTIN('pg_catalog','information_schema')ANDC.relkind<>'i'ANDnspname!~'^pg_toast'ORDERBYpg_total_relation_size(C.oid)DESCLIMIT10;

输出:

     relation      | total_size
-------------------+------------
 public.mock_data  | 48 kB
 public.product    | 32 kB
 public.products   | 32 kB
 public.books      | 32 kB
 public.book_lends | 32 kB
 public.employee   | 32 kB
 public.stores     | 32 kB
 public.users      | 32 kB
 public.events     | 16 kB
 public.prroducts  | 16 kB
(10 rows)

我们在数据库中的 postgres 下搜索了 10 个最大的表。

你可以点击以下 link 了解更多关于 Postgres 中磁盘大小的查询。你将找到用于查找最大集群、最大关系、分区表等的 SQL 查询。

这里更多关于数据库大小的函数。