[PATCH] Adjust autovacuum thresholds using relallvisible

From: wenhui qiu <qiuwenhuifx(at)gmail(dot)com>
To: Nathan Bossart <nathandbossart(at)gmail(dot)com>, Japin Li <japinli(at)hotmail(dot)com>, Sami Imseih <samimseih(at)gmail(dot)com>, Shinya Kato <shinya11(dot)kato(at)gmail(dot)com>, Melanie Plageman <melanieplageman(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: [PATCH] Adjust autovacuum thresholds using relallvisible
Date: 2026-06-26 03:43:48
Message-ID: CAGjGUAJ4Uji6gU6Hcko9ws0dUfmpf0-L56xAXBvuJtz4q8ABMg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I have been thinking about making the regular autovacuum threshold less
static. The current threshold is essentially a linear formula:

vacthresh = (float4) vac_base_thresh + vac_scale_factor * reltuples;
anlthresh = (float4) anl_base_thresh + anl_scale_factor * reltuples;

This works well as a simple size-based rule, but it does not take into
account how much of the table still consists of all-visible pages. For
tables with frequent updates, the number of all-visible pages can drop,
while the threshold still grows only according to reltuples.

Commit 06eae9e6218ab2acf64ea497bad0360e4c90e32d ("Trigger more frequent
autovacuums with relallfrozen") provides a useful reference for this kind
of adjustment:

That commit adjusts the insert-triggered autovacuum threshold using the
percentage of unfrozen pages, derived from pg_class.relallfrozen and
pg_class.relpages.

This patch follows a similar idea for regular vacuum and analyzes
thresholds. It uses pg_class.relallvisible and pg_class.relpages to
calculate the percentage of all-visible pages, and applies that
percentage to the scale-factor part of the threshold:

vacthresh = (float4) vac_base_thresh +
vac_scale_factor * reltuples * pcnt_allvisible;
anlthresh = (float4) anl_base_thresh +
anl_scale_factor * reltuples * pcnt_allvisible;

With this, tables that have more update activity can have their vacuum
and analyze thresholds adjusted dynamically according to the all-visible
page ratio, instead of relying only on the original linear size-based
formula.

During sysbench testing, I added temporary DEBUG logging to verify the
calculation. For example:

table: sbtest9, vacthresh = 50 + 0.02 * 999347 * 0.66,
vacthresh value is 13164.69

table: sbtest1, vacthresh = 50 + 0.02 * 1000615 * 0.87,
vacthresh value is 17491.68

This shows that the threshold can be adjusted according to the current
all-visible page ratio. In update-heavy workloads, as fewer pages remain
all-visible, the scale-factor portion of the threshold is reduced, so
autovacuum can react earlier.

This is also independent from the recent work around autovacuum scores
and prioritization, such as the relation_needs_vacanalyze() refactoring
in commit 8261ee24fe332a5042cd4bc3101b4fd99dee6a46. That work affects
how autovacuum candidates are scored or ordered, while this patch changes
when a table reaches the vacuum or analyzes threshold. So these two ideas
should not conflict with each other.

Feedback is welcome, especially on whether relallvisible is the right
signal to use for adjusting the analyze threshold as well as the vacuum
threshold.

Regards

Attachment Content-Type Size
autovacuum-relallvisible-threshold.patch application/octet-stream 2.2 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Smith 2026-06-26 03:43:57 Re: Proposal: Conflict log history table for Logical Replication
Previous Message Sutou Kouhei 2026-06-26 03:21:50 Re: Make COPY format extendable: Extract COPY TO format implementations