GCP- AlloyDB for PostgreSQL — new feature; adaptive autovacuum

Virinchi T
Fournine Cloud
Published in
7 min readSep 22, 2023

--

For those who are using PostgreSQL, you might be using the vacuum process already. In this post, let’s dive into why the VACUUM process is needed in the first place and how it is used check out what AlloyDB is offering which helps to balance huge workloads.

Why VACUUM is needed?

PostgreSQL’s Multi-Version Concurrency Control (MVCC) is a sophisticated mechanism that allows multiple transactions to work simultaneously without interfering with one another. It accomplishes this by creating distinct versions of each database row known as tuples, with each version corresponding to a specific moment in time. In the realm of MVCC, every transaction is assigned a unique Transaction ID (XID), which signifies the order of transaction execution.

Let’s try to understand that using an example
Imagine a busy library where multiple patrons can check out books simultaneously. Each book represents a row in the library’s database, and every transaction corresponds to a patron’s attempt to borrow a book. In this scenario, MVCC operates by creating distinct versions of each book, each version reflecting the book’s status at a particular point in time.

  • Patron A checks out a book at 10:00 AM, creating a version of the book with the checkout timestamp.
  • Simultaneously, Patron B checks out the same book at 10:05 AM, creating a different version of the book reflecting its availability to Patron B.

This way, both patrons can access the book without interference, as they are viewing different versions of it. However, as time goes on, the library’s system needs to manage these versions to avoid problems like running out of unique IDs for transactions (similar to Transaction ID wraparound) and ensuring that outdated versions of books don’t clutter the system (akin to bloat). This is similar to how PostgreSQL’s MVCC handles transactions and data management in a database, ensuring efficient and concurrent access to data.
However, MVCC introduces two notable challenges in PostgreSQL:

  1. Transaction ID wraparound: PostgreSQL employs 32-bit unsigned integers as Transaction IDs (XIDs) for tracking transactions. As transactions occur and XIDs increment, they eventually reach their maximum value and reset to zero, akin to a cyclic buffer. This reset can potentially result in data corruption.
  2. Bloat: Over time, MVCC can lead to the accumulation of redundant data in tables, indexes, and system catalogs. This redundancy, known as bloat, can degrade database performance by impeding the accuracy of the query planner and necessitating more extensive reads.

To tackle these challenges, PostgreSQL incorporates a process known as VACUUM. When manually triggered, VACUUM scans the database tables to remove inactive tuples (rows) and updates table statistics.

AUTOVACUUM

For automated maintenance, users can configure AUTOVACUUM, a background process in PostgreSQL. AUTOVACUUM initiates the VACUUM process based on configurable parameters such as
autovacuum_vacuum_cost_limit,
autovacuum_vacuum_cost_delay, vacuum_cost_page_hit, and vacuum_cost_page_miss.

Challenges with PostgreSQL AUTOVACUUM

  1. Default Settings May Not Suffice:
  • The default autovacuum settings may not be sufficient for all workloads.
  • Determining the optimal settings for a specific database or tables can be challenging.

2. Contention on Busy Systems:

  • Autovacuum can become a source of contention, especially in busy database systems.
  • Poor configuration or management of autovacuum can negatively impact overall performance.

3. Fixed Resource Budgets:

  • Autovacuum operates within fixed resource budgets, including a cost limit, number of vacuum worker processes, and memory allocation.
  • It cannot dynamically adjust vacuum workloads based on changing customer workloads or available system resources.

4. High Transaction Rates and XID Issues:

  • Databases with exceptionally high transaction rates may see the VACUUM process struggling to keep up.
  • This can result in an increase in open XIDs (transaction IDs), eventually leading to an XID wrap-around situation and causing extended system downtime.

5. Table Space Bloat:

  • When VACUUM lags behind, it can cause table space bloat by leaving many dead tuples and index entries.
  • This unnecessary storage usage impacts backup and restore times and can lead to decreased query performance.

6. Need for Careful Adjustment:

  • Autovacuum settings must be adjusted carefully to align with the workload to prevent availability and performance issues.
  • However, adjusting vacuum settings can be challenging, particularly when the workload is constantly changing.

In summary, while PostgreSQL’s AUTOVACUUM feature helps maintain database health, it requires diligent configuration and management to ensure optimal performance and prevent potential issues, especially in high-transaction-rate environments.

AlloyDB’s adaptive autovacuum

