postgresql list and order tables by size

前端 未结 9 1199
不思量自难忘°
不思量自难忘° 2020-12-12 11:16

How can I list all the tables of a PostgreSQL database and order them by size?

9条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 12:03

    This will show you the schema name, table name, size pretty and size (needed for sort).

    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;
    

    I build this based on the solutions from here list of schema with sizes (relative and absolute) in a PostgreSQL database

提交回复
热议问题