Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get each row size of a particular table in postgresql..?

Tags:

postgresql

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.

like image 923
Deepesh Goel Avatar asked Dec 11 '15 11:12

Deepesh Goel


People also ask

How do you get the size of a table in Postgres?

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') );

How do I find the index size in PostgreSQL?

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.

How do I find the size of a row in SQL?

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.

Can we use Rownum in PostgreSQL?

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.


2 Answers

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'

like image 148
Kaushal Sachan Avatar answered Oct 23 '22 03:10

Kaushal Sachan


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.

like image 24
a_horse_with_no_name Avatar answered Oct 23 '22 05:10

a_horse_with_no_name