Track skipped tables during autovacuum and autoanalyze

Поиск
Список
Период
Сортировка
Искать

Track skipped tables during autovacuum and autoanalyze

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
Вложения:
Hi,

I would like to propose adding new fields to pg_stat_all_tables to track
tables skipped during autovacuum and autoanalyze.

Currently, when autovacuum or autoanalyze is skipped because a lock cannot
be acquired, this information is only emitted in log messages. However, it
would be more useful if users could access this information via a system view,
alongside related fields such as last_autovacuum, on a per-table basis.

The attached patch add the following fields to pg_stat_all_tables:
 - last_skipped_autovacuum
 - last_skipped_autoanalyze
 - skipped_autovacuum_count
 - skipped_autoanalyze_count

Are there any concerns about exposing this in pg_stat_all_tables, or suggestions
for a better approach?

Regards,
Yugo Nagata

-- 
Yugo Nagata 

Re: Track skipped tables during autovacuum and autoanalyze

От:
Sami Imseih <samimseih@gmail.com>
Дата:
Hi,

Thanks for the patch!

> The attached patch add the following fields to pg_stat_all_tables:
>  - last_skipped_autovacuum
>  - last_skipped_autoanalyze
>  - skipped_autovacuum_count
>  - skipped_autoanalyze_count
>
> Are there any concerns about exposing this in pg_stat_all_tables, or suggestions
> for a better approach?

I am not sure about the timestamp columns. I am not saying they will
not be useful,
but I think it will be better to just start with counters for this.
The way the views get
used, a dashboard built for tracking the deltas of the counters can easily spot
when there is a spike of skipped autovacuum/autoanalyze count.
Also, for tables that are being autovacuumed and skipped quickly,
the timestamps will just be overwritten.

So, I am +1 on the counters, -1 on the timestamps.

Out of scope for this patch, but I also wonder if we should add another counter,
autovacuum_started_count. If there are other types of failure scenarios such as
corrupt indexes, checksum failures, etc. which terminate the
autovacuum in flight,
we would be able to catch this by looking at the number of autovacuums
started vs completed. The skipped counters in this patch and a started
counter would capture different stages of the autovacuum lifecycle;
skipped means
"never started" (lock contention), while a started-minus-completed delta means
"started but failed." Both are useful signals, but for different reasons.

In terms of the patch:

1/

+                       if (AmAutoVacuumWorkerProcess())
+                               pgstat_report_skipped_vacuum(relid);

Any reason why this should not also include manual vacuum/analyze?
If someone has a vacuum/analyze script that uses SKIP_LOCKED, and
the operation gets skipped, this should be included in the counter.
this can be done with separate counter fields for autovacuum/autoanalyze and
vacuum/analyze

2/

+            pg_stat_get_skipped_autovacuum_count(C.oid) AS
skipped_autovacuum_count,

How about a name like "autovacuum_lock_skip_count"?


--
Sami Imseih
Amazon Web Services (AWS)


Re: Track skipped tables during autovacuum and autoanalyze

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
On Tue, 24 Mar 2026 09:58:48 -0500
Sami Imseih  wrote:

Thank you for your comments!

> > The attached patch add the following fields to pg_stat_all_tables:
> >  - last_skipped_autovacuum
> >  - last_skipped_autoanalyze
> >  - skipped_autovacuum_count
> >  - skipped_autoanalyze_count
> >
> > Are there any concerns about exposing this in pg_stat_all_tables, or suggestions
> > for a better approach?
> 
> I am not sure about the timestamp columns. I am not saying they will
> not be useful,
> but I think it will be better to just start with counters for this.
> The way the views get
> used, a dashboard built for tracking the deltas of the counters can easily spot
> when there is a spike of skipped autovacuum/autoanalyze count.
> Also, for tables that are being autovacuumed and skipped quickly,
> the timestamps will just be overwritten.
> 
> So, I am +1 on the counters, -1 on the timestamps.

Although the timestamps are overwritten on each skipped autovacuum or
autoanalyze, they still indicate when the last attempt was made. This
can help users confirm that autovacuum is actively attempting to run,
and that the issue is due to repeated skips rather than inactivity.

While counters can indicate overall activity, they do not reveal when
the last skip occurred. With timestamps, users can immediately see the
most recent attempt, even without a separate dashboard or historical
tracking.

Therefore, counters are useful for monitoring overall activity, but
timestamps give additional, complementary information, so it seems
worthwhile to include them too.
 
> Out of scope for this patch, but I also wonder if we should add another counter,
> autovacuum_started_count. If there are other types of failure scenarios such as
> corrupt indexes, checksum failures, etc. which terminate the
> autovacuum in flight,
> we would be able to catch this by looking at the number of autovacuums
> started vs completed. The skipped counters in this patch and a started
> counter would capture different stages of the autovacuum lifecycle;
> skipped means
> "never started" (lock contention), while a started-minus-completed delta means
> "started but failed." Both are useful signals, but for different reasons.

That makes sense. I was considering adding a counter to track
"canceled" autovacuum or autoanalyze, but tracking the number of started
or attempted autovacuums might provide a more general information than counting
only the failed ones.

> In terms of the patch:
> 
> 1/
> 
> +                       if (AmAutoVacuumWorkerProcess())
> +                               pgstat_report_skipped_vacuum(relid);
> 
> Any reason why this should not also include manual vacuum/analyze?
> If someone has a vacuum/analyze script that uses SKIP_LOCKED, and
> the operation gets skipped, this should be included in the counter.
> this can be done with separate counter fields for autovacuum/autoanalyze and
> vacuum/analyze

For manual vacuum/analyze, an explicit WARNING is output when the
operation is skipped, so I initially thought that reporting it in the
stats view was not necessary. However, I now agree that it should be
included.

> 2/
> 
> +            pg_stat_get_skipped_autovacuum_count(C.oid) AS
> skipped_autovacuum_count,
> 
> How about a name like "autovacuum_lock_skip_count"?

I'm not sure this is a good fit, since it may be better to keep the
naming consistent with other fields whose names end with
"...vacuum_count" or "...analyze_count". Happy to hear other opinions.

Regards,
Yugo Nagata

-- 
Yugo Nagata 


Re: Track skipped tables during autovacuum and autoanalyze

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Wed, Mar 25, 2026 at 01:28:47AM +0900, Yugo Nagata wrote:
> Although the timestamps are overwritten on each skipped autovacuum or
> autoanalyze, they still indicate when the last attempt was made. This
> can help users confirm that autovacuum is actively attempting to run,
> and that the issue is due to repeated skips rather than inactivity.
> 
> While counters can indicate overall activity, they do not reveal when
> the last skip occurred. With timestamps, users can immediately see the
> most recent attempt, even without a separate dashboard or historical
> tracking.
> 
> Therefore, counters are useful for monitoring overall activity, but
> timestamps give additional, complementary information, so it seems
> worthwhile to include them too.

Hmm..  I can buy this argument for the timestamps, especially for
database with many relations of various sizes that could take a
various amount of time to process.  The timestamps could offer hints
about the time it takes between the skips, even if snapshots of the
stats data are not taken at a very aggressive frequency.

This is v20 material at this stage, of course..
--
Michael

Re: Track skipped tables during autovacuum and autoanalyze

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
On Wed, 25 Mar 2026 11:07:08 +0900
Michael Paquier  wrote:

> On Wed, Mar 25, 2026 at 01:28:47AM +0900, Yugo Nagata wrote:
> > Although the timestamps are overwritten on each skipped autovacuum or
> > autoanalyze, they still indicate when the last attempt was made. This
> > can help users confirm that autovacuum is actively attempting to run,
> > and that the issue is due to repeated skips rather than inactivity.
> > 
> > While counters can indicate overall activity, they do not reveal when
> > the last skip occurred. With timestamps, users can immediately see the
> > most recent attempt, even without a separate dashboard or historical
> > tracking.
> > 
> > Therefore, counters are useful for monitoring overall activity, but
> > timestamps give additional, complementary information, so it seems
> > worthwhile to include them too.
> 
> Hmm..  I can buy this argument for the timestamps, especially for
> database with many relations of various sizes that could take a
> various amount of time to process.  The timestamps could offer hints
> about the time it takes between the skips, even if snapshots of the
> stats data are not taken at a very aggressive frequency.
> 
> This is v20 material at this stage, of course..

Thank you for your comments.
Yes, I understand that it's too late for this to be included in v19.

Regards,
Yugo Nagata

-- 
Yugo Nagata 


Re: Track skipped tables during autovacuum and autoanalyze

От:
Sami Imseih <samimseih@gmail.com>
Дата:
> > On Wed, Mar 25, 2026 at 01:28:47AM +0900, Yugo Nagata wrote:
> > > Although the timestamps are overwritten on each skipped autovacuum or
> > > autoanalyze, they still indicate when the last attempt was made. This
> > > can help users confirm that autovacuum is actively attempting to run,
> > > and that the issue is due to repeated skips rather than inactivity.
> > >
> > > While counters can indicate overall activity, they do not reveal when
> > > the last skip occurred. With timestamps, users can immediately see the
> > > most recent attempt, even without a separate dashboard or historical
> > > tracking.
> > >
> > > Therefore, counters are useful for monitoring overall activity, but
> > > timestamps give additional, complementary information, so it seems
> > > worthwhile to include them too.
> >
> > Hmm..  I can buy this argument for the timestamps, especially for
> > database with many relations of various sizes that could take a
> > various amount of time to process.  The timestamps could offer hints
> > about the time it takes between the skips, even if snapshots of the
> > stats data are not taken at a very aggressive frequency.

I'm fine with adding timestamps, as there seem to be convincing
reasons to add them.
My other concern is bloat of the pg_stat_all_tables view. This patch
adds 4 columns, or
8 if we also include manual vacuum and analyze (which I think we should).

Given that, should we also start thinking about splitting the vacuum
activity related
columns into a dedicated view and out of pg_stat_all_tables for v20?

--
Sami Imseih
Amazon Web Services (AWS)


Re: Track skipped tables during autovacuum and autoanalyze

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Wed, Mar 25, 2026 at 12:12:35PM -0500, Sami Imseih wrote:
> I'm fine with adding timestamps, as there seem to be convincing
> reasons to add them.
> My other concern is bloat of the pg_stat_all_tables view. This patch
> adds 4 columns, or
> 8 if we also include manual vacuum and analyze (which I think we should).
> 
> Given that, should we also start thinking about splitting the vacuum
> activity related
> columns into a dedicated view and out of pg_stat_all_tables for v20?

PgStat_StatTabEntry is shared between indexes and tables.  A bunch of
its fields apply only to tables, not indexes (aka all the vacuum and
analyze ones).  Few fields apply only to indexes, not tables.  Not
that many are shared between both.  I would advocate for a clean split
between indexes and tables, as a start, with a new variable-sized
stats kind dedicated to indexes.
--
Michael

Re: Track skipped tables during autovacuum and autoanalyze

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
On Thu, 26 Mar 2026 08:26:26 +0900
Michael Paquier  wrote:

> On Wed, Mar 25, 2026 at 12:12:35PM -0500, Sami Imseih wrote:
> > I'm fine with adding timestamps, as there seem to be convincing
> > reasons to add them.
> > My other concern is bloat of the pg_stat_all_tables view. This patch
> > adds 4 columns, or
> > 8 if we also include manual vacuum and analyze (which I think we should).
> > 
> > Given that, should we also start thinking about splitting the vacuum
> > activity related
> > columns into a dedicated view and out of pg_stat_all_tables for v20?
> 
> PgStat_StatTabEntry is shared between indexes and tables.  A bunch of
> its fields apply only to tables, not indexes (aka all the vacuum and
> analyze ones).  Few fields apply only to indexes, not tables.  Not
> that many are shared between both.  I would advocate for a clean split
> between indexes and tables, as a start, with a new variable-sized
> stats kind dedicated to indexes.

I see that the following fields are used in pg_stat_all_indexes:

- numscans
- lastscan
- tuples_returned
- tuples_fetched
- stat_reset_time

