Home Explore Blog CI



postgresql

2nd chunk of `doc/src/sgml/bgworker.sgml`
b7eb5aa9654be5ddb91e2e9f0cdc9d20c506c04f164652e20000000100000fa1
 are
   strings to be used in log messages, process listings and similar contexts.
   <structfield>bgw_type</structfield> should be the same for all background
   workers of the same type, so that it is possible to group such workers in a
   process listing, for example.  <structfield>bgw_name</structfield> on the
   other hand can contain additional information about the specific process.
   (Typically, the string for <structfield>bgw_name</structfield> will contain
   the type somehow, but that is not strictly required.)
  </para>

  <para>
   <structfield>bgw_flags</structfield> is a bitwise-or'd bit mask indicating the
   capabilities that the module wants.  Possible values are:
   <variablelist>

    <varlistentry>
     <term><literal>BGWORKER_SHMEM_ACCESS</literal></term>
     <listitem>
      <para>
       <indexterm><primary>BGWORKER_SHMEM_ACCESS</primary></indexterm>
       Requests shared memory access.  This flag is required.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry>
     <term><literal>BGWORKER_BACKEND_DATABASE_CONNECTION</literal></term>
     <listitem>
      <para>
       <indexterm><primary>BGWORKER_BACKEND_&zwsp;DATABASE_CONNECTION</primary></indexterm>
       Requests the ability to establish a database connection through which it
       can later run transactions and queries.  A background worker using
       <literal>BGWORKER_BACKEND_DATABASE_CONNECTION</literal> to connect to a
       database must also attach shared memory using
       <literal>BGWORKER_SHMEM_ACCESS</literal>, or worker start-up will fail.
      </para>
     </listitem>
    </varlistentry>

   </variablelist>

  </para>

  <para>
   <structfield>bgw_start_time</structfield> is the server state during which
   <command>postgres</command> should start the process; it can be one of
   <literal>BgWorkerStart_PostmasterStart</literal> (start as soon as
   <command>postgres</command> itself has finished its own initialization; processes
   requesting this are not eligible for database connections),
   <literal>BgWorkerStart_ConsistentState</literal> (start as soon as a consistent state
   has been reached in a hot standby, allowing processes to connect to
   databases and run read-only queries), and
   <literal>BgWorkerStart_RecoveryFinished</literal> (start as soon as the system has
   entered normal read-write state).  Note the last two values are equivalent
   in a server that's not a hot standby.  Note that this setting only indicates
   when the processes are to be started; they do not stop when a different state
   is reached.
  </para>

  <para>
   <structfield>bgw_restart_time</structfield> is the interval, in seconds, that
   <command>postgres</command> should wait before restarting the process in
   the event that it crashes.  It can be any positive value,
   or <literal>BGW_NEVER_RESTART</literal>, indicating not to restart the
   process in case of a crash.
  </para>

  <para>
   <structfield>bgw_library_name</structfield> is the name of a library in
   which the initial entry point for the background worker should be sought.
   The named library will be dynamically loaded by the worker process and
   <structfield>bgw_function_name</structfield> will be used to identify the
   function to be called.  If calling a function in the core code, this must
   be set to <literal>"postgres"</literal>.
  </para>

  <para>
   <structfield>bgw_function_name</structfield> is the name of the function
   to use as the initial entry point for the new background worker.  If
   this function is in a dynamically loaded library, it must be marked
   <literal>PGDLLEXPORT</literal> (and not <literal>static</literal>).
  </para>

  <para>
   <structfield>bgw_main_arg</structfield> is the <type>Datum</type> argument
   to the background worker main function.  This main function should take a
   single argument of type <type>Datum</type> and return <type>void</type>.
   <structfield>bgw_main_arg</structfield> will be passed as the

Title: Background Worker Configuration Details
Summary
This section elaborates on configuring background workers in PostgreSQL. `bgw_name` and `bgw_type` are strings for logging and process listing. `bgw_flags` specify capabilities like shared memory access (`BGWORKER_SHMEM_ACCESS`, required) and database connections (`BGWORKER_BACKEND_DATABASE_CONNECTION`). `bgw_start_time` determines when the worker starts (e.g., after postmaster initialization, in a consistent state, or after recovery). `bgw_restart_time` sets the restart interval upon crashing. `bgw_library_name` and `bgw_function_name` specify the library and function for the worker's entry point. `bgw_main_arg` is the argument passed to the worker's main function.