Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7d17e68

Browse files
committed
Add support for systemd service notifications
Insert sd_notify() calls at server start and stop for integration withsystemd. This allows the use of systemd service units of type "notify",which greatly simplifies the systemd configuration.Reviewed-by: Pavel Stěhule <pavel.stehule@gmail.com>
1 parentac7238d commit7d17e68

File tree

8 files changed

+147
-0
lines changed

8 files changed

+147
-0
lines changed

‎configure

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ with_libxml
709709
XML2_CONFIG
710710
UUID_EXTRA_OBJS
711711
with_uuid
712+
with_systemd
712713
with_selinux
713714
with_openssl
714715
krb_srvtab
@@ -830,6 +831,7 @@ with_ldap
830831
with_bonjour
831832
with_openssl
832833
with_selinux
834+
with_systemd
833835
with_readline
834836
with_libedit_preferred
835837
with_uuid
@@ -1518,6 +1520,7 @@ Optional Packages:
15181520
--with-bonjour build with Bonjour support
15191521
--with-openssl build with OpenSSL support
15201522
--with-selinux build with SELinux support
1523+
--with-systemd build with systemd support
15211524
--without-readline do not use GNU Readline nor BSD Libedit for editing
15221525
--with-libedit-preferred
15231526
prefer BSD Libedit over GNU Readline
@@ -5694,6 +5697,41 @@ fi
56945697
{$as_echo"$as_me:${as_lineno-$LINENO}: result:$with_selinux">&5
56955698
$as_echo"$with_selinux">&6; }
56965699

5700+
#
5701+
# Systemd
5702+
#
5703+
{$as_echo"$as_me:${as_lineno-$LINENO}: checking whether to build with systemd support">&5
5704+
$as_echo_n"checking whether to build with systemd support...">&6; }
5705+
5706+
5707+
5708+
# Check whether --with-systemd was given.
5709+
iftest"${with_systemd+set}" =set;then:
5710+
withval=$with_systemd;
5711+
case$withvalin
5712+
yes)
5713+
5714+
$as_echo"#define USE_SYSTEMD 1">>confdefs.h
5715+
5716+
;;
5717+
no)
5718+
:
5719+
;;
5720+
*)
5721+
as_fn_error$?"no argument expected for --with-systemd option""$LINENO" 5
5722+
;;
5723+
esac
5724+
5725+
else
5726+
with_systemd=no
5727+
5728+
fi
5729+
5730+
5731+
5732+
{$as_echo"$as_me:${as_lineno-$LINENO}: result:$with_systemd">&5
5733+
$as_echo"$with_systemd">&6; }
5734+
56975735
#
56985736
# Readline
56995737
#
@@ -10473,6 +10511,17 @@ fi
1047310511

1047410512
done
1047510513

10514+
fi
10515+
10516+
iftest"$with_systemd" = yes;then
10517+
ac_fn_c_check_header_mongrel"$LINENO""systemd/sd-daemon.h""ac_cv_header_systemd_sd_daemon_h""$ac_includes_default"
10518+
iftest"x$ac_cv_header_systemd_sd_daemon_h" = xyes;then:
10519+
10520+
else
10521+
as_fn_error$?"header file <systemd/sd-daemon.h> is required for systemd support""$LINENO" 5
10522+
fi
10523+
10524+
1047610525
fi
1047710526

1047810527
iftest"$with_libxml" = yes;then

‎configure.in

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,15 @@ PGAC_ARG_BOOL(with, selinux, no, [build with SELinux support])
699699
AC_SUBST(with_selinux)
700700
AC_MSG_RESULT([$with_selinux])
701701

