Skip to content

Commit cfdf4dc

Browse files
committed
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
1 parent d392e9b commit cfdf4dc

File tree

32 files changed

+174
-242
lines changed

32 files changed

+174
-242
lines changed

-

Whitespace-only changes.

contrib/pg_prewarm/autoprewarm.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ autoprewarm_main(Datum main_arg)
220220
{
221221
/* We're only dumping at shutdown, so just wait forever. */
222222
rc = WaitLatch(&MyProc->procLatch,
223-
WL_LATCH_SET | WL_POSTMASTER_DEATH,
223+
WL_LATCH_SET | WL_EXIT_ON_PM_DEATH,
224224
-1L,
225225
PG_WAIT_EXTENSION);
226226
}
@@ -249,15 +249,13 @@ autoprewarm_main(Datum main_arg)
249249

250250
/* Sleep until the next dump time. */
251251
rc = WaitLatch(&MyProc->procLatch,
252-
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
252+
WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
253253
delay_in_ms,
254254
PG_WAIT_EXTENSION);
255255
}
256256

257-
/* Reset the latch, bail out if postmaster died, otherwise loop. */
257+
/* Reset the latch, loop. */
258258
ResetLatch(&MyProc->procLatch);
259-
if (rc & WL_POSTMASTER_DEATH)
260-
proc_exit(1);
261259
}
262260

263261
/*

contrib/postgres_fdw/connection.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ pgfdw_get_result(PGconn *conn, const char *query)
546546

547547
/* Sleep until there's something to do */
548548
wc = WaitLatchOrSocket(MyLatch,
549-
WL_LATCH_SET | WL_SOCKET_READABLE,
549+
WL_LATCH_SET | WL_SOCKET_READABLE |
550+
WL_EXIT_ON_PM_DEATH,
550551
PQsocket(conn),
551552
-1L, PG_WAIT_EXTENSION);
552553
ResetLatch(MyLatch);
@@ -1152,7 +1153,8 @@ pgfdw_get_cleanup_result(PGconn *conn, TimestampTz endtime, PGresult **result)
11521153

11531154
/* Sleep until there's something to do */
11541155
wc = WaitLatchOrSocket(MyLatch,
1155-
WL_LATCH_SET | WL_SOCKET_READABLE | WL_TIMEOUT,
1156+
WL_LATCH_SET | WL_SOCKET_READABLE |
1157+
WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
11561158
PQsocket(conn),
11571159
cur_timeout, PG_WAIT_EXTENSION);
11581160
ResetLatch(MyLatch);

src/backend/access/transam/parallel.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -692,13 +692,9 @@ WaitForParallelWorkersToAttach(ParallelContext *pcxt)
692692
* just end up waiting for the same worker again.
693693
*/
694694
rc = WaitLatch(MyLatch,
695-
WL_LATCH_SET | WL_POSTMASTER_DEATH,
695+
WL_LATCH_SET | WL_EXIT_ON_PM_DEATH,
696696
-1, WAIT_EVENT_BGWORKER_STARTUP);
697697

698-
/* emergency bailout if postmaster has died */
699-
if (rc & WL_POSTMASTER_DEATH)
700-
proc_exit(1);
701-
702698
if (rc & WL_LATCH_SET)
703699
ResetLatch(MyLatch);
704700
}
@@ -815,8 +811,8 @@ WaitForParallelWorkersToFinish(ParallelContext *pcxt)
815811
}
816812
}
817813

818-
WaitLatch(MyLatch, WL_LATCH_SET, -1,
819-
WAIT_EVENT_PARALLEL_FINISH);
814+
(void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, -1,
815+
WAIT_EVENT_PARALLEL_FINISH);
820816
ResetLatch(MyLatch);
821817
}
822818

src/backend/access/transam/xlog.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6189,10 +6189,10 @@ recoveryApplyDelay(XLogReaderState *record)
61896189
elog(DEBUG2, "recovery apply delay %ld seconds, %d milliseconds",
61906190
secs, microsecs / 1000);
61916191

6192-
WaitLatch(&XLogCtl->recoveryWakeupLatch,
6193-
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
6194-
secs * 1000L + microsecs / 1000,
6195-
WAIT_EVENT_RECOVERY_APPLY_DELAY);
6192+
(void) WaitLatch(&XLogCtl->recoveryWakeupLatch,
6193+
WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
6194+
secs * 1000L + microsecs / 1000,
6195+
WAIT_EVENT_RECOVERY_APPLY_DELAY);
61966196
}
61976197
return true;
61986198
}
@@ -12093,9 +12093,11 @@ WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess,
1209312093
wait_time = wal_retrieve_retry_interval -
1209412094
(secs * 1000 + usecs / 1000);
1209512095

