@@ -606,14 +606,25 @@ PostmasterMain(int argc, char *argv[])
606
606
/*
607
607
* Set up signal handlers for the postmaster process.
608
608
*
609
- * In the postmaster, we want to install non-ignored handlers *without*
610
- * SA_RESTART. This is because they'll be blocked at all times except
611
- * when ServerLoop is waiting for something to happen, and during that
612
- * window, we want signals to exit the select(2) wait so that ServerLoop
613
- * can respond if anything interesting happened. On some platforms,
614
- * signals marked SA_RESTART would not cause the select() wait to end.
615
- * Child processes will generally want SA_RESTART, but we expect them to
616
- * set up their own handlers before unblocking signals.
609
+ * In the postmaster, we use pqsignal_pm() rather than pqsignal() (which
610
+ * is used by all child processes and client processes). That has a
611
+ * couple of special behaviors:
612
+ *
613
+ * 1. Except on Windows, we tell sigaction() to block all signals for the
614
+ * duration of the signal handler. This is faster than our old approach
615
+ * of blocking/unblocking explicitly in the signal handler, and it should
616
+ * also prevent excessive stack consumption if signals arrive quickly.
617
+ *
618
+ * 2. We do not set the SA_RESTART flag. This is because signals will be
619
+ * blocked at all times except when ServerLoop is waiting for something to
620
+ * happen, and during that window, we want signals to exit the select(2)
621
+ * wait so that ServerLoop can respond if anything interesting happened.
622
+ * On some platforms, signals marked SA_RESTART would not cause the
623
+ * select() wait to end.
624
+ *
625
+ * Child processes will generally want SA_RESTART, so pqsignal() sets that
626
+ * flag. We expect children to set up their own handlers before
627
+ * unblocking signals.
617
628
*
618
629
* CAUTION: when changing this list, check for side-effects on the signal
619
630
* handling setup of child processes. See tcop/postgres.c,
@@ -625,18 +636,16 @@ PostmasterMain(int argc, char *argv[])
625
636
pqinitmask ();
626
637
PG_SETMASK (& BlockSig );
627
638
628
- pqsignal_no_restart (SIGHUP , SIGHUP_handler ); /* reread config file and
629
- * have children do same */
630
- pqsignal_no_restart (SIGINT , pmdie ); /* send SIGTERM and shut down */
631
- pqsignal_no_restart (SIGQUIT , pmdie ); /* send SIGQUIT and die */
632
- pqsignal_no_restart (SIGTERM , pmdie ); /* wait for children and shut down */
633
- pqsignal (SIGALRM , SIG_IGN ); /* ignored */
634
- pqsignal (SIGPIPE , SIG_IGN ); /* ignored */
635
- pqsignal_no_restart (SIGUSR1 , sigusr1_handler ); /* message from child
636
- * process */
637
- pqsignal_no_restart (SIGUSR2 , dummy_handler ); /* unused, reserve for
638
- * children */
639
- pqsignal_no_restart (SIGCHLD , reaper ); /* handle child termination */
639
+ pqsignal_pm (SIGHUP , SIGHUP_handler ); /* reread config file and have
640
+ * children do same */
641
+ pqsignal_pm (SIGINT , pmdie ); /* send SIGTERM and shut down */
642
+ pqsignal_pm (SIGQUIT , pmdie ); /* send SIGQUIT and die */
643
+ pqsignal_pm (SIGTERM , pmdie ); /* wait for children and shut down */
644
+ pqsignal_pm (SIGALRM , SIG_IGN ); /* ignored */
645
+ pqsignal_pm (SIGPIPE , SIG_IGN ); /* ignored */
646
+ pqsignal_pm (SIGUSR1 , sigusr1_handler ); /* message from child process */
647
+ pqsignal_pm (SIGUSR2 , dummy_handler ); /* unused, reserve for children */
648
+ pqsignal_pm (SIGCHLD , reaper ); /* handle child termination */
640
649
641
650
/*
642
651
* No other place in Postgres should touch SIGTTIN/SIGTTOU handling. We
@@ -646,15 +655,15 @@ PostmasterMain(int argc, char *argv[])
646
655
* child processes should just allow the inherited settings to stand.
647
656
*/
648
657
#ifdef SIGTTIN
649
- pqsignal (SIGTTIN , SIG_IGN ); /* ignored */
658
+ pqsignal_pm (SIGTTIN , SIG_IGN ); /* ignored */
650
659
#endif
651
660
#ifdef SIGTTOU
652
- pqsignal (SIGTTOU , SIG_IGN ); /* ignored */
661
+ pqsignal_pm (SIGTTOU , SIG_IGN ); /* ignored */
653
662
#endif
654
663
655
664
/* ignore SIGXFSZ, so that ulimit violations work like disk full */
656
665
#ifdef SIGXFSZ
657
- pqsignal (SIGXFSZ , SIG_IGN ); /* ignored */
666
+ pqsignal_pm (SIGXFSZ , SIG_IGN ); /* ignored */
658
667
#endif
659
668
660
669
/*
@@ -2640,7 +2649,13 @@ SIGHUP_handler(SIGNAL_ARGS)
2640
2649
{
2641
2650
int save_errno = errno ;
2642
2651
2652
+ /*
2653
+ * We rely on the signal mechanism to have blocked all signals ... except
2654
+ * on Windows, which lacks sigaction(), so we have to do it manually.
2655
+ */
2656
+ #ifdef WIN32
2643
2657
PG_SETMASK (& BlockSig );
2658
+ #endif
2644
2659
2645
2660
if (Shutdown <= SmartShutdown )
2646
2661
{
@@ -2700,7 +2715,9 @@ SIGHUP_handler(SIGNAL_ARGS)
2700
2715
#endif
2701
2716
}
2702
2717
2718
+ #ifdef WIN32
2703
2719
PG_SETMASK (& UnBlockSig );
2720
+ #endif
2704
2721
2705
2722
errno = save_errno ;
2706
2723
}
@@ -2714,7 +2731,13 @@ pmdie(SIGNAL_ARGS)
2714
2731
{
2715
2732
int save_errno = errno ;
2716
2733
2734
+ /*
2735
+ * We rely on the signal mechanism to have blocked all signals ... except
2736
+ * on Windows, which lacks sigaction(), so we have to do it manually.
2737
+ */
2738
+ #ifdef WIN32
2717
2739
PG_SETMASK (& BlockSig );
2740
+ #endif
2718
2741
2719
2742
ereport (DEBUG2 ,
2720
2743
(errmsg_internal ("postmaster received signal %d" ,
@@ -2880,7 +2903,9 @@ pmdie(SIGNAL_ARGS)
2880
2903
break ;
2881
2904
}
2882
2905
2906
+ #ifdef WIN32
2883
2907
PG_SETMASK (& UnBlockSig );
2908
+ #endif
2884
2909
2885
2910
errno = save_errno ;
2886
2911
}
@@ -2895,7 +2920,13 @@ reaper(SIGNAL_ARGS)
2895
2920
int pid ; /* process id of dead child process */
2896
2921
int exitstatus ; /* its exit status */
2897
2922
2923
+ /*
2924
+ * We rely on the signal mechanism to have blocked all signals ... except
2925
+ * on Windows, which lacks sigaction(), so we have to do it manually.
2926
+ */
2927
+ #ifdef WIN32
2898
2928
PG_SETMASK (& BlockSig );
2929
+ #endif
2899
2930
2900
2931
ereport (DEBUG4 ,
2901
2932
(errmsg_internal ("reaping dead processes" )));
@@ -3212,7 +3243,9 @@ reaper(SIGNAL_ARGS)
3212
3243
PostmasterStateMachine ();
3213
3244
3214
3245
/* Done with signal handler */
3246
+ #ifdef WIN32
3215
3247
PG_SETMASK (& UnBlockSig );
3248
+ #endif
3216
3249
3217
3250
errno = save_errno ;
3218
3251
}
@@ -5114,7 +5147,13 @@ sigusr1_handler(SIGNAL_ARGS)
5114
5147
{
5115
5148
int save_errno = errno ;
5116
5149
5150
+ /*
5151
+ * We rely on the signal mechanism to have blocked all signals ... except
5152
+ * on Windows, which lacks sigaction(), so we have to do it manually.
5153
+ */
5154
+ #ifdef WIN32
5117
5155
PG_SETMASK (& BlockSig );
5156
+ #endif
5118
5157
5119
5158
/* Process background worker state change. */
5120
5159
if (CheckPostmasterSignal (PMSIGNAL_BACKGROUND_WORKER_CHANGE ))
@@ -5272,7 +5311,9 @@ sigusr1_handler(SIGNAL_ARGS)
5272
5311
signal_child (StartupPID , SIGUSR2 );
5273
5312
}
5274
5313
5314
+ #ifdef WIN32
5275
5315
PG_SETMASK (& UnBlockSig );
5316
+ #endif
5276
5317
5277
5318
errno = save_errno ;
5278
5319
}
0 commit comments