pgsql: Add rudimentary table prioritization to autovacuum. - Mailing list pgsql-committers

From Nathan Bossart
Subject pgsql: Add rudimentary table prioritization to autovacuum.
Date
Msg-id E1w68wa-001iA8-2c@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Add rudimentary table prioritization to autovacuum.

Autovacuum workers scan pg_class twice to collect the set of tables
to process.  The first pass is for plain relations and materialized
views, and the second is for TOAST tables.  When the worker finds a
table to process, it adds it to the end of a list.  Later on, it
processes the tables in the same order as the list.  This simple
strategy has worked surprisingly well for a long time, but there
have been many discussions over the years about trying to improve
it.

This commit introduces a scoring system that is used to sort the
aforementioned list of tables to process.  The idea is to have
autovacuum workers prioritize tables that are furthest beyond their
thresholds (e.g., a table nearing transaction ID wraparound should
be vacuumed first).  This prioritization scheme is certainly far
from perfect; there are simply too many possibilities for any
scoring technique to work across all workloads, and the situation
might change significantly between the time we calculate the score
and the time that autovacuum processes it.  However, we have
attemped to develop something that is expected to work for a large
portion of workloads with reasonable parameter settings.

The score is calculated as the maximum of the ratios of each of the
table's relevant values to its threshold.  For example, if the
number of inserted tuples is 100, and the insert threshold for the
table is 80, the insert score is 1.25.  If all other scores are
below that value, the table's score will be 1.25.  The other
criteria considered for the score are the table ages (both
relfrozenxid and relminmxid) compared to the corresponding
freeze-max-age setting, the number of update/deleted tuples
compared to the vacuum threshold, and the number of
inserted/updated/deleted tuples compared to the analyze threshold.

Once exception to the previous paragraph is for tables nearing
wraparound, i.e., those that have surpassed the effective failsafe
ages.  In that case, the relfrozenxid/relminmxid-based score is
scaled aggressively so that the table has a decent chance of
sorting to the front of the list.

To adjust how strongly each component contributes to the score, the
following parameters can be adjusted from their default of 1.0 to
anywhere between 0.0 and 10.0 (inclusive).  Setting all of these to
0.0 restores pre-v19 prioritization behavior:

        autovacuum_freeze_score_weight
        autovacuum_multixact_freeze_score_weight
        autovacuum_vacuum_score_weight
        autovacuum_vacuum_insert_score_weight
        autovacuum_analyze_score_weight

This is intended to be a baby step towards smarter autovacuum
workers.  Possible future improvements include, but are not limited
to, periodic reprioritization, automatic cost limit adjustments,
and better observability (e.g., a system view that shows current
scores).  While we do not expect this commit to produce any
earth-shattering improvements, it is arguably a prerequisite for
the aforementioned follow-up changes.

Reviewed-by: Sami Imseih <samimseih@gmail.com>
Reviewed-by: David Rowley <dgrowleyml@gmail.com>
Reviewed-by: Robert Haas <robertmhaas@gmail.com>
Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com>
Reviewed-by: Greg Burd <greg@burd.me>
Reviewed-by: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Discussion: https://postgr.es/m/aOaAuXREwnPZVISO%40nathan

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/d7965d65fc5bb2139bc51c051c11428414c65160

Modified Files
--------------
doc/src/sgml/config.sgml                      |  90 ++++++++
doc/src/sgml/maintenance.sgml                 | 103 ++++++++++
src/backend/postmaster/autovacuum.c           | 283 +++++++++++++++++++++-----
src/backend/utils/misc/guc_parameters.dat     |  40 ++++
src/backend/utils/misc/postgresql.conf.sample |   5 +
src/include/postmaster/autovacuum.h           |   6 +-
src/tools/pgindent/typedefs.list              |   2 +
7 files changed, 481 insertions(+), 48 deletions(-)


pgsql-committers by date:

Previous
From: Peter Eisentraut
Date:
Subject: pgsql: Align tests for stored and virtual generated columns
Next
From: Masahiko Sawada
Date:
Subject: pgsql: doc: Clarify collation requirements for base32hex sortability.