12096-
WaitLatch(&XLogCtl->recoveryWakeupLatch,
12097-
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
12098-
wait_time, WAIT_EVENT_RECOVERY_WAL_STREAM);
12096+
(void) WaitLatch(&XLogCtl->recoveryWakeupLatch,
12097+
WL_LATCH_SET | WL_TIMEOUT |
12098+
WL_EXIT_ON_PM_DEATH,
12099+
wait_time,
12100+
WAIT_EVENT_RECOVERY_WAL_STREAM);
1209912101
ResetLatch(&XLogCtl->recoveryWakeupLatch);
1210012102
now = GetCurrentTimestamp();
1210112103
}
@@ -12269,9 +12271,10 @@ WaitForWALToBecomeAvailable(XLogRecPtr RecPtr, bool randAccess,
1226912271
* Wait for more WAL to arrive. Time out after 5 seconds
1227012272
* to react to a trigger file promptly.
1227112273
*/
12272-
WaitLatch(&XLogCtl->recoveryWakeupLatch,
12273-
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
12274-
5000L, WAIT_EVENT_RECOVERY_WAL_ALL);
12274+
(void) WaitLatch(&XLogCtl->recoveryWakeupLatch,
12275+
WL_LATCH_SET | WL_TIMEOUT |
12276+
WL_EXIT_ON_PM_DEATH,
12277+
5000L, WAIT_EVENT_RECOVERY_WAL_ALL);
1227512278
ResetLatch(&XLogCtl->recoveryWakeupLatch);
1227612279
break;
1227712280
}

src/backend/access/transam/xlogfuncs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -764,10 +764,10 @@ pg_promote(PG_FUNCTION_ARGS)
764764

765765
CHECK_FOR_INTERRUPTS();
766766

767-
WaitLatch(MyLatch,
768-
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
769-
1000L / WAITS_PER_SECOND,
770-
WAIT_EVENT_PROMOTE);
767+
(void) WaitLatch(MyLatch,
768+
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
769+
1000L / WAITS_PER_SECOND,
770+
WAIT_EVENT_PROMOTE);
771771
}
772772

773773
ereport(WARNING,

src/backend/executor/nodeGather.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ gather_readnext(GatherState *gatherstate)
383383
return NULL;
384384

385385
/* Nothing to do except wait for developments. */
386-
WaitLatch(MyLatch, WL_LATCH_SET, 0, WAIT_EVENT_EXECUTE_GATHER);
386+
(void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0,
387+
WAIT_EVENT_EXECUTE_GATHER);
387388
ResetLatch(MyLatch);
388389
nvisited = 0;
389390
}

src/backend/libpq/be-secure-openssl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,8 @@ be_tls_open_server(Port *port)
410410
else
411411
waitfor = WL_SOCKET_WRITEABLE;
412412

413-
WaitLatchOrSocket(MyLatch, waitfor, port->sock, 0,
414-
WAIT_EVENT_SSL_OPEN_SERVER);
413+
(void) WaitLatchOrSocket(MyLatch, waitfor, port->sock, 0,
414+
WAIT_EVENT_SSL_OPEN_SERVER);
415415
goto aloop;
416416
case SSL_ERROR_SYSCALL:
417417
if (r < 0)

src/backend/libpq/pqmq.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ mq_putmessage(char msgtype, const char *s, size_t len)
168168
if (result != SHM_MQ_WOULD_BLOCK)
169169
break;
170170

171-
WaitLatch(MyLatch, WL_LATCH_SET, 0,
172-
WAIT_EVENT_MQ_PUT_MESSAGE);
171+
(void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0,
172+
WAIT_EVENT_MQ_PUT_MESSAGE);
173173
ResetLatch(MyLatch);
174174
CHECK_FOR_INTERRUPTS();
175175
}

src/backend/postmaster/autovacuum.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,6 @@ AutoVacLauncherMain(int argc, char *argv[])
628628
struct timeval nap;
629629
TimestampTz current_time = 0;
630630
bool can_launch;
631-
int rc;
632631

633632
/*
634633
* This loop is a bit different from the normal use of WaitLatch,
@@ -644,23 +643,16 @@ AutoVacLauncherMain(int argc, char *argv[])
644643
* Wait until naptime expires or we get some type of signal (all the
645644
* signal handlers will wake us by calling SetLatch).
646645
*/
647-
rc = WaitLatch(MyLatch,
648-
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
649-
(nap.tv_sec * 1000L) + (nap.tv_usec / 1000L),
650-
WAIT_EVENT_AUTOVACUUM_MAIN);
646+
(void) WaitLatch(MyLatch,
647+
WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
648+
(nap.tv_sec * 1000L) + (nap.tv_usec / 1000L),
649+
WAIT_EVENT_AUTOVACUUM_MAIN);
651650

652651
ResetLatch(MyLatch);
653652

654653
/* Process sinval catchup interrupts that happened while sleeping */
655654
ProcessCatchupInterrupt();
656655

657-
/*
658-
* Emergency bailout if postmaster has died. This is to avoid the
659-
* necessity for manual cleanup of all postmaster children.
660-
*/
661-
if (rc & WL_POSTMASTER_DEATH)
662-
proc_exit(1);
663-
664656
/* the normal shutdown case */
665657
if (got_SIGTERM)
666658
break;

0 commit comments

Comments
 (0)