Course contentsShow
← Databases & SQL
Lesson 838 of 1,9538. PostgreSQL InternalsFree lesson

Heap-Only Tuple (HOT) Updates

You will optimize storage by leveraging HOT updates to avoid index overhead during row modifications.

In this lesson, you will learn how to optimize database performance by using Heap-Only Tuple (HOT) updates to avoid unnecessary index maintenance during row modifications.

The Problem: Index Bloat

In PostgreSQL, every table row is a "tuple." When you update a row, PostgreSQL doesn't overwrite the existing data. Instead, it marks the old tuple as dead and inserts a brand new version of the row. Normally, if that table has indexes (like a B-Tree), the database must also create a new entry in every index to point to the new tuple—even if the indexed column hasn't changed. This causes significant "write amplification" and index bloat.

The Solution: HOT Updates

A HOT update is a clever optimization that skips index updates when possible. If you update a row but do not change any of the columns that are currently indexed, PostgreSQL realizes it doesn't need to update the index pointers.

Instead, it places the new version of the tuple on the same "page" (the physical storage block) as the old version. It then creates a "link" from the old tuple to the new one. Because the index still points to the old tuple, and the old tuple knows exactly where the new version lives on the same page, the index remains perfectly valid without any changes.

Putting it into Practice

Imagine a users table:

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username TEXT UNIQUE,
    login_count INT
);

If you execute this:

UPDATE users SET login_count = login_count + 1 WHERE id = 1;

Because login_count is not part of the PRIMARY KEY or the UNIQUE index on username, PostgreSQL will perform a HOT update. The index on id and username remains untouched, saving both I/O and storage space. If you had updated the username instead, the database would be forced to perform a standard update, requiring a full index modification.

To keep HOT updates efficient, ensure your fillfactor is set correctly so there is enough "free space" on the page to accommodate these new tuple versions.

Key Takeaway: HOT updates allow PostgreSQL to modify rows without updating indexes, provided the change doesn't affect indexed columns and the page has enough free space.