but they are also shared with pg_stat_all_tables. Are you suggesting
splitting these shared fields from those that are specific to tables?

I'm not sure this would significantly reduce the size of
PgStat_StatTabEntry. Could you elaborate on the expected benefits?

Regards,
Yugo Nagata

-- 
Yugo Nagata 


Re: Track skipped tables during autovacuum and autoanalyze

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Thu, Mar 26, 2026 at 10:18:39AM +0900, Yugo Nagata wrote:
> I'm not sure this would significantly reduce the size of
> PgStat_StatTabEntry. Could you elaborate on the expected benefits?

The point is that this reduces the shmem footprint for indexes (well,
it's also benefitial for tables, just less), on top of being cleaner
because the stats views would only need to store and query the fields
they care about for each relkind.
--
Michael

Re: Track skipped tables during autovacuum and autoanalyze

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
On Thu, 26 Mar 2026 10:31:19 +0900
Michael Paquier  wrote:

> On Thu, Mar 26, 2026 at 10:18:39AM +0900, Yugo Nagata wrote:
> > I'm not sure this would significantly reduce the size of
> > PgStat_StatTabEntry. Could you elaborate on the expected benefits?
> 
> The point is that this reduces the shmem footprint for indexes (well,
> it's also benefitial for tables, just less), on top of being cleaner
> because the stats views would only need to store and query the fields
> they care about for each relkind.

Thank you for the clarification. I think I understand your point now.

So, you mean introducing a separate stats kind for indexes, such as
PGSTAT_KIND_INDEX?

Regards,
Yugo Nagata

> --
> Michael


-- 
Yugo Nagata 


Re: Track skipped tables during autovacuum and autoanalyze

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
Вложения:

> On Tue, 24 Mar 2026 09:58:48 -0500
> Sami Imseih  wrote:

> > 1/
> > 
> > +                       if (AmAutoVacuumWorkerProcess())
> > +                               pgstat_report_skipped_vacuum(relid);
> > 
> > Any reason why this should not also include manual vacuum/analyze?
> > If someone has a vacuum/analyze script that uses SKIP_LOCKED, and
> > the operation gets skipped, this should be included in the counter.
> > this can be done with separate counter fields for autovacuum/autoanalyze and
> > vacuum/analyze
> 
> For manual vacuum/analyze, an explicit WARNING is output when the
> operation is skipped, so I initially thought that reporting it in the
> stats view was not necessary. However, I now agree that it should be
> included.

I've attached an updated patch to also report skipped manual
vacuum/analyze.

The pgstat reporting functions are unified into a single function,
pgstat_report_skipped_vacuum_analyze(), which handles both
auto/manual and vacuum/analyze cases. Also it is fixed to use
InvalidOid for shared relations.

To support manual vacuum/analyze, some hack were needed to
obtain the relid before the lock attempt. When SKIP_LOCKED is specified,
the relid is obtained using RangeVarGetRelid() with NoLock prior to
locking. If ConditionalLockRelationOid() then fails, the skip is
reported.

To handle the possibility that the table is dropped between
RangeVarGetRelid() and the lock attempt, SearchSysCacheExists1() is
used after acquiring the lock.

Regards,
Yugo Nagata

-- 
Yugo Nagata 

Re: Track skipped tables during autovacuum and autoanalyze

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Thu, Mar 26, 2026 at 07:22:03PM +0900, Yugo Nagata wrote:
> To handle the possibility that the table is dropped between
> RangeVarGetRelid() and the lock attempt, SearchSysCacheExists1() is
> used after acquiring the lock.

(Noticed while skimming through my emails this morning..)

+void
+pgstat_report_skipped_vacuum_analyze(Oid relid, bool vacuum, bool analyze,
+                                     bool autovacuum)

I'd recommend to replace this interface with three booleans with a set
of three bitwise flags.  That would be less error prone for the
callers of this function, or we could finish by aggregating counters
we don't want to.
--
Michael

Re: Track skipped tables during autovacuum and autoanalyze

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
Вложения:
On Fri, 27 Mar 2026 08:07:29 +0900
Michael Paquier  wrote:

> On Thu, Mar 26, 2026 at 07:22:03PM +0900, Yugo Nagata wrote:
> > To handle the possibility that the table is dropped between
> > RangeVarGetRelid() and the lock attempt, SearchSysCacheExists1() is
> > used after acquiring the lock.
> 
> (Noticed while skimming through my emails this morning..)
> 
> +void
> +pgstat_report_skipped_vacuum_analyze(Oid relid, bool vacuum, bool analyze,
> +                                     bool autovacuum)
> 
> I'd recommend to replace this interface with three booleans with a set
> of three bitwise flags.  That would be less error prone for the
> callers of this function, or we could finish by aggregating counters
> we don't want to.

Thank you for the suggestion.
I've attached a revised patch reflecting this change, and it also includes
the documentation.

Regards,
Yugo Nagata

-- 
Yugo Nagata 

Re: Track skipped tables during autovacuum and autoanalyze

От:
Sami Imseih <samimseih@gmail.com>
Дата:
> I've attached a revised patch reflecting this change, and it also includes
> the documentation.

Thanks fo the update!

I have some comments:

1/
+pgstat_report_skipped_vacuum_analyze(Oid relid, bits8 flags)

using bit8 is fine here, but I would have just used int. For this
case, it's just a matter of prefernace.

2/
+/* flags for pgstat_flush_backend() */
+#define PGSTAT_REPORT_SKIPPED_VACUUM   (1 << 0)        /* vacuum is skipped */
+#define PGSTAT_REPORT_SKIPPED_ANALYZE  (1 << 1)        /* analyze is skipped */
+#define PGSTAT_REPORT_SKIPPED_AUTOVAC  (1 << 2)        /* skipped
during autovacuum/autoanalyze */
+#define PGSTAT_REPORT_SKIPPED_ANY   (PGSTAT_REPORT_SKIPPED_VACUUM |
PGSTAT_REPORT_SKIPPED_ANALYZE)

can we just have 4 flags, SKIPPED_VACUUM, SKIPPED_ANALYZE,
SKIPPED_AUTOVACUUM, SKIPPED_AUTOANALYZE,
which can then remove the nested if/else and makes the mapping more obvious

+       if (flags & PGSTAT_REPORT_SKIPPED_AUTOVAC)
+       {
+               if (flags & PGSTAT_REPORT_SKIPPED_VACUUM)
+               {
+                       tabentry->last_skipped_autovacuum_time = ts;
+                       tabentry->skipped_autovacuum_count++;
+               }
+               if (flags & PGSTAT_REPORT_SKIPPED_ANALYZE)
+               {
+                       tabentry->last_skipped_autoanalyze_time = ts;
+                       tabentry->skipped_autoanalyze_count++;
+               }
+       }
+       else
+       {
+               if (flags & PGSTAT_REPORT_SKIPPED_VACUUM)
+               {
+                       tabentry->last_skipped_vacuum_time = ts;
+                       tabentry->skipped_vacuum_count++;
+               }
+               if (flags & PGSTAT_REPORT_SKIPPED_ANALYZE)
+               {
+                       tabentry->last_skipped_analyze_time = ts;
+                       tabentry->skipped_analyze_count++;
+               }
+       }

3/
For the sake of consistency, can we rename the fields from

skipped_vacuum_count to vacuum_skipped_count, etc. ? to be similar
to fields like vacuum_count

4/
field documentation could be a bit better to match existing phrasing

For example, the timestamp fields:

-       Last time a manual vacuum on this table was attempted but skipped due to
-       lock unavailability (not counting VACUUM FULL)
+       The time of the last manual vacuum on this table that was skipped
+       due to lock unavailability (not counting VACUUM FULL)

and the counter fields

-       Number of times vacuums on this table have been attempted but skipped
+       Number of times a manual vacuum on this table has been skipped

5/
Partitioned table asymmetry between vacuum_count and vacuum_skipped_count.

vacuum_count never increments on a the parenttable, because the parent is never
pocessed. On the other hand, if the manual VACUUM/ANALYZE is on the
parent table,
then we will skip all the children. So, we should still report the skip on the
parent table, but we should add a Notes section in the docs perhaps to
document this caveat?

6/
It would be nice to add a test for this, but this requires concurrency and I'm
not sure it's woth it.

Also, can you create a CF entry in
https://commitfest.postgresql.org/59/, please.

Thanks!

--
Sami Imseih
Amazon Web Services (AWS)


Re: Track skipped tables during autovacuum and autoanalyze

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
On Fri, 27 Mar 2026 11:48:27 -0500
Sami Imseih  wrote:

> > I've attached a revised patch reflecting this change, and it also includes
> > the documentation.
> 
> Thanks fo the update!
> 
> I have some comments:
> 
> 1/
> +pgstat_report_skipped_vacuum_analyze(Oid relid, bits8 flags)
> 
> using bit8 is fine here, but I would have just used int. For this
> case, it's just a matter of prefernace.

That makes sense, since using int for flags seems common in other
places in the code. I'm not sure how much it affects performance,
though.

> 2/
> +/* flags for pgstat_flush_backend() */
> +#define PGSTAT_REPORT_SKIPPED_VACUUM   (1 << 0)        /* vacuum is skipped */
> +#define PGSTAT_REPORT_SKIPPED_ANALYZE  (1 << 1)        /* analyze is skipped */
> +#define PGSTAT_REPORT_SKIPPED_AUTOVAC  (1 << 2)        /* skipped
> during autovacuum/autoanalyze */
> +#define PGSTAT_REPORT_SKIPPED_ANY   (PGSTAT_REPORT_SKIPPED_VACUUM |
> PGSTAT_REPORT_SKIPPED_ANALYZE)
> 
> can we just have 4 flags, SKIPPED_VACUUM, SKIPPED_ANALYZE,
> SKIPPED_AUTOVACUUM, SKIPPED_AUTOANALYZE,
> which can then remove the nested if/else and makes the mapping more obvious

