Home Explore Blog CI



postgresql

6th chunk of `doc/src/sgml/bgworker.sgml`
0f395b9e942d0db3d95d78ce07e78ab1edda1e5a360fd0310000000100000be1
 <function>TerminateBackgroundWorker(<parameter>BackgroundWorkerHandle *</parameter>)</function>.
   <function>GetBackgroundWorkerPid</function> can be used to poll the status of the
   worker: a return value of <literal>BGWH_NOT_YET_STARTED</literal> indicates that
   the worker has not yet been started by the postmaster;
   <literal>BGWH_STOPPED</literal> indicates that it has been started but is
   no longer running; and <literal>BGWH_STARTED</literal> indicates that it is
   currently running.  In this last case, the PID will also be returned via the
   second argument.
   <function>TerminateBackgroundWorker</function> causes the postmaster to send
   <literal>SIGTERM</literal> to the worker if it is running, and to unregister it
   as soon as it is not.
  </para>

  <para>
   In some cases, a process which registers a background worker may wish to
   wait for the worker to start up.  This can be accomplished by initializing
   <structfield>bgw_notify_pid</structfield> to <literal>MyProcPid</literal> and
   then passing the <type>BackgroundWorkerHandle *</type> obtained at
   registration time to
   <function>WaitForBackgroundWorkerStartup(<parameter>BackgroundWorkerHandle
   *handle</parameter>, <parameter>pid_t *</parameter>)</function> function.
   This function will block until the postmaster has attempted to start the
   background worker, or until the postmaster dies.  If the background worker
   is running, the return value will be <literal>BGWH_STARTED</literal>, and
   the PID will be written to the provided address.  Otherwise, the return
   value will be <literal>BGWH_STOPPED</literal> or
   <literal>BGWH_POSTMASTER_DIED</literal>.
  </para>

  <para>
   A process can also wait for a background worker to shut down, by using the
   <function>WaitForBackgroundWorkerShutdown(<parameter>BackgroundWorkerHandle
   *handle</parameter>)</function> function and passing the
   <type>BackgroundWorkerHandle *</type> obtained at registration. This
   function will block until the background worker exits, or postmaster dies.
   When the background worker exits, the return value is
   <literal>BGWH_STOPPED</literal>, if postmaster dies it will return
   <literal>BGWH_POSTMASTER_DIED</literal>.
  </para>

  <para>
   Background workers can send asynchronous notification messages, either by
   using the <command>NOTIFY</command> command via <acronym>SPI</acronym>,
   or directly via <function>Async_Notify()</function>.  Such notifications
   will be sent at transaction commit.
   Background workers should not register to receive asynchronous
   notifications with the <command>LISTEN</command> command, as there is no
   infrastructure for a worker to consume such notifications.
  </para>

  <para>
   The <filename>src/test/modules/worker_spi</filename> module
   contains a working example,
   which demonstrates some useful techniques.
  </para>

  <para>
   The maximum number of registered background workers is limited by
   <xref linkend="guc-max-worker-processes"/>.
  </para>
</chapter>

Title: Waiting for Background Worker Startup/Shutdown and Asynchronous Notifications
Summary
The GetBackgroundWorkerPid function can poll worker status, and TerminateBackgroundWorker sends SIGTERM to the worker and unregisters it. A registering process can wait for a worker to start by initializing bgw_notify_pid and calling WaitForBackgroundWorkerStartup, which blocks until the worker starts or the postmaster dies. WaitForBackgroundWorkerShutdown blocks until the worker exits or the postmaster dies. Background workers can send asynchronous notifications via NOTIFY or Async_Notify(), but should not register to receive them. The worker_spi module provides a working example. The maximum number of registered background workers is limited by max_worker_processes.