pg_stat_get_autovacuum_scores ignores the main table's reloptions for TOAST tables
Hi all,
(CCing Nathan as the committer of 87f61f0c8280 and fad70a09ff43)
I found that pg_get_autovacuum_scores reports wrong values for TOAST
tables. In do_autovacuum(), we fall back to the main table's reloption
when the TOAST table has no reloptions of its own, and
table_recheck_autovac() does the same. However,
pg_stat_get_autovacuum_scores() only calls extract_autovac_opts() and
passes NULL when the TOAST table has none.
So the view computes the TOAST table's scores from rht eGUC defaults
whenever the user sets autovacuum_* on the main table without setting
the corresponding toast.* option. Here is a simple reproducer:
create table test (a int, b text);
alter table test alter column b set storage external;
alter table test set (autovacuum_vacuum_threshold = 1,
autovacuum_vacuum_scale_factor = 0, autovacuum_enabled = false);
insert into test select i, repeat('z',4000) from generate_series(1,200) i;
delete from test where a <= 100;
select relid::regclass, vacuum_score, do_vacuum from
pg_stat_autovacuum_scores where relid in ('test'::regclass, (select
reltoastrelid from pg_class where relname = 'test'));
relid | vacuum_score | do_vacuum
-------------------------+--------------+-----------
test | 100 | f
pg_toast.pg_toast_16384 | 6 | t
(2 rows)
And what the autovacuum worker computes for the same two relations is:
DEBUG: test: vac: 100 (thresh 1, score 100.00), ...
DEBUG: pg_toast_16384: vac: 300 (thresh 1, score 300.00), ...
Which doesn't match what the view shows.
Note that this issue happens only in v19 as commit fad70a09ff43 fixed
autovacuum's handling of TOAST reloptions. While I agree that the
commit was not backpatched to v19, I think we should fix how the view
computes the toast table's reloptions.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
On Thu, Aug 27, 2026 at 03:42:23PM -0700, Masahiko Sawada wrote:
I found that pg_get_autovacuum_scores reports wrong values for TOAST
tables. In do_autovacuum(), we fall back to the main table's reloption
when the TOAST table has no reloptions of its own, and
table_recheck_autovac() does the same. However,
pg_stat_get_autovacuum_scores() only calls extract_autovac_opts() and
passes NULL when the TOAST table has none.So the view computes the TOAST table's scores from rht eGUC defaults
whenever the user sets autovacuum_* on the main table without setting
the corresponding toast.* option. Here is a simple reproducer:
You're right. I will work on a patch for this and get it fixed ASAP.
--
nathan
Here is a patch.
--
nathan
Attachments:
v1-0001-Fix-pg_stat_autovacuum_scores-for-TOAST-tables.patchtext/plain; charset=us-asciiDownload+73-3
On Fri, Aug 28, 2026 at 10:02:42AM -0500, Nathan Bossart wrote:
Here is a patch.
Sorry for the noise. I noticed some silly mistakes in v1, so here's a v2
with those fixed.
--
nathan
Attachments:
v2-0001-Fix-pg_stat_autovacuum_scores-for-TOAST-tables.patchtext/plain; charset=us-asciiDownload+73-3
On Fri, Aug 28, 2026 at 8:16 AM Nathan Bossart <nathandbossart@gmail.com> wrote:
On Fri, Aug 28, 2026 at 10:02:42AM -0500, Nathan Bossart wrote:
Here is a patch.
Sorry for the noise. I noticed some silly mistakes in v1, so here's a v2
with those fixed.
Thank you for making the patch quickly! The patch looks good to me. A nitpick:
+ if (found && hentry->ar_hasrelopts)
+ avopts = &hentry->ar_reloptions;
ar_hasrelopts is always true here, since entries are only created when
extract_autovac_opts() returns non-NULL, so the second conjunct is
redundant actually. Having said that, it seems safer for future
changes and keeping it for symmetry with do_autovacuum() seems fine to
me.
Do we want to have regression tests for it? FWIW no test exercises
pg_stat_get_autovacuum_scores(). The only reference in the tree is the
view definition in rules.out. That's presumably why this went
unnoticed.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
On Fri, Aug 28, 2026 at 11:12:34AM -0700, Masahiko Sawada wrote:
Thank you for making the patch quickly! The patch looks good to me. A nitpick:
Thanks for reviewing.
+ if (found && hentry->ar_hasrelopts) + avopts = &hentry->ar_reloptions;ar_hasrelopts is always true here, since entries are only created when
extract_autovac_opts() returns non-NULL, so the second conjunct is
redundant actually. Having said that, it seems safer for future
changes and keeping it for symmetry with do_autovacuum() seems fine to
me.
Yeah, this is about what I was thinking.
Do we want to have regression tests for it? FWIW no test exercises
pg_stat_get_autovacuum_scores(). The only reference in the tree is the
view definition in rules.out. That's presumably why this went
unnoticed.
It might be worth adding a test or two for this view, but I doubt it
would've caught this issue. IIRC I held off adding tests originally
because I was worried about test stability.
--
nathan
On Fri, Aug 28, 2026 at 11:50 AM Nathan Bossart
<nathandbossart@gmail.com> wrote:
On Fri, Aug 28, 2026 at 11:12:34AM -0700, Masahiko Sawada wrote:
Thank you for making the patch quickly! The patch looks good to me. A nitpick:
Thanks for reviewing.
+ if (found && hentry->ar_hasrelopts) + avopts = &hentry->ar_reloptions;ar_hasrelopts is always true here, since entries are only created when
extract_autovac_opts() returns non-NULL, so the second conjunct is
redundant actually. Having said that, it seems safer for future
changes and keeping it for symmetry with do_autovacuum() seems fine to
me.Yeah, this is about what I was thinking.
Do we want to have regression tests for it? FWIW no test exercises
pg_stat_get_autovacuum_scores(). The only reference in the tree is the
view definition in rules.out. That's presumably why this went
unnoticed.It might be worth adding a test or two for this view, but I doubt it
would've caught this issue. IIRC I held off adding tests originally
because I was worried about test stability.
Setting autovacuum_enabled = off to the tables while checking
pg_stat_get_autovacuum_scores() would help the test stability. Adding
regression tests to the view would be a separate topic so I think we
can fix the issue by your patch separately from the regression tests.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com