In this post, I am sharing one script to measure the size of a PostgreSQL Table Row.
I have taken reference from this dba.statckexchange.
But again, I would like to share this information with some additional information.
Before changing the default value of Fillfactor, we should measure the size of Table Row.
This is very important to know because if table row 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 Data page and Table row, otherwise unnecessary we are dealing with high fragmentation and executing VACUUM FULL again and again.
If your total row size is under 8kb, you can take decision to alter table storage parameters.
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.table_name'::regclass AS TableName
FROM public.tbl_testjson AS t
SELECT ARRAY [pg_relation_size(TableName)
, pg_relation_size(TableName, 'vm')
, pg_relation_size(TableName, 'fsm')
] 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
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;
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.