8000 Avoid useless respawining the autovacuum launcher at high speed. · hackingwu/postgres@5dff230 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5dff230

Browse files
committed
Avoid useless respawining the autovacuum launcher at high speed.
When (1) autovacuum = off and (2) there's at least one database with an XID age greater than autovacuum_freeze_max_age and (3) all tables in that database that need vacuuming are already being processed by a worker and (4) the autovacuum launcher is started, a kind of infinite loop occurs. The launcher starts a worker and immediately exits. The worker, finding no worker to do, immediately starts the launcher, supposedly so that the next database can be processed. But because datfrozenxid for that database hasn't been advanced yet, the new worker gets put right back into the same database as the old one, where it once again starts the launcher and exits. High-speed ping pong ensues. There are several possible ways to break the cycle; this seems like the safest one. Amit Khandekar (code) and Robert Haas (comments), reviewed by Álvaro Herrera. Discussion: http://postgr.es/m/CAJ3gD9eWejf72HKquKSzax0r+epS=nAbQKNnykkMA0E8c+rMDg@mail.gmail.com
1 parent 154875a commit 5dff230

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/backend/postmaster/autovacuum.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,8 @@ do_autovacuum(void)
19271927
BufferAccessStrategy bstrategy;
19281928
ScanKeyData key;
19291929
TupleDesc pg_class_desc;
1930+
bool did_vacuum = false;
1931+
bool found_concurrent_worker = false;
19301932

19311933
/*
19321934
* StartTransactionCommand and CommitTransactionCommand will automatically
@@ -2243,6 +2245,7 @@ do_autovacuum(void)
22432245
if (worker->wi_tableoid == relid)
22442246
{
22452247
skipit = true;
2248+
found_concurrent_worker = true;
22462249
break;
22472250
}
22482251

@@ -2373,6 +2376,8 @@ do_autovacuum(void)
23732376
}
23742377
PG_END_TRY();
23752378

2379+
did_vacuum = true;
2380+
23762381
/* the PGXACT flags are reset at the next end of transaction */
23772382

23782383
/* be tidy */
@@ -2410,8 +2415,25 @@ do_autovacuum(void)
24102415
/*
24112416
* Update pg_database.datfrozenxid, and truncate pg_clog if possible. We
24122417
* only need to do this once, not after each table.
2418+
*
2419+
* Even if we didn't vacuum anything, it may still be important to do
2420+
* this, because one indirect effect of vac_update_datfrozenxid() is to
2421+
* update ShmemVariableCache->xidVacLimit. That might need to be done
2422+
* even if we haven't vacuumed anything, because relations with older
2423+
* relfrozenxid values or other databases with older datfrozenxid values
2424+
* might have been dropped, allowing xidVacLimit to advance.
2425+
*
2426+
* However, it's also important not to do this blindly in all cases,
2427+
* because when autovacuum=off this will restart the autovacuum launcher.
2428+
* If we're not careful, an infinite loop can result, where workers find
2429+
* no work to do and restart the launcher, which starts another worker in
2430+
* the same database that finds no work to do. To prevent that, we skip
2431+
* this if (1) we found no work to do and (2) we skipped at least one
2432+
* table due to concurrent autovacuum activity. In that case, the other
2433+
* worker has already done it, or will do so when it finishes.
24132434
*/
2414-
vac_update_datfrozenxid();
2435+
if (did_vacuum || !found_concurrent_worker)
2436+
vac_update_datfrozenxid();
24152437

24162438
/* Finally close out the last transaction. */
24172439
CommitTransactionCommand();

0 commit comments

Comments
 (0)
0