Skip to content

Commit 5910d6c

Browse files
committed
Move interrupt-handling code into subroutines.
Some auxiliary processes, as well as the autovacuum launcher, have interrupt handling code directly in their main loops. Try to abstract things a little better by moving it into separate functions. This doesn't make any functional difference, and leaves in place relatively large differences among processes in how interrupts are handled, but hopefully it at least makes it easier to see the commonalities and differences across process types. Patch by me, reviewed by Andres Freund and Daniel Gustafsson. Discussion: http://postgr.es/m/CA+TgmoZwDk=BguVDVa+qdA6SBKef=PKbaKDQALTC_9qoz1mJqg@mail.gmail.com
1 parent af3290f commit 5910d6c

File tree

4 files changed

+133
-86
lines changed

4 files changed

+133
-86
lines changed

src/backend/postmaster/autovacuum.c

+45-27
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ NON_EXEC_STATIC void AutoVacWorkerMain(int argc, char *argv[]) pg_attribute_nore
311311
NON_EXEC_STATIC void AutoVacLauncherMain(int argc, char *argv[]) pg_attribute_noreturn();
312312

313313
static Oid do_start_worker(void);
314+
static void HandleAutoVacLauncherInterrupts(void);
315+
static void AutoVacLauncherShutdown() pg_attribute_noreturn();
314316
static void launcher_determine_sleep(bool canlaunch, bool recursing,
315317
struct timeval *nap);
316318
static void launch_worker(TimestampTz now);
@@ -554,7 +556,7 @@ AutoVacLauncherMain(int argc, char *argv[])
554556

555557
/* if in shutdown mode, no need for anything further; just go away */
556558
if (got_SIGTERM)
557-
goto shutdown;
559+
AutoVacLauncherShutdown();
558560

559561
/*
560562
* Sleep at least 1 second after any error. We don't want to be
@@ -649,30 +651,7 @@ AutoVacLauncherMain(int argc, char *argv[])
649651

650652
ResetLatch(MyLatch);
651653

652-
/* Process sinval catchup interrupts that happened while sleeping */
653-
ProcessCatchupInterrupt();
654-
655-
/* the normal shutdown case */
656-
if (got_SIGTERM)
657-
break;
658-
659-
if (got_SIGHUP)
660-
{
661-
got_SIGHUP = false;
662-
ProcessConfigFile(PGC_SIGHUP);
663-
664-
/* shutdown requested in config file? */
665-
if (!AutoVacuumingActive())
666-
break;
667-
668-
/* rebalance in case the default cost parameters changed */
669-
LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
670-
autovac_balance_cost();
671-
LWLockRelease(AutovacuumLock);
672-
673-
/* rebuild the list in case the naptime changed */
674-
rebuild_database_list(InvalidOid);
675-
}
654+
HandleAutoVacLauncherInterrupts();
676655

677656
/*
678657
* a worker finished, or postmaster signalled failure to start a
@@ -813,8 +792,47 @@ AutoVacLauncherMain(int argc, char *argv[])
813792
}
814793
}
815794

816-
/* Normal exit from the autovac launcher is here */
817-
shutdown:
795+
AutoVacLauncherShutdown();
796+
}
797+
798+
/*
799+
* Process any new interrupts.
800+
*/
801+
static void
802+
HandleAutoVacLauncherInterrupts(void)
803+
{
804+
/* the normal shutdown case */
805+
if (got_SIGTERM)
806+
AutoVacLauncherShutdown();
807+
808+
if (got_SIGHUP)
809+
{
810+
got_SIGHUP = false;
811+
ProcessConfigFile(PGC_SIGHUP);
812+
813+
/* shutdown requested in config file? */
814+
if (!AutoVacuumingActive())
815+
AutoVacLauncherShutdown();
816+
817+
/* rebalance in case the default cost parameters changed */
818+
LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
819+
autovac_balance_cost();
820+
LWLockRelease(AutovacuumLock);
821+
822+
/* rebuild the list in case the naptime changed */
823+
rebuild_database_list(InvalidOid);
824+
}
825+
826+
/* Process sinval catchup interrupts that happened while sleeping */
827+
ProcessCatchupInterrupt();
828+
}
829+
830+
/*
831+
* Perform a normal exit from the autovac launcher.
832+
*/
833+
static void
834+
AutoVacLauncherShutdown()
835+
{
818836
ereport(DEBUG1,
819837
(errmsg("autovacuum launcher shutting down")));
820838
AutoVacuumShmem->av_launcherpid = 0;

src/backend/postmaster/bgwriter.c

+27-15
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ static XLogRecPtr last_snapshot_lsn = InvalidXLogRecPtr;
9292
static volatile sig_atomic_t got_SIGHUP = false;
9393
static volatile sig_atomic_t shutdown_requested = false;
9494

95+
static void HandleBackgroundWriterInterrupts(void);
96+
9597
/* Signal handlers */
9698

9799
static void bg_quickdie(SIGNAL_ARGS);
@@ -241,21 +243,7 @@ BackgroundWriterMain(void)
241243
/* Clear any already-pending wakeups */
242244
ResetLatch(MyLatch);
243245

244-
if (got_SIGHUP)
245-
{
246-
got_SIGHUP = false;
247-
ProcessConfigFile(PGC_SIGHUP);
248-
}
249-
if (shutdown_requested)
250-
{
251-
/*
252-
* From here on, elog(ERROR) should end with exit(1), not send
253-
* control back to the sigsetjmp block above
254-
*/
255-
ExitOnAnyError = true;
256-
/* Normal exit from the bgwriter is here */
257-
proc_exit(0); /* done */
258-
}
246+
HandleBackgroundWriterInterrupts();
259247

260248
/*
261249
* Do one cycle of dirty-buffer writing.
@@ -369,6 +357,30 @@ BackgroundWriterMain(void)
369357
}
370358
}
371359

360+
/*
361+
* Process any new interrupts.
362+
*/
363+
static void
364+
HandleBackgroundWriterInterrupts(void)
365+
{
366+
if (got_SIGHUP)
367+
{
368+
got_SIGHUP = false;
369+
ProcessConfigFile(PGC_SIGHUP);
370+
}
371+
372+
if (shutdown_requested)
373+
{
374+
/*
375+
* From here on, elog(ERROR) should end with exit(1), not send
376+
* control back to the sigsetjmp block above
377+
*/
378+
ExitOnAnyError = true;
379+
/* Normal exit from the bgwriter is here */
380+
proc_exit(0); /* done */
381+
}
382+
}
383+
372384

373385
/* --------------------------------
374386
* signal handler routines

src/backend/postmaster/checkpointer.c

+40-31
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ static pg_time_t last_xlog_switch_time;
169169

170170
/* Prototypes for private functions */
171171

172+
static void HandleCheckpointerInterrupts();
172173
static void CheckArchiveTimeout(void);
173174
static bool IsCheckpointOnSchedule(double progress);
174175
static bool ImmediateCheckpointRequested(void);
@@ -350,37 +351,7 @@ CheckpointerMain(void)
350351
* Process any requests or signals received recently.
351352
*/
352353
AbsorbSyncRequests();
353-
354-
if (got_SIGHUP)
355-
{
356-
got_SIGHUP = false;
357-
ProcessConfigFile(PGC_SIGHUP);
358-
359-
/*
360-
* Checkpointer is the last process to shut down, so we ask it to
361-
* hold the keys for a range of other tasks required most of which
362-
* have nothing to do with checkpointing at all.
363-
*
364-
* For various reasons, some config values can change dynamically
365-
* so the primary copy of them is held in shared memory to make
366-
* sure all backends see the same value. We make Checkpointer
367-
* responsible for updating the shared memory copy if the
368-
* parameter setting changes because of SIGHUP.
369-
*/
370-
UpdateSharedMemoryConfig();
371-
}
372-
if (shutdown_requested)
373-
{
374-
/*
375-
* From here on, elog(ERROR) should end with exit(1), not send
376-
* control back to the sigsetjmp block above
377-
*/
378-
ExitOnAnyError = true;
379-
/* Close down the database */
380-
ShutdownXLOG(0, 0);
381-
/* Normal exit from the checkpointer is here */
382-
proc_exit(0); /* done */
383-
}
354+
HandleCheckpointerInterrupts();
384355

385356
/*
386357
* Detect a pending checkpoint request by checking whether the flags
@@ -558,6 +529,44 @@ CheckpointerMain(void)
558529
}
559530
}
560531

532+
/*
533+
* Process any new interrupts.
534+
*/
535+
static void
536+
HandleCheckpointerInterrupts(void)
537+
{
538+
if (got_SIGHUP)
539+
{
540+
got_SIGHUP = false;
541+
ProcessConfigFile(PGC_SIGHUP);
542+
543+
/*
544+
* Checkpointer is the last process to shut down, so we ask it to
545+
* hold the keys for a range of other tasks required most of which
546+
* have nothing to do with checkpointing at all.
547+
*
548+
* For various reasons, some config values can change dynamically
549+
* so the primary copy of them is held in shared memory to make
550+
* sure all backends see the same value. We make Checkpointer
551+
* responsible for updating the shared memory copy if the
552+
* parameter setting changes because of SIGHUP.
553+
*/
554+
UpdateSharedMemoryConfig();
555+
}
556+
if (shutdown_requested)
557+
{
558+
/*
559+
* From here on, elog(ERROR) should end with exit(1), not send
560+
* control back to the sigsetjmp block above
561+
*/
562+
ExitOnAnyError = true;
563+
/* Close down the database */
564+
ShutdownXLOG(0, 0);
565+
/* Normal exit from the checkpointer is here */
566+
proc_exit(0); /* done */
567+
}
568+
}
569+
561570
/*
562571
* CheckArchiveTimeout -- check for archive_timeout and switch xlog files
563572
*

src/backend/postmaster/walwriter.c

+21-13
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ int WalWriterFlushAfter = 128;
8383
static volatile sig_atomic_t got_SIGHUP = false;
8484
static volatile sig_atomic_t shutdown_requested = false;
8585

86+
static void HandleWalWriterInterrupts(void);
87+
8688
/* Signal handlers */
8789
static void wal_quickdie(SIGNAL_ARGS);
8890
static void WalSigHupHandler(SIGNAL_ARGS);
@@ -242,19 +244,7 @@ WalWriterMain(void)
242244
/* Clear any already-pending wakeups */
243245
ResetLatch(MyLatch);
244246

245-
/*
246-
* Process any requests or signals received recently.
247-
*/
248-
if (got_SIGHUP)
249-
{
250-
got_SIGHUP = false;
251-
ProcessConfigFile(PGC_SIGHUP);
252-
}
253-
if (shutdown_requested)
254-
{
255-
/* Normal exit from the walwriter is here */
256-
proc_exit(0); /* done */
257-
}
247+
HandleWalWriterInterrupts();
258248

259249
/*
260250
* Do what we're here for; then, if XLogBackgroundFlush() found useful
@@ -282,6 +272,24 @@ WalWriterMain(void)
282272
}
283273
}
284274

275+
/*
276+
* Process any new interrupts.
277+
*/
278+
static void
279+
HandleWalWriterInterrupts(void)
280+
{
281+
if (got_SIGHUP)
282+
{
283+
got_SIGHUP = false;
284+
ProcessConfigFile(PGC_SIGHUP);
285+
}
286+
if (shutdown_requested)
287+
{
288+
/* Normal exit from the walwriter is here */
289+
proc_exit(0); /* done */
290+
}
291+
}
292+
285293

286294
/* --------------------------------
287295
* signal handler routines

0 commit comments

Comments
 (0)