diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c new file mode 100644 index b53cfdb..4b4faad *** a/src/backend/postmaster/autovacuum.c --- b/src/backend/postmaster/autovacuum.c *************** do_start_worker(void) *** 1169,1174 **** --- 1169,1206 ---- avw_dbase *tmp = lfirst(cell); dlist_iter iter; + /* + * Also, skip a database that appears on the database list as having + * been processed recently (less than autovacuum_naptime seconds ago). + * We do this so that we don't select a database which we just + * selected, but that pgstat hasn't gotten around to updating the last + * autovacuum time yet. + */ + skipit = false; + + dlist_reverse_foreach(iter, &DatabaseList) + { + avl_dbase *dbp = dlist_container(avl_dbase, adl_node, iter.cur); + + if (dbp->adl_datid == tmp->adw_datid) + { + /* + * Skip this database if its next_worker value falls between + * the current time and the current time plus naptime. + */ + if (!TimestampDifferenceExceeds(dbp->adl_next_worker, + current_time, 0) && + !TimestampDifferenceExceeds(current_time, + dbp->adl_next_worker, + autovacuum_naptime * 1000)) + skipit = true; + + break; + } + } + if (skipit) + continue; + /* Check to see if this one is at risk of wraparound */ if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit)) { *************** do_start_worker(void) *** 1203,1240 **** continue; /* - * Also, skip a database that appears on the database list as having - * been processed recently (less than autovacuum_naptime seconds ago). - * We do this so that we don't select a database which we just - * selected, but that pgstat hasn't gotten around to updating the last - * autovacuum time yet. - */ - skipit = false; - - dlist_reverse_foreach(iter, &DatabaseList) - { - avl_dbase *dbp = dlist_container(avl_dbase, adl_node, iter.cur); - - if (dbp->adl_datid == tmp->adw_datid) - { - /* - * Skip this database if its next_worker value falls between - * the current time and the current time plus naptime. - */ - if (!TimestampDifferenceExceeds(dbp->adl_next_worker, - current_time, 0) && - !TimestampDifferenceExceeds(current_time, - dbp->adl_next_worker, - autovacuum_naptime * 1000)) - skipit = true; - - break; - } - } - if (skipit) - continue; - - /* * Remember the db with oldest autovac time. (If we are here, both * tmp->entry and db->entry must be non-null.) */ --- 1235,1240 ----