Make WaitForLSNReplay() issue FATAL on postmaster death
authorAlexander Korotkov <[email protected]>
Thu, 24 Oct 2024 11:38:06 +0000 (14:38 +0300)
committerAlexander Korotkov <[email protected]>
Thu, 24 Oct 2024 11:38:06 +0000 (14:38 +0300)
Reported-by: Michael Paquier
Discussion: https://postgr.es/m/ZvY2C8N4ZqgCFaLu%40paquier.xyz
Reviewed-by: Pavel Borisov
src/backend/access/transam/xlogwait.c

index eef58ce69ced4b3a8193226e818d8b1d533b26b0..353b7854dc86ba6a4548bfdc6544e1820b7e274f 100644 (file)
@@ -222,7 +222,7 @@ WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
 {
    XLogRecPtr  currentLSN;
    TimestampTz endtime = 0;
-   int         wake_events = WL_LATCH_SET | WL_EXIT_ON_PM_DEATH;
+   int         wake_events = WL_LATCH_SET | WL_POSTMASTER_DEATH;
 
    /* Shouldn't be called when shmem isn't initialized */
    Assert(waitLSNState);
@@ -313,6 +313,16 @@ WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
        rc = WaitLatch(MyLatch, wake_events, delay_ms,
                       WAIT_EVENT_WAIT_FOR_WAL_REPLAY);
 
+       /*
+        * Emergency bailout if postmaster has died.  This is to avoid the
+        * necessity for manual cleanup of all postmaster children.
+        */
+       if (rc & WL_POSTMASTER_DEATH)
+           ereport(FATAL,
+                   (errcode(ERRCODE_ADMIN_SHUTDOWN),
+                    errmsg("terminating connection due to unexpected postmaster exit"),
+                    errcontext("while waiting for LSN replay")));
+
        if (rc & WL_LATCH_SET)
            ResetLatch(MyLatch);
    }