postgresql列表和按大小排序的表

not*_*ere 86 sql postgresql postgresql-9.3

有没有一种简单的方法可以列出PostgreSQL数据库中的所有表并按大小排序?

伪代码

SELECT * FROM tables
ORDER by tables.size
Run Code Online (Sandbox Code Playgroud)

我在用PostgreSQL 9.3.2.

a_h*_*ame 107

select table_name, pg_relation_size(quote_ident(table_name))
from information_schema.tables
where table_schema = 'public'
order by 2
Run Code Online (Sandbox Code Playgroud)

public如果您有多个模式,则会显示模式中所有表的大小,您可能希望使用:

select table_schema, table_name, pg_relation_size('"'||table_schema||'"."'||table_name||'"')
from information_schema.tables
order by 3
Run Code Online (Sandbox Code Playgroud)

SQLFiddle示例:http://sqlfiddle.com/#!15/13157/3

手册中所有对象大小函数的列表:https:
//www.postgresql.org/docs/current/static/functions-admin.html#FUNCTIONS-ADMIN-DBSIZE

  • 您应该使用“pg_total_relation_size”来获取表的总大小**包括其索引** - 请参阅/sf/answers/2939409651/ (14认同)
  • 好的,这段代码可以工作:`select table_schema, table_name, pg_relation_size(table_schema||'.'||table_name) from information_schema.tables order by 3; ` 谢谢你的帮助! (5认同)

Kuc*_*chi 61

这将显示模式名称,表名称,大小漂亮和大小(排序所需).

SELECT
  schema_name,
  relname,
  pg_size_pretty(table_size) AS size,
  table_size

FROM (
       SELECT
         pg_catalog.pg_namespace.nspname           AS schema_name,
         relname,
         pg_relation_size(pg_catalog.pg_class.oid) AS table_size

       FROM pg_catalog.pg_class
         JOIN pg_catalog.pg_namespace ON relnamespace = pg_catalog.pg_namespace.oid
     ) t
WHERE schema_name NOT LIKE 'pg_%'
ORDER BY table_size DESC;
Run Code Online (Sandbox Code Playgroud)

我根据PostgreSQL数据库中大小(相对和绝对)的架构列表中的解决方案构建了这个

  • 这实在是太有用了;还考虑了索引。 (3认同)

Ben*_*ier 15

我需要找出哪些表使用的空间最多。

根据其他答案,我使用了该查询:

select table_name, pg_size_pretty( pg_relation_size(quote_ident(table_name)) )
from information_schema.tables
where table_schema = 'public'
order by pg_relation_size(quote_ident(table_name)) desc
Run Code Online (Sandbox Code Playgroud)

我得到以下结果:

table_name              pg_size_pretty
--------------------------------------
trade_binance           96 GB
closs_v2_binance_stash  46 GB
closs_bitfinex_stash    5725 MB
trade_bitfinex          5112 MB
...
api_requests            0 bytes
trade_huobi             0 bytes
Run Code Online (Sandbox Code Playgroud)

我应该买一个更大的SSD。


Vig*_*aja 12

这将更加清楚.

pg_size_pretty(<numeric_value>) - 将no.of字节转换为人类可读的格式.

pg_database_size(<db_name>)-在获取数据库大小字节.

pg_total_relation_size(<relation_name>)- 获取表及其索引的总大小(以字节为单位).

pg_relation_size(<relation_name>)- 以字节为单位获取关系大小.

pg_index_size(<relation_name>)- 获取关系的索引大小(以字节为单位).

current_database() - 获取正在执行此查询的当前使用的数据库.

查询:

select current_database() as database,
       pg_size_pretty(total_database_size) as total_database_size,
       schema_name,
       table_name,
       pg_size_pretty(total_table_size) as total_table_size,
       pg_size_pretty(table_size) as table_size,
       pg_size_pretty(index_size) as index_size
       from ( select table_name,
                table_schema as schema_name,
                pg_database_size(current_database()) as total_database_size,
                pg_total_relation_size(table_name) as total_table_size,
                pg_relation_size(table_name) as table_size,
                pg_indexes_size(table_name) as index_size
                from information_schema.tables
                where table_schema=current_schema() and table_name like 'table_%'
                order by total_table_size
            ) as sizes;
Run Code Online (Sandbox Code Playgroud)

结果:

 database  | total_database_size | schema_name | table_name | total_table_size | table_size | index_size
-----------+---------------------+-------------+------------+------------------+------------+------------
 vigneshdb | 1586 MB             | corpdata    | table_aaa  | 16 kB            | 0 bytes    | 8192 bytes
 vigneshdb | 1586 MB             | corpdata    | table_bbb  | 24 kB            | 0 bytes    | 16 kB
 vigneshdb | 1586 MB             | corpdata    | table_ccc  | 640 kB           | 112 kB     | 488 kB
 vigneshdb | 1586 MB             | corpdata    | table_ddd  | 9760 kB          | 3152 kB    | 6568 kB
 vigneshdb | 1586 MB             | corpdata    | table_eee  | 1120 MB          | 311 MB     | 808 MB
