I've handled SIGTERM signal. pg_terminate_backend send signals (SIGTERM) to backend processes identified by process ID. And also, after this call I am able to track in my logs that the background worker gets terminated.
Yet, I am only able to register first 8 background workers. I am using select worker_spi1_launch(1) to launch it every time. This is why I guess there is some metadata maintained which has got to be deleted.
(Please reply below other posts, not above)
The bgworker API currently offers no way to enumerate bgworkers or unregister them from the outside. The only way to unregister a dynamic bgworker is to:
* proc_exit(0) from within the worker; or
* register it with BGW_NO_RESTART so it doesn't auto-restart in the first place.
This is a deficiency in the bgworker API, but there are workarounds in place and other things are more important for now. Just make your your workers proc_exit(0) on SIGTERM or don't register them as auto-restarting.