Autovacuum freeze scores wrap around for just-vacuumed tables

Started by Baji Shaik13 days ago5 messageshackers
Jump to latest
#1Baji Shaik
baji.pgdev@gmail.com

Hi,

relation_needs_vacanalyze() computes the freeze score ages as
recentXid - relfrozenxid and recentMulti - relminmxid, but both are
read before pg_class is scanned. If a table's relfrozenxid or
relminmxid advances in between, for example by a concurrent VACUUM or
a table rewrite, the subtraction wraps to about 4 billion and the
just-vacuumed table gets a score around 1e44, which can put it at the
front of autovacuum's list. The window grows with the number of
relations; with a few thousand tables, pg_stat_autovacuum_scores shows
it hundreds of times a minute.

0001 treats the age as zero in that case. 0002 is a TAP test, using
the existing autovacuum-worker-start injection point, that shows it for
an autovacuum worker; it's for the record rather than for commit.

Please find the attached patches. Thoughts?

Regards,
Baji Shaik

Attachments:

t258500_1
v1-0002-Add-a-test-for-the-autovacuum-freeze-score-wrapar.patchapplication/octet-stream; name=v1-0002-Add-a-test-for-the-autovacuum-freeze-score-wrapar.patchDownload+91-1
v1-0001-Fix-wraparound-of-the-ages-used-for-autovacuum-fr.patchapplication/octet-stream; name=v1-0001-Fix-wraparound-of-the-ages-used-for-autovacuum-fr.patchDownload+12-3
#2Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com
In reply to: Baji Shaik (#1)
Re: Autovacuum freeze scores wrap around for just-vacuumed tables

Hi,

On Wed, Nov 11, 2026 at 6:47 AM Baji Shaik <baji.pgdev@gmail.com> wrote:

relation_needs_vacanalyze() computes the freeze score ages as
recentXid - relfrozenxid and recentMulti - relminmxid, but both are
read before pg_class is scanned. If a table's relfrozenxid or
relminmxid advances in between, for example by a concurrent VACUUM or
a table rewrite, the subtraction wraps to about 4 billion and the
just-vacuumed table gets a score around 1e44, which can put it at the
front of autovacuum's list.

Yes, your finding is correct. It depends on the window between when
the recent xids were read and the catalog scan happened, and in that
window if others could move the relfrozenxid or relminmxid ahead. And,
the window could be bigger with more number of relations in the
database.

The window grows with the number of
relations; with a few thousand tables, pg_stat_autovacuum_scores shows
it hundreds of times a minute.

I remember in the past, I measured how big this window could be by
having thousands of relations in a database and capturing the catalog
scan time, if my memory is right, the window isn't that big. However,
it's worth fixing it.

0001 treats the age as zero in that case.

Patch LGTM. I would have simplified the comments and commit message
but I will leave it to the committer.

Refreshing the recent xids and recomputing the ages could be another
option but that seems an overkill here.

0002 is a TAP test, using
the existing autovacuum-worker-start injection point, that shows it for
an autovacuum worker; it's for the record rather than for commit.

TAP test here is a nice way to repro the issue and yes it's not for
the commit but for the records. BTW, it could be simplified by just
using wait_for_event() instead (no need to change that though).

Please find the attached patches. Thoughts?

I think this needs to be back-patched to PG19 (assuming we have
another release after RC2 before the planned GA on Nov 19th). Although
the issue looks rare in practice, it is good for keeping the version
diff small.

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com

#3Nathan Bossart
nathandbossart@gmail.com
In reply to: Bharath Rupireddy (#2)
Re: Autovacuum freeze scores wrap around for just-vacuumed tables

On Wed, Nov 11, 2026 at 08:39:47AM -0800, Bharath Rupireddy wrote:

On Wed, Nov 11, 2026 at 6:47 AM Baji Shaik <baji.pgdev@gmail.com> wrote:

The window grows with the number of
relations; with a few thousand tables, pg_stat_autovacuum_scores shows
it hundreds of times a minute.

I remember in the past, I measured how big this window could be by
having thousands of relations in a database and capturing the catalog
scan time, if my memory is right, the window isn't that big. However,
it's worth fixing it.

Yeah, in both cases the pg_class scans pretty much immediately follow
retrieving the recent (M)XID, so the window should tend to be short. Plus,
there needs to be some kind of concurrent manual vacuum or rewrite, so it's
probably quite rare in practice.

I think this needs to be back-patched to PG19 (assuming we have
another release after RC2 before the planned GA on Nov 19th). Although
the issue looks rare in practice, it is good for keeping the version
diff small.

If the only consequence is that a table might be given unduly high
priority, then I don't view this as a release blocker. I might not be able
to get this committed in time for the GA code freeze, but I do think it's
worth fixing. Baji, can you please make sure there's a commitfest entry so
it doesn't get lost? Please mark me as the committer.

--
nathan

#4Baji Shaik
baji.pgdev@gmail.com
In reply to: Nathan Bossart (#3)
Re: Autovacuum freeze scores wrap around for just-vacuumed tables

On Wed, Nov 11, 2026 at 3:51 PM Nathan Bossart <nathandbossart@gmail.com>
wrote:

On Wed, Nov 11, 2026 at 08:39:47AM -0800, Bharath Rupireddy wrote:
Baji, can you please make sure there's a commitfest entry so
it doesn't get lost? Please mark me as the committer.

Thank you both for the review and feedback.
Created the CF entry: https://commitfest.postgresql.org/patch/7625/

Thanks,
Baji Shaik
Amazon Web Services: https://aws.amazon.com

#5Nathan Bossart
nathandbossart@gmail.com
In reply to: Baji Shaik (#4)
Re: Autovacuum freeze scores wrap around for just-vacuumed tables

On Wed, Nov 11, 2026 at 05:06:29PM -0600, Baji Shaik wrote:

Thank you both for the review and feedback.
Created the CF entry: https://commitfest.postgresql.org/patch/7625/

Committed.

--
nathan