iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🚨

Summary of Tuning Strategies for INSERT-Heavy Tables in PostgreSQL

に公開
2026/01/27

1. Introduction

When using PostgreSQL, it is common to perform INSERT, UPDATE, and DELETE operations. However, in specific use cases such as log tables, there may be scenarios where only INSERT operations are executed heavily, while UPDATE and DELETE rarely occur. In such cases, tuning must be performed based on a different approach than for regular tables.

I thought I had understood this once, but after a few months I found myself confused again, so I’ve organized what I researched about VACUUM, FREEZE, and tuning strategies for INSERT-heavy tables, partly as a memo for myself.

The environment used for verification is Amazon RDS for PostgreSQL 14.20.

2. What is AUTO VACUUM?

PostgreSQL adopts an architecture called MVCC (Multi-Version Concurrency Control) . Each SQL statement accesses a snapshot of the data as it existed at the beginning of the transaction, rather than the current data, thereby preventing update conflicts.

To realize this mechanism, even when an UPDATE or DELETE is executed, the targeted record is not physically deleted immediately. Instead, it is kept with a deletion mark to allow other transactions to reference it if needed.

Once such deleted rows are no longer accessible by any transaction, they become dead tuples and are treated as obsolete data. Leaving dead tuples untouched can lead to index inefficiency and degraded query performance. The role of VACUUM is to reclaim (remove) these dead tuples.

VACUUM can also be executed manually, but the database may automatically run it for maintenance purposes depending on how much the database is used. This automatic execution is called AUTO VACUUM.

3. What is FREEZE?

AUTO VACUUM has three roles, including reclaiming dead tuples. The following table briefly summarizes the three roles of AUTO VACUUM:

Role Description
Reclaim dead tuples Reclaim dead tuples generated by UPDATE and DELETE to prevent index inefficiency and degraded query performance.
Update statistics Update statistics so that the query planner can create optimal execution plans.
Prevent transaction ID wraparound FREEZE transaction IDs assigned to old records to prevent data corruption caused by transaction ID wraparound.

The key role for this article is preventing transaction ID wraparound.

In PostgreSQL, each transaction is assigned a unique “transaction ID.” Each record also receives this transaction ID, which is used in the MVCC mechanism to determine whether the record is from the past or the future from the perspective of the current transaction.

Transaction IDs are assigned as 32-bit unsigned integers, with a maximum value of approximately 4 billion (2^32). When the limit is reached, the value wraps back to 0 and begins to be reused (wraparound), making it impossible to distinguish between past and future transaction IDs.

To avoid this, PostgreSQL FREEZEs records whose transaction IDs have become sufficiently old relative to a defined threshold. Specifically, the transaction ID of a frozen record is replaced with the special value FrozenTransactionId (2), which represents the infinite past.

This prevents old records from being incorrectly recognized as future records even after a transaction ID wraparound.

4. When is FREEZE Executed?

FREEZE, which is performed as part of AUTO VACUUM, involves several parameters. The three parameters related to FREEZE are as follows:

Parameter Description Default Value
vacuum_freeze_table_age
(when set per table: autovacuum_freeze_table_age)
Threshold: when the difference between the current transaction ID and the oldest transaction ID for a table exceeds this value, FREEZE is triggered. Can be set per database or per table. 150 million
vacuum_freeze_min_age
(when set per table: autovacuum_freeze_min_age)
Threshold: when freezing transaction IDs, IDs older than this value will be frozen. Can be set per database or per table. 50 million
autovacuum_freeze_max_age Threshold: when the difference between the current transaction ID and the oldest transaction ID for the entire database exceeds this value, FREEZE is forced. Can only be set per database. 200 million

Based on these parameters, FREEZE is executed during AUTO VACUUM according to the thresholds. The three types of AUTO VACUUM behave differently, as summarized in the table below.
(Note that autovacuum_vacuum_threshold and autovacuum_vacuum_scale_factor are parameters for AUTO VACUUM itself, so their detailed explanation is omitted.)

