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?
One of those limits is the Maximum Table Size, listed as 32TB. It's been that way for many years now.
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 to check the size of a table? psql> select pg_size_pretty(pg_relation_size('schema. tablename')); Replace schema.
There is a limit on how many columns a table can contain. Depending on the column types, it is between 250 and 1600.
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.
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
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.
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;
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With