Skip to content

Commit 970d1f7

Browse files
committed
Restart bgworkers immediately after a crash-and-restart cycle.
Just as we would start bgworkers immediately after an initial startup of the server, we should restart them immediately when reinitializing. Petr Jelinek and Robert Haas
1 parent 364ddc3 commit 970d1f7

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

src/backend/postmaster/bgworker.c

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,27 @@ BackgroundWorkerStopNotifications(pid_t pid)
394394
}
395395
}
396396

397+
/*
398+
* Reset background worker crash state.
399+
*
400+
* We assume that, after a crash-and-restart cycle, background workers should
401+
* be restarted immediately, instead of waiting for bgw_restart_time to
402+
* elapse.
403+
*/
404+
void
405+
ResetBackgroundWorkerCrashTimes(void)
406+
{
407+
slist_mutable_iter iter;
408+
409+
slist_foreach_modify(iter, &BackgroundWorkerList)
410+
{
411+
RegisteredBgWorker *rw;
412+
413+
rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
414+
rw->rw_crashed_at = 0;
415+
}
416+
}
417+
397418
#ifdef EXEC_BACKEND
398419
/*
399420
* In EXEC_BACKEND mode, workers use this to retrieve their details from
@@ -478,13 +499,14 @@ bgworker_quickdie(SIGNAL_ARGS)
478499
on_exit_reset();
479500

480501
/*
481-
* Note we do exit(0) here, not exit(2) like quickdie. The reason is that
482-
* we don't want to be seen this worker as independently crashed, because
483-
* then postmaster would delay restarting it again afterwards. If some
484-
* idiot DBA manually sends SIGQUIT to a random bgworker, the "dead man
485-
* switch" will ensure that postmaster sees this as a crash.
502+
* Note we do exit(2) not exit(0). This is to force the postmaster into a
503+
* system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
504+
* backend. This is necessary precisely because we don't clean up our
505+
* shared memory state. (The "dead man switch" mechanism in pmsignal.c
506+
* should ensure the postmaster sees this as a crash, too, but no harm in
507+
* being doubly sure.)
486508
*/
487-
exit(0);
509+
exit(2);
488510
}
489511

490512
/*

src/backend/postmaster/postmaster.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,7 +2616,7 @@ reaper(SIGNAL_ARGS)
26162616
if (PgStatPID == 0)
26172617
PgStatPID = pgstat_start();
26182618

2619-
/* some workers may be scheduled to start now */
2619+
/* workers may be scheduled to start now */
26202620
maybe_start_bgworker();
26212621

26222622
/* at this point we are really open for business */
@@ -2860,7 +2860,6 @@ CleanupBackgroundWorker(int pid,
28602860
{
28612861
if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
28622862
{
2863-
rw->rw_crashed_at = GetCurrentTimestamp();
28642863
HandleChildCrash(pid, exitstatus, namebuf);
28652864
return true;
28662865
}
@@ -2871,7 +2870,6 @@ CleanupBackgroundWorker(int pid,
28712870
* Uh-oh, the child failed to clean itself up. Treat as a
28722871
* crash after all.
28732872
*/
2874-
rw->rw_crashed_at = GetCurrentTimestamp();
28752873
HandleChildCrash(pid, exitstatus, namebuf);
28762874
return true;
28772875
}
@@ -3546,6 +3544,9 @@ PostmasterStateMachine(void)
35463544
ereport(LOG,
35473545
(errmsg("all server processes terminated; reinitializing")));
35483546

3547+
/* allow background workers to immediately restart */
3548+
ResetBackgroundWorkerCrashTimes();
3549+
35493550
shmem_exit(1);
35503551
reset_shared(PostPortNumber);
35513552

src/include/postmaster/bgworker_internals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extern void BackgroundWorkerStateChange(void);
4343
extern void ForgetBackgroundWorker(slist_mutable_iter *cur);
4444
extern void ReportBackgroundWorkerPID(RegisteredBgWorker *);
4545
extern void BackgroundWorkerStopNotifications(pid_t pid);
46+
extern void ResetBackgroundWorkerCrashTimes(void);
4647

4748
/* Function to start a background worker, called from postmaster.c */
4849
extern void StartBackgroundWorker(void);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy