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

Commit 38f7831

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 b32fad5 commit 38f7831

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

src/backend/postmaster/autovacuum.c

+37-16
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_tableoid which 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_tableoid and
216+
* wi_sharedrel which are protected by AutovacuumScheduleLock (note these
217+
* two fields are read-only for everyone except that worker itself).
218218
*-------------
219219
*/
220220
typedef struct WorkerInfoData
@@ -2317,7 +2317,9 @@ do_autovacuum(void)
23172317
foreach(cell, table_oids)
23182318
{
23192319
Oid relid = lfirst_oid(cell);
2320+
HeapTuple classTup;
23202321
autovac_table *tab;
2322+
bool isshared;
23212323
bool skipit;
23222324
int stdVacuumCostDelay;
23232325
int stdVacuumCostLimit;
@@ -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)