Run Code Online (Sandbox Code Playgroud)

人性化的格式表示bytes,kB,MB,GB,和TB.

byteskB- 开始10240 bytes

bytesto MB- 从10485248 bytes= 10239.5 kB〜开始10 MB

bytesto GB- 从10736893952 bytes= 10239.5 MB〜开始10 BG

bytesto TB- 从10994579406848 bytes= 10239.5 GB〜开始10 TB

所有单位转换都从10 + <unit>.

供参考 - Postgres官方文档

  • 此示例不适用于大写表名 (3认同)
  • 在较新的 Postgres 版本(例如 12.4)中,此查询会出现错误 - 修复方法是在 `table_name` 周围使用 quote_ident()](/sf/answers/4579255901/)。 (3认同)

小智 12

SELECT nspname || '.' || relname AS "relation",
    pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
  FROM pg_class C
  LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
  WHERE nspname NOT IN ('pg_catalog', 'information_schema')
    AND C.relkind <> 'i'
    AND nspname !~ '^pg_toast'
  ORDER BY pg_total_relation_size(C.oid) DESC
  ;

Run Code Online (Sandbox Code Playgroud)

信用: https://makandracards.com/makandra/52141-postgresql-how-to-show-table-sizes


mic*_*ael 11

如果您要查找总数、toast 和索引大小的详细信息,请使用以下命令:

SELECT *, pg_size_pretty(total_bytes) AS total
, pg_size_pretty(index_bytes) AS INDEX
, pg_size_pretty(toast_bytes) AS toast
, pg_size_pretty(table_bytes) AS TABLE
FROM (
SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes 
FROM (
  SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
          , c.reltuples AS row_estimate
          , pg_total_relation_size(c.oid) AS total_bytes
          , pg_indexes_size(c.oid) AS index_bytes
          , pg_total_relation_size(reltoastrelid) AS toast_bytes
      FROM pg_class c
      LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
      WHERE relkind = 'r'
  ) a
) a ORDER BY total_bytes DESC;
Run Code Online (Sandbox Code Playgroud)


aki*_*aki 10

SELECT
   relname as "Table",
   pg_size_pretty(pg_total_relation_size(relid)) As "Size",
   pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid)) as "External Size"
   FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC;
Run Code Online (Sandbox Code Playgroud)

取自这里https://wiki-bsse.ethz.ch/display/ITDOC/Check+size+of+tables+and+objects+in+PostgreSQL+database


mic*_*czy 6

我喜欢以下声明:

SELECT 
  table_name, 
  pg_size_pretty( pg_total_relation_size(quote_ident(table_name))), 
  pg_total_relation_size(quote_ident(table_name))
FROM 
  information_schema.tables
WHERE 
  table_schema = 'public'
ORDER BY 
  pg_total_relation_size(quote_ident(table_name)) DESC
Run Code Online (Sandbox Code Playgroud)

您可以以漂亮的格式查看总大小,但它的排序也正确。


Atu*_*uri 6

可以从psql控制台轻松完成此操作,请参阅元命令参考

桌子尺寸:\dt+

数据库大小\l+

索引大小:\di+

对于表大小,首先使用连接数据库\c db_name

请注意:上面不会按照OP的要求按大小排序,但它仍然可以提供帮助。


小智 5

select table_name,n_live_tup, pg_size_pretty(pg_relation_size(table_name))
from information_schema.tables
inner join pg_stat_user_tables  on table_name=relname
where table_schema = 'public'
order by 2 desc
Run Code Online (Sandbox Code Playgroud)

另一种选择


小智 5

您可以获得总关系大小和关系大小,这可能会有所不同,具体取决于您的表关系。现在介绍如何获取数据库中前 100 个表:

SELECT schemaname                                    AS table_schema,
       relname                                       AS table_name,
       PG_SIZE_PRETTY(PG_TOTAL_RELATION_SIZE(relid)) AS total_size,
       PG_SIZE_PRETTY(PG_RELATION_SIZE(relid))       AS data_size,
       PG_SIZE_PRETTY(PG_TOTAL_RELATION_SIZE(relid) - PG_RELATION_SIZE(relid))
                                                     AS external_size
FROM pg_catalog.pg_statio_user_tables
ORDER BY PG_TOTAL_RELATION_SIZE(relid) DESC,
         PG_RELATION_SIZE(relid) DESC
LIMIT 100;
Run Code Online (Sandbox Code Playgroud)


Kon*_*ard 5

此查询为每个表提供:

  • total_size- 人类可读格式的表和索引的总大小
  • data_size- 桌子本身的大小
  • index_size- 该表的索引大小
  • rows- 表中的行数
  • total_row_size- 每行索引和数据的大致大小
  • row_size- 每行数据的大概大小

在 SQL Fiddle 中执行

SELECT
  nspname                                               AS "schema",
  pg_class.relname                                      AS "table",
  pg_size_pretty(pg_total_relation_size(pg_class.oid))  AS "total_size",
  pg_size_pretty(pg_relation_size(pg_class.oid))        AS "data_size",
  pg_size_pretty(pg_indexes_size(pg_class.oid))         AS "index_size",
  pg_stat_user_tables.n_live_tup                        AS "rows",
  pg_size_pretty(
    pg_total_relation_size(pg_class.oid) / 
    (pg_stat_user_tables.n_live_tup + 1)
  )                                                     AS "total_row_size",
  pg_size_pretty(
    pg_relation_size(pg_class.oid) / 
    (pg_stat_user_tables.n_live_tup + 1)
  )                                                     AS "row_size"
FROM 
  pg_stat_user_tables 
JOIN 
  pg_class
ON
  pg_stat_user_tables.relid = pg_class.oid
JOIN 
  pg_catalog.pg_namespace AS ns
ON
  pg_class.relnamespace = ns.oid
-- WHERE
--   nspname = 'public'
ORDER BY 
  pg_total_relation_size(pg_class.oid) DESC;
Run Code Online (Sandbox Code Playgroud)

要按模式过滤,请取消注释该WHERE子句。

为了更好地理解查询:

pg_size_pretty(<numeric_value>)- 将字节数转换为人类可读的格式。

pg_total_relation_size(<relation_id>)- 获取表及其索引的总大小(以字节为单位)

pg_relation_size(<relation_id>)- 获取关系(表/索引)大小(以字节为单位) 。

pg_indexes_size(<relation_id>)- 获取关系的索引大小(以字节为单位)

pg_stat_user_tables.n_live_tup- 是表中的行数。

输出可能如下所示:

   schema    |                table                | total_size | data_size  | index_size |  rows  | total_row_size |  row_size  
-------------+-------------------------------------+------------+------------+------------+--------+----------------+------------
 public      | mp                                  | 66 MB      | 32 MB      | 34 MB      | 200659 | 344 bytes      | 167 bytes
 hdb_catalog | event_invocation_logs               | 18 MB      | 16 MB      | 1736 kB    |  11034 | 1667 bytes     | 1495 bytes
 hdb_catalog | event_log                           | 6760 kB    | 5032 kB    | 1608 kB    |  11038 | 627 bytes      | 466 bytes
 public      | links                               | 3208 kB    | 760 kB     | 2416 kB    |   7565 | 434 bytes      | 102 bytes
 hdb_catalog | hdb_cron_event_invocation_logs      | 1712 kB    | 1464 kB    | 216 kB     |   1280 | 1368 bytes     | 1170 bytes
 hdb_catalog | hdb_cron_events                     | 672 kB     | 208 kB     | 424 kB     |   1600 | 429 bytes      | 133 bytes
 public      | selectors_cache                     | 656 kB     | 48 kB      | 576 kB     |    333 | 2011 bytes     | 147 bytes
 public      | objects                             | 408 kB     | 120 kB     | 256 kB     |   1288 | 324 bytes      | 95 bytes
 public      | strings                             | 360 kB     | 96 kB      | 160 kB     |    451 | 815 bytes      | 217 bytes
 public      | promise_links                       | 328 kB     | 8192 bytes | 256 kB     |      0 | 328 kB         | 8192 bytes
 hdb_catalog | hdb_metadata                        | 296 kB     | 48 kB      | 32 kB      |      1 | 148 kB         | 24 kB
 public      | bool_exp                            | 176 kB     | 16 kB      | 128 kB     |     24 | 7208 bytes     | 655 bytes
 public      | links__tables                       | 120 kB     | 0 bytes    | 112 kB     |      0 | 120 kB         | 0 bytes
 public      | numbers                             | 120 kB     | 0 bytes    | 112 kB     |      0 | 120 kB         | 0 bytes
 hdb_catalog | hdb_schema_notifications            | 64 kB      | 8192 bytes | 16 kB      |      1 | 32 kB          | 4096 bytes
 hdb_catalog | hdb_version                         | 48 kB      | 8192 bytes | 32 kB      |      1 | 24 kB          | 4096 bytes
 storage     | buckets                             | 32 kB      | 8192 bytes | 16 kB      |      1 | 16 kB          | 4096 bytes
 public      | reserved                            | 32 kB      | 8192 bytes | 16 kB      |      8 | 3640 bytes     | 910 bytes
 hdb_catalog | hdb_source_catalog_version          | 32 kB      | 8192 bytes | 16 kB      |      1 | 16 kB          | 4096 bytes
 hdb_catalog | hdb_scheduled_events                | 24 kB      | 0 bytes    | 16 kB      |      0 | 24 kB          | 0 bytes
 storage     | files                               | 24 kB      | 0 bytes    | 16 kB      |      0 | 24 kB          | 0 bytes
 hdb_catalog | hdb_scheduled_event_invocation_logs | 16 kB      | 0 bytes    | 8192 bytes |      0 | 16 kB          | 0 bytes
 hdb_catalog | hdb_action_log                      | 16 kB      | 0 bytes    | 8192 bytes |      0 | 16 kB          | 0 bytes
(23 rows)
Run Code Online (Sandbox Code Playgroud)

聚苯乙烯

我做+1只是为了忽略devision by zero错误,如果你知道更好的方法请告诉我。

更多函数请参见 PostgreSQL 文档