From 090d0f2050647958865cb495dff74af7257d2bb4 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Wed, 28 Aug 2013 14:08:13 -0400 Subject: Allow discovery of whether a dynamic background worker is running. Using the infrastructure provided by this patch, it's possible either to wait for the startup of a dynamically-registered background worker, or to poll the status of such a worker without waiting. In either case, the current PID of the worker process can also be obtained. As usual, worker_spi is updated to demonstrate the new functionality. Patch by me. Review by Andres Freund. --- doc/src/sgml/bgworker.sgml | 54 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 5 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/bgworker.sgml b/doc/src/sgml/bgworker.sgml index 268e1cd2153..102d46372b5 100644 --- a/doc/src/sgml/bgworker.sgml +++ b/doc/src/sgml/bgworker.sgml @@ -37,11 +37,11 @@ RegisterBackgroundWorker(BackgroundWorker *worker) from its _PG_init(). Background workers can also be started after the system is up and running by calling the function - RegisterDynamicBackgroundWorker(BackgroundWorker - *worker). Unlike RegisterBackgroundWorker, which can - only be called from within the postmaster, - RegisterDynamicBackgroundWorker must be called from - a regular backend. + RegisterDynamicBackgroundWorker(BackgroundWorker + *worker, BackgroundWorkerHandle **handle). Unlike + RegisterBackgroundWorker, which can only be called from within + the postmaster, RegisterDynamicBackgroundWorker must be + called from a regular backend. @@ -58,6 +58,7 @@ typedef struct BackgroundWorker char bgw_library_name[BGW_MAXLEN]; /* only if bgw_main is NULL */ char bgw_function_name[BGW_MAXLEN]; /* only if bgw_main is NULL */ Datum bgw_main_arg; + int bgw_notify_pid; } BackgroundWorker; @@ -135,6 +136,15 @@ typedef struct BackgroundWorker bgw_main is NULL. + + bgw_notify_pid is the PID of a PostgreSQL + backend process to which the postmaster should send SIGUSR1 + when the process is started or exits. It should be 0 for workers registered + at postmaster startup time, or when the backend registering the worker does + not wish to wait for the worker to start up. Otherwise, it should be + initialized to MyProcPid. + + Once running, the process can connect to a database by calling BackgroundWorkerInitializeConnection(char *dbname, char *username). This allows the process to run transactions and queries using the @@ -165,6 +175,40 @@ typedef struct BackgroundWorker postgres itself has terminated. + + When a background worker is registered using the + RegisterDynamicBackgroundWorker function, it is + possible for the backend performing the registration to obtain information + the status of the worker. Backends wishing to do this should pass the + address of a BackgroundWorkerHandle * as the second argument + to RegisterDynamicBackgroundWorker. If the worker + is successfully registered, this pointer will be initialized with an + opaque handle that can subsequently be passed to + GetBackgroundWorkerPid(BackgroundWorkerHandle *, pid_t *). + This function can be used to poll the status of the worker: a return + value of BGWH_NOT_YET_STARTED indicates that the worker has not + yet been started by the postmaster; BGWH_STOPPED + indicates that it has been started but is no longer running; and + BGWH_STARTED indicates that it is currently running. + In this last case, the PID will also be returned via the second argument. + + + + 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 + bgw_notify_pid to MyProcPid and + then passing the BackgroundWorkerHandle * obtained at + registration time to + WaitForBackgroundWorkerStartup(BackgroundWorkerHandle + *handle, pid_t *) function. + This function will block until the postmaster has attempted to start the + background worker, or until the postmaster dies. If the background runner + is running, the return value will BGWH_STARTED, and + the PID will be written to the provided address. Otherwise, the return + value will be BGWH_STOPPED or + BGWH_POSTMASTER_DIED. + + The worker_spi contrib module contains a working example, which demonstrates some useful techniques. -- cgit v1.2.3