I am fine with that. In that case, the nested logic would move to the
caller side.

> 3/
> For the sake of consistency, can we rename the fields from
> 
> skipped_vacuum_count to vacuum_skipped_count, etc. ? to be similar
> to fields like vacuum_count

Hmm, I think skipped_vacuum_count is more consistent with
fields like last_vacuum and total_vacuum_time, where the modifier
comes before vacuum/analyze. What do you think about that?

> 4/
> field documentation could be a bit better to match existing phrasing
> 
> For example, the timestamp fields:
> 
> -       Last time a manual vacuum on this table was attempted but skipped due to
> -       lock unavailability (not counting VACUUM FULL)
> +       The time of the last manual vacuum on this table that was skipped
> +       due to lock unavailability (not counting VACUUM FULL)

I intended to keep consistency with the existing last_vacuum:

  Last time at which this table was manually vacuumed (not counting VACUUM FULL)

although "at which" was accidentally omitted. Your suggestion seems
simpler and more natural to me. Should we prioritize that over consistency?
 
> and the counter fields
> 
> -       Number of times vacuums on this table have been attempted but skipped
> +       Number of times a manual vacuum on this table has been skipped

The "a munual" was also accidentally omitted, so I'll fix it.

> 5/
> Partitioned table asymmetry between vacuum_count and vacuum_skipped_count.
> 
> vacuum_count never increments on a the parenttable, because the parent is never
> pocessed. On the other hand, if the manual VACUUM/ANALYZE is on the
> parent table,
> then we will skip all the children. So, we should still report the skip on the
> parent table, but we should add a Notes section in the docs perhaps to
> document this caveat?

Yeah, we cannot report skips on the children when a manual
vacuum/analyze on the parent table is skipped. (It might be possible
to obtain child information with NoLock, but that would not be safe.)

Therefore, I agree that the best we can do here is to add a note to the
documentation of last_skipped_vacuum/analyze and skipped_vacuum/analyze_count.

For example:

  When a manual vacuum or analyze on a parent table in an inheritance
  or partitioning hierarchy is skipped, the statistics are recorded
  only for the parent table, not for its children.

> 6/
> It would be nice to add a test for this, but this requires concurrency and I'm
> not sure it's woth it.

I'm not sure what meaningful tests we could add for these statistics.
I couldn't find any existing tests for fields like last_vacuum.

Regards,
Yugo Nagata

-- 
Yugo Nagata 


Re: Track skipped tables during autovacuum and autoanalyze

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
Вложения:
Hello Sami Imseih,

On Sat, 28 Mar 2026 16:18:02 +0900
Yugo Nagata  wrote:

> On Fri, 27 Mar 2026 11:48:27 -0500
> Sami Imseih  wrote:
> 
> > > I've attached a revised patch reflecting this change, and it also includes
> > > the documentation.
> > 
> > Thanks fo the update!
> > 
> > I have some comments:
> > 
> > 1/
> > +pgstat_report_skipped_vacuum_analyze(Oid relid, bits8 flags)
> > 
> > using bit8 is fine here, but I would have just used int. For this
> > case, it's just a matter of prefernace.
> 
> That makes sense, since using int for flags seems common in other
> places in the code. I'm not sure how much it affects performance,
> though.
> 
> > 2/
> > +/* flags for pgstat_flush_backend() */
> > +#define PGSTAT_REPORT_SKIPPED_VACUUM   (1 << 0)        /* vacuum is skipped */
> > +#define PGSTAT_REPORT_SKIPPED_ANALYZE  (1 << 1)        /* analyze is skipped */
> > +#define PGSTAT_REPORT_SKIPPED_AUTOVAC  (1 << 2)        /* skipped
> > during autovacuum/autoanalyze */
> > +#define PGSTAT_REPORT_SKIPPED_ANY   (PGSTAT_REPORT_SKIPPED_VACUUM |
> > PGSTAT_REPORT_SKIPPED_ANALYZE)
> > 
> > can we just have 4 flags, SKIPPED_VACUUM, SKIPPED_ANALYZE,
> > SKIPPED_AUTOVACUUM, SKIPPED_AUTOANALYZE,
> > which can then remove the nested if/else and makes the mapping more obvious
> 
> I am fine with that. In that case, the nested logic would move to the
> caller side.
> 
> > 3/
> > For the sake of consistency, can we rename the fields from
> > 
> > skipped_vacuum_count to vacuum_skipped_count, etc. ? to be similar
> > to fields like vacuum_count
> 
> Hmm, I think skipped_vacuum_count is more consistent with
> fields like last_vacuum and total_vacuum_time, where the modifier
> comes before vacuum/analyze. What do you think about that?
> 
> > 4/
> > field documentation could be a bit better to match existing phrasing
> > 
> > For example, the timestamp fields:
> > 
> > -       Last time a manual vacuum on this table was attempted but skipped due to
> > -       lock unavailability (not counting VACUUM FULL)
> > +       The time of the last manual vacuum on this table that was skipped
> > +       due to lock unavailability (not counting VACUUM FULL)
> 
> I intended to keep consistency with the existing last_vacuum:
> 
>   Last time at which this table was manually vacuumed (not counting VACUUM FULL)
> 
> although "at which" was accidentally omitted. Your suggestion seems
> simpler and more natural to me. Should we prioritize that over consistency?
>  
> > and the counter fields
> > 
> > -       Number of times vacuums on this table have been attempted but skipped
> > +       Number of times a manual vacuum on this table has been skipped
> 
> The "a munual" was also accidentally omitted, so I'll fix it.
> 
> > 5/
> > Partitioned table asymmetry between vacuum_count and vacuum_skipped_count.
> > 
> > vacuum_count never increments on a the parenttable, because the parent is never
> > pocessed. On the other hand, if the manual VACUUM/ANALYZE is on the
> > parent table,
> > then we will skip all the children. So, we should still report the skip on the
> > parent table, but we should add a Notes section in the docs perhaps to
> > document this caveat?
> 
> Yeah, we cannot report skips on the children when a manual
> vacuum/analyze on the parent table is skipped. (It might be possible
> to obtain child information with NoLock, but that would not be safe.)
> 
> Therefore, I agree that the best we can do here is to add a note to the
> documentation of last_skipped_vacuum/analyze and skipped_vacuum/analyze_count.
> 
> For example:
> 
>   When a manual vacuum or analyze on a parent table in an inheritance
>   or partitioning hierarchy is skipped, the statistics are recorded
>   only for the parent table, not for its children.
> 
> > 6/
> > It would be nice to add a test for this, but this requires concurrency and I'm
> > not sure it's woth it.
> 
> I'm not sure what meaningful tests we could add for these statistics.
> I couldn't find any existing tests for fields like last_vacuum.

I've attached a patch reflecting your comments on items 1, 2, and 5.
As for items 3, 4, and 6, I am waiting for your comments, so the patch
is left unchanged for now.

Regards,
Yugo Nagata

-- 
Yugo Nagata 

Re: Track skipped tables during autovacuum and autoanalyze

От:
Sami Imseih <samimseih@gmail.com>
Дата:
Thanks for the updated patch!

> I've attached a patch reflecting your comments on items 1, 2, and 5.
> As for items 3, 4, and 6, I am waiting for your comments, so the patch
> is left unchanged for now.

A few more comments:

1/

+            relid = RangeVarGetRelid(vrel->relation, NoLock, false);

Should this be called with "true" as the 3rd (missing_ok) argument, otherwise
we end up with an error instead of a "--- relation no longer exists" log. right?

2/

Can the isolation tests
src/test/isolation/specs/vacuum-skip-locked.spec be updated
to check pg_stat_user_tables as well?

3/ comment fix:

This:
* Relation could not be opened hence generate if possible a log

Should be:
* Relation could not be opened, hence generate if possible a log

--
Sami Imseih
Amazon Web Services (AWS)


Re: Track skipped tables during autovacuum and autoanalyze

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
Вложения:
On Wed, 22 Apr 2026 07:49:55 -0500
Sami Imseih  wrote:

Thank you for your comments!
> 
> 1/
> 
> +            relid = RangeVarGetRelid(vrel->relation, NoLock, false);
> 
> Should this be called with "true" as the 3rd (missing_ok) argument, otherwise
> we end up with an error instead of a "--- relation no longer exists" log. right?

No, it should be false. If missing_ok is true, VACUUM (SKIP_LOCKED) on a not-existing
table would emit a "skipping vacuum of ... --- relation no longer exists" message, but
it should be "relation ... does not exist".

> 2/
> 
> Can the isolation tests
> src/test/isolation/specs/vacuum-skip-locked.spec be updated
> to check pg_stat_user_tables as well?

Yes, we can. I've attached an updated patch including that test.

While working on the test, I noticed that skipped FULL VACUUM was counted
in the previous patch, so I fixed it not to avoid counting those cases.

> 3/ comment fix:
> 
> This:
> * Relation could not be opened hence generate if possible a log
> 
> Should be:
> * Relation could not be opened, hence generate if possible a log

Fixed.

The names of the new fields are still open. The current pattern is
"last_skipped_..." and "skipped_..._count". Alternatively, we could use
"..._last_skip" and "..._skip_count", which would be consistent with
slotsync_skip_count and slosync_last_skip.

Which do you think is better?

Regards,
Yugo Nagata

-- 
Yugo Nagata 

Re: Track skipped tables during autovacuum and autoanalyze

От:
Sami Imseih <samimseih@gmail.com>
Дата:
Вложения:
Thanks for the update!

> >
> > 1/
> >
> > +            relid = RangeVarGetRelid(vrel->relation, NoLock, false);
> >
> > Should this be called with "true" as the 3rd (missing_ok) argument, otherwise
> > we end up with an error instead of a "--- relation no longer exists" log. right?
>
> No, it should be false. If missing_ok is true, VACUUM (SKIP_LOCKED) on a not-existing
> table would emit a "skipping vacuum of ... --- relation no longer exists" message, but
> it should be "relation ... does not exist".

Yeah you are right.