702+
#
703+
# Systemd
704+
#
705+
AC_MSG_CHECKING([whether to build with systemd support])
706+
PGAC_ARG_BOOL(with, systemd, no, [build with systemd support],
707+
[AC_DEFINE([USE_SYSTEMD], 1, [Define to build with systemd support. (--with-systemd)])])
708+
AC_SUBST(with_systemd)
709+
AC_MSG_RESULT([$with_systemd])
710+
702711
#
703712
# Readline
704713
#
@@ -1249,6 +1258,10 @@ if test "$with_pam" = yes ; then
12491258
[AC_MSG_ERROR([header file <security/pam_appl.h> or <pam/pam_appl.h> is required for PAM.])])])
12501259
fi
12511260

1261+
if test "$with_systemd" = yes ; then
1262+
AC_CHECK_HEADER(systemd/sd-daemon.h, [], [AC_MSG_ERROR([header file <systemd/sd-daemon.h> is required for systemd support])])
1263+
fi
1264+
12521265
if test "$with_libxml" = yes ; then
12531266
AC_CHECK_HEADER(libxml/parser.h, [], [AC_MSG_ERROR([header file <libxml/parser.h> is required for XML support])])
12541267
fi

‎doc/src/sgml/installation.sgml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,22 @@ su - postgres
812812
</listitem>
813813
</varlistentry>
814814

815+
<varlistentry>
816+
<term><option>--with-systemd</option></term>
817+
<listitem>
818+
<para>
819+
Build with support
820+
for <application>systemd</application><indexterm><primary>systemd</primary></indexterm>
821+
service notifications. This improves integration if the server binary
822+
is started under <application>systemd</application> but has no impact
823+
otherwise; see <xref linkend="server-start"> for more
824+
information. <application>libsystemd</application> and the
825+
associated header files need to be installed to be able to use this
826+
option.
827+
</para>
828+
</listitem>
829+
</varlistentry>
830+
815831
<varlistentry>
816832
<term><option>--without-readline</option></term>
817833
<listitem>

‎doc/src/sgml/runtime.sgml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,41 @@ fi
369369
<filename>contrib/start-scripts/linux</filename> in the
370370
<productname>PostgreSQL</productname> source distribution.
371371
</para>
372+
373+
<para>
374+
When using <application>systemd</application>, you can use the following
375+
service unit file (e.g.,
376+
at <filename>/etc/systemd/system/postgresql.service</filename>):<indexterm><primary>systemd</primary></indexterm>
377+
<programlisting>
378+
[Unit]
379+
Description=PostgreSQL database server
380+
Documentation=man:postgres(1)
381+
382+
[Service]
383+
Type=notify
384+
User=postgres
385+
ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
386+
ExecReload=/bin/kill -HUP $MAINPID
387+
KillMode=mixed
388+
KillSignal=SIGINT
389+
TimeoutSec=0
390+
391+
[Install]
392+
WantedBy=multi-user.target
393+
</programlisting>
394+
Using <literal>Type=notify</literal> requires that the server binary was
395+
built with <literal>configure --with-systemd</literal>.
396+
</para>
397+
398+
<para>
399+
Consider carefully the timeout
400+
setting. <application>systemd</application> has a default timeout of 90
401+
seconds as of this writing and will kill a process that does not notify
402+
readiness within that time. But a <productname>PostgreSQL</productname>
403+
server that might have to perform crash recovery at startup could take
404+
much longer to become ready. The suggested value of 0 disables the
405+
timeout logic.
406+
</para>
372407
</listitem>
373408

374409
<listitem>

‎src/Makefile.global.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ with_python= @with_python@
184184
with_tcl= @with_tcl@
185185
with_openssl= @with_openssl@
186186
with_selinux= @with_selinux@
187+
with_systemd= @with_systemd@
187188
with_libxml= @with_libxml@
188189
with_libxslt= @with_libxslt@
189190
with_system_tzdata = @with_system_tzdata@

‎src/backend/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ LIBS := $(filter-out -lpgport -lpgcommon, $(LIBS)) $(LDAP_LIBS_BE)
4545
# The backend doesn't need everything that's in LIBS, however
4646
LIBS :=$(filter-out -lz -lreadline -ledit -ltermcap -lncurses -lcurses,$(LIBS))
4747

48+
ifeq ($(with_systemd),yes)
49+
LIBS += -lsystemd
50+
endif
51+
4852
##########################################################################
4953

5054
all: submake-libpgport submake-schemapg postgres$(POSTGRES_IMP)

‎src/backend/postmaster/postmaster.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
#include<dns_sd.h>
8888
#endif
8989

90+
#ifdefUSE_SYSTEMD
91+
#include<systemd/sd-daemon.h>
92+
#endif
93+
9094
#ifdefHAVE_PTHREAD_IS_THREADED_NP
9195
#include<pthread.h>
9296
#endif
@@ -2533,6 +2537,9 @@ pmdie(SIGNAL_ARGS)
25332537
Shutdown=SmartShutdown;
25342538
ereport(LOG,
25352539
(errmsg("received smart shutdown request")));
2540+
#ifdefUSE_SYSTEMD
2541+
sd_notify(0,"STOPPING=1");
2542+
#endif
25362543

25372544
if (pmState==PM_RUN||pmState==PM_RECOVERY||
25382545
pmState==PM_HOT_STANDBY||pmState==PM_STARTUP)
@@ -2585,6 +2592,9 @@ pmdie(SIGNAL_ARGS)
25852592
Shutdown=FastShutdown;
25862593
ereport(LOG,
25872594
(errmsg("received fast shutdown request")));
2595+
#ifdefUSE_SYSTEMD
2596+
sd_notify(0,"STOPPING=1");
2597+
#endif
25882598

25892599
if (StartupPID!=0)
25902600
signal_child(StartupPID,SIGTERM);
@@ -2645,6 +2655,9 @@ pmdie(SIGNAL_ARGS)
26452655
Shutdown=ImmediateShutdown;
26462656
ereport(LOG,
26472657
(errmsg("received immediate shutdown request")));
2658+
#ifdefUSE_SYSTEMD
2659+
sd_notify(0,"STOPPING=1");
2660+
#endif
26482661

26492662
TerminateChildren(SIGQUIT);
26502663
pmState=PM_WAIT_BACKENDS;
@@ -2787,6 +2800,10 @@ reaper(SIGNAL_ARGS)
27872800
ereport(LOG,
27882801
(errmsg("database system is ready to accept connections")));
27892802

2803+
#ifdefUSE_SYSTEMD
2804+
sd_notify(0,"READY=1");
2805+
#endif
2806+
27902807
continue;
27912808
}
27922809

@@ -4916,6 +4933,11 @@ sigusr1_handler(SIGNAL_ARGS)
49164933
if (XLogArchivingAlways())
49174934
PgArchPID=pgarch_start();
49184935

4936+
#ifdefUSE_SYSTEMD
4937+
if (!EnableHotStandby)
4938+
sd_notify(0,"READY=1");
4939+
#endif
4940+
49194941
pmState=PM_RECOVERY;
49204942
}
49214943
if (CheckPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY)&&
@@ -4930,6 +4952,10 @@ sigusr1_handler(SIGNAL_ARGS)
49304952
ereport(LOG,
49314953
(errmsg("database system is ready to accept read only connections")));
49324954

4955+
#ifdefUSE_SYSTEMD
4956+
sd_notify(0,"READY=1");
4957+
#endif
4958+
49334959
pmState=PM_HOT_STANDBY;
49344960
/* Some workers may be scheduled to start now */
49354961
StartWorkerNeeded= true;

‎src/include/pg_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,9 @@
833833
/* Define to 1 to use Intel SSSE 4.2 CRC instructions with a runtime check. */
834834
#undef USE_SSE42_CRC32C_WITH_RUNTIME_CHECK
835835

836+
/* Define to build with systemd support. (--with-systemd) */
837+
#undef USE_SYSTEMD
838+
836839
/* Define to select SysV-style semaphores. */
837840
#undef USE_SYSV_SEMAPHORES
838841

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp