Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the right way to calculate table size in postgres?

Tags:

sql

postgresql

I'm working on a project where calculations are being made for certain tables in the postgres DB, and in different parts of code, there are 2 different calculations being made:

select pg_total_relation_size ('TABLENAME');

and the second one:

select (relpages*8/1024) from pg_class where relname='TABLENAME' (Size in MB)

I know that pg_total_relation_size is what I need to use, but I was wondering what does this other calculation represents, and if the second one is also some kind of size calculation, why am I getting different results for the same table?

like image 781
Zoran Trifunovski Avatar asked Jul 18 '14 12:07

Zoran Trifunovski


People also ask

What is the capacity of a table in PostgreSQL?

One of those limits is the Maximum Table Size, listed as 32TB. It's been that way for many years now.

How big is too big for a PostgreSQL table?

PostgreSQL normally stores its table data in chunks of 8KB. The number of these blocks is limited to a 32-bit signed integer (just over two billion), giving a maximum table size of 16TB.

How do you measure a Greenplum table?

How to check the size of a table? psql> select pg_size_pretty(pg_relation_size('schema. tablename')); Replace schema.

How many columns should a Postgres table have?

There is a limit on how many columns a table can contain. Depending on the column types, it is between 250 and 1600.

How to get the size of a table in PostgreSQL?

As with most database systems, PostgreSQL offers us various system functions to easily calculate the disk size of the objects. We can get the size of a table using these functions. These functions; pg_table_size: The size of a table, excluding indexes.

How to check PostgreSQL database size with pgadmin?

First, open a shell and connect to the Postgres terminal. Then use inbuild function pg_database_size () to find database size in PostgreSQL server. You can also use pg_size_pretty () function along with above to determine database size in human readable format like KB, MB, and GB etc. 2. Check PostgreSQL Database Size with pgAdmin

How to list indexes of a table in PostgreSQL?

The indexes of a table in PostgreSQL can be in different tablespace (on different disk if desired). In addition, we could list tables using pg_class instead of pg_tables. Now it’s your creativity. If you want to list the databases by their size, you should read the following article.

What is PG_size_Pretty in PostgreSQL?

pg_relation_size: The size of an object (table index, etc.) on disk. It is possible to get more detailed information from this function with additional parameters. pg_size_pretty: Other functions return results in bytes. Converts this into readable format (kb, mb, gb) index (primary key) in size_test_table. We get the table size as follows;


2 Answers

You can use pg_size_pretty(pg_total_relation_side(oid)) to pretty print table size:

select  nspname
,       relname
,       pg_size_pretty(pg_total_relation_size(c.oid)) as "size"
from    pg_class c
left join
        pg_namespace n 
on      n.oid = c.relnamespace
where   nspname not in ('pg_catalog', 'information_schema')
order by
        pg_relation_size(c.oid) desc;
like image 91
Andomar Avatar answered Oct 05 '22 04:10

Andomar


pg_total_relation_size - is the right way to see full table size because it includes disk space utilized by indices and TOAST data.

When you do the second-one select - you get table size only (relpages is table size in 8Kb pages). You can get TOAST and index sizes from pg_class as well but you need to do some more sql queries as described in here and sum all the sizes you've got.

like image 36
Konstantin V. Salikhov Avatar answered Oct 05 '22 04:10

Konstantin V. Salikhov