Summarize Heap-Only Tuple and Index-Only Scans in PostgreSQL
Tags: postgressql, apacheage, database, index
Published: 2023-06-30 10:05:42


In the world of PostgreSQL, where performance and optimization are paramount, two powerful techniques stand out: Heap Only Tuple (HOT) and Index-Only Scans. These features aim to improve query execution and enhance data access efficiency. In this blog post, we will delve into Chapter 7 of [The Internals of PostgreSQL](https://www.interdb.jp/pg/pgsql07.html), exploring the concepts of Heap Only Tuple and Index-Only Scans, their benefits, and how they can elevate the performance of your PostgreSQL database.


## Heap Only Tuple (HOT)
Heap Only Tuple (HOT) is a feature introduced in PostgreSQL 8.3 improves index and table page utilization during row updates by eliminating the need for inserting index tuples and reducing VACUUM processing. This optimization minimizes disk I/O, storage requirements, and improves overall performance.


- Update a Row Without HOT:
When updating a row without using HOT in PostgreSQL, both the new table tuple and the new index tuple are inserted into the index page. This results in increased consumption of index page space and higher costs for both insertion and vacuuming of the index tuples.

- How HOT Performs:
The Heap Only Tuple (HOT) technique in PostgreSQL optimizes updates by minimizing the insertion of index tuples when updating a row on the same table page. Instead, specific bits in the old and new tuples are set to indicate the update, resulting in reduced consumption of index and table pages. This approach also minimizes the number of tuples requiring VACUUM processing and improves overall performance. The cost of defragmentation is lower compared to regular VACUUM processing since index tuples are not removed. HOT provides an efficient mechanism for handling updates, enhancing PostgreSQL's data storage and retrieval efficiency.



**The Cases in which HOT is not available**
to better understand how HOT performs, it's important to be aware of the cases where HOT is not available:

1. When the updated tuple is stored on a different page that does not store the old tuple, the index tuple pointing to the tuple is also inserted in the index page.

2. When the key value of the index tuple is updated, a new index tuple is inserted in the index page.


##Index-Only Scans
In PostgreSQL, Index-Only Scans optimize query performance by directly using the index key without accessing the corresponding table pages, reducing I/O costs. This feature, available since version 9.2, is commonly found in other commercial RDBMS like DB2 and Oracle. When all requested data in a SELECT statement is present in the index key, Index-Only Scans are employed. However, PostgreSQL needs to check the visibility of tuples and lacks transaction information in index tuples. To address this, PostgreSQL utilizes the visibility map of the target table. If all tuples on a page are visible, it can rely on the index tuple's key without accessing the table page. Otherwise, it reads the table tuple and performs visibility checks. By efficiently leveraging the visibility map and index tuples, Index-Only Scans minimize I/O costs and enhance query performance, making data retrieval more efficient.


#### Conclusion:
Heap Only Tuple (HOT) and Index-Only Scans are important optimizations in PostgreSQL that improve performance and efficiency. HOT reduces the need for inserting index tuples during updates, reducing resource consumption and the need for VACUUM processing. Index-Only Scans minimize I/O costs by directly utilizing index keys, enhancing query performance. These features demonstrate PostgreSQL's commitment to optimizing query execution and improving overall database efficiency.