What's the difference between pg_table_size, pg_relation_size & pg_total_relation_size? (PostgreSQL)

后端 未结 2 1984
感情败类
感情败类 2020-12-10 01:24

What\'s the difference between pg_table_size(), pg_relation_size() & pg_total_relation_size()?

I understand the basic diff

2条回答
  •  死守一世寂寞
    2020-12-10 02:20

    pg_table_size: Disk space used by the specified table, excluding indexes (but including TOAST, free space map, and visibility map)

    pg_relation_size: The size of the main data fork of the relation

    select
          pg_size_pretty(pg_total_relation_size(relid)) as total_size,
          pg_size_pretty(pg_relation_size(relid, 'main')) as relation_size_main,
          pg_size_pretty(pg_relation_size(relid, 'fsm')) as relation_size_fsm,
          pg_size_pretty(pg_relation_size(relid, 'vm')) as relation_size_vm,
          pg_size_pretty(pg_relation_size(relid, 'init')) as relation_size_init,
          pg_size_pretty(pg_table_size(relid)) as table_size,
          pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid)) as external_size
     from 
          pg_catalog.pg_statio_user_tables
    where 
          schemaname = 'XXXX'
      and relname like 'XXXXXX';
    
    total_size         | 6946 MB
    relation_size_main | 953 MB
    relation_size_fsm  | 256 kB
    relation_size_vm   | 32 kB
    relation_size_init | 0 bytes
    table_size         | 6701 MB
    external_size      | 5994 MB
    

    so pg_table_size is not only the sum of all the return values of pg_relation_size but you have to add toast size

    toast_bytes         | 5748 MB
    

提交回复
热议问题