AlloyDB, a PostgreSQL-based solution, is specially designed to efficiently handle both critical operational and analytical workloads. Managing large and dynamic workloads can be challenging when it comes to manually configuring autovacuum settings. That’s where AlloyDB’s adaptive autovacuum feature comes into play. It automatically fine-tunes the vacuuming process and evaluates database operations based on the workload, ensuring the database consistently delivers peak performance even as workloads change. Importantly, this is achieved without any interruptions caused by the vacuuming process.

Here are the key objectives of AlloyDB’s adaptive autovacuum:

  1. Guaranteeing dependable and uniform transactional performance for applications.
  2. Safeguarding high system availability by preventing XID wraparound issues.
  3. Facilitating a hands-off approach to vacuum configuration, freeing database administrators (DBAs) from the need to manually adjust settings for each unique workload.
  4. Respecting any autovacuum settings that users have modified or tuned, and adjust the adaptive settings accordingly.

AlloyDB’s adaptive autovacuum constantly monitors and dynamically updates PostgreSQL parameters related to autovacuum operations in real time. For instance, it manages the simultaneous execution of multiple autovacuum workers on different tables, a setting controlled by autovacuum_max_workers. Additionally, it dynamically adjusts the value of maintenance_work_mem, which specifies the working memory allocated to each autovacuum worker.

Enabling AlloyDB adaptive autovacuum

Adaptive Autovacuum is enabled by default but can be disabled (or enabled again in the future) using the flag enable_google_adaptive_autovacuum.

How does adaptive autovacuum work?

AlloyDB Adaptive autovacuum uses a number of factors to determine the frequency of vacuuming and analyze operations, including:

  • The size of the database
  • The number of dead tuples in the database
  • The age of the data in the database
  • The number of transactions per second vs. estimated vacuum speed.

The following are the adaptive autovacuum improvements and automatically adjusted settings in AlloyDB:

  1. Dynamic vacuum resource management: Instead of using a fixed cost limit, AlloyDB uses real-time resource statistics to adjust the vacuum workers. When the system is busy, the vacuum process and resources are throttled. If enough memory is available, additional memory is allocated for vacuum workers to accelerate the index vacuum.
  2. Dynamic XID Throttling: AlloyDB automatically and continuously monitors the progress of vacuuming and the speed of XID consumption. If a risk of XID wraparound is detected, AlloyDB will gently begin to throttle XID consumption by slowing down transactions. It also allocates more resources to vacuuming so that vacuuming can catch up and return to the safe zone. During this process, the overall transactions per second will be reduced until the XIDs are in the safe zone. When the XID age increases, the vacuum workers are dynamically increased.
  1. Efficient vacuuming for larger tables: The default vacuum is based on table-specific statistics stored in pg_stat_all_tables which has the dead tuple ratio. That works for small tables and may not work efficiently for larger, frequently updated tables. AlloyDB has an updated scan mechanism that helps trigger the autovacuum more often scans chunks of large tables and helps remove dead tuples more efficiently.
  2. Log warning messages: In AlloyDB, vacuum blockers such as long-running transactions, orphaned prepared transactions, orphaned replication slots, etc., are detected and warnings are registered in the PostgreSQL logs so that users can handle the cases in a timely manner.
  • “Found a backend process XXX with a long-running transaction whose transaction id age XXX is larger than or equal to the transaction age threshold XXX.”
  • “Found an old prepared transaction XXX whose transaction id age XXX is larger than or equal to the transaction age threshold XXX, database oid: XXX, owner oid: XXX”
  • “Found a replication slot XXX whose min transaction id age XXX is larger than or equal to the transaction id age threshold XXX.”

Benefits of AlloyDB adaptive autovacuum

AlloyDB adaptive autovacuum is designed to adjust the vacuum process based on the workload’s real-time resource utilization and without requiring users to tune the vacuum parameters. However, users can still tune the auto vacuum-related parameters, and will AlloyDB honor their settings.

There are a number of benefits to using adaptive autovacuum, including:

  • Increased performance: Adaptive autovacuum helps to ensure that the database is always running at peak performance by removing the bloat, even as the workload changes.
  • Reduced maintenance: Adaptive autovacuum automatically adjusts the frequency of vacuuming and analyzes operations, so you don’t have to worry about it.
  • Improved availability: Adaptive autovacuum helps to prevent XID wraparound, and thus avoids database outages and increases availability.

In conclusion, If you are in a situation where the vacuum process is complicated and impacting workloads and affecting your business needs, considering adaptable autovacuum is good (after testing your needs and seeing for yourself how it is working for you)

Will be glad to know your thoughts on this. cheers!!

--

--