Change shutdown sequence to terminate checkpointer last
authorAndres Freund <andres@anarazel.de>
Sat, 25 Jan 2025 16:37:13 +0000 (11:37 -0500)
committerAndres Freund <andres@anarazel.de>
Sat, 25 Jan 2025 16:37:13 +0000 (11:37 -0500)
commit87a6690cc69530703b7da7e72769bae2ac5b2e77
tree4628068b7a52517436d671b93e6dfa1e3c6d270f
parent04ace176e08f2c694bb66b5b91cbd9d4d0bd77ea
Change shutdown sequence to terminate checkpointer last

The main motivation for this change is to have a process that can serialize
stats after all other processes have terminated. Serializing stats already
happens in checkpointer, even though walsenders can be active longer.

The only reason the current shutdown sequence does not actively cause problems
is that walsender currently does not generate any stats. However, there is an
upcoming patch changing that.

Another need for this change originates in the AIO patchset, where IO
workers (which, in some edge cases, can emit stats of their own) need to run
while the shutdown checkpoint is being written.

This commit changes the shutdown sequence so checkpointer is signalled (via
SIGINT) to trigger writing the shutdown checkpoint without also causing
checkpointer to exit.  Once checkpointer wrote the shutdown checkpoint it
notifies postmaster via PMSIGNAL_XLOG_IS_SHUTDOWN and waits for the
termination signal (SIGUSR2, as before).  Checkpointer now is terminated after
all children, other than dead-end children and logger, have been terminated,
tracked using the new PM_WAIT_CHECKPOINTER PMState.

Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi>
Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Reviewed-by: Nazir Bilal Yavuz <byavuz81@gmail.com>
Discussion: https://postgr.es/m/kgng5nrvnlv335evmsuvpnh354rw7qyazl73kdysev2cr2v5zu@m3cfzxicm5kp
src/backend/postmaster/checkpointer.c
src/backend/postmaster/postmaster.c
src/backend/utils/activity/wait_event_names.txt
src/include/storage/pmsignal.h