PostgreSQL检察表的大小
在 PostgreSQL 中,可以利用一系列函数和体系视图来检察表的大小。这些工具可以帮助您获取表的基本存储大小、包括索引和 TOAST 的总大小等信息。下列方法演示了如何获取这些信息。
利用函数检察表的大小
- pg_relation_size():返回表的底子存储大小(不包括索引和 TOAST 数据)。
- white=# SELECT pg_relation_size('yewu1.t1');
- pg_relation_size
- ------------------
- 8192
- (1 row)
复制代码
- pg_table_size():返回表的总大小,包括底子存储和 TOAST 数据,但不包括索引。
- white=# SELECT pg_table_size('yewu1.t1');
- pg_table_size
- ---------------
- 40960
- (1 row)
复制代码
- pg_indexes_size():返回表的全部索引的总大小。
- white=# SELECT pg_indexes_size('yewu1.t1');
- pg_indexes_size
- -----------------
- 16384
- (1 row)
复制代码
- pg_total_relation_size():返回表的总大小,包括底子存储、TOAST 数据和全部索引。
- white=# SELECT pg_total_relation_size('yewu1.t1');
- pg_total_relation_size
- ------------------------
- 57344
- (1 row)
复制代码
- pg_size_pretty():将大小值转换为可读格式,可以与上述函数联合利用。
- white=# SELECT pg_size_pretty(pg_relation_size('yewu1.t1'));
- pg_size_pretty
- ----------------
- 8192 bytes
- (1 row)
复制代码 示例1
如何综合利用这些函数来获取表的详细存储信息
- SELECT
- pg_size_pretty(pg_relation_size('yewu1.t1')) AS base_size,
- pg_size_pretty(pg_table_size('yewu1.t1')) AS table_size,
- pg_size_pretty(pg_indexes_size('yewu1.t1')) AS indexes_size,
- pg_size_pretty(pg_total_relation_size('yewu1.t1')) AS total_size;
复制代码 输出效果
- white=# SELECT
- white-# pg_size_pretty(pg_relation_size('yewu1.t1')) AS base_size,
- white-# pg_size_pretty(pg_table_size('yewu1.t1')) AS table_size,
- white-# pg_size_pretty(pg_indexes_size('yewu1.t1')) AS indexes_size,
- white-# pg_size_pretty(pg_total_relation_size('yewu1.t1')) AS total_size;
- base_size | table_size | indexes_size | total_size
- ------------+------------+--------------+------------
- 8192 bytes | 40 kB | 16 kB | 56 kB
- (1 row)
复制代码 示例2
利用体系视图检察大小
可以利用体系视图 pg_class、pg_namespace 以及函数 pg_size_pretty 联合来获取数据库中全部表的大小信息。
- SELECT
- ns.nspname AS schema_name,
- cls.relname AS table_name,
- pg_size_pretty(pg_relation_size(cls.oid)) AS base_size,
- pg_size_pretty(pg_total_relation_size(cls.oid)) AS total_size
- FROM
- pg_class cls
- JOIN
- pg_namespace ns ON cls.relnamespace = ns.oid
- WHERE
- cls.relkind = 'r' -- 仅选择普通表
- AND ns.nspname NOT IN ('pg_catalog', 'information_schema') -- 排除系统模式
- ORDER BY
- pg_total_relation_size(cls.oid) DESC;
复制代码 示例效果
- white=# SELECT
- white-# ns.nspname AS schema_name,
- white-# cls.relname AS table_name,
- white-# pg_size_pretty(pg_relation_size(cls.oid)) AS base_size,
- white-# pg_size_pretty(pg_total_relation_size(cls.oid)) AS total_size
- white-# FROM
- white-# pg_class cls
- white-# JOIN
- white-# pg_namespace ns ON cls.relnamespace = ns.oid
- white-# WHERE
- white-# cls.relkind = 'r' -- 仅选择普通表
- white-# AND ns.nspname NOT IN ('pg_catalog', 'information_schema') -- 排除系统模式
- white-# ORDER BY
- white-# pg_total_relation_size(cls.oid) DESC;
- schema_name | table_name | base_size | total_size
- -------------+------------------+------------+------------
- public | pgbench_accounts | 128 MB | 150 MB
- public | pgbench_history | 13 MB | 13 MB
- yewu1 | t4 | 8192 bytes | 88 kB
- public | pgbench_tellers | 8192 bytes | 56 kB
- yewu2 | t4 | 8192 bytes | 56 kB
- yewu1 | t1 | 8192 bytes | 56 kB
- public | pgbench_branches | 8192 bytes | 56 kB
- yewu1 | t2 | 0 bytes | 0 bytes
- (8 rows)
复制代码 示例3
通过视图检察特定命据库中全部表的大小
上述查询展示了如安在特定命据库中检察全部表的大小。假如你想仅导出一个特定模式下的表数据或包含更多详细信息,可以调整查询。
- SELECT
- ns.nspname AS schema_name,
- cls.relname AS table_name,
- pg_size_pretty(pg_relation_size(cls.oid)) AS base_size,
- pg_size_pretty(pg_total_relation_size(cls.oid)) AS total_size,
- pg_size_pretty(pg_indexes_size(cls.oid)) AS indexes_size,
- pg_size_pretty(pg_table_size(cls.oid)) AS table_size
- FROM
- pg_class cls
- JOIN
- pg_namespace ns ON cls.relnamespace = ns.oid
- WHERE
- cls.relkind = 'r' -- 仅选择普通表
- AND ns.nspname = 'public' -- 替换为你要查询的模式
- ORDER BY
- pg_total_relation_size(cls.oid) DESC;
复制代码 输出效果
- white=# SELECT
- white-# ns.nspname AS schema_name,
- white-# cls.relname AS table_name,
- white-# pg_size_pretty(pg_relation_size(cls.oid)) AS base_size,
- white-# pg_size_pretty(pg_total_relation_size(cls.oid)) AS total_size,
- white-# pg_size_pretty(pg_indexes_size(cls.oid)) AS indexes_size,
- white-# pg_size_pretty(pg_table_size(cls.oid)) AS table_size
- white-# FROM
- white-# pg_class cls
- white-# JOIN
- white-# pg_namespace ns ON cls.relnamespace = ns.oid
- white-# WHERE
- white-# cls.relkind = 'r' -- 仅选择普通表
- white-# AND ns.nspname = 'public' -- 替换为你要查询的模式
- white-# ORDER BY
- white-# pg_total_relation_size(cls.oid) DESC;
- schema_name | table_name | base_size | total_size | indexes_size | table_size
- -------------+------------------+------------+------------+--------------+------------
- public | pgbench_accounts | 128 MB | 150 MB | 21 MB | 128 MB
- public | pgbench_history | 13 MB | 13 MB | 0 bytes | 13 MB
- public | pgbench_branches | 8192 bytes | 56 kB | 16 kB | 40 kB
- public | pgbench_tellers | 8192 bytes | 56 kB | 16 kB | 40 kB
- (4 rows)
复制代码 自动化查询全部数据库中的表大小
您也可以写一个脚本来循环遍历全部数据库并查询每个数据库的表大小。例如,可以利用以下 Python 脚本:
- import psycopg2
- import sys
- def get_table_sizes(dbname, user, password, host):
- try:
- conn = psycopg2.connect(dbname=dbname, user=user, password=password, host=host)
- cur = conn.cursor()
- query = """
- SELECT
- ns.nspname AS schema_name,
- cls.relname AS table_name,
- pg_size_pretty(pg_relation_size(cls.oid)) AS base_size,
- pg_size_pretty(pg_total_relation_size(cls.oid)) AS total_size,
- pg_size_pretty(pg_indexes_size(cls.oid)) AS indexes_size,
- pg_size_pretty(pg_table_size(cls.oid)) AS table_size
- FROM
- pg_class cls
- JOIN
- pg_namespace ns ON cls.relnamespace = ns.oid
- WHERE
- cls.relkind = 'r' -- 仅选择普通表
- AND ns.nspname NOT IN ('pg_catalog', 'information_schema') -- 排除系统模式
- ORDER BY
- pg_total_relation_size(cls.oid) DESC;
- """
-
- cur.execute(query)
- rows = cur.fetchall()
- for row in rows:
- print(row)
- cur.close()
- conn.close()
- except Exception as e:
- print(f"Error connecting to database {dbname}: {e}", file=sys.stderr)
- # 替换为实际的数据库名称、用户名、密码和主机
- databases = ["db1", "db2"]
- user = "your_user"
- password = "your_password"
- host = "your_host"
- for db in databases:
- print(f"Database: {db}")
- get_table_sizes(db, user, password, host)
- print("\n")
复制代码 通过这种方式,可以轻松自动化地获取多个数据库中全部表的大小信息。
总结
利用 PostgreSQL 提供的函数和体系视图,可以有效地获取数据库中表的各种尺寸信息。这对于数据库管理、性能优化和容量规划非常有用。假如有更复杂的需求或碰到任何问题,随时提问!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |