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

Commit 16b0f6c

Browse files
author
Commitfest Bot
committed
[CF 5732] v2 - Introduce some randomness to autovacuum
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5732 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/CAEG8a3Jw77SV7XWkYTN50jgYQ6CxcTCch=1a8a5ZmbLdD2yR8w@mail.gmail.com Author(s): Zhao Junwang
2 parents 5987553 + b6cb063 commit 16b0f6c

File tree

7 files changed

+76
-0
lines changed

7 files changed

+76
-0
lines changed

src/backend/postmaster/autovacuum.c

Lines changed: 49 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
*/
@@ -3061,6 +3093,22 @@ relation_needs_vacanalyze(Oid relid,
30613093
float4 reltuples = classForm->reltuples;
30623094
int32 relpages = classForm->relpages;
30633095
int32 relallfrozen = classForm->relallfrozen;
3096+
float4 last_remove_tuples_percent = tabentry->last_autovacuum_removed_tuples_percent;
3097+
TimestampTz last_autovacuum_time = tabentry->last_autovacuum_time;
3098+
3099+
/*
3100+
* If the last autovacuum removed tuples is less than 10% of the
3101+
* current dead tuples, then skip vacuuming this table for some time.
3102+
*/
3103+
if (!TimestampDifferenceExceedsSeconds(last_autovacuum_time,
3104+
GetCurrentTimestamp(),
3105+
autovacuum_naptime) &&
3106+
last_remove_tuples_percent < 10.0)
3107+
{
3108+
*doanalyze = false;
3109+
*dovacuum = false;
3110+
return;
3111+
}
30643112

30653113
vactuples = tabentry->dead_tuples;
30663114
instuples = tabentry->ins_since_vacuum;
@@ -3282,6 +3330,7 @@ AutoVacuumRequestWork(AutoVacuumWorkItemType type, Oid relationId,
32823330
workitem->avw_used = true;
32833331
workitem->avw_active = false;
32843332
workitem->avw_type = type;
3333+
32853334
workitem->avw_database = MyDatabaseId;
32863335
workitem->avw_relation = relationId;
32873336
workitem->avw_blockNumber = blkno;

src/backend/utils/activity/pgstat_relation.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ pgstat_report_vacuum(Oid tableoid, bool shared,
217217
Oid dboid = (shared ? InvalidOid : MyDatabaseId);
218218
TimestampTz ts;
219219
PgStat_Counter elapsedtime;
220+
PgStat_Counter old_dead_tuples;
220221

221222
if (!pgstat_track_counts)
222223
return;
@@ -232,6 +233,7 @@ pgstat_report_vacuum(Oid tableoid, bool shared,
232233
shtabentry = (PgStatShared_Relation *) entry_ref->shared_stats;
233234
tabentry = &shtabentry->stats;
234235

236+
old_dead_tuples = tabentry->dead_tuples;
235237
tabentry->live_tuples = livetuples;
236238
tabentry->dead_tuples = deadtuples;
237239

@@ -250,6 +252,11 @@ pgstat_report_vacuum(Oid tableoid, bool shared,
250252
if (AmAutoVacuumWorkerProcess())
251253
{
252254
tabentry->last_autovacuum_time = ts;
255+
if (old_dead_tuples > deadtuples)
256+
tabentry->last_autovacuum_removed_tuples_percent =
257+
100 * (old_dead_tuples - deadtuples) / old_dead_tuples;
258+
else
259+
tabentry->last_autovacuum_removed_tuples_percent = 0;
253260
tabentry->autovacuum_count++;
254261
tabentry->total_autovacuum_time += elapsedtime;
255262
}

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/pgstat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ typedef struct PgStat_StatTabEntry
443443
TimestampTz last_vacuum_time; /* user initiated vacuum */
444444
PgStat_Counter vacuum_count;
445445
TimestampTz last_autovacuum_time; /* autovacuum initiated */
446+
PgStat_Counter last_autovacuum_removed_tuples_percent;
446447
PgStat_Counter autovacuum_count;
447448
TimestampTz last_analyze_time; /* user initiated */
448449
PgStat_Counter analyze_count;

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)