Excessive PostmasterIsAlive calls slow down WAL redo
I started looking at the "Improve compactify_tuples and
PageRepairFragmentation" patch, and set up a little performance test of
WAL replay. I ran pgbench, scale 5, to generate about 1 GB of WAL, and
timed how long it takes to replay that WAL. To focus purely on CPU
overhead, I kept the data directory in /dev/shm/.
Profiling that, without any patches applied, I noticed that a lot of
time was spent in read()s on the postmaster-death pipe, i.e. in
PostmasterIsAlive(). We call that between *every* WAL record.
As a quick test to see how much that matters, I commented out the
PostmasterIsAlive() call from HandleStartupProcInterrupts(). On
unpatched master, replaying that 1 GB of WAL takes about 20 seconds on
my laptop. Without the PostmasterIsAlive() call, 17 seconds.
That seems like an utter waste of time. I'm almost inclined to call that
a performance bug. As a straightforward fix, I'd suggest that we call
HandleStartupProcInterrupts() in the WAL redo loop, not on every record,
but only e.g. every 32 records. That would make the main redo loop less
responsive to shutdown, SIGHUP, or postmaster death, but that seems OK.
There are also calls to HandleStartupProcInterrupts() in the various
other loops, that wait for new WAL to arrive or recovery delay, so this
would only affect the case where we're actively replaying records.
- Heikki
Attachments:
0001-Call-HandleStartupProcInterrupts-less-frequently-in-.patchtext/x-patch; name=0001-Call-HandleStartupProcInterrupts-less-frequently-in-.patchDownload
From 596d3804c784b06f2125aa7727b82a265b08ccfb Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Thu, 5 Apr 2018 10:18:01 +0300
Subject: [PATCH 1/1] Call HandleStartupProcInterrupts() less frequently in WAL
redo.
HandleStartupProcInterrupts() calls PostmasterIsAlive(), which calls read()
on the postmaster death watch pipe. That's relatively expensive, when we do
it between every WAL record.
---
src/backend/access/transam/xlog.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index b4fd8395b7..3406c58c9b 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -7072,6 +7072,7 @@ StartupXLOG(void)
{
ErrorContextCallback errcallback;
TimestampTz xtime;
+ uint32 records_replayed = 0;
InRedo = true;
@@ -7105,8 +7106,13 @@ StartupXLOG(void)
}
#endif
- /* Handle interrupt signals of startup process */
- HandleStartupProcInterrupts();
+ /*
+ * Handle interrupt signals of startup process. This includes
+ * a call to PostmasterIsAlive(), which isn't totally free, so
+ * don't do this on every record, to avoid the overhead.
+ */
+ if (records_replayed % 32 == 0)
+ HandleStartupProcInterrupts();
/*
* Pause WAL replay, if requested by a hot-standby session via
@@ -7269,6 +7275,7 @@ StartupXLOG(void)
/* Remember this record as the last-applied one */
LastRec = ReadRecPtr;
+ records_replayed++;
/* Allow read-only connections if we're consistent now */
CheckRecoveryConsistency();
--
2.11.0
Heikki Linnakangas wrote:
That seems like an utter waste of time. I'm almost inclined to call that a
performance bug. As a straightforward fix, I'd suggest that we call
HandleStartupProcInterrupts() in the WAL redo loop, not on every record, but
only e.g. every 32 records. That would make the main redo loop less
responsive to shutdown, SIGHUP, or postmaster death, but that seems OK.
There are also calls to HandleStartupProcInterrupts() in the various other
loops, that wait for new WAL to arrive or recovery delay, so this would only
affect the case where we're actively replaying records.
I think calling PostmasterIsAlive only every 32 records is sensible, but
I'm not so sure about the other two things happening in
HandleStartupProcInterrupts (which are much cheaper anyway, since they
only read a local flag); if records are large or otherwise slow to
replay, I'd rather not wait for 32 of them before the process honoring
my shutdown request. Why not split the function in two, or maybe add a
flag "check for postmaster alive too", passed as true every 32 records?
The lesson seems to be that PostmasterIsAlive is moderately expensive
(one syscall). It's also used in WaitLatchOrSocket when
WL_POSTMASTER_DEATH is given. I didn't audit its callers terribly
carefully but I think these uses are not as performance-sensitive.
--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
On 5 April 2018 at 08:23, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
That seems like an utter waste of time. I'm almost inclined to call that a
performance bug. As a straightforward fix, I'd suggest that we call
HandleStartupProcInterrupts() in the WAL redo loop, not on every record, but
only e.g. every 32 records. That would make the main redo loop less
responsive to shutdown, SIGHUP, or postmaster death, but that seems OK.
There are also calls to HandleStartupProcInterrupts() in the various other
loops, that wait for new WAL to arrive or recovery delay, so this would only
affect the case where we're actively replaying records.
+1
--
Simon Riggs http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Hi,
On 2018-04-05 10:23:43 +0300, Heikki Linnakangas wrote:
Profiling that, without any patches applied, I noticed that a lot of time
was spent in read()s on the postmaster-death pipe, i.e. in
PostmasterIsAlive(). We call that between *every* WAL record.
That seems like an utter waste of time. I'm almost inclined to call that a
performance bug. As a straightforward fix, I'd suggest that we call
HandleStartupProcInterrupts() in the WAL redo loop, not on every record, but
only e.g. every 32 records.
I agree this is a performance problem. I do however not like the fix.
ISTM the better approach would be to try to reduce the cost of
PostmasterIsAlive() on common platforms - it should be nearly free if
done right.
One way to achieve that would e.g. to stop ignoring SIGPIPE and instead
check for postmaster death inside the handler, without reacting to
it. Then the the actual PostmasterIsAlive() checks are just a check of a
single sig_atomic_t.
Greetings,
Andres Freund
Andres Freund <andres@anarazel.de> writes:
ISTM the better approach would be to try to reduce the cost of
PostmasterIsAlive() on common platforms - it should be nearly free if
done right.
+1 if it's doable.
One way to achieve that would e.g. to stop ignoring SIGPIPE and instead
check for postmaster death inside the handler, without reacting to
it. Then the the actual PostmasterIsAlive() checks are just a check of a
single sig_atomic_t.
AFAIR, we do not get SIGPIPE on the postmaster pipe, because nobody
ever writes to it. So this sketch seems off to me, even assuming that
not-ignoring SIGPIPE causes no problems elsewhere.
While it's not POSIX, at least some platforms are capable of delivering
a separate signal on parent process death. Perhaps using that where
available would be enough of an answer.
regards, tom lane
On 2018-04-05 14:39:27 -0400, Tom Lane wrote:
Andres Freund <andres@anarazel.de> writes:
ISTM the better approach would be to try to reduce the cost of
PostmasterIsAlive() on common platforms - it should be nearly free if
done right.+1 if it's doable.
One way to achieve that would e.g. to stop ignoring SIGPIPE and instead
check for postmaster death inside the handler, without reacting to
it. Then the the actual PostmasterIsAlive() checks are just a check of a
single sig_atomic_t.AFAIR, we do not get SIGPIPE on the postmaster pipe, because nobody
ever writes to it. So this sketch seems off to me, even assuming that
not-ignoring SIGPIPE causes no problems elsewhere.
Yea, you're probably right. I'm mostly brainstorming here.
(FWIW, I don't think not ignoring SIGPIPE would be a huge issue if we
don't immediately take any action on its account)
While it's not POSIX, at least some platforms are capable of delivering
a separate signal on parent process death. Perhaps using that where
available would be enough of an answer.
Yea, that'd work on linux. Which is probably the platform 80-95% of
performance critical PG workloads run on. There's
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE on windows, which might also work,
but I'm not sure it provides enough opportunity for cleanup.
Greetings,
Andres Freund
On Thu, Apr 05, 2018 at 02:39:27PM -0400, Tom Lane wrote:
While it's not POSIX, at least some platforms are capable of delivering
a separate signal on parent process death. Perhaps using that where
available would be enough of an answer.
Are you referring to prctl here?
+1 on improving performance of PostmasterIsAlive() if possible instead
of checking for it every N records. You barely see records creating a
database from a large template close to each other but that could hurt
the response time of the redo process.
--
Michael
Greetings,
* Andres Freund (andres@anarazel.de) wrote:
On 2018-04-05 14:39:27 -0400, Tom Lane wrote:
Andres Freund <andres@anarazel.de> writes:
ISTM the better approach would be to try to reduce the cost of
PostmasterIsAlive() on common platforms - it should be nearly free if
done right.+1 if it's doable.
[...]
While it's not POSIX, at least some platforms are capable of delivering
a separate signal on parent process death. Perhaps using that where
available would be enough of an answer.Yea, that'd work on linux. Which is probably the platform 80-95% of
performance critical PG workloads run on. There's
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE on windows, which might also work,
but I'm not sure it provides enough opportunity for cleanup.
While I tend to agree that it'd be nice to just make it cheaper, that
doesn't seem like something that we'd be likely to back-patch and I tend
to share Heikki's feelings that this is a performance regression we
should be considering fixing in released versions.
What Alvaro posted up-thread seems like it might be a small enough
change to still be reasonable to back-patch and we can still think about
ways to make PostmasterIsAlive() cheaper in the future.
Thanks!
Stephen
Hi,
On 2018-04-06 07:39:28 -0400, Stephen Frost wrote:
While I tend to agree that it'd be nice to just make it cheaper, that
doesn't seem like something that we'd be likely to back-patch and I tend
to share Heikki's feelings that this is a performance regression we
should be considering fixing in released versions.
I'm doubtful about fairly characterizing this as a performance bug. It's
not like we've O(n^2) behaviour on our hand, and if your replay isn't of
a toy workload normally that one syscall isn't going to make a huge
difference because you've actual IO and such going on.
I'm also doubtful that it's sane to just check every 32 records. There's
records that can take a good chunk of time, and just continuing for
another 31 records seems like a bad idea.
Greetings,
Andres Freund
On 06/04/18 19:39, Andres Freund wrote:
On 2018-04-06 07:39:28 -0400, Stephen Frost wrote:
While I tend to agree that it'd be nice to just make it cheaper, that
doesn't seem like something that we'd be likely to back-patch and I tend
to share Heikki's feelings that this is a performance regression we
should be considering fixing in released versions.
To be clear, this isn't a performance *regression*. It's always been bad.
I'm not sure if I'd backpatch this. Maybe after it's been in 'master'
for a while and we've gotten some field testing of it.
I'm doubtful about fairly characterizing this as a performance bug. It's
not like we've O(n^2) behaviour on our hand, and if your replay isn't of
a toy workload normally that one syscall isn't going to make a huge
difference because you've actual IO and such going on.
If all the data fits in the buffer cache, then there would be no I/O.
Think of a smallish database that's heavily updated. There are a lot of
real applications like that.
I'm also doubtful that it's sane to just check every 32 records. There's
records that can take a good chunk of time, and just continuing for
another 31 records seems like a bad idea.
It's pretty arbitrary, I admit. It's the best I could come with, though.
If we could get a signal on postmaster death, that'd be best, but that's
a much bigger patch, and I'm worried that it would bring new portability
and reliability issues.
I'm not too worried about 32 records being too long an interval. True,
replaying 32 CREATE DATABASE records would take a long time. But pretty
much all other WAL records are fast enough to apply. We could make it
every 8 records rather than 32, if that makes you feel better. Or add
some extra conditions, like always check it when stepping to a new WAL
segment. In any case, the fundamental difference would be though to not
check it between every record.
- Heikki
Greetings,
* Heikki Linnakangas (hlinnaka@iki.fi) wrote:
On 06/04/18 19:39, Andres Freund wrote:
On 2018-04-06 07:39:28 -0400, Stephen Frost wrote:
While I tend to agree that it'd be nice to just make it cheaper, that
doesn't seem like something that we'd be likely to back-patch and I tend
to share Heikki's feelings that this is a performance regression we
should be considering fixing in released versions.To be clear, this isn't a performance *regression*. It's always been bad.
Oh, I see, apologies for the confusion, my initial read was that this
was due to some patch that had gone in previously, hence it was an
actual regression. I suppose I tend to view performance issues as
either "regression" or "opportunity for improvement" and when you said
"bug" it made me think it was a regression. :)
I'm not sure if I'd backpatch this. Maybe after it's been in 'master' for a
while and we've gotten some field testing of it.
If it's not a regression then there's I definitely think the bar is much
higher to consider this something to back-patch. I wouldn't typically
argue for back-patching a performance improvement unless it's to address
a specific regression.
Thanks!
Stephen
Hi,
On 2018-04-05 12:20:38 -0700, Andres Freund wrote:
While it's not POSIX, at least some platforms are capable of delivering
a separate signal on parent process death. Perhaps using that where
available would be enough of an answer.Yea, that'd work on linux. Which is probably the platform 80-95% of
performance critical PG workloads run on. There's
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE on windows, which might also work,
but I'm not sure it provides enough opportunity for cleanup.
I coincidentally got pinged about our current approach causing
performance problems on FreeBSD and started writing a patch. The
problem there appears to be that constantly attaching events to the read
pipe end, from multiple processes, causes significant contention inside
the kernel. Which isn't that surprising. That's distinct from the
problem netbsd/openbsd reported a while back (superflous wakeups).
That person said he'd work on adding an equivalent of linux'
prctl(PR_SET_PDEATHSIG) to FreeBSD.
It's not particularly hard to whip something up that kind of
works. Getting some of the details right isn't entirely as clear
however:
It's trivial to make PostmasterIsAlive() cheap. In my prototype I've
setup PR_SET_PDEATHSIG to deliver SIGINFO to postmaster children. That
sets parent_potentially_died to true. PostmasterIsAlive() only runs the
main body when it's true, making it very cheap in the common case.
What I'm however not quite as clear on is how to best change
WL_POSTMASTER_DEATH.
One appraoch approach I can come up with is to use the self-pipe for
both WL_POSTMASTER_DEATH and WL_LATCH_SET. As we can cheaply check if
either set->latch->is_set or parent_potentially_died, re-using the
selfpipe works well enough. What I'm however not quite sure about is
how to best do so with epoll() - there we explicitly do not iterate over
all registered events, but use epoll_event->data to get just the wait
event associated with a readyness event. Therefor we've to keep track
of whether a WaitEventSet has both WL_POSTMASTER_DEATH and WL_LATCH_SET
registered, and check in both WL_POSTMASTER_DEATH/WL_LATCH_SET whether
the event is being waited for.
Another approach, that's simpler to implement, is to simply have a
second selfpipe, just for WL_POSTMASTER_DEATH.
A third question is whether we want to keep postmaster_alive_fds if we
have PR_SET_PDEATHSIG. I'd want to continue re-checking that the parent
is actually dead, but we could also do so by kill()ing postmaster like
we used to do before 89fd72cbf26f5d2e3d86ab19c1ead73ab8fac0fe. It's not
bad to get rid of unnecessary filedescriptors. Would imply a semantic
change however.
Greetings,
Andres Freund
Andres Freund wrote:
Another approach, that's simpler to implement, is to simply have a
second selfpipe, just for WL_POSTMASTER_DEATH.
Would it work to use this second pipe, to which each child writes a byte
that postmaster never reads, and then rely on SIGPIPE when postmaster
dies? Then we never need to do a syscall.
--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
On April 9, 2018 6:31:07 PM PDT, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
Andres Freund wrote:
Another approach, that's simpler to implement, is to simply have a
second selfpipe, just for WL_POSTMASTER_DEATH.Would it work to use this second pipe, to which each child writes a
byte
that postmaster never reads, and then rely on SIGPIPE when postmaster
dies? Then we never need to do a syscall.
I'm not following, could you expand on what you're suggesting? Note that you do not get SIGPIPE for already buffered writes. Which syscall can we avoid?
Andres
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
On Tue, Apr 10, 2018 at 12:53 PM, Andres Freund <andres@anarazel.de> wrote:
I coincidentally got pinged about our current approach causing
performance problems on FreeBSD and started writing a patch. The
problem there appears to be that constantly attaching events to the read
pipe end, from multiple processes, causes significant contention inside
the kernel. Which isn't that surprising. That's distinct from the
problem netbsd/openbsd reported a while back (superflous wakeups).That person said he'd work on adding an equivalent of linux'
prctl(PR_SET_PDEATHSIG) to FreeBSD.
Just an idea, not tested: what about a reusable WaitEventSet with zero
timeout? Using the kqueue patch, that'd call kevent() which'd return
immediately and tell you if any postmaster death notifications had
arrive on your queue since last time you asked. It doesn't even touch
the pipe, or any other kernel objects apart from your own queue IIUC.
--
Thomas Munro
http://www.enterprisedb.com
On April 9, 2018 6:36:19 PM PDT, Thomas Munro <thomas.munro@enterprisedb.com> wrote:
On Tue, Apr 10, 2018 at 12:53 PM, Andres Freund <andres@anarazel.de>
wrote:I coincidentally got pinged about our current approach causing
performance problems on FreeBSD and started writing a patch. The
problem there appears to be that constantly attaching events to theread
pipe end, from multiple processes, causes significant contention
inside
the kernel. Which isn't that surprising. That's distinct from the
problem netbsd/openbsd reported a while back (superflous wakeups).That person said he'd work on adding an equivalent of linux'
prctl(PR_SET_PDEATHSIG) to FreeBSD.Just an idea, not tested: what about a reusable WaitEventSet with zero
timeout? Using the kqueue patch, that'd call kevent() which'd return
immediately and tell you if any postmaster death notifications had
arrive on your queue since last time you asked. It doesn't even touch
the pipe, or any other kernel objects apart from your own queue IIUC.
We still create a lot of WES adhoc in a number of places. I don't think this would solve that? The problem isn't that IsAlive is expensive, it's that polling is expensive. It's possible that using kqueue would fix that, because the highest frequency cases use a persistent WES.
Andres
Andres
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Andres Freund wrote:
On April 9, 2018 6:31:07 PM PDT, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
Would it work to use this second pipe, to which each child writes a
byte that postmaster never reads, and then rely on SIGPIPE when
postmaster dies? Then we never need to do a syscall.I'm not following, could you expand on what you're suggesting? Note
that you do not get SIGPIPE for already buffered writes. Which
syscall can we avoid?
Ah. I was thinking we'd get SIGPIPE from the byte sent at the start, as
soon as the kernel saw that postmaster abandoned the fd by dying.
Scratch that then.
--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
On April 9, 2018 6:57:23 PM PDT, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
Andres Freund wrote:
On April 9, 2018 6:31:07 PM PDT, Alvaro Herrera
<alvherre@alvh.no-ip.org> wrote:
Would it work to use this second pipe, to which each child writes a
byte that postmaster never reads, and then rely on SIGPIPE when
postmaster dies? Then we never need to do a syscall.I'm not following, could you expand on what you're suggesting? Note
that you do not get SIGPIPE for already buffered writes. Which
syscall can we avoid?Ah. I was thinking we'd get SIGPIPE from the byte sent at the start,
as
soon as the kernel saw that postmaster abandoned the fd by dying.
Scratch that then.
Had the same idea, but unfortunately reality, in the form of a test program, cured me of my hope ;)
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
On 10/04/18 04:36, Thomas Munro wrote:
On Tue, Apr 10, 2018 at 12:53 PM, Andres Freund <andres@anarazel.de> wrote:
I coincidentally got pinged about our current approach causing
performance problems on FreeBSD and started writing a patch. The
problem there appears to be that constantly attaching events to the read
pipe end, from multiple processes, causes significant contention inside
the kernel. Which isn't that surprising. That's distinct from the
problem netbsd/openbsd reported a while back (superflous wakeups).That person said he'd work on adding an equivalent of linux'
prctl(PR_SET_PDEATHSIG) to FreeBSD.Just an idea, not tested: what about a reusable WaitEventSet with zero
timeout? Using the kqueue patch, that'd call kevent() which'd return
immediately and tell you if any postmaster death notifications had
arrive on your queue since last time you asked. It doesn't even touch
the pipe, or any other kernel objects apart from your own queue IIUC.
Hmm. In PostmasterIsAlive(), you'd still need to call kevent() to check
if postmaster has died. It would just replace the current read() syscall
on the pipe with the kevent() syscall. Is it faster?
- Heikki
On Wed, Apr 11, 2018 at 10:22 PM, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
On 10/04/18 04:36, Thomas Munro wrote:
Just an idea, not tested: what about a reusable WaitEventSet with zero
timeout? Using the kqueue patch, that'd call kevent() which'd return
immediately and tell you if any postmaster death notifications had
arrive on your queue since last time you asked. It doesn't even touch
the pipe, or any other kernel objects apart from your own queue IIUC.Hmm. In PostmasterIsAlive(), you'd still need to call kevent() to check if
postmaster has died. It would just replace the current read() syscall on the
pipe with the kevent() syscall. Is it faster?
It should be (based on the report of read() being slow here because of
contention on the pipe itself, I guess because of frequent poll() in
WaitLatch() elsewhere?).
But as I said over on another thread[1]/messages/by-id/CAEepm=298omvRS2C8WO=Cxp+WcM-Vn8V3x4_QhxURhKTRCSfYg@mail.gmail.com (sorry, it got tangled up with
that other conversation about a related topic), maybe testing
getppid() would be simpler and about as fast as possible given you
have to make a syscall (all processes should normally be children of
postmaster, right?). And only check every nth time through the loop,
as you said, to avoid high frequency syscalls. I think I might have
been guilty of having a solution looking for a problem, there ;-)
[1]: /messages/by-id/CAEepm=298omvRS2C8WO=Cxp+WcM-Vn8V3x4_QhxURhKTRCSfYg@mail.gmail.com
--
Thomas Munro
http://www.enterprisedb.com
On Wed, Apr 11, 2018 at 10:22 PM, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
On Tue, Apr 10, 2018 at 12:53 PM, Andres Freund <andres@anarazel.de>
wrote:That person said he'd work on adding an equivalent of linux'
prctl(PR_SET_PDEATHSIG) to FreeBSD.
Here is an implementation of Andres's idea for Linux, and also for
patched FreeBSD (for later if/when that lands). Do you think this
makes sense Heikki? I am planning to add this to the next CF.
--
Thomas Munro
http://www.enterprisedb.com
Attachments:
0002-Use-signals-for-postmaster-death-detection-on-FreeBS.patchapplication/octet-stream; name=0002-Use-signals-for-postmaster-death-detection-on-FreeBS.patchDownload
From a7b5825fa100cb97fdaf2565a6cd9f3203688806 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@enterprisedb.com>
Date: Wed, 18 Apr 2018 16:17:54 +1200
Subject: [PATCH 2/2] Use signals for postmaster death detection on FreeBSD.
Use FreeBSD's new support for detecting parent process death to make
PostmasterIsAlive() very cheap, as was done earlier for Linux.
NOT FOR COMMIT: waiting for https://reviews.freebsd.org/D15106 to land.
Sharing this patch just to justify the way the macros are laid out in the
earlier patch, to support future implementations.
Author: Thomas Munro
Discussion: https://postgr.es/m/7261eb39-0369-f2f4-1bb5-62f3b6083b5e@iki.fi
---
configure | 29 +++++++++++++++++++++++++++++
configure.in | 13 +++++++++++++
src/backend/storage/ipc/pmsignal.c | 7 +++++++
src/include/c.h | 2 +-
src/include/pg_config.h.in | 3 +++
5 files changed, 53 insertions(+), 1 deletion(-)
diff --git a/configure b/configure
index 29ba38ee174..ad3455be1f2 100755
--- a/configure
+++ b/configure
@@ -9746,12 +9746,41 @@ if ac_fn_c_try_compile "$LINENO"; then :
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+# FreeBSD 12.0 has procctl(..., PROC_PDEATHSIG_SET, ...)
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+#include <sys/procctl.h>
+
+int
+main ()
+{
+
+#ifndef PROC_PDEATHSIG_SET
+#error PROC_PDEATHSIG_SET not defined
+#endif
+
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+ HAVE_PROC_PDEATHSIG_SET=1
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+
if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: PR_SET_PDEATHSIG" >&5
$as_echo "PR_SET_PDEATHSIG" >&6; }
$as_echo "#define HAVE_PR_SET_PDEATHSIG 1" >>confdefs.h
+elif test x"$HAVE_PROC_PDEATHSIG_SET" = "x1"; then
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: PROC_PDEATHSIG_SET" >&5
+$as_echo "PROC_PDEATHSIG_SET" >&6; }
+
+$as_echo "#define HAVE_PROC_PDEATHSIG_SET 1" >>confdefs.h
+
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
diff --git a/configure.in b/configure.in
index 31dda503628..5cb111580be 100644
--- a/configure.in
+++ b/configure.in
@@ -1036,10 +1036,23 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
#endif
])], [HAVE_PR_SET_PDEATHSIG=1])
+# FreeBSD 12.0 has procctl(..., PROC_PDEATHSIG_SET, ...)
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
+#include <sys/procctl.h>
+], [
+#ifndef PROC_PDEATHSIG_SET
+#error PROC_PDEATHSIG_SET not defined
+#endif
+])], [HAVE_PROC_PDEATHSIG_SET=1])
+
if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
AC_MSG_RESULT(PR_SET_PDEATHSIG)
AC_DEFINE(HAVE_PR_SET_PDEATHSIG, 1,
[Define to 1 if the system supports PR_SET_PDEATHSIG])
+elif test x"$HAVE_PROC_PDEATHSIG_SET" = "x1"; then
+ AC_MSG_RESULT(PROC_PDEATHSIG_SET)
+ AC_DEFINE(HAVE_PROC_PDEATHSIG_SET, 1,
+ [Define to 1 if the system supports PROC_PDEATHSIG_SET])
else
AC_MSG_RESULT(no)
fi
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index c415b8b5dbb..2f53da43975 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -27,6 +27,10 @@
#include <sys/prctl.h>
#endif
+#if defined(HAVE_PROC_PDEATHSIG_SET)
+#include <sys/procctl.h>
+#endif
+
/*
* The postmaster is signaled by its children by sending SIGUSR1. The
* specific reason is communicated via flags in shared memory. We keep
@@ -334,6 +338,9 @@ PostmasterDeathInit(void)
#ifdef HAVE_PR_SET_PDEATHSIG
if (prctl(PR_SET_PDEATHSIG, signum) < 0)
elog(ERROR, "could not request parent death signal: %m");
+#elif defined(HAVE_PROC_PDEATHSIG_SET)
+ if (procctl(P_PID, 0, PROC_PDEATHSIG_SET, &signum) < 0)
+ elog(ERROR, "could not request parent death signal: %m");
#endif
/*
diff --git a/src/include/c.h b/src/include/c.h
index 116c2a24814..16083afca55 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1161,7 +1161,7 @@ extern int fdatasync(int fildes);
* platforms that support it, for testing purposes.
*/
#ifndef NO_POSTMASTER_DEATH_SIGNAL
-#if defined(HAVE_PR_SET_PDEATHSIG)
+#if defined(HAVE_PR_SET_PDEATHSIG) || defined(HAVE_PROC_PDEATHSIG_SET)
#define USE_POSTMASTER_DEATH_SIGNAL
#endif
#endif
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 5454f0d4807..941fe301b82 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -428,6 +428,9 @@
/* Define to 1 if the assembler supports PPC's LWARX mutex hint bit. */
#undef HAVE_PPC_LWARX_MUTEX_HINT
+/* Define to 1 if the system supports PROC_PDEATHSIG_SET */
+#undef HAVE_PROC_PDEATHSIG_SET
+
/* Define to 1 if the system supports PR_SET_PDEATHSIG */
#undef HAVE_PR_SET_PDEATHSIG
--
2.16.2
0001-Use-signals-for-postmaster-death-detection-on-Linux.patchapplication/octet-stream; name=0001-Use-signals-for-postmaster-death-detection-on-Linux.patchDownload
From b5b9356227fb890c21fa0a4d2fa4dc7a40c9d24e Mon Sep 17 00:00:00 2001
From: Thomas Munro <munro@ip9.org>
Date: Wed, 18 Apr 2018 00:59:39 +0100
Subject: [PATCH 1/2] Use signals for postmaster death detection on Linux.
Linux provides a way to ask for a signal when your parent process dies. Use
that to make PostmasterIsAlive() very cheap.
Author: Thomas Munro, based on a suggestion from Andres Freund
Discussion: https://postgr.es/m/7261eb39-0369-f2f4-1bb5-62f3b6083b5e%40iki.fi
https://postgr.es/m/20180411002643.6buofht4ranhei7k%40alap3.anarazel.de
---
configure | 40 +++++++++++++++++++++++++++
configure.in | 22 +++++++++++++++
src/backend/postmaster/postmaster.c | 2 ++
src/backend/storage/ipc/latch.c | 6 ++--
src/backend/storage/ipc/pmsignal.c | 55 ++++++++++++++++++++++++++++++++++++-
src/include/c.h | 11 ++++++++
src/include/pg_config.h.in | 3 ++
src/include/storage/pmsignal.h | 17 +++++++++++-
8 files changed, 151 insertions(+), 5 deletions(-)
diff --git a/configure b/configure
index 56f18dfbc26..29ba38ee174 100755
--- a/configure
+++ b/configure
@@ -9717,6 +9717,46 @@ program to use during the build." "$LINENO" 5
fi
fi
+#
+# Signals to detect parent process death
+#
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for parent death signal support" >&5
+$as_echo_n "checking for parent death signal support... " >&6; }
+
+# Linux has prctl(PR_SET_PDEATHSIG, ...)
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+#include <sys/prctl.h>
+
+int
+main ()
+{
+
+#ifndef PR_SET_PDEATHSIG
+#error PR_SET_PDEATHSIG not defined
+#endif
+
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+ HAVE_PR_SET_PDEATHSIG=1
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+
+if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: PR_SET_PDEATHSIG" >&5
+$as_echo "PR_SET_PDEATHSIG" >&6; }
+
+$as_echo "#define HAVE_PR_SET_PDEATHSIG 1" >>confdefs.h
+
+else
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
#
# Pthreads
#
diff --git a/configure.in b/configure.in
index da02a56ec66..31dda503628 100644
--- a/configure.in
+++ b/configure.in
@@ -1022,6 +1022,28 @@ program to use during the build.])
fi
fi
+#
+# Signals to detect parent process death
+#
+AC_MSG_CHECKING([for parent death signal support])
+
+# Linux has prctl(PR_SET_PDEATHSIG, ...)
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
+#include <sys/prctl.h>
+], [
+#ifndef PR_SET_PDEATHSIG
+#error PR_SET_PDEATHSIG not defined
+#endif
+])], [HAVE_PR_SET_PDEATHSIG=1])
+
+if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
+ AC_MSG_RESULT(PR_SET_PDEATHSIG)
+ AC_DEFINE(HAVE_PR_SET_PDEATHSIG, 1,
+ [Define to 1 if the system supports PR_SET_PDEATHSIG])
+else
+ AC_MSG_RESULT(no)
+fi
+
#
# Pthreads
#
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index d948369f3ea..c3ef7ee38ca 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -2484,6 +2484,8 @@ ClosePostmasterPorts(bool am_syslogger)
if (bonjour_sdref)
close(DNSServiceRefSockFD(bonjour_sdref));
#endif
+
+ PostmasterDeathInit();
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index e6706f7fb80..f6dda9cc9ac 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1112,7 +1112,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
* cost much.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
@@ -1230,7 +1230,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
* cost much.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
@@ -1390,7 +1390,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* even though there is no known reason to think that the event could
* be falsely set on Windows.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index be61858fc67..c415b8b5dbb 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -23,6 +23,9 @@
#include "storage/pmsignal.h"
#include "storage/shmem.h"
+#if defined(HAVE_PR_SET_PDEATHSIG)
+#include <sys/prctl.h>
+#endif
/*
* The postmaster is signaled by its children by sending SIGUSR1. The
@@ -71,6 +74,28 @@ struct PMSignalData
NON_EXEC_STATIC volatile PMSignalData *PMSignalState = NULL;
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+sig_atomic_t postmaster_possibly_dead = false;
+
+static void
+postmaster_death_handler(int signo)
+{
+ postmaster_possibly_dead = true;
+}
+
+/*
+ * The available signals depends on the OS and architecture. SIGUSR1 and
+ * SIGUSR2 are already used for other things, so choose another one.
+ */
+#if defined(SIGINFO)
+#define POSTMASTER_DEATH_SIGNAL SIGINFO
+#elif defined(SIGPWR)
+#define POSTMASTER_DEATH_SIGNAL SIGPWR
+#else
+#error "cannot find a signal to use for postmaster death"
+#endif
+
+#endif
/*
* PMSignalShmemSize
@@ -269,7 +294,7 @@ MarkPostmasterChildInactive(void)
* PostmasterIsAlive - check whether postmaster process is still alive
*/
bool
-PostmasterIsAlive(void)
+PostmasterIsAliveInternal(void)
{
#ifndef WIN32
char c;
@@ -291,3 +316,31 @@ PostmasterIsAlive(void)
return (WaitForSingleObject(PostmasterHandle, 0) == WAIT_TIMEOUT);
#endif /* WIN32 */
}
+
+/*
+ * PostmasterDeathInit - request signal on postmaster death if possible
+ */
+void
+PostmasterDeathInit(void)
+{
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+ int signum;
+
+ /* Register our signal handler. */
+ signum = POSTMASTER_DEATH_SIGNAL;
+ pqsignal(signum, postmaster_death_handler);
+
+ /* Request a signal on parent exit. */
+#ifdef HAVE_PR_SET_PDEATHSIG
+ if (prctl(PR_SET_PDEATHSIG, signum) < 0)
+ elog(ERROR, "could not request parent death signal: %m");
+#endif
+
+ /*
+ * Just in case the parent was gone already and we missed it, we'd
+ * better check the slow way.
+ */
+ if (!PostmasterIsAliveInternal())
+ postmaster_possibly_dead = true;
+#endif
+}
diff --git a/src/include/c.h b/src/include/c.h
index 95e9aeded9d..116c2a24814 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1155,6 +1155,17 @@ extern int fdatasync(int fildes);
#define NON_EXEC_STATIC static
#endif
+/*
+ * Do we have a way to ask for a signal on parent death? Build with
+ * NO_POSTMASTER_DEATH_SIGNAL defined to inhibit the use of death signals on
+ * platforms that support it, for testing purposes.
+ */
+#ifndef NO_POSTMASTER_DEATH_SIGNAL
+#if defined(HAVE_PR_SET_PDEATHSIG)
+#define USE_POSTMASTER_DEATH_SIGNAL
+#endif
+#endif
+
/* /port compatibility functions */
#include "port.h"
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index f3620231a71..5454f0d4807 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -428,6 +428,9 @@
/* Define to 1 if the assembler supports PPC's LWARX mutex hint bit. */
#undef HAVE_PPC_LWARX_MUTEX_HINT
+/* Define to 1 if the system supports PR_SET_PDEATHSIG */
+#undef HAVE_PR_SET_PDEATHSIG
+
/* Define to 1 if you have the `pstat' function. */
#undef HAVE_PSTAT
diff --git a/src/include/storage/pmsignal.h b/src/include/storage/pmsignal.h
index bec162cc16d..a6a05e08774 100644
--- a/src/include/storage/pmsignal.h
+++ b/src/include/storage/pmsignal.h
@@ -51,6 +51,21 @@ extern bool IsPostmasterChildWalSender(int slot);
extern void MarkPostmasterChildActive(void);
extern void MarkPostmasterChildInactive(void);
extern void MarkPostmasterChildWalSender(void);
-extern bool PostmasterIsAlive(void);
+extern bool PostmasterIsAliveInternal(void);
+extern void PostmasterDeathInit(void);
+
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+extern sig_atomic_t postmaster_possibly_dead;
+#endif
+
+static inline bool
+PostmasterIsAlive(void)
+{
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+ if (likely(!postmaster_possibly_dead))
+ return false;
+#endif
+ return PostmasterIsAliveInternal();
+}
#endif /* PMSIGNAL_H */
--
2.16.2
On Wed, Apr 18, 2018 at 5:04 PM, Thomas Munro
<thomas.munro@enterprisedb.com> wrote:
On Wed, Apr 11, 2018 at 10:22 PM, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
On Tue, Apr 10, 2018 at 12:53 PM, Andres Freund <andres@anarazel.de>
wrote:That person said he'd work on adding an equivalent of linux'
prctl(PR_SET_PDEATHSIG) to FreeBSD.Here is an implementation of Andres's idea for Linux, and also for
patched FreeBSD (for later if/when that lands). Do you think this
makes sense Heikki? I am planning to add this to the next CF.
Here's a new version with a stupid bug fixed (I accidentally posted a
testing version that returned false instead of true, as cfbot quickly
pointed out -- d'oh).
By the way, these patches only use the death signal to make
PostmasterIsAlive() fast, for use by busy loops like recovery. The
postmaster pipe is still used for IO/timeout loops to detect
postmaster death. In theory you could get rid of the postmaster pipe
completely when USE_POSTMASTER_DEATH_SIGNAL is defined and make it
like the latch code, using the same self-pipe. I'm not sure if there
is anything to be gained by that (that wasn't already gained by using
epoll/kqueue) so I'm not proposing it.
--
Thomas Munro
http://www.enterprisedb.com
Attachments:
0001-Use-signals-for-postmaster-death-on-Linux-v2.patchapplication/octet-stream; name=0001-Use-signals-for-postmaster-death-on-Linux-v2.patchDownload
From c1cf4c743597a776fbbd50a276a4177ad4e5a34b Mon Sep 17 00:00:00 2001
From: Thomas Munro <munro@ip9.org>
Date: Wed, 18 Apr 2018 00:59:39 +0100
Subject: [PATCH 1/2] Use signals for postmaster death on Linux.
Linux provides a way to ask for a signal when your parent process dies. Use
that to make PostmasterIsAlive() very cheap.
Author: Thomas Munro, based on a suggestion from Andres Freund
Discussion: https://postgr.es/m/7261eb39-0369-f2f4-1bb5-62f3b6083b5e%40iki.fi
https://postgr.es/m/20180411002643.6buofht4ranhei7k%40alap3.anarazel.de
---
configure | 40 +++++++++++++++++++++++++++
configure.in | 22 +++++++++++++++
src/backend/postmaster/postmaster.c | 2 ++
src/backend/storage/ipc/latch.c | 6 ++--
src/backend/storage/ipc/pmsignal.c | 55 ++++++++++++++++++++++++++++++++++++-
src/include/c.h | 11 ++++++++
src/include/pg_config.h.in | 3 ++
src/include/storage/pmsignal.h | 17 +++++++++++-
8 files changed, 151 insertions(+), 5 deletions(-)
diff --git a/configure b/configure
index 56f18dfbc26..29ba38ee174 100755
--- a/configure
+++ b/configure
@@ -9717,6 +9717,46 @@ program to use during the build." "$LINENO" 5
fi
fi
+#
+# Signals to detect parent process death
+#
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for parent death signal support" >&5
+$as_echo_n "checking for parent death signal support... " >&6; }
+
+# Linux has prctl(PR_SET_PDEATHSIG, ...)
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+#include <sys/prctl.h>
+
+int
+main ()
+{
+
+#ifndef PR_SET_PDEATHSIG
+#error PR_SET_PDEATHSIG not defined
+#endif
+
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+ HAVE_PR_SET_PDEATHSIG=1
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+
+if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: PR_SET_PDEATHSIG" >&5
+$as_echo "PR_SET_PDEATHSIG" >&6; }
+
+$as_echo "#define HAVE_PR_SET_PDEATHSIG 1" >>confdefs.h
+
+else
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
#
# Pthreads
#
diff --git a/configure.in b/configure.in
index da02a56ec66..31dda503628 100644
--- a/configure.in
+++ b/configure.in
@@ -1022,6 +1022,28 @@ program to use during the build.])
fi
fi
+#
+# Signals to detect parent process death
+#
+AC_MSG_CHECKING([for parent death signal support])
+
+# Linux has prctl(PR_SET_PDEATHSIG, ...)
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
+#include <sys/prctl.h>
+], [
+#ifndef PR_SET_PDEATHSIG
+#error PR_SET_PDEATHSIG not defined
+#endif
+])], [HAVE_PR_SET_PDEATHSIG=1])
+
+if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
+ AC_MSG_RESULT(PR_SET_PDEATHSIG)
+ AC_DEFINE(HAVE_PR_SET_PDEATHSIG, 1,
+ [Define to 1 if the system supports PR_SET_PDEATHSIG])
+else
+ AC_MSG_RESULT(no)
+fi
+
#
# Pthreads
#
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index d948369f3ea..c3ef7ee38ca 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -2484,6 +2484,8 @@ ClosePostmasterPorts(bool am_syslogger)
if (bonjour_sdref)
close(DNSServiceRefSockFD(bonjour_sdref));
#endif
+
+ PostmasterDeathInit();
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index e6706f7fb80..f6dda9cc9ac 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1112,7 +1112,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
* cost much.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
@@ -1230,7 +1230,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
* cost much.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
@@ -1390,7 +1390,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* even though there is no known reason to think that the event could
* be falsely set on Windows.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index be61858fc67..c415b8b5dbb 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -23,6 +23,9 @@
#include "storage/pmsignal.h"
#include "storage/shmem.h"
+#if defined(HAVE_PR_SET_PDEATHSIG)
+#include <sys/prctl.h>
+#endif
/*
* The postmaster is signaled by its children by sending SIGUSR1. The
@@ -71,6 +74,28 @@ struct PMSignalData
NON_EXEC_STATIC volatile PMSignalData *PMSignalState = NULL;
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+sig_atomic_t postmaster_possibly_dead = false;
+
+static void
+postmaster_death_handler(int signo)
+{
+ postmaster_possibly_dead = true;
+}
+
+/*
+ * The available signals depends on the OS and architecture. SIGUSR1 and
+ * SIGUSR2 are already used for other things, so choose another one.
+ */
+#if defined(SIGINFO)
+#define POSTMASTER_DEATH_SIGNAL SIGINFO
+#elif defined(SIGPWR)
+#define POSTMASTER_DEATH_SIGNAL SIGPWR
+#else
+#error "cannot find a signal to use for postmaster death"
+#endif
+
+#endif
/*
* PMSignalShmemSize
@@ -269,7 +294,7 @@ MarkPostmasterChildInactive(void)
* PostmasterIsAlive - check whether postmaster process is still alive
*/
bool
-PostmasterIsAlive(void)
+PostmasterIsAliveInternal(void)
{
#ifndef WIN32
char c;
@@ -291,3 +316,31 @@ PostmasterIsAlive(void)
return (WaitForSingleObject(PostmasterHandle, 0) == WAIT_TIMEOUT);
#endif /* WIN32 */
}
+
+/*
+ * PostmasterDeathInit - request signal on postmaster death if possible
+ */
+void
+PostmasterDeathInit(void)
+{
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+ int signum;
+
+ /* Register our signal handler. */
+ signum = POSTMASTER_DEATH_SIGNAL;
+ pqsignal(signum, postmaster_death_handler);
+
+ /* Request a signal on parent exit. */
+#ifdef HAVE_PR_SET_PDEATHSIG
+ if (prctl(PR_SET_PDEATHSIG, signum) < 0)
+ elog(ERROR, "could not request parent death signal: %m");
+#endif
+
+ /*
+ * Just in case the parent was gone already and we missed it, we'd
+ * better check the slow way.
+ */
+ if (!PostmasterIsAliveInternal())
+ postmaster_possibly_dead = true;
+#endif
+}
diff --git a/src/include/c.h b/src/include/c.h
index 95e9aeded9d..116c2a24814 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1155,6 +1155,17 @@ extern int fdatasync(int fildes);
#define NON_EXEC_STATIC static
#endif
+/*
+ * Do we have a way to ask for a signal on parent death? Build with
+ * NO_POSTMASTER_DEATH_SIGNAL defined to inhibit the use of death signals on
+ * platforms that support it, for testing purposes.
+ */
+#ifndef NO_POSTMASTER_DEATH_SIGNAL
+#if defined(HAVE_PR_SET_PDEATHSIG)
+#define USE_POSTMASTER_DEATH_SIGNAL
+#endif
+#endif
+
/* /port compatibility functions */
#include "port.h"
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index f3620231a71..5454f0d4807 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -428,6 +428,9 @@
/* Define to 1 if the assembler supports PPC's LWARX mutex hint bit. */
#undef HAVE_PPC_LWARX_MUTEX_HINT
+/* Define to 1 if the system supports PR_SET_PDEATHSIG */
+#undef HAVE_PR_SET_PDEATHSIG
+
/* Define to 1 if you have the `pstat' function. */
#undef HAVE_PSTAT
diff --git a/src/include/storage/pmsignal.h b/src/include/storage/pmsignal.h
index bec162cc16d..6667853bdc3 100644
--- a/src/include/storage/pmsignal.h
+++ b/src/include/storage/pmsignal.h
@@ -51,6 +51,21 @@ extern bool IsPostmasterChildWalSender(int slot);
extern void MarkPostmasterChildActive(void);
extern void MarkPostmasterChildInactive(void);
extern void MarkPostmasterChildWalSender(void);
-extern bool PostmasterIsAlive(void);
+extern bool PostmasterIsAliveInternal(void);
+extern void PostmasterDeathInit(void);
+
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+extern sig_atomic_t postmaster_possibly_dead;
+#endif
+
+static inline bool
+PostmasterIsAlive(void)
+{
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+ if (likely(!postmaster_possibly_dead))
+ return true;
+#endif
+ return PostmasterIsAliveInternal();
+}
#endif /* PMSIGNAL_H */
--
2.16.2
0002-Use-signals-for-postmaster-death-on-FreeBSD-v2.patchapplication/octet-stream; name=0002-Use-signals-for-postmaster-death-on-FreeBSD-v2.patchDownload
From 1e027111dac326fa34138c1451e6b6d9dd222489 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@enterprisedb.com>
Date: Wed, 18 Apr 2018 16:17:54 +1200
Subject: [PATCH 2/2] Use signals for postmaster death on FreeBSD.
Use FreeBSD's new support for detecting parent process death to make
PostmasterIsAlive() very cheap, as was done earlier for Linux.
NOT FOR COMMIT: Support on the FreeBSD side is committed but not yet shipped
and could change. There is a chance it'll ship before CF1, in FreeBSD 11.2.
Sharing this patch now just to justify the way the macros are laid out in the
earlier patch, to support future implementations.
Author: Thomas Munro
Discussion: https://postgr.es/m/7261eb39-0369-f2f4-1bb5-62f3b6083b5e@iki.fi
---
configure | 29 +++++++++++++++++++++++++++++
configure.in | 13 +++++++++++++
src/backend/storage/ipc/pmsignal.c | 7 +++++++
src/include/c.h | 2 +-
src/include/pg_config.h.in | 3 +++
5 files changed, 53 insertions(+), 1 deletion(-)
diff --git a/configure b/configure
index 29ba38ee174..ad3455be1f2 100755
--- a/configure
+++ b/configure
@@ -9746,12 +9746,41 @@ if ac_fn_c_try_compile "$LINENO"; then :
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+# FreeBSD 12.0 has procctl(..., PROC_PDEATHSIG_SET, ...)
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+#include <sys/procctl.h>
+
+int
+main ()
+{
+
+#ifndef PROC_PDEATHSIG_SET
+#error PROC_PDEATHSIG_SET not defined
+#endif
+
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+ HAVE_PROC_PDEATHSIG_SET=1
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+
if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: PR_SET_PDEATHSIG" >&5
$as_echo "PR_SET_PDEATHSIG" >&6; }
$as_echo "#define HAVE_PR_SET_PDEATHSIG 1" >>confdefs.h
+elif test x"$HAVE_PROC_PDEATHSIG_SET" = "x1"; then
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: PROC_PDEATHSIG_SET" >&5
+$as_echo "PROC_PDEATHSIG_SET" >&6; }
+
+$as_echo "#define HAVE_PROC_PDEATHSIG_SET 1" >>confdefs.h
+
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
diff --git a/configure.in b/configure.in
index 31dda503628..5cb111580be 100644
--- a/configure.in
+++ b/configure.in
@@ -1036,10 +1036,23 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
#endif
])], [HAVE_PR_SET_PDEATHSIG=1])
+# FreeBSD 12.0 has procctl(..., PROC_PDEATHSIG_SET, ...)
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
+#include <sys/procctl.h>
+], [
+#ifndef PROC_PDEATHSIG_SET
+#error PROC_PDEATHSIG_SET not defined
+#endif
+])], [HAVE_PROC_PDEATHSIG_SET=1])
+
if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
AC_MSG_RESULT(PR_SET_PDEATHSIG)
AC_DEFINE(HAVE_PR_SET_PDEATHSIG, 1,
[Define to 1 if the system supports PR_SET_PDEATHSIG])
+elif test x"$HAVE_PROC_PDEATHSIG_SET" = "x1"; then
+ AC_MSG_RESULT(PROC_PDEATHSIG_SET)
+ AC_DEFINE(HAVE_PROC_PDEATHSIG_SET, 1,
+ [Define to 1 if the system supports PROC_PDEATHSIG_SET])
else
AC_MSG_RESULT(no)
fi
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index c415b8b5dbb..2f53da43975 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -27,6 +27,10 @@
#include <sys/prctl.h>
#endif
+#if defined(HAVE_PROC_PDEATHSIG_SET)
+#include <sys/procctl.h>
+#endif
+
/*
* The postmaster is signaled by its children by sending SIGUSR1. The
* specific reason is communicated via flags in shared memory. We keep
@@ -334,6 +338,9 @@ PostmasterDeathInit(void)
#ifdef HAVE_PR_SET_PDEATHSIG
if (prctl(PR_SET_PDEATHSIG, signum) < 0)
elog(ERROR, "could not request parent death signal: %m");
+#elif defined(HAVE_PROC_PDEATHSIG_SET)
+ if (procctl(P_PID, 0, PROC_PDEATHSIG_SET, &signum) < 0)
+ elog(ERROR, "could not request parent death signal: %m");
#endif
/*
diff --git a/src/include/c.h b/src/include/c.h
index 116c2a24814..16083afca55 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1161,7 +1161,7 @@ extern int fdatasync(int fildes);
* platforms that support it, for testing purposes.
*/
#ifndef NO_POSTMASTER_DEATH_SIGNAL
-#if defined(HAVE_PR_SET_PDEATHSIG)
+#if defined(HAVE_PR_SET_PDEATHSIG) || defined(HAVE_PROC_PDEATHSIG_SET)
#define USE_POSTMASTER_DEATH_SIGNAL
#endif
#endif
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 5454f0d4807..941fe301b82 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -428,6 +428,9 @@
/* Define to 1 if the assembler supports PPC's LWARX mutex hint bit. */
#undef HAVE_PPC_LWARX_MUTEX_HINT
+/* Define to 1 if the system supports PROC_PDEATHSIG_SET */
+#undef HAVE_PROC_PDEATHSIG_SET
+
/* Define to 1 if the system supports PR_SET_PDEATHSIG */
#undef HAVE_PR_SET_PDEATHSIG
--
2.16.2
On April 18, 2018 8:05:50 PM PDT, Thomas Munro <thomas.munro@enterprisedb.com> wrote:
On Wed, Apr 18, 2018 at 5:04 PM, Thomas Munro
<thomas.munro@enterprisedb.com> wrote:On Wed, Apr 11, 2018 at 10:22 PM, Heikki Linnakangas
<hlinnaka@iki.fi> wrote:
On Tue, Apr 10, 2018 at 12:53 PM, Andres Freund
<andres@anarazel.de>
wrote:
That person said he'd work on adding an equivalent of linux'
prctl(PR_SET_PDEATHSIG) to FreeBSD.Here is an implementation of Andres's idea for Linux, and also for
patched FreeBSD (for later if/when that lands). Do you think this
makes sense Heikki? I am planning to add this to the next CF.Here's a new version with a stupid bug fixed (I accidentally posted a
testing version that returned false instead of true, as cfbot quickly
pointed out -- d'oh).By the way, these patches only use the death signal to make
PostmasterIsAlive() fast, for use by busy loops like recovery. The
postmaster pipe is still used for IO/timeout loops to detect
postmaster death. In theory you could get rid of the postmaster pipe
completely when USE_POSTMASTER_DEATH_SIGNAL is defined and make it
like the latch code, using the same self-pipe. I'm not sure if there
is anything to be gained by that (that wasn't already gained by using
epoll/kqueue) so I'm not proposing it.
In my local prototype patch I'd done so. And I think it makes sense, because it's s somewhat contended object, even when using epoll/kqueue. On the other hand, it makes the chide changed a bit harder, making it pretty was were I suspended the work for a bit...
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
On Thu, Apr 19, 2018 at 6:20 PM, Andres Freund <andres@anarazel.de> wrote:
On April 18, 2018 8:05:50 PM PDT, Thomas Munro <thomas.munro@enterprisedb.com> wrote:
By the way, these patches only use the death signal to make
PostmasterIsAlive() fast, for use by busy loops like recovery. The
postmaster pipe is still used for IO/timeout loops to detect
postmaster death. In theory you could get rid of the postmaster pipe
completely when USE_POSTMASTER_DEATH_SIGNAL is defined and make it
like the latch code, using the same self-pipe. I'm not sure if there
is anything to be gained by that (that wasn't already gained by using
epoll/kqueue) so I'm not proposing it.In my local prototype patch I'd done so. And I think it makes sense, because it's s somewhat contended object, even when using epoll/kqueue. On the other hand, it makes the chide changed a bit harder, making it pretty was were I suspended the work for a bit...
Hmm. I thought the whole idea of these interfaces was "don't call us,
we'll call you" inside the kernel, so you can add thousands of pipes
if you like, but epoll/kevent will only check the queue and then wait
for notification, rather than interacting with the referenced objects?
If that is true, and given that we need to maintain the pipe-based
code anyway for platforms that need it, then what would we gain by
adding a different code path just because we can? Obviously
WaitLatch() (the thing that creates, adds pipe, waits, then destroys
every time) could save time by not having to deal with a postmaster
pipe, but the solution to that is another patch that switches more
things to reusable WES objects.
--
Thomas Munro
http://www.enterprisedb.com
Here's a new version, because FreeBSD's new interface changed slightly.
--
Thomas Munro
http://www.enterprisedb.com
Attachments:
0001-Use-signals-for-postmaster-death-on-Linux-v3.patchapplication/octet-stream; name=0001-Use-signals-for-postmaster-death-on-Linux-v3.patchDownload
From c1cf4c743597a776fbbd50a276a4177ad4e5a34b Mon Sep 17 00:00:00 2001
From: Thomas Munro <munro@ip9.org>
Date: Wed, 18 Apr 2018 00:59:39 +0100
Subject: [PATCH 1/2] Use signals for postmaster death on Linux.
Linux provides a way to ask for a signal when your parent process dies. Use
that to make PostmasterIsAlive() very cheap.
Author: Thomas Munro, based on a suggestion from Andres Freund
Discussion: https://postgr.es/m/7261eb39-0369-f2f4-1bb5-62f3b6083b5e%40iki.fi
https://postgr.es/m/20180411002643.6buofht4ranhei7k%40alap3.anarazel.de
---
configure | 40 +++++++++++++++++++++++++++
configure.in | 22 +++++++++++++++
src/backend/postmaster/postmaster.c | 2 ++
src/backend/storage/ipc/latch.c | 6 ++--
src/backend/storage/ipc/pmsignal.c | 55 ++++++++++++++++++++++++++++++++++++-
src/include/c.h | 11 ++++++++
src/include/pg_config.h.in | 3 ++
src/include/storage/pmsignal.h | 17 +++++++++++-
8 files changed, 151 insertions(+), 5 deletions(-)
diff --git a/configure b/configure
index 56f18dfbc26..29ba38ee174 100755
--- a/configure
+++ b/configure
@@ -9717,6 +9717,46 @@ program to use during the build." "$LINENO" 5
fi
fi
+#
+# Signals to detect parent process death
+#
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for parent death signal support" >&5
+$as_echo_n "checking for parent death signal support... " >&6; }
+
+# Linux has prctl(PR_SET_PDEATHSIG, ...)
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+#include <sys/prctl.h>
+
+int
+main ()
+{
+
+#ifndef PR_SET_PDEATHSIG
+#error PR_SET_PDEATHSIG not defined
+#endif
+
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+ HAVE_PR_SET_PDEATHSIG=1
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+
+if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: PR_SET_PDEATHSIG" >&5
+$as_echo "PR_SET_PDEATHSIG" >&6; }
+
+$as_echo "#define HAVE_PR_SET_PDEATHSIG 1" >>confdefs.h
+
+else
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
#
# Pthreads
#
diff --git a/configure.in b/configure.in
index da02a56ec66..31dda503628 100644
--- a/configure.in
+++ b/configure.in
@@ -1022,6 +1022,28 @@ program to use during the build.])
fi
fi
+#
+# Signals to detect parent process death
+#
+AC_MSG_CHECKING([for parent death signal support])
+
+# Linux has prctl(PR_SET_PDEATHSIG, ...)
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
+#include <sys/prctl.h>
+], [
+#ifndef PR_SET_PDEATHSIG
+#error PR_SET_PDEATHSIG not defined
+#endif
+])], [HAVE_PR_SET_PDEATHSIG=1])
+
+if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
+ AC_MSG_RESULT(PR_SET_PDEATHSIG)
+ AC_DEFINE(HAVE_PR_SET_PDEATHSIG, 1,
+ [Define to 1 if the system supports PR_SET_PDEATHSIG])
+else
+ AC_MSG_RESULT(no)
+fi
+
#
# Pthreads
#
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index d948369f3ea..c3ef7ee38ca 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -2484,6 +2484,8 @@ ClosePostmasterPorts(bool am_syslogger)
if (bonjour_sdref)
close(DNSServiceRefSockFD(bonjour_sdref));
#endif
+
+ PostmasterDeathInit();
}
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index e6706f7fb80..f6dda9cc9ac 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1112,7 +1112,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
* cost much.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
@@ -1230,7 +1230,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
* cost much.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
@@ -1390,7 +1390,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* even though there is no known reason to think that the event could
* be falsely set on Windows.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index be61858fc67..c415b8b5dbb 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -23,6 +23,9 @@
#include "storage/pmsignal.h"
#include "storage/shmem.h"
+#if defined(HAVE_PR_SET_PDEATHSIG)
+#include <sys/prctl.h>
+#endif
/*
* The postmaster is signaled by its children by sending SIGUSR1. The
@@ -71,6 +74,28 @@ struct PMSignalData
NON_EXEC_STATIC volatile PMSignalData *PMSignalState = NULL;
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+sig_atomic_t postmaster_possibly_dead = false;
+
+static void
+postmaster_death_handler(int signo)
+{
+ postmaster_possibly_dead = true;
+}
+
+/*
+ * The available signals depends on the OS and architecture. SIGUSR1 and
+ * SIGUSR2 are already used for other things, so choose another one.
+ */
+#if defined(SIGINFO)
+#define POSTMASTER_DEATH_SIGNAL SIGINFO
+#elif defined(SIGPWR)
+#define POSTMASTER_DEATH_SIGNAL SIGPWR
+#else
+#error "cannot find a signal to use for postmaster death"
+#endif
+
+#endif
/*
* PMSignalShmemSize
@@ -269,7 +294,7 @@ MarkPostmasterChildInactive(void)
* PostmasterIsAlive - check whether postmaster process is still alive
*/
bool
-PostmasterIsAlive(void)
+PostmasterIsAliveInternal(void)
{
#ifndef WIN32
char c;
@@ -291,3 +316,31 @@ PostmasterIsAlive(void)
return (WaitForSingleObject(PostmasterHandle, 0) == WAIT_TIMEOUT);
#endif /* WIN32 */
}
+
+/*
+ * PostmasterDeathInit - request signal on postmaster death if possible
+ */
+void
+PostmasterDeathInit(void)
+{
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+ int signum;
+
+ /* Register our signal handler. */
+ signum = POSTMASTER_DEATH_SIGNAL;
+ pqsignal(signum, postmaster_death_handler);
+
+ /* Request a signal on parent exit. */
+#ifdef HAVE_PR_SET_PDEATHSIG
+ if (prctl(PR_SET_PDEATHSIG, signum) < 0)
+ elog(ERROR, "could not request parent death signal: %m");
+#endif
+
+ /*
+ * Just in case the parent was gone already and we missed it, we'd
+ * better check the slow way.
+ */
+ if (!PostmasterIsAliveInternal())
+ postmaster_possibly_dead = true;
+#endif
+}
diff --git a/src/include/c.h b/src/include/c.h
index 95e9aeded9d..116c2a24814 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1155,6 +1155,17 @@ extern int fdatasync(int fildes);
#define NON_EXEC_STATIC static
#endif
+/*
+ * Do we have a way to ask for a signal on parent death? Build with
+ * NO_POSTMASTER_DEATH_SIGNAL defined to inhibit the use of death signals on
+ * platforms that support it, for testing purposes.
+ */
+#ifndef NO_POSTMASTER_DEATH_SIGNAL
+#if defined(HAVE_PR_SET_PDEATHSIG)
+#define USE_POSTMASTER_DEATH_SIGNAL
+#endif
+#endif
+
/* /port compatibility functions */
#include "port.h"
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index f3620231a71..5454f0d4807 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -428,6 +428,9 @@
/* Define to 1 if the assembler supports PPC's LWARX mutex hint bit. */
#undef HAVE_PPC_LWARX_MUTEX_HINT
+/* Define to 1 if the system supports PR_SET_PDEATHSIG */
+#undef HAVE_PR_SET_PDEATHSIG
+
/* Define to 1 if you have the `pstat' function. */
#undef HAVE_PSTAT
diff --git a/src/include/storage/pmsignal.h b/src/include/storage/pmsignal.h
index bec162cc16d..6667853bdc3 100644
--- a/src/include/storage/pmsignal.h
+++ b/src/include/storage/pmsignal.h
@@ -51,6 +51,21 @@ extern bool IsPostmasterChildWalSender(int slot);
extern void MarkPostmasterChildActive(void);
extern void MarkPostmasterChildInactive(void);
extern void MarkPostmasterChildWalSender(void);
-extern bool PostmasterIsAlive(void);
+extern bool PostmasterIsAliveInternal(void);
+extern void PostmasterDeathInit(void);
+
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+extern sig_atomic_t postmaster_possibly_dead;
+#endif
+
+static inline bool
+PostmasterIsAlive(void)
+{
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+ if (likely(!postmaster_possibly_dead))
+ return true;
+#endif
+ return PostmasterIsAliveInternal();
+}
#endif /* PMSIGNAL_H */
--
2.16.2
0002-Use-signals-for-postmaster-death-on-FreeBSD-v3.patchapplication/octet-stream; name=0002-Use-signals-for-postmaster-death-on-FreeBSD-v3.patchDownload
From f4094f8dff8465b3ca53afa7a517276c5bd178b2 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@enterprisedb.com>
Date: Wed, 18 Apr 2018 16:17:54 +1200
Subject: [PATCH 2/2] Use signals for postmaster death on FreeBSD.
Use FreeBSD's new support for detecting parent process death to make
PostmasterIsAlive() very cheap, as was done earlier for Linux.
NB: This works on FreeBSD 12.0-CURRENT as of today, but the interface
might change. It'll hopefully be nailed down and land in FreeBSD 11.2-RELEASE,
due out in June/July, in time for the first PostgreSQL 12 commitfest.
I'm sharing this patch now just to justify the way the macros are laid
out in the earlier patch, supporting more than one implementation of death
signals; it's also good for bleeding edge FreeBSD users to test.
Author: Thomas Munro
Discussion: https://postgr.es/m/7261eb39-0369-f2f4-1bb5-62f3b6083b5e@iki.fi
---
configure | 29 +++++++++++++++++++++++++++++
configure.in | 13 +++++++++++++
src/backend/storage/ipc/pmsignal.c | 7 +++++++
src/include/c.h | 2 +-
src/include/pg_config.h.in | 3 +++
5 files changed, 53 insertions(+), 1 deletion(-)
diff --git a/configure b/configure
index 29ba38ee174..894008e9af2 100755
--- a/configure
+++ b/configure
@@ -9746,12 +9746,41 @@ if ac_fn_c_try_compile "$LINENO"; then :
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+# FreeBSD 12.0 has procctl(..., PROC_PDEATHSIG_CTL, ...)
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+#include <sys/procctl.h>
+
+int
+main ()
+{
+
+#ifndef PROC_PDEATHSIG_CTL
+#error PROC_PDEATHSIG_CTL not defined
+#endif
+
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+ HAVE_PROC_PDEATHSIG_CTL=1
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+
if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: PR_SET_PDEATHSIG" >&5
$as_echo "PR_SET_PDEATHSIG" >&6; }
$as_echo "#define HAVE_PR_SET_PDEATHSIG 1" >>confdefs.h
+elif test x"$HAVE_PROC_PDEATHSIG_CTL" = "x1"; then
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: PROC_PDEATHSIG_CTL" >&5
+$as_echo "PROC_PDEATHSIG_CTL" >&6; }
+
+$as_echo "#define HAVE_PROC_PDEATHSIG_CTL 1" >>confdefs.h
+
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
diff --git a/configure.in b/configure.in
index 31dda503628..e19a1c645e9 100644
--- a/configure.in
+++ b/configure.in
@@ -1036,10 +1036,23 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
#endif
])], [HAVE_PR_SET_PDEATHSIG=1])
+# FreeBSD 12.0 has procctl(..., PROC_PDEATHSIG_CTL, ...)
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
+#include <sys/procctl.h>
+], [
+#ifndef PROC_PDEATHSIG_CTL
+#error PROC_PDEATHSIG_CTL not defined
+#endif
+])], [HAVE_PROC_PDEATHSIG_CTL=1])
+
if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
AC_MSG_RESULT(PR_SET_PDEATHSIG)
AC_DEFINE(HAVE_PR_SET_PDEATHSIG, 1,
[Define to 1 if the system supports PR_SET_PDEATHSIG])
+elif test x"$HAVE_PROC_PDEATHSIG_CTL" = "x1"; then
+ AC_MSG_RESULT(PROC_PDEATHSIG_CTL)
+ AC_DEFINE(HAVE_PROC_PDEATHSIG_CTL, 1,
+ [Define to 1 if the system supports PROC_PDEATHSIG_CTL])
else
AC_MSG_RESULT(no)
fi
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index c415b8b5dbb..6a00ef6c2ff 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -27,6 +27,10 @@
#include <sys/prctl.h>
#endif
+#if defined(HAVE_PROC_PDEATHSIG_CTL)
+#include <sys/procctl.h>
+#endif
+
/*
* The postmaster is signaled by its children by sending SIGUSR1. The
* specific reason is communicated via flags in shared memory. We keep
@@ -334,6 +338,9 @@ PostmasterDeathInit(void)
#ifdef HAVE_PR_SET_PDEATHSIG
if (prctl(PR_SET_PDEATHSIG, signum) < 0)
elog(ERROR, "could not request parent death signal: %m");
+#elif defined(HAVE_PROC_PDEATHSIG_CTL)
+ if (procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &signum) < 0)
+ elog(ERROR, "could not request parent death signal: %m");
#endif
/*
diff --git a/src/include/c.h b/src/include/c.h
index 116c2a24814..4a0dccac559 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1161,7 +1161,7 @@ extern int fdatasync(int fildes);
* platforms that support it, for testing purposes.
*/
#ifndef NO_POSTMASTER_DEATH_SIGNAL
-#if defined(HAVE_PR_SET_PDEATHSIG)
+#if defined(HAVE_PR_SET_PDEATHSIG) || defined(HAVE_PROC_PDEATHSIG_CTL)
#define USE_POSTMASTER_DEATH_SIGNAL
#endif
#endif
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 5454f0d4807..52103a86ef2 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -428,6 +428,9 @@
/* Define to 1 if the assembler supports PPC's LWARX mutex hint bit. */
#undef HAVE_PPC_LWARX_MUTEX_HINT
+/* Define to 1 if the system supports PROC_PDEATHSIG_CTL */
+#undef HAVE_PROC_PDEATHSIG_CTL
+
/* Define to 1 if the system supports PR_SET_PDEATHSIG */
#undef HAVE_PR_SET_PDEATHSIG
--
2.16.2
On Sat, Apr 21, 2018 at 12:25:27PM +1200, Thomas Munro wrote:
Here's a new version, because FreeBSD's new interface changed slightly.
I have been looking at the proposed set for Linux, and the numbers are
here. By replaying 1GB worth of WAL after a pgbench run with the data
folder on a tmpfs the recovery time goes from 33s to 28s, so that's a
nice gain.
Do you have numbers with FreeBSD? I get that this would be more
difficult to set up without a GA release perhaps...
I can also see the difference in profiles by looking for
HandleStartupProcInterrupts which gets close 10% of the attention when
unpatched, and down to 0.1% when patched.
@@ -2484,6 +2484,8 @@ ClosePostmasterPorts(bool am_syslogger)
if (bonjour_sdref)
close(DNSServiceRefSockFD(bonjour_sdref));
#endif
+
+ PostmasterDeathInit();
Thomas, trying to understand here... Why this place for the signal
initialization? Wouldn't InitPostmasterChild() be a more logical place
as we'd want to have this logic caught by all other processes?
--
Michael
On Tue, Apr 24, 2018 at 7:37 PM, Michael Paquier <michael@paquier.xyz> wrote:
I have been looking at the proposed set for Linux, and the numbers are
here. By replaying 1GB worth of WAL after a pgbench run with the data
folder on a tmpfs the recovery time goes from 33s to 28s, so that's a
nice gain.
Thanks for testing.
Do you have numbers with FreeBSD? I get that this would be more
difficult to set up without a GA release perhaps...
I don't have production build numbers, but a similar test to yours
went from 91s to 61s on a debug kernel in a virtual machine.
I can also see the difference in profiles by looking for
HandleStartupProcInterrupts which gets close 10% of the attention when
unpatched, and down to 0.1% when patched.@@ -2484,6 +2484,8 @@ ClosePostmasterPorts(bool am_syslogger) if (bonjour_sdref) close(DNSServiceRefSockFD(bonjour_sdref)); #endif + + PostmasterDeathInit();Thomas, trying to understand here... Why this place for the signal
initialization? Wouldn't InitPostmasterChild() be a more logical place
as we'd want to have this logic caught by all other processes?
Yeah, you're right. Here's one like that.
--
Thomas Munro
http://www.enterprisedb.com
Attachments:
0001-Use-signals-for-postmaster-death-on-Linux-v4.patchapplication/octet-stream; name=0001-Use-signals-for-postmaster-death-on-Linux-v4.patchDownload
From 400fed6311086fc7fec4463fe5c093c0931cc5ba Mon Sep 17 00:00:00 2001
From: Thomas Munro <munro@ip9.org>
Date: Wed, 18 Apr 2018 00:59:39 +0100
Subject: [PATCH 1/2] Use signals for postmaster death on Linux.
Linux provides a way to ask for a signal when your parent process dies. Use
that to make PostmasterIsAlive() very cheap.
Author: Thomas Munro, based on a suggestion from Andres Freund
Reviewed-By: Michael Paquier
Discussion: https://postgr.es/m/7261eb39-0369-f2f4-1bb5-62f3b6083b5e%40iki.fi
https://postgr.es/m/20180411002643.6buofht4ranhei7k%40alap3.anarazel.de
---
configure | 40 +++++++++++++++++++++++++++
configure.in | 22 +++++++++++++++
src/backend/storage/ipc/latch.c | 6 ++---
src/backend/storage/ipc/pmsignal.c | 55 +++++++++++++++++++++++++++++++++++++-
src/backend/utils/init/miscinit.c | 4 +++
src/include/c.h | 11 ++++++++
src/include/pg_config.h.in | 3 +++
src/include/storage/pmsignal.h | 17 +++++++++++-
8 files changed, 153 insertions(+), 5 deletions(-)
diff --git a/configure b/configure
index 56f18dfbc26..29ba38ee174 100755
--- a/configure
+++ b/configure
@@ -9717,6 +9717,46 @@ program to use during the build." "$LINENO" 5
fi
fi
+#
+# Signals to detect parent process death
+#
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for parent death signal support" >&5
+$as_echo_n "checking for parent death signal support... " >&6; }
+
+# Linux has prctl(PR_SET_PDEATHSIG, ...)
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+#include <sys/prctl.h>
+
+int
+main ()
+{
+
+#ifndef PR_SET_PDEATHSIG
+#error PR_SET_PDEATHSIG not defined
+#endif
+
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+ HAVE_PR_SET_PDEATHSIG=1
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+
+if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: PR_SET_PDEATHSIG" >&5
+$as_echo "PR_SET_PDEATHSIG" >&6; }
+
+$as_echo "#define HAVE_PR_SET_PDEATHSIG 1" >>confdefs.h
+
+else
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
#
# Pthreads
#
diff --git a/configure.in b/configure.in
index da02a56ec66..31dda503628 100644
--- a/configure.in
+++ b/configure.in
@@ -1022,6 +1022,28 @@ program to use during the build.])
fi
fi
+#
+# Signals to detect parent process death
+#
+AC_MSG_CHECKING([for parent death signal support])
+
+# Linux has prctl(PR_SET_PDEATHSIG, ...)
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
+#include <sys/prctl.h>
+], [
+#ifndef PR_SET_PDEATHSIG
+#error PR_SET_PDEATHSIG not defined
+#endif
+])], [HAVE_PR_SET_PDEATHSIG=1])
+
+if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
+ AC_MSG_RESULT(PR_SET_PDEATHSIG)
+ AC_DEFINE(HAVE_PR_SET_PDEATHSIG, 1,
+ [Define to 1 if the system supports PR_SET_PDEATHSIG])
+else
+ AC_MSG_RESULT(no)
+fi
+
#
# Pthreads
#
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index e6706f7fb80..f6dda9cc9ac 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1112,7 +1112,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
* cost much.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
@@ -1230,7 +1230,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
* cost much.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
@@ -1390,7 +1390,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* even though there is no known reason to think that the event could
* be falsely set on Windows.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index be61858fc67..082da9351be 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -23,6 +23,9 @@
#include "storage/pmsignal.h"
#include "storage/shmem.h"
+#if defined(HAVE_PR_SET_PDEATHSIG)
+#include <sys/prctl.h>
+#endif
/*
* The postmaster is signaled by its children by sending SIGUSR1. The
@@ -71,6 +74,28 @@ struct PMSignalData
NON_EXEC_STATIC volatile PMSignalData *PMSignalState = NULL;
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+sig_atomic_t postmaster_possibly_dead = false;
+
+static void
+postmaster_death_handler(int signo)
+{
+ postmaster_possibly_dead = true;
+}
+
+/*
+ * The available signals depends on the OS and architecture. SIGUSR1 and
+ * SIGUSR2 are already used for other things, so choose another one.
+ */
+#if defined(SIGINFO)
+#define POSTMASTER_DEATH_SIGNAL SIGINFO
+#elif defined(SIGPWR)
+#define POSTMASTER_DEATH_SIGNAL SIGPWR
+#else
+#error "cannot find a signal to use for postmaster death"
+#endif
+
+#endif
/*
* PMSignalShmemSize
@@ -269,7 +294,7 @@ MarkPostmasterChildInactive(void)
* PostmasterIsAlive - check whether postmaster process is still alive
*/
bool
-PostmasterIsAlive(void)
+PostmasterIsAliveInternal(void)
{
#ifndef WIN32
char c;
@@ -291,3 +316,31 @@ PostmasterIsAlive(void)
return (WaitForSingleObject(PostmasterHandle, 0) == WAIT_TIMEOUT);
#endif /* WIN32 */
}
+
+/*
+ * PostmasterDeathSignalInit - request signal on postmaster death if possible
+ */
+void
+PostmasterDeathSignalInit(void)
+{
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+ int signum;
+
+ /* Register our signal handler. */
+ signum = POSTMASTER_DEATH_SIGNAL;
+ pqsignal(signum, postmaster_death_handler);
+
+ /* Request a signal on parent exit. */
+#ifdef HAVE_PR_SET_PDEATHSIG
+ if (prctl(PR_SET_PDEATHSIG, signum) < 0)
+ elog(ERROR, "could not request parent death signal: %m");
+#endif
+
+ /*
+ * Just in case the parent was gone already and we missed it, we'd
+ * better check the slow way.
+ */
+ if (!PostmasterIsAliveInternal())
+ postmaster_possibly_dead = true;
+#endif
+}
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index 03b28c3604a..4bb28938c27 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -43,6 +43,7 @@
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/pg_shmem.h"
+#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/builtins.h"
@@ -304,6 +305,9 @@ InitPostmasterChild(void)
if (setsid() < 0)
elog(FATAL, "setsid() failed: %m");
#endif
+
+ /* Request a signal if the postmaster dies, if possible. */
+ PostmasterDeathSignalInit();
}
/*
diff --git a/src/include/c.h b/src/include/c.h
index 95e9aeded9d..116c2a24814 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1155,6 +1155,17 @@ extern int fdatasync(int fildes);
#define NON_EXEC_STATIC static
#endif
+/*
+ * Do we have a way to ask for a signal on parent death? Build with
+ * NO_POSTMASTER_DEATH_SIGNAL defined to inhibit the use of death signals on
+ * platforms that support it, for testing purposes.
+ */
+#ifndef NO_POSTMASTER_DEATH_SIGNAL
+#if defined(HAVE_PR_SET_PDEATHSIG)
+#define USE_POSTMASTER_DEATH_SIGNAL
+#endif
+#endif
+
/* /port compatibility functions */
#include "port.h"
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index f3620231a71..5454f0d4807 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -428,6 +428,9 @@
/* Define to 1 if the assembler supports PPC's LWARX mutex hint bit. */
#undef HAVE_PPC_LWARX_MUTEX_HINT
+/* Define to 1 if the system supports PR_SET_PDEATHSIG */
+#undef HAVE_PR_SET_PDEATHSIG
+
/* Define to 1 if you have the `pstat' function. */
#undef HAVE_PSTAT
diff --git a/src/include/storage/pmsignal.h b/src/include/storage/pmsignal.h
index bec162cc16d..d55e30e4156 100644
--- a/src/include/storage/pmsignal.h
+++ b/src/include/storage/pmsignal.h
@@ -51,6 +51,21 @@ extern bool IsPostmasterChildWalSender(int slot);
extern void MarkPostmasterChildActive(void);
extern void MarkPostmasterChildInactive(void);
extern void MarkPostmasterChildWalSender(void);
-extern bool PostmasterIsAlive(void);
+extern bool PostmasterIsAliveInternal(void);
+extern void PostmasterDeathSignalInit(void);
+
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+extern sig_atomic_t postmaster_possibly_dead;
+#endif
+
+static inline bool
+PostmasterIsAlive(void)
+{
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+ if (likely(!postmaster_possibly_dead))
+ return true;
+#endif
+ return PostmasterIsAliveInternal();
+}
#endif /* PMSIGNAL_H */
--
2.16.2
0002-Use-signals-for-postmaster-death-on-FreeBSD-v4.patchapplication/octet-stream; name=0002-Use-signals-for-postmaster-death-on-FreeBSD-v4.patchDownload
From e6e5447dc453788d0e9986cf191c91b8199f0ff3 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@enterprisedb.com>
Date: Wed, 18 Apr 2018 16:17:54 +1200
Subject: [PATCH 2/2] Use signals for postmaster death on FreeBSD.
Use FreeBSD's new support for detecting parent process death to make
PostmasterIsAlive() very cheap, as was done earlier for Linux.
NB: This works on FreeBSD 12.0-CURRENT as of today, but the interface
might change. It'll hopefully be nailed down and land in FreeBSD 11.2-RELEASE,
due out in June/July, in time for the first PostgreSQL 12 commitfest.
I'm sharing this patch now just to justify the way the macros are laid
out in the earlier patch, supporting more than one implementation of death
signals; it's also good for bleeding edge FreeBSD users to test.
Author: Thomas Munro
Discussion: https://postgr.es/m/7261eb39-0369-f2f4-1bb5-62f3b6083b5e@iki.fi
---
configure | 29 +++++++++++++++++++++++++++++
configure.in | 13 +++++++++++++
src/backend/storage/ipc/pmsignal.c | 7 +++++++
src/include/c.h | 2 +-
src/include/pg_config.h.in | 3 +++
5 files changed, 53 insertions(+), 1 deletion(-)
diff --git a/configure b/configure
index 29ba38ee174..894008e9af2 100755
--- a/configure
+++ b/configure
@@ -9746,12 +9746,41 @@ if ac_fn_c_try_compile "$LINENO"; then :
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+# FreeBSD 12.0 has procctl(..., PROC_PDEATHSIG_CTL, ...)
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+#include <sys/procctl.h>
+
+int
+main ()
+{
+
+#ifndef PROC_PDEATHSIG_CTL
+#error PROC_PDEATHSIG_CTL not defined
+#endif
+
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+ HAVE_PROC_PDEATHSIG_CTL=1
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+
if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: PR_SET_PDEATHSIG" >&5
$as_echo "PR_SET_PDEATHSIG" >&6; }
$as_echo "#define HAVE_PR_SET_PDEATHSIG 1" >>confdefs.h
+elif test x"$HAVE_PROC_PDEATHSIG_CTL" = "x1"; then
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: PROC_PDEATHSIG_CTL" >&5
+$as_echo "PROC_PDEATHSIG_CTL" >&6; }
+
+$as_echo "#define HAVE_PROC_PDEATHSIG_CTL 1" >>confdefs.h
+
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
diff --git a/configure.in b/configure.in
index 31dda503628..e19a1c645e9 100644
--- a/configure.in
+++ b/configure.in
@@ -1036,10 +1036,23 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
#endif
])], [HAVE_PR_SET_PDEATHSIG=1])
+# FreeBSD 12.0 has procctl(..., PROC_PDEATHSIG_CTL, ...)
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
+#include <sys/procctl.h>
+], [
+#ifndef PROC_PDEATHSIG_CTL
+#error PROC_PDEATHSIG_CTL not defined
+#endif
+])], [HAVE_PROC_PDEATHSIG_CTL=1])
+
if test x"$HAVE_PR_SET_PDEATHSIG" = "x1"; then
AC_MSG_RESULT(PR_SET_PDEATHSIG)
AC_DEFINE(HAVE_PR_SET_PDEATHSIG, 1,
[Define to 1 if the system supports PR_SET_PDEATHSIG])
+elif test x"$HAVE_PROC_PDEATHSIG_CTL" = "x1"; then
+ AC_MSG_RESULT(PROC_PDEATHSIG_CTL)
+ AC_DEFINE(HAVE_PROC_PDEATHSIG_CTL, 1,
+ [Define to 1 if the system supports PROC_PDEATHSIG_CTL])
else
AC_MSG_RESULT(no)
fi
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index 082da9351be..77065c39b13 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -27,6 +27,10 @@
#include <sys/prctl.h>
#endif
+#if defined(HAVE_PROC_PDEATHSIG_CTL)
+#include <sys/procctl.h>
+#endif
+
/*
* The postmaster is signaled by its children by sending SIGUSR1. The
* specific reason is communicated via flags in shared memory. We keep
@@ -334,6 +338,9 @@ PostmasterDeathSignalInit(void)
#ifdef HAVE_PR_SET_PDEATHSIG
if (prctl(PR_SET_PDEATHSIG, signum) < 0)
elog(ERROR, "could not request parent death signal: %m");
+#elif defined(HAVE_PROC_PDEATHSIG_CTL)
+ if (procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &signum) < 0)
+ elog(ERROR, "could not request parent death signal: %m");
#endif
/*
diff --git a/src/include/c.h b/src/include/c.h
index 116c2a24814..4a0dccac559 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1161,7 +1161,7 @@ extern int fdatasync(int fildes);
* platforms that support it, for testing purposes.
*/
#ifndef NO_POSTMASTER_DEATH_SIGNAL
-#if defined(HAVE_PR_SET_PDEATHSIG)
+#if defined(HAVE_PR_SET_PDEATHSIG) || defined(HAVE_PROC_PDEATHSIG_CTL)
#define USE_POSTMASTER_DEATH_SIGNAL
#endif
#endif
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 5454f0d4807..52103a86ef2 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -428,6 +428,9 @@
/* Define to 1 if the assembler supports PPC's LWARX mutex hint bit. */
#undef HAVE_PPC_LWARX_MUTEX_HINT
+/* Define to 1 if the system supports PROC_PDEATHSIG_CTL */
+#undef HAVE_PROC_PDEATHSIG_CTL
+
/* Define to 1 if the system supports PR_SET_PDEATHSIG */
#undef HAVE_PR_SET_PDEATHSIG
--
2.16.2
On Wed, Apr 25, 2018 at 6:23 PM, Thomas Munro
<thomas.munro@enterprisedb.com> wrote:
On Tue, Apr 24, 2018 at 7:37 PM, Michael Paquier <michael@paquier.xyz> wrote:
Thomas, trying to understand here... Why this place for the signal
initialization? Wouldn't InitPostmasterChild() be a more logical place
as we'd want to have this logic caught by all other processes?Yeah, you're right. Here's one like that.
Amazingly, due to the way the project schedules fell and thanks to the
help of a couple of very supportive FreeBSD committers and reviewers,
the new PROC_PDEATHSIG_CTL kernel facility[1]https://www.freebsd.org/cgi/man.cgi?query=procctl&apropos=0&sektion=2&manpath=FreeBSD+11.2-RELEASE&arch=default&format=html landed in a production
release today, beating the Commitfest by several days.
My question is whether this patch set is enough, or people (Andres?) want
to go further and actually kill the postmaster death pipe completely.
My kqueue patch would almost completely kill it on BSDs as it happens,
but for Linux there are a number of races and complications to plug to
do that IIUC. I don't immediately see what there is to gain by doing
that work (assuming we reuse WaitEventSet objects in enough places),
and we'll always need to maintain the pipe code for other OSes anyway.
So I'm -0.5 on doing that, even though the technical puzzle is
appealing...
--
Thomas Munro
http://www.enterprisedb.com
On 27/06/18 08:26, Thomas Munro wrote:
On Wed, Apr 25, 2018 at 6:23 PM, Thomas Munro
<thomas.munro@enterprisedb.com> wrote:On Tue, Apr 24, 2018 at 7:37 PM, Michael Paquier <michael@paquier.xyz> wrote:
Thomas, trying to understand here... Why this place for the signal
initialization? Wouldn't InitPostmasterChild() be a more logical place
as we'd want to have this logic caught by all other processes?Yeah, you're right. Here's one like that.
Amazingly, due to the way the project schedules fell and thanks to the
help of a couple of very supportive FreeBSD committers and reviewers,
the new PROC_PDEATHSIG_CTL kernel facility[1] landed in a production
release today, beating the Commitfest by several days.My question is whether this patch set is enough, or people (Andres?) want
to go further and actually kill the postmaster death pipe completely.
My kqueue patch would almost completely kill it on BSDs as it happens,
but for Linux there are a number of races and complications to plug to
do that IIUC. I don't immediately see what there is to gain by doing
that work (assuming we reuse WaitEventSet objects in enough places),
and we'll always need to maintain the pipe code for other OSes anyway.
So I'm -0.5 on doing that, even though the technical puzzle is
appealing...
Yeah, I don't think we can kill the pipe completely. As long as we still
need it for the other OSes, I don't see much point in eliminating it on
Linux and BSDs either. I'd rather keep the code similar across platforms.
Looking at the patch:
The 'postmaster_possibly_dead' flag is not reset anywhere. So if a
process receives a spurious death signal, even though postmaster is
still alive, PostmasterIsAlive() will continue to use the slow path.
postmaster_possibly_dead needs to be marked as 'volatile', no?
The autoconf check for PR_SET_PDEATHSIG seems slightly misplaced. And I
think we can simplify it with AC_CHECK_HEADER(). I'd also like to avoid
adding code to c.h for this, that seems too global.
After some kibitzing, I ended up with the attached. It fixes the
postmaster_possible_dead issues mentioned above, and moves around the
autoconf and #ifdef logic a bit to make it a bit nicer, at least in my
opinion. I don't have a FreeBSD machine at hand, so I didn't try fixing
that patch.
- Heikki
Attachments:
0001-Use-signals-for-postmaster-death-on-Linux-2.patchtext/x-patch; name=0001-Use-signals-for-postmaster-death-on-Linux-2.patchDownload
From 9418ed472d113a80bf0fbb209efb0835767a5c50 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Tue, 10 Jul 2018 14:31:48 +0300
Subject: [PATCH 1/1] Use signals for postmaster death on Linux.
Linux provides a way to ask for a signal when your parent process dies.
Use that to make PostmasterIsAlive() very cheap.
Author: Thomas Munro, based on a suggestion from Andres Freund
Reviewed-By: Michael Paquier
Discussion: https://postgr.es/m/7261eb39-0369-f2f4-1bb5-62f3b6083b5e%40iki.fi
Discussion: https://postgr.es/m/20180411002643.6buofht4ranhei7k%40alap3.anarazel.de
---
configure | 2 +-
configure.in | 2 +-
src/backend/storage/ipc/latch.c | 6 +-
src/backend/storage/ipc/pmsignal.c | 123 +++++++++++++++++++++++++++++++++----
src/backend/utils/init/miscinit.c | 4 ++
src/include/pg_config.h.in | 3 +
src/include/pg_config.h.win32 | 3 +
src/include/storage/pmsignal.h | 33 +++++++++-
8 files changed, 157 insertions(+), 19 deletions(-)
diff --git a/configure b/configure
index 1bc465b942..41e0e1cf34 100755
--- a/configure
+++ b/configure
@@ -12494,7 +12494,7 @@ $as_echo "#define HAVE_STDBOOL_H 1" >>confdefs.h
fi
-for ac_header in atomic.h crypt.h dld.h fp_class.h getopt.h ieeefp.h ifaddrs.h langinfo.h mbarrier.h poll.h sys/epoll.h sys/ipc.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/shm.h sys/sockio.h sys/tas.h sys/un.h termios.h ucred.h utime.h wchar.h wctype.h
+for ac_header in atomic.h crypt.h dld.h fp_class.h getopt.h ieeefp.h ifaddrs.h langinfo.h mbarrier.h poll.h sys/epoll.h sys/ipc.h sys/prctl.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/shm.h sys/sockio.h sys/tas.h sys/un.h termios.h ucred.h utime.h wchar.h wctype.h
do :
as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default"
diff --git a/configure.in b/configure.in
index a6b3b88cfa..1e76c9ee46 100644
--- a/configure.in
+++ b/configure.in
@@ -1260,7 +1260,7 @@ AC_SUBST(UUID_LIBS)
AC_HEADER_STDBOOL
-AC_CHECK_HEADERS([atomic.h crypt.h dld.h fp_class.h getopt.h ieeefp.h ifaddrs.h langinfo.h mbarrier.h poll.h sys/epoll.h sys/ipc.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/shm.h sys/sockio.h sys/tas.h sys/un.h termios.h ucred.h utime.h wchar.h wctype.h])
+AC_CHECK_HEADERS([atomic.h crypt.h dld.h fp_class.h getopt.h ieeefp.h ifaddrs.h langinfo.h mbarrier.h poll.h sys/epoll.h sys/ipc.h sys/prctl.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/shm.h sys/sockio.h sys/tas.h sys/un.h termios.h ucred.h utime.h wchar.h wctype.h])
# On BSD, test for net/if.h will fail unless sys/socket.h
# is included first.
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index e6706f7fb8..f6dda9cc9a 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -1112,7 +1112,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
* cost much.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
@@ -1230,7 +1230,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
* cost much.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
@@ -1390,7 +1390,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
* even though there is no known reason to think that the event could
* be falsely set on Windows.
*/
- if (!PostmasterIsAlive())
+ if (!PostmasterIsAliveInternal())
{
occurred_events->fd = PGINVALID_SOCKET;
occurred_events->events = WL_POSTMASTER_DEATH;
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index be61858fc6..c20549cacc 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -17,6 +17,10 @@
#include <signal.h>
#include <unistd.h>
+#ifdef HAVE_SYS_PRCTL_H
+#include <sys/prctl.h>
+#endif
+
#include "miscadmin.h"
#include "postmaster/postmaster.h"
#include "replication/walsender.h"
@@ -71,6 +75,35 @@ struct PMSignalData
NON_EXEC_STATIC volatile PMSignalData *PMSignalState = NULL;
+/*
+ * Signal handler to be notified if postmaster dies.
+ */
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+volatile sig_atomic_t postmaster_possibly_dead = false;
+
+static void
+postmaster_death_handler(int signo)
+{
+ postmaster_possibly_dead = true;
+}
+
+/*
+ * The available signals depend on the OS. SIGUSR1 and SIGUSR2 are already
+ * used for other things, so choose another one.
+ *
+ * Currently, we assume that we can always find a signal to use. That
+ * seems like a reasonable assumption for all platforms that are modern
+ * enough to have a parent-death signaling mechanism.
+ */
+#if defined(SIGINFO)
+#define POSTMASTER_DEATH_SIGNAL SIGINFO
+#elif defined(SIGPWR)
+#define POSTMASTER_DEATH_SIGNAL SIGPWR
+#else
+#error "cannot find a signal to use for postmaster death"
+#endif
+
+#endif /* USE_POSTMASTER_DEATH_SIGNAL */
/*
* PMSignalShmemSize
@@ -266,28 +299,92 @@ MarkPostmasterChildInactive(void)
/*
- * PostmasterIsAlive - check whether postmaster process is still alive
+ * PostmasterIsAliveInternal - check whether postmaster process is still alive
+ *
+ * This is the slow path of PostmasterIsAlive(), where the caller has already
+ * checked 'postmaster_possibly_dead'. (On platforms that don't support
+ * a signal for parent death, PostmasterIsAlive() is just an alias for this.)
*/
bool
-PostmasterIsAlive(void)
+PostmasterIsAliveInternal(void)
{
-#ifndef WIN32
- char c;
- ssize_t rc;
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+ /*
+ * Reset the flag before checking, so that we don't miss a signal if
+ * postmaster dies right after the check. If postmaster was indeed dead,
+ * we'll re-arm it before returning to caller.
+ */
+ postmaster_possibly_dead = false;
+#endif
- rc = read(postmaster_alive_fds[POSTMASTER_FD_WATCH], &c, 1);
- if (rc < 0)
+#ifndef WIN32
{
- if (errno == EAGAIN || errno == EWOULDBLOCK)
+ char c;
+ ssize_t rc;
+
+ rc = read(postmaster_alive_fds[POSTMASTER_FD_WATCH], &c, 1);
+
+ /*
+ * In the usual case, the postmaster is still alive, and there is no
+ * data in the pipe.
+ */
+ if (rc < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
return true;
else
- elog(FATAL, "read on postmaster death monitoring pipe failed: %m");
+ {
+ /*
+ * Postmaster is dead, or something went wrong with the read()
+ * call.
+ */
+
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+ postmaster_possibly_dead = true;
+#endif
+
+ if (rc < 0)
+ elog(FATAL, "read on postmaster death monitoring pipe failed: %m");
+ else if (rc > 0)
+ elog(FATAL, "unexpected data in postmaster death monitoring pipe");
+
+ return false;
+ }
}
- else if (rc > 0)
- elog(FATAL, "unexpected data in postmaster death monitoring pipe");
- return false;
#else /* WIN32 */
- return (WaitForSingleObject(PostmasterHandle, 0) == WAIT_TIMEOUT);
+ if (WaitForSingleObject(PostmasterHandle, 0) == WAIT_TIMEOUT)
+ return true;
+ else
+ {
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+ postmaster_possibly_dead = true;
+#endif
+ return false;
+ }
#endif /* WIN32 */
}
+
+/*
+ * PostmasterDeathSignalInit - request signal on postmaster death if possible
+ */
+void
+PostmasterDeathSignalInit(void)
+{
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+ /* Register our signal handler. */
+ pqsignal(POSTMASTER_DEATH_SIGNAL, postmaster_death_handler);
+
+ /* Request a signal on parent exit. */
+#ifdef PR_SET_PDEATHSIG
+ if (prctl(PR_SET_PDEATHSIG, POSTMASTER_DEATH_SIGNAL) < 0)
+ elog(ERROR, "could not request parent death signal: %m");
+#else
+#error "USE_POSTMASTER_DEATH_SIGNAL set, but there is no mechanism to request the signal"
+#endif
+
+ /*
+ * Just in case the parent was gone already and we missed it, we'd better
+ * check the slow way on the first call.
+ */
+ postmaster_possibly_dead = true;
+#endif /* USE_POSTMASTER_DEATH_SIGNAL */
+}
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index 03b28c3604..4bb28938c2 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -43,6 +43,7 @@
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/pg_shmem.h"
+#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/builtins.h"
@@ -304,6 +305,9 @@ InitPostmasterChild(void)
if (setsid() < 0)
elog(FATAL, "setsid() failed: %m");
#endif
+
+ /* Request a signal if the postmaster dies, if possible. */
+ PostmasterDeathSignalInit();
}
/*
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index dcb25bb723..4ef5341678 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -600,6 +600,9 @@
/* Define to 1 if you have the <sys/ipc.h> header file. */
#undef HAVE_SYS_IPC_H
+/* Define to 1 if you have the <sys/prctl.h> header file. */
+#undef HAVE_SYS_PRCTL_H
+
/* Define to 1 if you have the <sys/pstat.h> header file. */
#undef HAVE_SYS_PSTAT_H
diff --git a/src/include/pg_config.h.win32 b/src/include/pg_config.h.win32
index 456b5c6b66..01cf8daf43 100644
--- a/src/include/pg_config.h.win32
+++ b/src/include/pg_config.h.win32
@@ -482,6 +482,9 @@
/* Define to 1 if you have the <sys/ipc.h> header file. */
/* #undef HAVE_SYS_IPC_H */
+/* Define to 1 if you have the <sys/prctl.h> header file. */
+/* #undef HAVE_SYS_PRCTL_H */
+
/* Define to 1 if you have the <sys/pstat.h> header file. */
/* #undef HAVE_SYS_PSTAT_H */
diff --git a/src/include/storage/pmsignal.h b/src/include/storage/pmsignal.h
index bec162cc16..54e7108ac0 100644
--- a/src/include/storage/pmsignal.h
+++ b/src/include/storage/pmsignal.h
@@ -14,6 +14,10 @@
#ifndef PMSIGNAL_H
#define PMSIGNAL_H
+#ifdef HAVE_SYS_PRCTL_H
+#include "sys/prctl.h"
+#endif
+
/*
* Reasons for signaling the postmaster. We can cope with simultaneous
* signals for different reasons. If the same reason is signaled multiple
@@ -51,6 +55,33 @@ extern bool IsPostmasterChildWalSender(int slot);
extern void MarkPostmasterChildActive(void);
extern void MarkPostmasterChildInactive(void);
extern void MarkPostmasterChildWalSender(void);
-extern bool PostmasterIsAlive(void);
+extern bool PostmasterIsAliveInternal(void);
+extern void PostmasterDeathSignalInit(void);
+
+
+/*
+ * Do we have a way to ask for a signal on parent death?
+ *
+ * If we do, pmsignal.c will set up a signal handler, that sets a flag when
+ * the parent dies. Checking the flag first makes PostmasterIsAlive() a lot
+ * cheaper in usual case that the postmaster is alive.
+ */
+#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_PDEATHSIG)
+#define USE_POSTMASTER_DEATH_SIGNAL
+#endif
+
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+extern volatile sig_atomic_t postmaster_possibly_dead;
+
+static inline bool
+PostmasterIsAlive(void)
+{
+ if (likely(!postmaster_possibly_dead))
+ return true;
+ return PostmasterIsAliveInternal();
+}
+#else
+#define PostmasterIsAlive() PostmasterIsAliveInternal()
+#endif
#endif /* PMSIGNAL_H */
--
2.11.0
On Tue, Jul 10, 2018 at 11:39 PM, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
The 'postmaster_possibly_dead' flag is not reset anywhere. So if a process
receives a spurious death signal, even though postmaster is still alive,
PostmasterIsAlive() will continue to use the slow path.
+1
postmaster_possibly_dead needs to be marked as 'volatile', no?
+1
The autoconf check for PR_SET_PDEATHSIG seems slightly misplaced. And I
think we can simplify it with AC_CHECK_HEADER(). I'd also like to avoid
adding code to c.h for this, that seems too global.
+1, much nicer, thanks.
After some kibitzing, I ended up with the attached. It fixes the
postmaster_possible_dead issues mentioned above, and moves around the
autoconf and #ifdef logic a bit to make it a bit nicer, at least in my
opinion.
Thanks, that looks good to me. I added your name as co-author and
pushed to master.
I also made a couple of minor cosmetic changes in
PostmasterDeathSignalInit() to make the follow-up patch prettier (#if
defined() instead of #ifdef, and a signum variable because I later
need its address).
I don't have a FreeBSD machine at hand, so I didn't try fixing that
patch.
I updated the FreeBSD version to use the header test approach you
showed, and pushed that too. FWIW the build farm has some FreeBSD
animals with and without PROC_PDEATHSIG_CTL.
I suppose it's possibly that we might want to reconsider the choice of
signal in the future (SIGINFO or SIGPWR).
(Random archeological note: TIL that Linux stole <sys/prctl.h> from
Irix (RIP), but it had PR_TERMCHILD instead of PR_SET_PRDEATHSIG.)
--
Thomas Munro
http://www.enterprisedb.com
On 11/07/18 04:16, Thomas Munro wrote:
On Tue, Jul 10, 2018 at 11:39 PM, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
I don't have a FreeBSD machine at hand, so I didn't try fixing that
patch.I updated the FreeBSD version to use the header test approach you
showed, and pushed that too. FWIW the build farm has some FreeBSD
animals with and without PROC_PDEATHSIG_CTL.
Thanks!
I suppose it's possibly that we might want to reconsider the choice of
signal in the future (SIGINFO or SIGPWR).
We could reuse SIGUSR1 for this. If we set the flag in SIGUSR1 handler,
then some PostmasterIsAlive() calls would take the slow path
unnecessarily, but it would probably be OK. The slow path isn't that
slow. But using SIGINFO/SIGPWR seems fine.
- Heikki