This article is half-done without your Comment! *** Please share your thoughts via Comment ***
If you don’t know about that data pages, you must visit below article.
What is MVCC ?
Because of MVCC architecture, PostgreSQL generates dead rows at every UPDATE and DELETE.
You must remove the fragmentation by executing VACUUM ANALYZE command which removes the dead rows and update the related database statistics.
You can also use VACUUM VERBOSE to troubleshoot total number of data pages, total number of removable dead row version while executing a VACUUM ANALYZE.
(Check your query message window, where you can find detailed information generated by VACUUM VERBOSE.)This is really very helpful information for PostgreSQL DBA to make sure about the fragmentation.
I have prepared a detailed demonstration of this.
Create a table with sample data:
1 2 3 4 5 6 7 8 9 10 11 |
CREATE TABLE tbl_ItemTransactions ( TranID SERIAL ,TransactionDate TIMESTAMPTZ ,TransactionName TEXT ) DISTRIBUTED BY (TranID); INSERT INTO tbl_ItemTransactions (TransactionDate, TransactionName) SELECT x, 'ggkproduct' FROM generate_series('2015-01-01 00:00:00'::timestamptz, '2016-08-01 00:00:00'::timestamptz,'5 seconds'::interval) a(x); |
Check the size of table:
1 2 3 |
SELECT pg_size_pretty(pg_total_relation_size('tbl_ItemTransactions')) AS TableSize; The size is: "496 MB" |
Execute VACUUM VERBOSE to get data pages and dead row versions related information:
1 2 3 |
VACUUM VERBOSE ANALYZE tbl_ItemTransactions; "tbl_itemtransactions": found 0 removable, 2496960 nonremovable row versions in 3970 pages (seg0 sdw1:40000 pid=17647) |
Execute one DELETE to generate few dead tuples:
1 2 |
DELETE FROM tbl_ItemTransactions WHERE TransactionDate BETWEEN '2016-04-01' AND '2016-08-01'; |
Now, examine the result of VACUUM VERBOSE where you can find 527040 removable rows:
You can find total 3970 pages and 527040 removable rows.
1 2 3 4 |
VACUUM VERBOSE ANALYZE tbl_ItemTransactions; INFO: "tbl_itemtransactions": found 527040 removable, 1969920 nonremovable row versions in 3970 pages (seg1 sdw1:40001 pid=17649) 839 pages contain useful free space. |
Now, execute one more time and you can find 3132 pages and 0 removable rows:
1 2 3 4 |
VACUUM VERBOSE ANALYZE tbl_ItemTransactions; "tbl_itemtransactions": found 0 removable, 1969920 nonremovable row versions in 3132 pages (seg0 sdw1:40000 pid=17647) 1 pages contain useful free space. |
Check the size of table:
1 2 3 |
SELECT pg_size_pretty(pg_total_relation_size('tbl_ItemTransactions')) AS TableSize; The size is: "392 MB" |
Leave a Reply