PostgreSQL: How to check Table Fragmentation using pgstattuple module

This is very important task for PostgreSQL DBA to check the fragmentation level of Table.postgresql fragmentation

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.

What is Multi Version Concurrency Control (MVCC)

PostgreSQL: Short note on VACUUM, VACUUM FULL and ANALYZE

PostgreSQL: Script to find total Live Tuples and Dead Tuples (Row) of a Table

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 one sample table:
CREATE TABLE tbl_ItemTransactions

Insert few millions of data:

Check the tuple information using pgstattuple:

The Result:

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, 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.

More from