Is it possible to get each row size of a particular table in postgres?
For example a table has 50 rows and total table size is 10gb but i want to know each row size rather than complete table size.
To determine the size of a Postgres database table from the command line, log in to your server using SSH and access the database with the table you want to check. Type this code in the command line to determine the size of any of the database's tables. SELECT pg_size_pretty( pg_total_relation_size('tablename') );
PostgreSQL index size To get total size of all indexes attached to a table, you use the pg_indexes_size() function. The pg_indexes_size() function accepts the OID or table name as the argument and returns the total disk space used by all indexes attached of that table.
Below is a SQL query to find row size. The query uses DATALENGTH function, which returns the number of bytes used to represent a column data. A row may consist of fixed, variable data types. A varchar is a variable datatype which means that a varchar(50) column may contain a value with only 20 characters.
Postgresql does not have an equivalent of Oracle's ROWNUM. In many cases you can achieve the same result by using LIMIT and OFFSET in your query.
Function to calculate row size of postgress table row is something like this
SELECT sum(pg_column_size(t.*)) as filesize, count(*) as filerow FROM TABLE_NAME as t;
replace TABLE_NAME
with your table name;
to add condition t.COLUMN_NAME = 'YOUR_VALUE'
There is no function that calculates the size of a row, only a function that calculates the size of a column value. So you can do something like this:
select pg_column_size(column_1) +
pg_column_size(column_2) +
pg_column_size(column_3) as row_size
from the_table;
This could be automated using dynamic SQL and a PL/pgSQL function if you need this.
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