Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit38f7831

Browse files
committed
Avoid holding AutovacuumScheduleLock while rechecking table statistics.
In databases with many tables, re-fetching the statistics takes some time,so that this behavior seriously decreases the available concurrency formultiple autovac workers. There's discussion afoot about more completefixes, but a simple and back-patchable amelioration is to claim the tableand release the lock before rechecking stats. If we find out there's nolonger a reason to process the table, re-taking the lock to un-claim thetable is cheap enough.(This patch is quite old, but got lost amongst a discussion of moreaggressive fixes. It's not clear when or if such a fix will beaccepted, but in any case it'd be unlikely to get back-patched.Let's do this now so we have some improvement for the back branches.)In passing, make the normal un-claim step take AutovacuumScheduleLocknot AutovacuumLock, since that is what is documented to protect thewi_tableoid field. This wasn't an actual bug in view of the fact thatreaders of that field hold both locks, but it creates some concurrencypenalty against operations that need only AutovacuumLock.Back-patch to all supported versions.Jeff JanesDiscussion:https://postgr.es/m/26118.1520865816@sss.pgh.pa.us
1 parentb32fad5 commit38f7831

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

‎src/backend/postmaster/autovacuum.c

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ typedef struct autovac_table
212212
* wi_launchtime Time at which this worker was launched
213213
* wi_cost_*Vacuum cost-based delay parameters current in this worker
214214
*
215-
* All fields are protected by AutovacuumLock, except for wi_tableoidwhich is
216-
* protected by AutovacuumScheduleLock (which is read-only for everyone except
217-
* that worker itself).
215+
* All fields are protected by AutovacuumLock, except for wi_tableoidand
216+
*wi_sharedrel which areprotected by AutovacuumScheduleLock (note these
217+
*two fields are read-only for everyone exceptthat worker itself).
218218
*-------------
219219
*/
220220
typedefstructWorkerInfoData
@@ -2317,7 +2317,9 @@ do_autovacuum(void)
23172317
foreach(cell,table_oids)
23182318
{
23192319
Oidrelid=lfirst_oid(cell);
2320+
HeapTupleclassTup;
23202321
autovac_table*tab;
2322+
boolisshared;
23212323
boolskipit;
23222324
intstdVacuumCostDelay;
23232325
intstdVacuumCostLimit;
@@ -2342,9 +2344,23 @@ do_autovacuum(void)
23422344
}
23432345

23442346
/*
2345-
* hold schedule lock from here until we're sure that this table still
2346-
* needs vacuuming. We also need the AutovacuumLock to walk the
2347-
* worker array, but we'll let go of that one quickly.
2347+
* Find out whether the table is shared or not. (It's slightly
2348+
* annoying to fetch the syscache entry just for this, but in typical
2349+
* cases it adds little cost because table_recheck_autovac would
2350+
* refetch the entry anyway. We could buy that back by copying the
2351+
* tuple here and passing it to table_recheck_autovac, but that
2352+
* increases the odds of that function working with stale data.)
2353+
*/
2354+
classTup=SearchSysCache1(RELOID,ObjectIdGetDatum(relid));
2355+
if (!HeapTupleIsValid(classTup))
2356+
continue;/* somebody deleted the rel, forget it */
2357+
isshared= ((Form_pg_class)GETSTRUCT(classTup))->relisshared;
2358+
ReleaseSysCache(classTup);
2359+
2360+
/*
2361+
* Hold schedule lock from here until we've claimed the table. We
2362+
* also need the AutovacuumLock to walk the worker array, but that one
2363+
* can just be a shared lock.
23482364
*/
23492365
LWLockAcquire(AutovacuumScheduleLock,LW_EXCLUSIVE);
23502366
LWLockAcquire(AutovacuumLock,LW_SHARED);
@@ -2380,6 +2396,16 @@ do_autovacuum(void)
23802396
continue;
23812397
}
23822398

2399+
/*
2400+
* Store the table's OID in shared memory before releasing the
2401+
* schedule lock, so that other workers don't try to vacuum it
2402+
* concurrently. (We claim it here so as not to hold
2403+
* AutovacuumScheduleLock while rechecking the stats.)
2404+
*/
2405+
MyWorkerInfo->wi_tableoid=relid;
2406+
MyWorkerInfo->wi_sharedrel=isshared;
2407+
LWLockRelease(AutovacuumScheduleLock);
2408+
23832409
/*
23842410
* Check whether pgstat data still says we need to vacuum this table.
23852411
* It could have changed if something else processed the table while
@@ -2396,18 +2422,13 @@ do_autovacuum(void)
23962422
if (tab==NULL)
23972423
{
23982424
/* someone else vacuumed the table, or it went away */
2425+
LWLockAcquire(AutovacuumScheduleLock,LW_EXCLUSIVE);
2426+
MyWorkerInfo->wi_tableoid=InvalidOid;
2427+
MyWorkerInfo->wi_sharedrel= false;
23992428
LWLockRelease(AutovacuumScheduleLock);
24002429
continue;
24012430
}
24022431

2403-
/*
2404-
* Ok, good to go. Store the table in shared memory before releasing
2405-
* the lock so that other workers don't vacuum it concurrently.
2406-
*/
2407-
MyWorkerInfo->wi_tableoid=relid;
2408-
MyWorkerInfo->wi_sharedrel=tab->at_sharedrel;
2409-
LWLockRelease(AutovacuumScheduleLock);
2410-
24112432
/*
24122433
* Remember the prevailing values of the vacuum cost GUCs. We have to
24132434
* restore these at the bottom of the loop, else we'll compute wrong
@@ -2522,10 +2543,10 @@ do_autovacuum(void)
25222543
* settings, so we don't want to give up our share of I/O for a very
25232544
* short interval and thereby thrash the global balance.
25242545
*/
2525-
LWLockAcquire(AutovacuumLock,LW_EXCLUSIVE);
2546+
LWLockAcquire(AutovacuumScheduleLock,LW_EXCLUSIVE);
25262547
MyWorkerInfo->wi_tableoid=InvalidOid;
25272548
MyWorkerInfo->wi_sharedrel= false;
2528-
LWLockRelease(AutovacuumLock);
2549+
LWLockRelease(AutovacuumScheduleLock);
25292550

25302551
/* restore vacuum cost GUCs for the next iteration */
25312552
VacuumCostDelay=stdVacuumCostDelay;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp