This is very important task for PostgreSQL DBA to check the fragmentation level of Table.
Please do not forget about that the PostgreSQL is based on MVCC architecture. Which is good in one way, but bad in another way.
Using below article, You must read about MVCC and also access other related articles.
PostgreSQL provides pgstattuple module to get all tuples information of a Table. You can find information like live_tuple, dead_tuple, free_space and other.
Using this information you can find fragmentation of table which you can remove using VACUUM / VACUUM FULL command.
You must install the pgstattuple to find tuples related information. You can also use pgstatindex() to find information related to indexes.
Load pgstattuple module:
CREATE EXTENSION pgstattuple;
Create one sample table:
CREATE TABLE tbl_ItemTransactions
Insert few millions of data:
INSERT INTO tbl_ItemTransactions
SELECT x, 'dbrnd'
FROM generate_series('2014-01-01 00:00:00'::timestamptz, '2016-08-01 00:00:00'::timestamptz,'2 seconds'::interval) a(x);
Check the tuple information using pgstattuple:
SELECT *FROM pgstattuple('public.tbl_itemtransactions');
table_len | 2125627392
tuple_count | 40737601
tuple_len | 1873929646
tuple_percent | 88.16
dead_tuple_count | 45
dead_tuple_len | 7787
dead_tuple_percent | 0.88
free_space | 9923
free_percent | 1.96
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.