Skip to content

Commit 28a520c

Browse files
committed
Refactor code to handle death of a backend or bgworker in postmaster
Currently, when a child process exits, the postmaster first scans through BackgroundWorkerList, to see if it the child process was a background worker. If not found, then it scans through BackendList to see if it was a regular backend. That leads to some duplication between the bgworker and regular backend cleanup code, as both have an entry in the BackendList that needs to be cleaned up in the same way. Refactor that so that we scan just the BackendList to find the child process, and if it was a background worker, do the additional bgworker-specific cleanup in addition to the normal Backend cleanup. Change HandleChildCrash so that it doesn't try to handle the cleanup of the process that already exited, only the signaling of all the other processes. When called for any of the aux processes, the caller had already cleared the *PID global variable, so the code in HandleChildCrash() to do that was unused. On Windows, if a child process exits with ERROR_WAIT_NO_CHILDREN, it's now logged with that exit code, instead of 0. Also, if a bgworker exits with ERROR_WAIT_NO_CHILDREN, it's now treated as crashed and is restarted. Previously it was treated as a normal exit. If a child process is not found in the BackendList, the log message now calls it "untracked child process" rather than "server process". Arguably that should be a PANIC, because we do track all the child processes in the list, so failing to find a child process is highly unexpected. But if we want to change that, let's discuss and do that as a separate commit. Reviewed-by: Thomas Munro <thomas.munro@gmail.com> Discussion: https://www.postgresql.org/message-id/835232c0-a5f7-4f20-b95b-5b56ba57d741@iki.fi
1 parent b43100f commit 28a520c

File tree

3 files changed

+170
-280
lines changed

3 files changed

+170
-280
lines changed

src/backend/postmaster/bgworker.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,7 @@ BackgroundWorkerStateChange(bool allow_new_workers)
401401
}
402402

403403
/* Initialize postmaster bookkeeping. */
404-
rw->rw_backend = NULL;
405404
rw->rw_pid = 0;
406-
rw->rw_child_slot = 0;
407405
rw->rw_crashed_at = 0;
408406
rw->rw_shmem_slot = slotno;
409407
rw->rw_terminate = false;
@@ -1026,9 +1024,7 @@ RegisterBackgroundWorker(BackgroundWorker *worker)
10261024
}
10271025

10281026
rw->rw_worker = *worker;
1029-
rw->rw_backend = NULL;
10301027
rw->rw_pid = 0;
1031-
rw->rw_child_slot = 0;
10321028
rw->rw_crashed_at = 0;
10331029
rw->rw_terminate = false;
10341030

0 commit comments

Comments
 (0)