diff options
author | Thomas Munro <tmunro@postgresql.org> | 2018-11-23 20:16:41 +1300 |
---|---|---|
committer | Thomas Munro <tmunro@postgresql.org> | 2018-11-23 20:46:34 +1300 |
commit | cfdf4dc4fc9635ac8bf6eaaa5dbbcd364ab29f0c (patch) | |
tree | c1e3c40912c5f9274fb9381bea1082f6a2cc2296 /src/backend/postmaster/syslogger.c | |
parent | d392e9bdea957964e1fa6a5481e5adb5904d759a (diff) | |
download | postgresql-cfdf4dc4fc9635ac8bf6eaaa5dbbcd364ab29f0c.tar.gz postgresql-cfdf4dc4fc9635ac8bf6eaaa5dbbcd364ab29f0c.zip |
Add WL_EXIT_ON_PM_DEATH pseudo-event.
Users of the WaitEventSet and WaitLatch() APIs can now choose between
asking for WL_POSTMASTER_DEATH and then handling it explicitly, or asking
for WL_EXIT_ON_PM_DEATH to trigger immediate exit on postmaster death.
This reduces code duplication, since almost all callers want the latter.
Repair all code that was previously ignoring postmaster death completely,
or requesting the event but ignoring it, or requesting the event but then
doing an unconditional PostmasterIsAlive() call every time through its
event loop (which is an expensive syscall on platforms for which we don't
have USE_POSTMASTER_DEATH_SIGNAL support).
Assert that callers of WaitLatchXXX() under the postmaster remember to
ask for either WL_POSTMASTER_DEATH or WL_EXIT_ON_PM_DEATH, to prevent
future bugs.
The only process that doesn't handle postmaster death is syslogger. It
waits until all backends holding the write end of the syslog pipe
(including the postmaster) have closed it by exiting, to be sure to
capture any parting messages. By using the WaitEventSet API directly
it avoids the new assertion, and as a by-product it may be slightly
more efficient on platforms that have epoll().
Author: Thomas Munro
Reviewed-by: Kyotaro Horiguchi, Heikki Linnakangas, Tom Lane
Discussion: https://postgr.es/m/CAEepm%3D1TCviRykkUb69ppWLr_V697rzd1j3eZsRMmbXvETfqbQ%40mail.gmail.com,
https://postgr.es/m/CAEepm=2LqHzizbe7muD7-2yHUbTOoF7Q+qkSD5Q41kuhttRTwA@mail.gmail.com
Diffstat (limited to 'src/backend/postmaster/syslogger.c')
-rw-r--r-- | src/backend/postmaster/syslogger.c | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c index 35755138890..fbeee311092 100644 --- a/src/backend/postmaster/syslogger.c +++ b/src/backend/postmaster/syslogger.c @@ -168,6 +168,7 @@ SysLoggerMain(int argc, char *argv[]) char *currentLogFilename; int currentLogRotationAge; pg_time_t now; + WaitEventSet *wes; now = MyStartTime; @@ -294,13 +295,29 @@ SysLoggerMain(int argc, char *argv[]) */ whereToSendOutput = DestNone; + /* + * Set up a reusable WaitEventSet object we'll use to wait for our latch, + * and (except on Windows) our socket. + * + * Unlike all other postmaster child processes, we'll ignore postmaster + * death because we want to collect final log output from all backends and + * then exit last. We'll do that by running until we see EOF on the + * syslog pipe, which implies that all other backends have exited + * (including the postmaster). + */ + wes = CreateWaitEventSet(CurrentMemoryContext, 2); + AddWaitEventToSet(wes, WL_LATCH_SET, PGINVALID_SOCKET, MyLatch, NULL); +#ifndef WIN32 + AddWaitEventToSet(wes, WL_SOCKET_READABLE, syslogPipe[0], NULL, NULL); +#endif + /* main worker loop */ for (;;) { bool time_based_rotation = false; int size_rotation_for = 0; long cur_timeout; - int cur_flags; + WaitEvent event; #ifndef WIN32 int rc; @@ -436,25 +453,18 @@ SysLoggerMain(int argc, char *argv[]) } else cur_timeout = 0; - cur_flags = WL_TIMEOUT; } else - { cur_timeout = -1L; - cur_flags = 0; - } /* * Sleep until there's something to do */ #ifndef WIN32 - rc = WaitLatchOrSocket(MyLatch, - WL_LATCH_SET | WL_SOCKET_READABLE | cur_flags, - syslogPipe[0], - cur_timeout, - WAIT_EVENT_SYSLOGGER_MAIN); + rc = WaitEventSetWait(wes, cur_timeout, &event, 1, + WAIT_EVENT_SYSLOGGER_MAIN); - if (rc & WL_SOCKET_READABLE) + if (rc == 1 && event.events == WL_SOCKET_READABLE) { int bytesRead; @@ -501,10 +511,8 @@ SysLoggerMain(int argc, char *argv[]) */ LeaveCriticalSection(&sysloggerSection); - (void) WaitLatch(MyLatch, - WL_LATCH_SET | cur_flags, - cur_timeout, - WAIT_EVENT_SYSLOGGER_MAIN); + (void) WaitEventSetWait(wes, cur_timeout, &event, 1, + WAIT_EVENT_SYSLOGGER_MAIN); EnterCriticalSection(&sysloggerSection); #endif /* WIN32 */ |