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

Commit 746ba76

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 fd081ca commit 746ba76

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
@@ -1895,6 +1895,8 @@ do_autovacuum(void)
18951895
ScanKeyData key;
18961896
TupleDesc pg_class_desc;
18971897
int effective_multixact_freeze_max_age;
1898+
bool did_vacuum = false;
1899+
bool found_concurrent_worker = false;
18981900

18991901
/*
19001902
* StartTransactionCommand and CommitTransactionCommand will automatically
@@ -2233,6 +2235,7 @@ do_autovacuum(void)
22332235
if (worker->wi_tableoid == relid)
22342236
{
22352237
skipit = true;
2238+
found_concurrent_worker = true;
22362239
break;
22372240
}
22382241
}
@@ -2359,6 +2362,8 @@ do_autovacuum(void)
23592362
}
23602363
PG_END_TRY();
23612364

2365+
did_vacuum = true;
2366+
23622367
/* the PGXACT flags are reset at the next end of transaction */
23632368

23642369
/* be tidy */
@@ -2396,8 +2401,25 @@ do_autovacuum(void)
23962401
/*
23972402
* Update pg_database.datfrozenxid, and truncate pg_clog if possible. We
23982403
* only need to do this once, not after each table.
2404+
*
2405+
* Even if we didn't vacuum anything, it may still be important to do
2406+
* this, because one indirect effect of vac_update_datfrozenxid() is to
2407+
* update ShmemVariableCache->xidVacLimit. That might need to be done
2408+
* even if we haven't vacuumed anything, because relations with older
2409+
* relfrozenxid values or other databases with older datfrozenxid values
2410+
* might have been dropped, allowing xidVacLimit to advance.
2411+
*
2412+
* However, it's also important not to do this blindly in all cases,
2413+
* because when autovacuum=off this will restart the autovacuum launcher.
2414+
* If we're not careful, an infinite loop can result, where workers find
2415+
* no work to do and restart the launcher, which starts another worker in
2416+
* the same database that finds no work to do. To prevent that, we skip
2417+
* this if (1) we found no work to do and (2) we skipped at least one
2418+
* table due to concurrent autovacuum activity. In that case, the other
2419+
* worker has already done it, or will do so when it finishes.
23992420
*/
2400-
vac_update_datfrozenxid();
2421+
if (did_vacuum || !found_concurrent_worker)
2422+
vac_update_datfrozenxid();
24012423

24022424
/* Finally close out the last transaction. */
24032425
CommitTransactionCommand();

0 commit comments

Comments
 (0)