Skip to content

Commit 57dcc2e

Browse files
committed
Poll postmaster less frequently in recovery.
Since commits 9f09529 and f98b847 we don't poll the postmaster pipe at all during crash recovery on Linux and FreeBSD, but on other operating systems we were still doing it for every WAL record. Do it less frequently on operating systems where system calls are required, at the cost of delaying exit a bit after postmaster death. This avoids expensive system calls reported to slow down CPU-bound recovery by as much as 10-30%. Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi> Reviewed-by: Fujii Masao <masao.fujii@oss.nttdata.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/CA%2BhUKGK1607VmtrDUHQXrsooU%3Dap4g4R2yaoByWOOA3m8xevUQ%40mail.gmail.com Discussion: https://postgr.es/m/7261eb39-0369-f2f4-1bb5-62f3b6083b5e@iki.fi
1 parent de829dd commit 57dcc2e

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/backend/postmaster/startup.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@
3434
#include "utils/timeout.h"
3535

3636

37+
#ifndef USE_POSTMASTER_DEATH_SIGNAL
38+
/*
39+
* On systems that need to make a system call to find out if the postmaster has
40+
* gone away, we'll do so only every Nth call to HandleStartupProcInterrupts().
41+
* This only affects how long it takes us to detect the condition while we're
42+
* busy replaying WAL. Latch waits and similar which should react immediately
43+
* through the usual techniques.
44+
*/
45+
#define POSTMASTER_POLL_RATE_LIMIT 1024
46+
#endif
47+
3748
/*
3849
* Flags set by interrupt handlers for later service in the redo loop.
3950
*/
@@ -134,6 +145,10 @@ StartupRereadConfig(void)
134145
void
135146
HandleStartupProcInterrupts(void)
136147
{
148+
#ifdef POSTMASTER_POLL_RATE_LIMIT
149+
static uint32 postmaster_poll_count = 0;
150+
#endif
151+
137152
/*
138153
* Process any requests or signals received recently.
139154
*/
@@ -151,9 +166,15 @@ HandleStartupProcInterrupts(void)
151166

152167
/*
153168
* Emergency bailout if postmaster has died. This is to avoid the
154-
* necessity for manual cleanup of all postmaster children.
169+
* necessity for manual cleanup of all postmaster children. Do this less
170+
* frequently on systems for which we don't have signals to make that
171+
* cheap.
155172
*/
156-
if (IsUnderPostmaster && !PostmasterIsAlive())
173+
if (IsUnderPostmaster &&
174+
#ifdef POSTMASTER_POLL_RATE_LIMIT
175+
postmaster_poll_count++ % POSTMASTER_POLL_RATE_LIMIT == 0 &&
176+
#endif
177+
!PostmasterIsAlive())
157178
exit(1);
158179

159180
/* Process barrier events */

0 commit comments

Comments
 (0)