

This main function should take a single argument of type Datum and return void. If loading a function from the core code, this must be set to "postgres".īgw_function_name is the name of a function in a dynamically loaded library which should be used as the initial entry point for a new background worker.īgw_main_arg is the Datum argument to the background worker main function.

The named library will be dynamically loaded by the worker process and bgw_function_name will be used to identify the function to be called. It can be any positive value, or BGW_NEVER_RESTART, indicating not to restart the process in case of a crash.īgw_library_name is the name of a library in which the initial entry point for the background worker should be sought. Note that this setting only indicates when the processes are to be started they do not stop when a different state is reached.īgw_restart_time is the interval, in seconds, that postgres should wait before restarting the process in the event that it crashes. Note the last two values are equivalent in a server that's not a hot standby. A background worker using BGWORKER_BACKEND_DATABASE_CONNECTION to connect to a database must also attach shared memory using BGWORKER_SHMEM_ACCESS, or worker start-up will fail.īgw_start_time is the server state during which postgres should start the process it can be one of BgWorkerStart_PostmasterStart (start as soon as postgres itself has finished its own initialization processes requesting this are not eligible for database connections), BgWorkerStart_ConsistentState (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 BgWorkerStart_RecoveryFinished (start as soon as the system has entered normal read-write state). Requests the ability to establish a database connection through which it can later run transactions and queries. (Typically, the string for bgw_name will contain the type somehow, but that is not strictly required.)īgw_flags is a bitwise-or'd bit mask indicating the capabilities that the module wants. bgw_name on the other hand can contain additional information about the specific process.

bgw_type 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. Int bgw_restart_time /* in seconds, or BGW_NEVER_RESTART */īgw_name and bgw_type are strings to be used in log messages, process listings and similar contexts. Typedef void (*bgworker_main_type)(Datum main_arg) The structure BackgroundWorker is defined thus: Unlike RegisterBackgroundWorker, which can only be called from within the postmaster process, RegisterDynamicBackgroundWorker must be called from a regular backend or another background worker. Background workers can also be started after the system is up and running by calling RegisterDynamicBackgroundWorker( BackgroundWorker * worker, BackgroundWorkerHandle ** handle). A module wishing to run a background worker can register it by calling RegisterBackgroundWorker( BackgroundWorker * worker) from its _PG_init() function. Only carefully audited modules should be permitted to run background worker processes.īackground workers can be initialized at the time that PostgreSQL is started by including the module name in shared_preload_libraries. Administrators wishing to enable modules that include background worker processes should exercise extreme caution. There are considerable robustness and security risks in using background worker processes because, being written in the C language, they have unrestricted access to data.
