Skip to content

Commit 769d681

Browse files
committed
Don't balance vacuum cost delay when per-table settings are in effect
When there are cost-delay-related storage options set for a table, trying to make that table participate in the autovacuum cost-limit balancing algorithm produces undesirable results: instead of using the configured values, the global values are always used, as illustrated by Mark Kirkwood in http://www.postgresql.org/message-id/52FACF15.8020507@catalyst.net.nz Since the mechanism is already complicated, just disable it for those cases rather than trying to make it cope. There are undesirable side-effects from this too, namely that the total I/O impact on the system will be higher whenever such tables are vacuumed. However, this is seen as less harmful than slowing down vacuum, because that would cause bloat to accumulate. Anyway, in the new system it is possible to tweak options to get the precise behavior one wants, whereas with the previous system one was simply hosed. This has been broken forever, so backpatch to all supported branches. This might affect systems where cost_limit and cost_delay have been set for individual tables.
1 parent 8e137b0 commit 769d681

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

doc/src/sgml/maintenance.sgml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,10 +730,13 @@ analyze threshold = analyze base threshold + analyze scale factor * number of tu
730730
</para>
731731

732732
<para>
733-
When multiple workers are running, the cost limit is
733+
When multiple workers are running, the cost delay parameters are
734734
<quote>balanced</quote> among all the running workers, so that the
735-
total impact on the system is the same, regardless of the number
736-
of workers actually running.
735+
total I/O impact on the system is the same regardless of the number
736+
of workers actually running. However, any workers processing tables whose
737+
<literal>autovacuum_vacuum_cost_delay</> or
738+
<literal>autovacuum_vacuum_cost_limit</> have been set are not considered
739+
in the balancing algorithm.
737740
</para>
738741
</sect2>
739742
</sect1>

src/backend/postmaster/autovacuum.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ typedef struct autovac_table
180180
int at_freeze_table_age;
181181
int at_vacuum_cost_delay;
182182
int at_vacuum_cost_limit;
183+
bool at_dobalance;
183184
bool at_wraparound;
184185
char *at_relname;
185186
char *at_nspname;
@@ -210,6 +211,7 @@ typedef struct WorkerInfoData
210211
Oid wi_tableoid;
211212
PGPROC *wi_proc;
212213
TimestampTz wi_launchtime;
214+
bool wi_dobalance;
213215
int wi_cost_delay;
214216
int wi_cost_limit;
215217
int wi_cost_limit_base;
@@ -1684,6 +1686,7 @@ FreeWorkerInfo(int code, Datum arg)
16841686
MyWorkerInfo->wi_tableoid = InvalidOid;
16851687
MyWorkerInfo->wi_proc = NULL;
16861688
MyWorkerInfo->wi_launchtime = 0;
1689+
MyWorkerInfo->wi_dobalance = false;
16871690
MyWorkerInfo->wi_cost_delay = 0;
16881691
MyWorkerInfo->wi_cost_limit = 0;
16891692
MyWorkerInfo->wi_cost_limit_base = 0;
@@ -1743,14 +1746,15 @@ autovac_balance_cost(void)
17431746
if (vac_cost_limit <= 0 || vac_cost_delay <= 0)
17441747
return;
17451748

1746-
/* caculate the total base cost limit of active workers */
1749+
/* calculate the total base cost limit of participating active workers */
17471750
cost_total = 0.0;
17481751
worker = (WorkerInfo) SHMQueueNext(&AutoVacuumShmem->av_runningWorkers,
17491752
&AutoVacuumShmem->av_runningWorkers,
17501753
offsetof(WorkerInfoData, wi_links));
17511754
while (worker)
17521755
{
17531756
if (worker->wi_proc != NULL &&
1757+
worker->wi_dobalance &&
17541758
worker->wi_cost_limit_base > 0 && worker->wi_cost_delay > 0)
17551759
cost_total +=
17561760
(double) worker->wi_cost_limit_base / worker->wi_cost_delay;
@@ -1759,6 +1763,7 @@ autovac_balance_cost(void)
17591763
&worker->wi_links,
17601764
offsetof(WorkerInfoData, wi_links));
17611765
}
1766+
17621767
/* there are no cost limits -- nothing to do */
17631768
if (cost_total <= 0)
17641769
return;
@@ -1774,6 +1779,7 @@ autovac_balance_cost(void)
17741779
while (worker)
17751780
{
17761781
if (worker->wi_proc != NULL &&
1782+
worker->wi_dobalance &&
17771783
worker->wi_cost_limit_base > 0 && worker->wi_cost_delay > 0)
17781784
{
17791785
int limit = (int)
@@ -1788,12 +1794,14 @@ autovac_balance_cost(void)
17881794
worker->wi_cost_limit = Max(Min(limit,
17891795
worker->wi_cost_limit_base),
17901796
1);
1797+
}
17911798

1792-
elog(DEBUG2, "autovac_balance_cost(pid=%u db=%u, rel=%u, cost_limit=%d, cost_limit_base=%d, cost_delay=%d)",
1799+
if (worker->wi_proc != NULL)
1800+
elog(DEBUG2, "autovac_balance_cost(pid=%u db=%u, rel=%u, dobalance=%s cost_limit=%d, cost_limit_base=%d, cost_delay=%d)",
17931801
worker->wi_proc->pid, worker->wi_dboid, worker->wi_tableoid,
1802+
worker->wi_dobalance ? "yes" : "no",
17941803
worker->wi_cost_limit, worker->wi_cost_limit_base,
17951804
worker->wi_cost_delay);
1796-
}
17971805

17981806
worker = (WorkerInfo) SHMQueueNext(&AutoVacuumShmem->av_runningWorkers,
17991807
&worker->wi_links,
@@ -2263,6 +2271,7 @@ do_autovacuum(void)
22632271
LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
22642272

22652273
/* advertise my cost delay parameters for the balancing algorithm */
2274+
MyWorkerInfo->wi_dobalance = tab->at_dobalance;
22662275
MyWorkerInfo->wi_cost_delay = tab->at_vacuum_cost_delay;
22672276
MyWorkerInfo->wi_cost_limit = tab->at_vacuum_cost_limit;
22682277
MyWorkerInfo->wi_cost_limit_base = tab->at_vacuum_cost_limit;
@@ -2543,6 +2552,14 @@ table_recheck_autovac(Oid relid, HTAB *table_toast_map,
25432552
tab->at_relname = NULL;
25442553
tab->at_nspname = NULL;
25452554
tab->at_datname = NULL;
2555+
2556+
/*
2557+
* If any of the cost delay parameters has been set individually for
2558+
* this table, disable the balancing algorithm.
2559+
*/
2560+
tab->at_dobalance =
2561+
!(avopts && (avopts->vacuum_cost_limit > 0 ||
2562+
avopts->vacuum_cost_delay > 0));
25462563
}
25472564

25482565
heap_freetuple(classTup);

0 commit comments

Comments
 (0)