Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 December PostgreSQL: How to check Table Fragmentation using pgstattuple module

PostgreSQL: How to check Table Fragmentation using pgstattuple module

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

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:

1
CREATE EXTENSION pgstattuple;
Create one sample table:
CREATE TABLE tbl_ItemTransactions
1
2
3
4
5
(
TranID SERIAL
,TransactionDate TIMESTAMPTZ
,TransactionName TEXT
);
Insert few millions of data:
1
2
3
4
INSERT INTO tbl_ItemTransactions
(TransactionDate, TransactionName)
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:
1
SELECT *FROM pgstattuple('public.tbl_itemtransactions');
The Result:
1
2
3
4
5
6
7
8
9
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
Dec 22, 2016Anvesh Patel
SQL Server: Script to find Outdated Index StatisticsSQL Server: Kill your running Sessions or Change Database Mode (Multi_User to Single_User)
Comments: 4
  1. kashi
    April 19, 2017 at 6:52 pm

    is this new feature ? I am using 9.3 I could not create extension.

    • Anvesh Patel
      April 19, 2017 at 7:26 pm

      Yes, it is possible. Please visit this link… https://www.postgresql.org/docs/9.3/static/pgstattuple.html
      Can you share your error please…

  2. Pavanteja
    February 1, 2018 at 9:57 am

    I’m on PostgreSQL 9.1. How can I find corrupted pages for a relation and for a database as a whole. And fragmented pages for relation & database by using postgresql internals?

    Regards,
    Pavan

    • Anvesh Patel
      February 1, 2018 at 5:23 pm

      Try this,
      https://www.dbrnd.com/2016/10/postgresql-script-to-find-total-live-tuples-and-dead-tuples-row-of-a-table-execute-vacuum-remove-fragmentation-improve-performance/

Anvesh Patel

Database Engineer

December 22, 2016 PostgreSQL, PostgreSQL DBA ScriptAnvesh Patel, database, database research and development, dbrnd, dead tuple, fragmentation, free space, live tuple, pgstattuple, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....