Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

measure the size of a Table Row and Data Page

In this post, I am sharing a script to measure the size of a PostgreSQL Table Row.

I taken reference from this dba.statckexchange.

But again, I would like to share this information with some additional information.

Fillfactor storage parameter of PostgreSQL Table.

Before changing the default value of Fillfactor, we should measure the size of Table Row.

For knowing the row size is very important because if table row size is larger, we should not change the default value of Fillfactor.

When we are doing performance optimization, this is very important to find the size of the Data page and Table row, otherwise unnecessary we are dealing with high fragmentation and executing VACUUM FULL or VACUUM again and again.

If your total row size is under 8kb, you can take decision to alter table storage parameters.
Create sample table using JSON data type:

 ,DepartName VARCHAR(250)
 ,EmployeeDetails JSON
 ,Address JSON
Insert few sample JSON formatted record:

 ,'{"firstName": "Anvesh", "lastName": "Patel"}'
 {"address" : 
   "India": "Hyderabad"
   ,"USA": "Newyork"
 ,'{"firstName": "Neevan", "lastName": "Patel"}'
 {"address" : 
   "India": "Ahmedabad"
   ,"USA": "Washington DC"
 ,'{"firstName": "Eric", "lastName": "Lorn"}'
 {"address" : 
   "India": "Mumbai"
   ,"USA": "Chicago"
Script to measure the size of Table row and Data page:

WITH cteTableInfo AS 
  COUNT(1) AS ct
  ,SUM(length(t::text)) AS TextLength  
  ,'public.tbl_testjson'::regclass AS TableName  
 FROM public.tbl_testjson AS t  
,cteRowSize AS 
   SELECT ARRAY [pg_relation_size(TableName)
               , pg_relation_size(TableName, 'vm')
               , pg_relation_size(TableName, 'fsm')
               , pg_table_size(TableName)
               , pg_indexes_size(TableName)
               , pg_total_relation_size(TableName)
               , TextLength
             ] AS val
        , ARRAY ['Total Relation Size'
               , 'Visibility Map'
               , 'Free Space Map'
               , 'Table Included Toast Size'
               , 'Indexes Size'
               , 'Total Toast and Indexes Size'
               , 'Live Row Byte Size'
             ] AS Name
   FROM cteTableInfo
 unnest(name) AS Description
 ,unnest(val) AS Bytes
 ,pg_size_pretty(unnest(val)) AS BytesPretty
 ,unnest(val) / ct AS bytes_per_row
FROM cteTableInfo, cteRowSize
UNION ALL SELECT '------------------------------', NULL, NULL, NULL
UNION ALL SELECT 'TotalRows', ct, NULL, NULL FROM cteTableInfo
UNION ALL SELECT 'LiveTuples', pg_stat_get_live_tuples(TableName), NULL, NULL FROM cteTableInfo
UNION ALL SELECT 'DeadTuples', pg_stat_get_dead_tuples(TableName), NULL, NULL FROM cteTableInfo;


Popular posts from this blog

PostgreSQL Database startup / shutdown /restart

How to Get Table Size, Database Size, Indexes Size, schema Size, Tablespace Size, column Size in PostgreSQL Database

2017 Shortern URL - Other Website Like ADFOC.US

ORA-01261: Parameter db_recovery_file_dest destination string cannot be translated ORA-01262: Stat failed on a file destination directory Linux-x86_64 Error: 2: No such file or directory

PostgreSQL Pgbadger Installation On Linux