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

Commit 4460964

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 for multiple autovac workers. There's discussion afoot about more complete fixes, but a simple and back-patchable amelioration is to claim the table and release the lock before rechecking stats. If we find out there's no longer a reason to process the table, re-taking the lock to un-claim the table is cheap enough. (This patch is quite old, but got lost amongst a discussion of more aggressive fixes. It's not clear when or if such a fix will be accepted, 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 AutovacuumScheduleLock not AutovacuumLock, since that is what is documented to protect the wi_tableoid field. This wasn't an actual bug in view of the fact that readers of that field hold both locks, but it creates some concurrency penalty against operations that need only AutovacuumLock. Back-patch to all supported versions. Jeff Janes Discussion: https://postgr.es/m/26118.1520865816@sss.pgh.pa.us
1 parent fe65f59 commit 4460964

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
@@ -211,9 +211,9 @@ typedef struct autovac_table
211211
* wi_launchtime Time at which this worker was launched
212212
* wi_cost_* Vacuum cost-based delay parameters current in this worker
213213
*
214-
* All fields are protected by AutovacuumLock, except for wi_tableoid which is
215-
* protected by AutovacuumScheduleLock (which is read-only for everyone except
216-
* that worker itself).
214+
* All fields are protected by AutovacuumLock, except for wi_tableoid and
215+
* wi_sharedrel which are protected by AutovacuumScheduleLock (note these
216+
* two fields are read-only for everyone except that worker itself).
217217
*-------------
218218
*/
219219
typedef struct WorkerInfoData
@@ -2316,7 +2316,9 @@ do_autovacuum(void)
23162316
foreach(cell, table_oids)
23172317
{
23182318
Oid relid = lfirst_oid(cell);
2319+
HeapTuple classTup;
23192320
autovac_table *tab;
2321+
bool isshared;
23202322
bool skipit;
23212323
int stdVacuumCostDelay;
23222324
int stdVacuumCostLimit;
@@ -2341,9 +2343,23 @@ do_autovacuum(void)
23412343
}
23422344

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

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

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

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

0 commit comments

Comments
 (0)