Calcular tamanho de uma tabela no Postgres

Published: 2018-03-14, Updated: 2021-11-08

Links

Pegar tamanho da tabela

Tamanho apenas dos dados da tabela

SELECT pg_size_pretty (pg_relation_size('actor'));

Tamanho com indices

SELECT pg_size_pretty (pg_total_relation_size ('actor'));

Tamanho apenas dos indices da tabela

SELECT pg_size_pretty (pg_indexes_size('actor'));

Tamanho de um indice especifico

SELECT pg_size_pretty(pg_relation_size('my_idx'))

Tamanho aproximado da linha

SELECT pg_column_size(t), octet_length(t::text) FROM activemq_msgs AS t 

Maiores tabelas

SELECT
	nspname AS "schema", relname AS "table",
	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
LIMIT 10;

De forma detalhada (lento)

SELECT l.what, l.nr AS "bytes/ct"
     , CASE WHEN is_size THEN pg_size_pretty(nr) END AS bytes_pretty
     , CASE WHEN is_size THEN nr / x.ct END          AS bytes_per_row
FROM  (
   SELECT min(tableoid)        AS tbl      -- same as 'public.tbl'::regclass::oid
        , count(*)             AS ct
        , sum(length(t::text)) AS txt_len  -- length in characters
   FROM   activemq_msgs t  -- provide table name *once*
   ) x
 , LATERAL (
   VALUES
      (true , 'core_relation_size'               , pg_relation_size(tbl))
    , (true , 'visibility_map'                   , pg_relation_size(tbl, 'vm'))
    , (true , 'free_space_map'                   , pg_relation_size(tbl, 'fsm'))
    , (true , 'table_size_incl_toast'            , pg_table_size(tbl))
    , (true , 'indexes_size'                     , pg_indexes_size(tbl))
    , (true , 'total_size_incl_toast_and_indexes', pg_total_relation_size(tbl))
    , (true , 'live_rows_in_text_representation' , txt_len)
    , (false, '------------------------------'   , NULL)
    , (false, 'row_count'                        , ct)
    , (false, 'live_tuples'                      , pg_stat_get_live_tuples(tbl))
    , (false, 'dead_tuples'                      , pg_stat_get_dead_tuples(tbl))
   ) l(is_size, what, nr);

keywords

tamanho da tabela, table size, database size, tamanho banco de dados, tamanho do banco de dados tamanho tabela, postgres commands, postgresql commands


Conversão de fotos no Linux Configurando o openbox do zero

Comments