Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd71421

Browse files
committed
Improve tests for postmaster death in auxiliary processes.
In checkpointer and walwriter, avoid calling PostmasterIsAlive unless WaitLatch has reported WL_POSTMASTER_DEATH. This saves a kernel call per iteration of the process's outer loop, which is not all that much, but a cycle shaved is a cycle earned. I had already removed the unconditional PostmasterIsAlive calls in bgwriter and pgstat in previous patches, but forgot that WL_POSTMASTER_DEATH is supposed to be treated as untrustworthy (per comment in unix_latch.c); so adjust those two cases to match. There are a few other places where the same idea might be applied, but only after substantial code rearrangement, so I didn't bother.
1 parent e78cc62 commit fd71421

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

src/backend/postmaster/bgwriter.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,11 @@ BackgroundWriterMain(void)
323323

324324
/*
325325
* Emergency bailout if postmaster has died. This is to avoid the
326-
* necessity for manual cleanup of all postmaster children.
326+
* necessity for manual cleanup of all postmaster children. Note
327+
* that we mustn't trust the WL_POSTMASTER_DEATH result flag entirely;
328+
* if it is set, recheck with PostmasterIsAlive before believing it.
327329
*/
328-
if (rc & WL_POSTMASTER_DEATH)
330+
if ((rc & WL_POSTMASTER_DEATH) && !PostmasterIsAlive())
329331
exit(1);
330332

331333
prev_hibernate = can_hibernate;

src/backend/postmaster/checkpointer.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -374,17 +374,11 @@ CheckpointerMain(void)
374374
pg_time_t now;
375375
int elapsed_secs;
376376
int cur_timeout;
377+
int rc;
377378

378379
/* Clear any already-pending wakeups */
379380
ResetLatch(&MyProc->procLatch);
380381

381-
/*
382-
* Emergency bailout if postmaster has died. This is to avoid the
383-
* necessity for manual cleanup of all postmaster children.
384-
*/
385-
if (!PostmasterIsAlive())
386-
exit(1);
387-
388382
/*
389383
* Process any requests or signals received recently.
390384
*/
@@ -581,9 +575,18 @@ CheckpointerMain(void)
581575
cur_timeout = Min(cur_timeout, XLogArchiveTimeout - elapsed_secs);
582576
}
583577

584-
(void) WaitLatch(&MyProc->procLatch,
585-
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
586-
cur_timeout * 1000L /* convert to ms */);
578+
rc = WaitLatch(&MyProc->procLatch,
579+
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
580+
cur_timeout * 1000L /* convert to ms */);
581+
582+
/*
583+
* Emergency bailout if postmaster has died. This is to avoid the
584+
* necessity for manual cleanup of all postmaster children. Note
585+
* that we mustn't trust the WL_POSTMASTER_DEATH result flag entirely;
586+
* if it is set, recheck with PostmasterIsAlive before believing it.
587+
*/
588+
if ((rc & WL_POSTMASTER_DEATH) && !PostmasterIsAlive())
589+
exit(1);
587590
}
588591
}
589592

src/backend/postmaster/pgstat.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3225,8 +3225,13 @@ PgstatCollectorMain(int argc, char *argv[])
32253225
pgStatSock,
32263226
-1L);
32273227

3228-
/* Check for postmaster death */
3229-
if (wr & WL_POSTMASTER_DEATH)
3228+
/*
3229+
* Emergency bailout if postmaster has died. This is to avoid the
3230+
* necessity for manual cleanup of all postmaster children. Note
3231+
* that we mustn't trust the WL_POSTMASTER_DEATH result flag entirely;
3232+
* if it is set, recheck with PostmasterIsAlive before believing it.
3233+
*/
3234+
if ((wr & WL_POSTMASTER_DEATH) && !PostmasterIsAlive())
32303235
break;
32313236
} /* end of outer loop */
32323237

src/backend/postmaster/walwriter.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ WalWriterMain(void)
246246
for (;;)
247247
{
248248
long cur_timeout;
249+
int rc;
249250

250251
/*
251252
* Advertise whether we might hibernate in this cycle. We do this
@@ -265,13 +266,6 @@ WalWriterMain(void)
265266
/* Clear any already-pending wakeups */
266267
ResetLatch(&MyProc->procLatch);
267268

268-
/*
269-
* Emergency bailout if postmaster has died. This is to avoid the
270-
* necessity for manual cleanup of all postmaster children.
271-
*/
272-
if (!PostmasterIsAlive())
273-
exit(1);
274-
275269
/*
276270
* Process any requests or signals received recently.
277271
*/
@@ -305,9 +299,18 @@ WalWriterMain(void)
305299
else
306300
cur_timeout = WalWriterDelay * HIBERNATE_FACTOR;
307301

308-
(void) WaitLatch(&MyProc->procLatch,
309-
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
310-
cur_timeout);
302+
rc = WaitLatch(&MyProc->procLatch,
303+
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
304+
cur_timeout);
305+
306+
/*
307+
* Emergency bailout if postmaster has died. This is to avoid the
308+
* necessity for manual cleanup of all postmaster children. Note
309+
* that we mustn't trust the WL_POSTMASTER_DEATH result flag entirely;
310+
* if it is set, recheck with PostmasterIsAlive before believing it.
311+
*/
312+
if ((rc & WL_POSTMASTER_DEATH) && !PostmasterIsAlive())
313+
exit(1);
311314
}
312315
}
313316

0 commit comments

Comments
 (0)