Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit af51121

Browse files
zhjwpkuCommitfest Bot
authored and
Commitfest Bot
committed
Introduce autovacuum vacuum strategy
Add a new configuration option `autovacuum_vacuum_strategy` to control the order in which tables are vacuumed by the autovacuum process. Two strategies are supported for now: 1. Sequential: Tables are vacuumed in the order they are collected. 2. Random: The list of tables is rotated around a randomly chosen pivot before vacuuming to avoid always starting with the same table, which prevents vacuuming starvation for some tables. The default strategy is `sequential`. Signed-off-by: Junwang Zhao <zhjwpku@gmail.com>
1 parent 5987553 commit af51121

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

src/backend/postmaster/autovacuum.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#include "catalog/pg_namespace.h"
8080
#include "commands/dbcommands.h"
8181
#include "commands/vacuum.h"
82+
#include "common/pg_prng.h"
8283
#include "common/int.h"
8384
#include "lib/ilist.h"
8485
#include "libpq/pqsignal.h"
@@ -132,9 +133,16 @@ int autovacuum_multixact_freeze_max_age;
132133

133134
double autovacuum_vac_cost_delay;
134135
int autovacuum_vac_cost_limit;
136+
int autovacuum_vac_strategy;
135137

136138
int Log_autovacuum_min_duration = 600000;
137139

140+
const struct config_enum_entry autovacuum_vac_strategy_options[] = {
141+
{"sequential", AUTOVACUUM_VAC_STRATEGY_SEQUENTIAL, false},
142+
{"random", AUTOVACUUM_VAC_STRATEGY_RANDOM, false},
143+
{NULL, 0, false}
144+
};
145+
138146
/* the minimum allowed time between two awakenings of the launcher */
139147
#define MIN_AUTOVAC_SLEEPTIME 100.0 /* milliseconds */
140148
#define MAX_AUTOVAC_SLEEPTIME 300 /* seconds */
@@ -2267,6 +2275,30 @@ do_autovacuum(void)
22672275
"Autovacuum Portal",
22682276
ALLOCSET_DEFAULT_SIZES);
22692277

2278+
/*
2279+
* Randomly rotate the list of tables to vacuum. This is to avoid always
2280+
* vacuuming the same table first, which could lead to spinning on the
2281+
* same table or vacuuming starvation.
2282+
*/
2283+
if (list_length(table_oids) > 1 && autovacuum_vac_strategy == AUTOVACUUM_VAC_STRATEGY_RANDOM)
2284+
{
2285+
2286+
int rand = 0;
2287+
static pg_prng_state prng_state;
2288+
List *tmp_oids = NIL;
2289+
2290+
pg_prng_seed(&prng_state, (uint64) (getpid() ^ time(NULL)));
2291+
rand = (int) pg_prng_uint64_range(&prng_state, 0,
2292+
list_length(table_oids) - 1);
2293+
if (rand != 0)
2294+
{
2295+
tmp_oids = list_copy_tail(table_oids, rand);
2296+
table_oids = list_copy_head(table_oids,
2297+
list_length(table_oids) - rand);
2298+
table_oids = list_concat(table_oids, tmp_oids);
2299+
}
2300+
}
2301+
22702302
/*
22712303
* Perform operations on collected tables.
22722304
*/
@@ -3282,6 +3314,7 @@ AutoVacuumRequestWork(AutoVacuumWorkItemType type, Oid relationId,
32823314
workitem->avw_used = true;
32833315
workitem->avw_active = false;
32843316
workitem->avw_type = type;
3317+
32853318
workitem->avw_database = MyDatabaseId;
32863319
workitem->avw_relation = relationId;
32873320
workitem->avw_blockNumber = blkno;

src/backend/utils/misc/guc_tables.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5418,6 +5418,16 @@ struct config_enum ConfigureNamesEnum[] =
54185418
NULL, assign_io_method, NULL
54195419
},
54205420

5421+
{
5422+
{"autovacuum_vacuum_strategy", PGC_SIGHUP, VACUUM_AUTOVACUUM,
5423+
gettext_noop("Vacuum strategy for autovacuum."),
5424+
NULL
5425+
},
5426+
&autovacuum_vac_strategy,
5427+
AUTOVACUUM_VAC_STRATEGY_SEQUENTIAL, autovacuum_vac_strategy_options,
5428+
NULL, NULL, NULL
5429+
},
5430+
54215431
/* End-of-list marker */
54225432
{
54235433
{NULL, 0, 0, NULL, NULL}, NULL, 0, NULL, NULL, NULL, NULL

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ autovacuum_worker_slots = 16 # autovacuum worker slots to allocate
713713
#autovacuum_vacuum_cost_limit = -1 # default vacuum cost limit for
714714
# autovacuum, -1 means use
715715
# vacuum_cost_limit
716+
#autovacuum_vacuum_strategy = sequential
716717

717718
# - Cost-Based Vacuum Delay -
718719

src/include/postmaster/autovacuum.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ typedef enum
2525
AVW_BRINSummarizeRange,
2626
} AutoVacuumWorkItemType;
2727

28+
/* Autovacuum vacuum strategies */
29+
enum AutovacuumVacStrategy
30+
{
31+
AUTOVACUUM_VAC_STRATEGY_SEQUENTIAL = 0,
32+
AUTOVACUUM_VAC_STRATEGY_RANDOM,
33+
};
2834

2935
/* GUC variables */
3036
extern PGDLLIMPORT bool autovacuum_start_daemon;
@@ -43,6 +49,7 @@ extern PGDLLIMPORT int autovacuum_freeze_max_age;
4349
extern PGDLLIMPORT int autovacuum_multixact_freeze_max_age;
4450
extern PGDLLIMPORT double autovacuum_vac_cost_delay;
4551
extern PGDLLIMPORT int autovacuum_vac_cost_limit;
52+
extern PGDLLIMPORT int autovacuum_vac_strategy;
4653

4754
/* autovacuum launcher PID, only valid when worker is shutting down */
4855
extern PGDLLIMPORT int AutovacuumLauncherPid;

src/include/utils/guc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ extern PGDLLIMPORT const struct config_enum_entry io_method_options[];
322322
extern PGDLLIMPORT const struct config_enum_entry recovery_target_action_options[];
323323
extern PGDLLIMPORT const struct config_enum_entry wal_level_options[];
324324
extern PGDLLIMPORT const struct config_enum_entry wal_sync_method_options[];
325+
extern PGDLLIMPORT const struct config_enum_entry autovacuum_vac_strategy_options[];
325326

326327
/*
327328
* Functions exported by guc.c

0 commit comments

Comments
 (0)