From 6330c3662399baa5edfae637efae8712adf29b48 Mon Sep 17 00:00:00 2001 From: Colin McDonnell Date: Wed, 2 Dec 2026 18:44:27 +0000 Subject: [PATCH v4] Retry a failed worker start in the emergency autovacuum launcher When autovacuum is off, the launcher that the postmaster starts to prevent wraparound requests one worker and exits. Only the launcher's main loop retries a start that the postmaster reports as failed, or gives back the WorkerInfo of a worker that doesn't start in time, so a failed start in emergency mode left the WorkerInfo in av_startingWorker for good. Each later emergency launcher then found no free worker slot, and nothing was vacuumed to prevent wraparound until a restart. Since autovacuum workers got a pool of exactly autovacuum_worker_slots postmaster child slots, the start can fail without any fork() failure: the worker that just finished asks for the next launcher before it exits, and that launcher can take the WorkerInfo the worker gave back while the postmaster still holds the worker's child slot. With autovacuum_worker_slots = 1 that happens after almost every database. Make the emergency launcher wait for its worker to start before exiting, retrying a failed start, and replacing a worker that doesn't start in time, as the main loop does. If a worker is already starting when it gets there, as after an error, it waits for that one instead of starting another, and if autovacuum is turned on while it waits, it gives the WorkerInfo back and leaves the work to the normal launcher. The main loop now resends a failed start only if a WorkerInfo is still waiting in av_startingWorker. Backpatch-through: 18 --- src/backend/postmaster/autovacuum.c | 179 +++++++++++++++++++++++++--- 1 file changed, 164 insertions(+), 15 deletions(-) diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c index 29f8493..a332089 100644 --- a/src/backend/postmaster/autovacuum.c +++ b/src/backend/postmaster/autovacuum.c @@ -149,6 +149,9 @@ int Log_autoanalyze_min_duration = 600000; #define MIN_AUTOVAC_SLEEPTIME 100.0 /* milliseconds */ #define MAX_AUTOVAC_SLEEPTIME 300 /* seconds */ +/* how long a launcher waits for a worker to start, in milliseconds */ +#define AUTOVAC_WORKER_START_TIMEOUT (Min(autovacuum_naptime, 60) * 1000) + /* * Variables to save the cost-related storage parameters for the current * relation being vacuumed by this autovacuum worker. Using these, we can @@ -360,6 +363,8 @@ avl_dbase *avl_dbase_array; static WorkerInfo MyWorkerInfo = NULL; static Oid do_start_worker(void); +static void do_start_emergency_worker(void); +static void cancel_starting_worker(void); static void ProcessAutoVacLauncherInterrupts(void); pg_noreturn static void AutoVacLauncherShutdown(void); static void launcher_determine_sleep(bool canlaunch, bool recursing, @@ -585,12 +590,12 @@ AutoVacLauncherMain(const void *startup_data, size_t startup_data_len) /* * In emergency mode, just start a worker (unless shutdown was requested) - * and go away. + * and go away once it is running. */ if (!AutoVacuumingActive()) { if (!ShutdownRequestPending) - do_start_worker(); + do_start_emergency_worker(); proc_exit(0); /* done */ } @@ -650,19 +655,35 @@ AutoVacLauncherMain(const void *startup_data, size_t startup_data_len) if (AutoVacuumShmem->av_signal[AutoVacForkFailed]) { + WorkerInfo starting_worker; + /* * If the postmaster failed to start a new worker, we sleep * for a little while and resend the signal. The new worker's * state is still in memory, so this is sufficient. After * that, we restart the main loop. * + * Only resend the signal if a WorkerInfo is still waiting in + * av_startingWorker. The failed start may be one whose + * WorkerInfo was already given back. Asking again would then + * start a worker that finds no WorkerInfo to claim, so it + * would just log a warning and exit without vacuuming + * anything, while still taking a worker slot that the next + * worker we ask for might need. + * * XXX should we put a limit to the number of times we retry? * I don't think it makes much sense, because a future start * of a worker will continue to fail in the same way. */ AutoVacuumShmem->av_signal[AutoVacForkFailed] = false; pg_usleep(1000000L); /* 1s */ - SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER); + + LWLockAcquire(AutovacuumLock, LW_SHARED); + starting_worker = AutoVacuumShmem->av_startingWorker; + LWLockRelease(AutovacuumLock); + + if (starting_worker != NULL) + SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER); continue; } } @@ -681,7 +702,6 @@ AutoVacLauncherMain(const void *startup_data, size_t startup_data_len) if (AutoVacuumShmem->av_startingWorker != NULL) { - int waittime; WorkerInfo worker = AutoVacuumShmem->av_startingWorker; /* @@ -699,9 +719,8 @@ AutoVacLauncherMain(const void *startup_data, size_t startup_data_len) * before the worker removes the WorkerInfo from the * startingWorker pointer. */ - waittime = Min(autovacuum_naptime, 60) * 1000; if (TimestampDifferenceExceeds(worker->wi_launchtime, current_time, - waittime)) + AUTOVAC_WORKER_START_TIMEOUT)) { LWLockRelease(AutovacuumLock); LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE); @@ -714,15 +733,7 @@ AutoVacLauncherMain(const void *startup_data, size_t startup_data_len) */ if (AutoVacuumShmem->av_startingWorker != NULL) { - worker = AutoVacuumShmem->av_startingWorker; - worker->wi_dboid = InvalidOid; - worker->wi_tableoid = InvalidOid; - worker->wi_sharedrel = false; - worker->wi_proc = NULL; - worker->wi_launchtime = 0; - dclist_push_head(&AutoVacuumShmem->av_freeWorkers, - &worker->wi_links); - AutoVacuumShmem->av_startingWorker = NULL; + cancel_starting_worker(); ereport(WARNING, errmsg("autovacuum worker took too long to start; canceled")); } @@ -1323,6 +1334,144 @@ do_start_worker(void) return retval; } +/* + * do_start_emergency_worker + * + * Start a worker from an emergency launcher, and wait until it has taken its + * WorkerInfo out of av_startingWorker. A normal launcher's main loop retries + * a start that the postmaster reports as failed, and gives back the WorkerInfo + * of a worker that doesn't start in time and starts another. An emergency + * launcher exits after this, so it does both here. Otherwise a failed start + * would leave the WorkerInfo in av_startingWorker, where nothing gives it back + * while autovacuum is off, and every later emergency launcher would find no + * free worker slot. + * + * If a worker is already starting, as when an error brought us back here + * after starting it, wait for that one instead of starting another. If + * autovacuum is turned on meanwhile, give the WorkerInfo back and leave the + * work to the normal launcher, which the postmaster starts once we exit. + */ +static void +do_start_emergency_worker(void) +{ + WorkerInfo starting_worker; + bool ask_again = false; + + LWLockAcquire(AutovacuumLock, LW_SHARED); + starting_worker = AutoVacuumShmem->av_startingWorker; + LWLockRelease(AutovacuumLock); + + if (starting_worker == NULL) + { + /* with nothing starting, a report of a failed start is stale */ + AutoVacuumShmem->av_signal[AutoVacForkFailed] = false; + + if (!OidIsValid(do_start_worker())) + return; + } + + for (;;) + { + if (ShutdownRequestPending) + return; + if (ConfigReloadPending) + { + ConfigReloadPending = false; + ProcessConfigFile(PGC_SIGHUP); + } + if (ProcSignalBarrierPending) + ProcessProcSignalBarrier(); + if (LogMemoryContextPending) + ProcessLogMemoryContextInterrupt(); + ProcessCatchupInterrupt(); + + LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE); + starting_worker = AutoVacuumShmem->av_startingWorker; + if (starting_worker == NULL) + { + /* the worker is running */ + LWLockRelease(AutovacuumLock); + return; + } + if (AutoVacuumingActive()) + { + /* autovacuum was turned on; the normal launcher takes over */ + cancel_starting_worker(); + LWLockRelease(AutovacuumLock); + return; + } + if (TimestampDifferenceExceeds(starting_worker->wi_launchtime, + GetCurrentTimestamp(), + AUTOVAC_WORKER_START_TIMEOUT)) + { + cancel_starting_worker(); + LWLockRelease(AutovacuumLock); + ereport(WARNING, + errmsg("autovacuum worker took too long to start; canceled")); + + /* as the main loop does, start another worker in its place */ + AutoVacuumShmem->av_signal[AutoVacForkFailed] = false; + if (!OidIsValid(do_start_worker())) + return; + ask_again = false; + continue; + } + LWLockRelease(AutovacuumLock); + + /* + * Ask again only now that the checks above have passed, so that a + * worker that showed up during the pause, or a handover to the normal + * launcher, gets no second request. + */ + if (ask_again) + { + ask_again = false; + SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER); + } + + if (AutoVacuumShmem->av_signal[AutoVacForkFailed]) + { + /* as in the main loop, sleep for a little while and ask again */ + AutoVacuumShmem->av_signal[AutoVacForkFailed] = false; + pg_usleep(1000000L); /* 1s */ + ask_again = true; + continue; + } + + /* + * The worker signals us once it has started, and the postmaster + * signals us if it could not start it. + */ + (void) WaitLatch(MyLatch, + WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, + 1000L, + WAIT_EVENT_AUTOVACUUM_MAIN); + ResetLatch(MyLatch); + } +} + +/* + * cancel_starting_worker + * + * Give the WorkerInfo of a worker that has not started back to the free list. + * The caller must hold AutovacuumLock exclusively. + */ +static void +cancel_starting_worker(void) +{ + WorkerInfo worker = AutoVacuumShmem->av_startingWorker; + + Assert(LWLockHeldByMeInMode(AutovacuumLock, LW_EXCLUSIVE)); + + worker->wi_dboid = InvalidOid; + worker->wi_tableoid = InvalidOid; + worker->wi_sharedrel = false; + worker->wi_proc = NULL; + worker->wi_launchtime = 0; + dclist_push_head(&AutoVacuumShmem->av_freeWorkers, &worker->wi_links); + AutoVacuumShmem->av_startingWorker = NULL; +} + /* * launch_worker * -- 2.43.0