8787#include <dns_sd.h>
8888#endif
8989
90+ #ifdef USE_SYSTEMD
91+ #include <systemd/sd-daemon.h>
92+ #endif
93+
9094#ifdef HAVE_PTHREAD_IS_THREADED_NP
9195#include <pthread.h>
9296#endif
@@ -2533,6 +2537,9 @@ pmdie(SIGNAL_ARGS)
25332537Shutdown = SmartShutdown ;
25342538ereport (LOG ,
25352539(errmsg ("received smart shutdown request" )));
2540+ #ifdef USE_SYSTEMD
2541+ sd_notify (0 ,"STOPPING=1" );
2542+ #endif
25362543
25372544if (pmState == PM_RUN || pmState == PM_RECOVERY ||
25382545pmState == PM_HOT_STANDBY || pmState == PM_STARTUP )
@@ -2585,6 +2592,9 @@ pmdie(SIGNAL_ARGS)
25852592Shutdown = FastShutdown ;
25862593ereport (LOG ,
25872594(errmsg ("received fast shutdown request" )));
2595+ #ifdef USE_SYSTEMD
2596+ sd_notify (0 ,"STOPPING=1" );
2597+ #endif
25882598
25892599if (StartupPID != 0 )
25902600signal_child (StartupPID ,SIGTERM );
@@ -2645,6 +2655,9 @@ pmdie(SIGNAL_ARGS)
26452655Shutdown = ImmediateShutdown ;
26462656ereport (LOG ,
26472657(errmsg ("received immediate shutdown request" )));
2658+ #ifdef USE_SYSTEMD
2659+ sd_notify (0 ,"STOPPING=1" );
2660+ #endif
26482661
26492662TerminateChildren (SIGQUIT );
26502663pmState = PM_WAIT_BACKENDS ;
@@ -2787,6 +2800,10 @@ reaper(SIGNAL_ARGS)
27872800ereport (LOG ,
27882801 (errmsg ("database system is ready to accept connections" )));
27892802
2803+ #ifdef USE_SYSTEMD
2804+ sd_notify (0 ,"READY=1" );
2805+ #endif
2806+
27902807continue ;
27912808}
27922809
@@ -4916,6 +4933,11 @@ sigusr1_handler(SIGNAL_ARGS)
49164933if (XLogArchivingAlways ())
49174934PgArchPID = pgarch_start ();
49184935
4936+ #ifdef USE_SYSTEMD
4937+ if (!EnableHotStandby )
4938+ sd_notify (0 ,"READY=1" );
4939+ #endif
4940+
49194941pmState = PM_RECOVERY ;
49204942}
49214943if (CheckPostmasterSignal (PMSIGNAL_BEGIN_HOT_STANDBY )&&
@@ -4930,6 +4952,10 @@ sigusr1_handler(SIGNAL_ARGS)
49304952ereport (LOG ,
49314953(errmsg ("database system is ready to accept read only connections" )));
49324954
4955+ #ifdef USE_SYSTEMD
4956+ sd_notify (0 ,"READY=1" );
4957+ #endif
4958+
49334959pmState = PM_HOT_STANDBY ;
49344960/* Some workers may be scheduled to start now */
49354961StartWorkerNeeded = true;