But, after looking more into this, I still think the
expand_vacuum_rel() changes can be
improved. The branching
-                */
-               if (!OidIsValid(relid))
+               if (!(options & VACOPT_SKIP_LOCKED))
                {
-                       if (options & VACOPT_VACUUM)
-                               ereport(WARNING,
-
(errcode(ERRCODE_LOCK_NOT_AVAILABLE),
-                                                errmsg("skipping
vacuum of \"%s\" --- lock not available",
-
vrel->relation->relname)));
-                       else
-                               ereport(WARNING,
-
(errcode(ERRCODE_LOCK_NOT_AVAILABLE),
-                                                errmsg("skipping
analyze of \"%s\" --- lock not available",
+                       relid = RangeVarGetRelidExtended(vrel->relation,
+
                  AccessShareLock,
+
                  0, NULL, NULL);
+                       if (!OidIsValid(relid))
+                               return vacrels;
+               }
+               else
+               {
+                       /* Get relid for reporting before taking a lock */
+                       relid = RangeVarGetRelid(vrel->relation, NoLock, false);
+
+                       if (!ConditionalLockRelationOid(relid, AccessShareLock))

is not needed. We can continue just using RangeVarGetRelidExtended()
with the rvr_opts and an AccessExclusiveLock, and once we need to
report that we cannot obtain the lock, RangeVarGetRelid() can be
called at that point for the purpose of calling
pgstat_report_skipped_vacuum_analyze(). This is safer than calling
ConditionalLockRelationOid() on a relid obtained with NoLock. See
attached v6.

>> 2/
>>
>> Can the isolation tests
>> src/test/isolation/specs/vacuum-skip-locked.spec be updated
>> to check pg_stat_user_tables as well?

> Yes, we can. I've attached an updated patch including that test.

> While working on the test, I noticed that skipped FULL VACUUM was counted
> in the previous patch, so I fixed it not to avoid counting those cases.

The tests looks good to me.

> The names of the new fields are still open. The current pattern is
> "last_skipped_..." and "skipped_..._count". Alternatively, we could use
> "..._last_skip" and "..._skip_count", which would be consistent with
> slotsync_skip_count and slosync_last_skip.

> Which do you think is better?

I think last_skipped_* is better since we use last_vacuum, last_autovacuum, etc.

--
Sami

Re: Track skipped tables during autovacuum and autoanalyze

От:
Shinya Kato <shinya11.kato@gmail.com>
Дата:
On Wed, Jul 22, 2026 at 3:51 AM Sami Imseih  wrote:
>
> Sorry, I missed attaching the injection point test.
>
> v11 includes it.

Thanks for the updated patches! I have a few comments.

In 0001,

> +step check_stat
> +{
> + SELECT relname,
> +        vacuum_count, lock_skipped_vacuum_count,
> +        analyze_count, lock_skipped_analyze_count
> + FROM pg_stat_all_tables
> + WHERE relname IN ('parted', 'part1', 'part2');
> +}

How about adding "ORDER BY relname" here? The row order is not
guaranteed otherwise.

> +{ oid => '8142', descr => 'statistics: last skipped vacuum time for a table',
> +  proname => 'pg_stat_get_last_lock_skipped_vacuum_time', provolatile => 's',

The proname now says lock_skipped, but the descr still says just
"skipped". I think the descr should mention the lock cause as well,
and the same applies to the other seven entries.

> +#define PGSTAT_REPORT_SKIPPED_VACUUM     (1 << 0)    /* vacuum is skipped */
> +#define PGSTAT_REPORT_SKIPPED_ANALYZE        (1 << 1)    /* analyze is skipped */

The struct fields were renamed to lock_skipped_*, but these flags and
pgstat_report_skipped_vacuum_analyze() keep the generic name. Should
we rename them too, for consistency?

In 0002,

> +      'vacuum_skip_lock_stats',

This also needs to be added to the ISOLATION list in
src/test/modules/injection_points/Makefile. Without it the test never
runs in the autoconf build. I confirmed it passes once registered
there.

-- 
Best regards,
Shinya Kato
NTT OSS Center


Re: Track skipped tables during autovacuum and autoanalyze

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
Вложения:
Hi,

On Sat, 25 Jul 2026 01:48:32 +0900
Yugo Nagata  wrote:

> Hi,
> 
> I realized that I had been accidentally dropped from the CC list and had
> missed the recent discussions. I'm sorry for my late response, and thank
> you for keeping this moving.
> 
> I've attached the v12 patch, which incorporates Kato-san's feedback.

Sorry for the noise, but I'm resubmitting v12 to include the missing expected
files for the isolation tests.

I've also added Michael back to the CC list, as both he and I had accidentally
been dropped from the recent thread.
> 
> Regarding the column names, I noticed the change from skipped_* to lock_skipped_*.
> While I initially preferred skipped_*, I understand Kato-san's point considering
> that there could be other reasons for skipping in the future.
> 
> > In 0001,
> > 
> > > +step check_stat
> > > +{
> > > + SELECT relname,
> > > +        vacuum_count, lock_skipped_vacuum_count,
> > > +        analyze_count, lock_skipped_analyze_count
> > > + FROM pg_stat_all_tables
> > > + WHERE relname IN ('parted', 'part1', 'part2');
> > > +}
> > 
> > How about adding "ORDER BY relname" here? The row order is not
> > guaranteed otherwise.
> 
> Agreed. Fixed.
> 
> > 
> > > +{ oid => '8142', descr => 'statistics: last skipped vacuum time for a table',
> > > +  proname => 'pg_stat_get_last_lock_skipped_vacuum_time', provolatile => 's',
> > 
> > The proname now says lock_skipped, but the descr still says just
> > "skipped". I think the descr should mention the lock cause as well,
> > and the same applies to the other seven entries.
> 
> I changed them like:
> 
> 'statistics: last vacuum skip time due to lock unavailability for a table'
> 'statistics: number of vacuum skips due to lock unavailability for a table'
> 
> How do these sound?
> 
> > > +#define PGSTAT_REPORT_SKIPPED_VACUUM     (1 << 0)    /* vacuum is skipped */
> > > +#define PGSTAT_REPORT_SKIPPED_ANALYZE        (1 << 1)    /* analyze is skipped */
> > 
> > The struct fields were renamed to lock_skipped_*, but these flags and
> > pgstat_report_skipped_vacuum_analyze() keep the generic name. Should
> > we rename them too, for consistency?
> 
> I renamed them to PGSTAT_REPORT_LOCK_SKIPPED_*.
> 
> > 
> > In 0002,
> > 
> > > +      'vacuum_skip_lock_stats',
> > 
> > This also needs to be added to the ISOLATION list in
> > src/test/modules/injection_points/Makefile. Without it the test never
> > runs in the autoconf build. I confirmed it passes once registered
> > there.
> 
> I fixed the Makefile.
> 
> Also, I added a comment explaining the RangeVarGetRelid() call with
> NoLock in expand_vacuum_rel(), including why NoLock is used and why it is safe
> in this case.
> 
> +                       /*
> +                        * Get relid for statistics reporting.
> +                        *
> +                        * Since we failed to acquire the lock, use NoLock here.  Although
> +                        * a concurrent DDL may have dropped or renamed the relation,
> +                        * RangeVarGetRelid() with NoLock does not check for invalidation
> +                        * messages.
> +                        */
> +                       relid = RangeVarGetRelid(vrel->relation, NoLock, true);
> +


Regards,
Yugo Nagata

-- 
Yugo Nagata 

Re: Track skipped tables during autovacuum and autoanalyze

От:
Sami Imseih <samimseih@gmail.com>
Дата:
Вложения:
Hi,

> I've attached a patch to fix this by inserting "noop" step in session s2
> to ensure that the vacuum has finished.
>
> Does this look good to you?
>
> [1] https://github.com/postgresql-cfbot/postgresql/actions/runs/30115503531/attempts/1

Yes, this looks correct to me.

Thanks!

--
Sami Imseih
Amazon Web Services (AWS)


Re: Track skipped tables during autovacuum and autoanalyze

От:
Shinya Kato <shinya11.kato@gmail.com>
Дата:
Thanks for updating the patches!

On Tue, Sep 15, 2026 at 4:43 AM Sami Imseih  wrote:
> This is less about stats reporting and more about filtering out such
> relations earlier in expand_vacuum_rel() for manually specified
> VACUUM/ANALYZE targets. Not a big thing, but it would avoid an extra
> syscache lookup and a misleading "lock not available" warning.

I would like to fold one more case into that follow-up. The skip is
reported before vacuum_is_permitted_for_relation(), so a user with no
rights on the table can still move its counters.

The callback argument of RangeVarGetRelidExtended() looks like the
right place for all of this. It runs before the lock attempt, and the
comment there already says that is where permission checks belong. It
also hands us relId, so expand_vacuum_rel() would no longer need the
second RangeVarGetRelid(..., NoLock, true) lookup.


-- 
Shinya Kato
NTT OSS Center


Re: Track skipped tables during autovacuum and autoanalyze

От:
Sami Imseih <samimseih.pg@gmail.com>
Дата:
Вложения:
> On Tue, Sep 15, 2026 at 4:43 AM Sami Imseih  wrote:
> > This is less about stats reporting and more about filtering out such
> > relations earlier in expand_vacuum_rel() for manually specified
> > VACUUM/ANALYZE targets. Not a big thing, but it would avoid an extra
> > syscache lookup and a misleading "lock not available" warning.
>
> I would like to fold one more case into that follow-up. The skip is
> reported before vacuum_is_permitted_for_relation(), so a user with no
> rights on the table can still move its counters.
>
> The callback argument of RangeVarGetRelidExtended() looks like the
> right place for all of this. It runs before the lock attempt, and the
> comment there already says that is where permission checks belong. It
> also hands us relId, so expand_vacuum_rel() would no longer need the
> second RangeVarGetRelid(..., NoLock, true) lookup.

It may be worth cleaning this stuff up if someone is interested in taking
this on.

v16 is just a rebase. I also took out the catversion change for now, but made
it clear it's required in the commit message.

--
Sami Imseih
Amazon Web Services (AWS)

Re: Track skipped tables during autovacuum and autoanalyze

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
On Wed, 16 Sep 2026 10:30:44 -0500
Sami Imseih  wrote:

> > On Tue, Sep 15, 2026 at 4:43 AM Sami Imseih  wrote:
> > > This is less about stats reporting and more about filtering out such
> > > relations earlier in expand_vacuum_rel() for manually specified
> > > VACUUM/ANALYZE targets. Not a big thing, but it would avoid an extra
> > > syscache lookup and a misleading "lock not available" warning.
> >
> > I would like to fold one more case into that follow-up. The skip is
> > reported before vacuum_is_permitted_for_relation(), so a user with no
> > rights on the table can still move its counters.
> >
> > The callback argument of RangeVarGetRelidExtended() looks like the
> > right place for all of this. It runs before the lock attempt, and the
> > comment there already says that is where permission checks belong. It
> > also hands us relId, so expand_vacuum_rel() would no longer need the
> > second RangeVarGetRelid(..., NoLock, true) lookup.
> 
> It may be worth cleaning this stuff up if someone is interested in taking
> this on.
> 
> v16 is just a rebase. I also took out the catversion change for now, but made
> it clear it's required in the commit message.

Thank you for updating the patch.
I'm fine with the changes you made incorporating Kato-san's suggestions.

One minor comment is:

 		toast_vacuum_params.options |= VACOPT_PROCESS_MAIN;
+		toast_vacuum_params.options &= ~VACOPT_ANALYZE;
 		toast_vacuum_params.toast_parent = relid;

I wonder if this change is still necessary now that the filtering for TOAST
tables has been added to pgstat_report_skipped_vacuum_analyze().

If we keep this change, should we also add a comment explaining
why it is needed?

Regards,
Yugo Nagata

-- 
Yugo Nagata 


Re: Track skipped tables during autovacuum and autoanalyze

От:
Sami Imseih <samimseih.pg@gmail.com>
Дата:
Вложения:
> One minor comment is:
>
>                 toast_vacuum_params.options |= VACOPT_PROCESS_MAIN;
> +               toast_vacuum_params.options &= ~VACOPT_ANALYZE;
>                 toast_vacuum_params.toast_parent = relid;
>
> I wonder if this change is still necessary now that the filtering for TOAST
> tables has been added to pgstat_report_skipped_vacuum_analyze().
>
> If we keep this change, should we also add a comment explaining
> why it is needed?

Right. I removed it.

--
Sami

Re: Track skipped tables during autovacuum and autoanalyze

От:
Sami Imseih <samimseih.pg@gmail.com>
Дата:
Вложения:
Hi,

Mandatory rebase due to e0f633ca6bc.

--
Sami Imseih
Amazon Web Services (AWS)

Re: Track skipped tables during autovacuum and autoanalyze

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
Вложения:
Hi,

I've attached rebased patches.

I believe all review comments have been addressed. If there are no further
concerns, could we consider marking it Ready for Committer?

Regards,
Yugo Nagata

On Tue, 6 Oct 2026 12:46:47 -0500
Sami Imseih  wrote:

> Hi,
> 
> Mandatory rebase due to e0f633ca6bc.
> 
> --
> Sami Imseih
> Amazon Web Services (AWS)


-- 
Yugo Nagata 

Re: Track skipped tables during autovacuum and autoanalyze

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Hello!

I have only one small comment/question: it seems like the
PGSTAT_FILE_FORMAT_ID change was dropped between v14 and v15?


Re: Track skipped tables during autovacuum and autoanalyze

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Wed, Nov 04, 2026 at 03:55:31PM -0600, Zsolt Parragi wrote:
> I have only one small comment/question: it seems like the
> PGSTAT_FILE_FORMAT_ID change was dropped between v14 and v15?

That would be the kind of thing a committer would take care of once/if
committing such a patch.  I'd suggest to add a notice to the commit
message of the sent patch instead, to not forget that the counter
needs a bump when/if it gets merged.
--
Michael

Re: Track skipped tables during autovacuum and autoanalyze

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
Вложения:
On Thu, 5 Nov 2026 07:16:45 +0900
Michael Paquier  wrote:

> On Wed, Nov 04, 2026 at 03:55:31PM -0600, Zsolt Parragi wrote:
> > I have only one small comment/question: it seems like the
> > PGSTAT_FILE_FORMAT_ID change was dropped between v14 and v15?
> 
> That would be the kind of thing a committer would take care of once/if
> committing such a patch.  I'd suggest to add a notice to the commit
> message of the sent patch instead, to not forget that the counter
> needs a bump when/if it gets merged.

Thank you for the suggestion.

Attached is an updated patch with only the suggested comment added.

Regards,
Yugo Nagata


-- 
Yugo Nagata 

Re: Track skipped tables during autovacuum and autoanalyze

От:
vignesh C <vignesh21@gmail.com>
Дата:
Вложения:
On Thu, 5 Nov 2026 at 07:56, Yugo Nagata  wrote:
>
> On Thu, 5 Nov 2026 07:16:45 +0900
> Michael Paquier  wrote:
>
> > On Wed, Nov 04, 2026 at 03:55:31PM -0600, Zsolt Parragi wrote:
> > > I have only one small comment/question: it seems like the
> > > PGSTAT_FILE_FORMAT_ID change was dropped between v14 and v15?
> >
> > That would be the kind of thing a committer would take care of once/if
> > committing such a patch.  I'd suggest to add a notice to the commit
> > message of the sent patch instead, to not forget that the counter
> > needs a bump when/if it gets merged.
>
> Thank you for the suggestion.
>
> Attached is an updated patch with only the suggested comment added.

Thanks for the updated patch.
I reviewed v20 and found that a skip is recorded before any privilege
checks are performed. While a lock is held on a table, any role that
can see the table's schema can run:
VACUUM (SKIP_LOCKED) t;
ANALYZE (SKIP_LOCKED) t;

This allows the role to increment t's lock_skipped_vacuum_count /
lock_skipped_analyze_count and update the corresponding
last_lock_skipped_* timestamps, even if it has no privileges on the
table. Since it allows an unprivileged role to modify the shared
statistics of a table it cannot access.
Attached a patch which has the test to reproduce this issue.
Thoughts?

Regards,
Vignesh
FAQ