Type Trigger Condition Description
Normal VACUUM Per table: number of dead tuples > autovacuum_vacuum_threshold + number of rows * autovacuum_vacuum_scale_factor. Reclaims dead tuples and updates statistics. Freezing is also performed, but only for blocks that contain invisible data (i.e., blocks that are already being vacuumed).
Aggressive VACUUM When a VACUUM runs and, for the table, (current transaction ID - oldest transaction ID) > vacuum_freeze_table_age. In addition to reclaiming dead tuples and updating statistics, performs freezing. Unlike normal VACUUM, it also processes blocks where all data is visible, resulting in higher I/O.
Wraparound-prevention VACUUM For the entire database: (current transaction ID - oldest transaction ID) > autovacuum_freeze_max_age. Forces an aggressive VACUUM. This is always executed even if AUTO VACUUM is disabled.

5. Why are INSERT-heavy tables dangerous?

Here is the main topic. In INSERT-heavy tables where UPDATE and DELETE rarely occur, dead tuples are not generated. Therefore, normal VACUUM, which is triggered based on the number of dead tuples, is rarely executed. Similarly, aggressive VACUUM is also rarely executed because it is only triggered when normal VACUUM runs and the condition is met.

As a result, old transaction IDs remain for a long time, and when the difference with the current transaction ID exceeds autovacuum_freeze_max_age, a wraparound-prevention VACUUM suddenly occurs, causing high I/O. If this coincides with peak access times, performance may degrade.

6. Tuning strategies for INSERT-heavy tables

6.1. Strategy ①: Make AUTO VACUUM run frequently

From PostgreSQL 13, two parameters, autovacuum_vacuum_insert_threshold and autovacuum_vacuum_insert_scale_factor, were introduced to address this issue. These parameters trigger AUTO VACUUM based on the number of INSERTs rather than the number of dead tuples.

Parameter Description Default Value
autovacuum_vacuum_insert_threshold Table-level threshold for the number of INSERTs to trigger AUTO VACUUM. Can be set per database or per table. 1000
autovacuum_vacuum_insert_scale_factor Table-level threshold for the ratio of INSERTs to the number of rows in the table to trigger AUTO VACUUM. 0.2

Specifically, AUTO VACUUM is triggered when the number of INSERTs in the table is greater than (number of rows in the table × autovacuum_vacuum_insert_scale_factor + autovacuum_vacuum_insert_threshold).

By tuning autovacuum_vacuum_insert_threshold and autovacuum_vacuum_insert_scale_factor, you can make AUTO VACUUM run frequently based on the number of INSERTs, and thus FREEZE will also be performed frequently.

For INSERT-heavy tables, additionally consider reducing the value of vacuum_freeze_min_age so that more transaction IDs are frozen in each VACUUM. However, applying the same setting to tables with many UPDATEs would cause unnecessary FREEZE operations, so caution is needed.

To configure per table, execute an SQL statement like the following:

ALTER TABLE table_name
SET (
  autovacuum_vacuum_insert_threshold = 100,
  autovacuum_vacuum_insert_scale_factor = 0.05,
  autovacuum_freeze_min_age = 10000000
);

6.2. Strategy ②: Regularly run VACUUM manually

Another strategy is to manage the timing of FREEZE by running VACUUM manually on a regular basis.

By executing the VACUUM FREEZE command, you can run VACUUM in a state equivalent to setting vacuum_freeze_min_age and vacuum_freeze_table_age to 0.

Also, to prevent FREEZE from being executed at unexpected times, consider increasing the values of vacuum_freeze_table_age and autovacuum_freeze_max_age.

Using the pg_cron extension, you can schedule periodic execution of the VACUUM FREEZE command within PostgreSQL itself, just like cron.

7. Summary

INSERT-heavy tables require tuning AUTO VACUUM with a different approach than regular tables. This article explained two strategies: "Make AUTO VACUUM run frequently" and "Regularly run VACUUM manually."

Since AUTO VACUUM is a convenient mechanism for automating maintenance, it is advisable to first consider the strategy of making AUTO VACUUM run frequently, and then consider manual VACUUM only if that is difficult.

Be sure to tune AUTO VACUUM for INSERT-heavy tables to prevent unexpectedly high I/O at unwanted times!

8. References

Chapter 13. Concurrency Control
25.1. Routine Vacuuming
Performance Tuning: 9 Techniques – About the "Infrastructure"
Master PostgreSQL in 100 Days (Day 211) – Transaction ID Wraparound Problem

Discussion