From fd71421b0187de0e2bf76ff66b4a9433bd96c4a0 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 10 May 2012 00:54:32 -0400 Subject: 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. --- src/backend/postmaster/checkpointer.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'src/backend/postmaster/checkpointer.c') diff --git a/src/backend/postmaster/checkpointer.c b/src/backend/postmaster/checkpointer.c index fa3435710d7..7f8ba95c44d 100644 --- a/src/backend/postmaster/checkpointer.c +++ b/src/backend/postmaster/checkpointer.c @@ -374,17 +374,11 @@ CheckpointerMain(void) pg_time_t now; int elapsed_secs; int cur_timeout; + int rc; /* Clear any already-pending wakeups */ ResetLatch(&MyProc->procLatch); - /* - * Emergency bailout if postmaster has died. This is to avoid the - * necessity for manual cleanup of all postmaster children. - */ - if (!PostmasterIsAlive()) - exit(1); - /* * Process any requests or signals received recently. */ @@ -581,9 +575,18 @@ CheckpointerMain(void) cur_timeout = Min(cur_timeout, XLogArchiveTimeout - elapsed_secs); } - (void) WaitLatch(&MyProc->procLatch, - WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, - cur_timeout * 1000L /* convert to ms */); + rc = WaitLatch(&MyProc->procLatch, + WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, + cur_timeout * 1000L /* convert to ms */); + + /* + * Emergency bailout if postmaster has died. This is to avoid the + * necessity for manual cleanup of all postmaster children. Note + * that we mustn't trust the WL_POSTMASTER_DEATH result flag entirely; + * if it is set, recheck with PostmasterIsAlive before believing it. + */ + if ((rc & WL_POSTMASTER_DEATH) && !PostmasterIsAlive()) + exit(1); } } -- cgit v1.2.3