Autovacuum freeze scores wrap around for just-vacuumed tables
Autovacuum freeze scores wrap around for just-vacuumed tables
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
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
Re: Autovacuum freeze scores wrap around for just-vacuumed tables
От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Hi, On Wed, Nov 11, 2026 at 6:47 AM Baji Shaik 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
Re: Autovacuum freeze scores wrap around for just-vacuumed tables
От:
Nathan Bossart <nathandbossart@gmail.com>
Дата:
On Wed, Nov 11, 2026 at 08:39:47AM -0800, Bharath Rupireddy wrote: > On Wed, Nov 11, 2026 at 6:47 AM Baji Shaik 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
Re: Autovacuum freeze scores wrap around for just-vacuumed tables
От:
Baji Shaik <baji.pgdev@gmail.com>
Дата:
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/
Re: Autovacuum freeze scores wrap around for just-vacuumed tables
От:
Nathan Bossart <nathandbossart@gmail.com>
Дата:
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