Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Defines | Typedefs | Enumerations | Functions | Variables

bgworker.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  BackgroundWorker

Defines

#define BGWORKER_SHMEM_ACCESS   0x0001
#define BGWORKER_BACKEND_DATABASE_CONNECTION   0x0002
#define BGW_DEFAULT_RESTART_INTERVAL   60
#define BGW_NEVER_RESTART   -1

Typedefs

typedef void(* bgworker_main_type )(void *main_arg)
typedef void(* bgworker_sighdlr_type )(SIGNAL_ARGS)
typedef struct BackgroundWorker BackgroundWorker

Enumerations

enum  BgWorkerStartTime { BgWorkerStart_PostmasterStart, BgWorkerStart_ConsistentState, BgWorkerStart_RecoveryFinished }

Functions

void RegisterBackgroundWorker (BackgroundWorker *worker)
void BackgroundWorkerInitializeConnection (char *dbname, char *username)
void BackgroundWorkerBlockSignals (void)
void BackgroundWorkerUnblockSignals (void)

Variables

BackgroundWorkerMyBgworkerEntry

Define Documentation

#define BGW_DEFAULT_RESTART_INTERVAL   60

Definition at line 68 of file bgworker.h.

#define BGW_NEVER_RESTART   -1
#define BGWORKER_BACKEND_DATABASE_CONNECTION   0x0002
#define BGWORKER_SHMEM_ACCESS   0x0001

Typedef Documentation

typedef void(* bgworker_main_type)(void *main_arg)

Definition at line 55 of file bgworker.h.

typedef void(* bgworker_sighdlr_type)(SIGNAL_ARGS)

Definition at line 56 of file bgworker.h.


Enumeration Type Documentation

Enumerator:
BgWorkerStart_PostmasterStart 
BgWorkerStart_ConsistentState 
BgWorkerStart_RecoveryFinished 

Definition at line 61 of file bgworker.h.

{
    BgWorkerStart_PostmasterStart,
    BgWorkerStart_ConsistentState,
    BgWorkerStart_RecoveryFinished
} BgWorkerStartTime;


Function Documentation

void BackgroundWorkerBlockSignals ( void   ) 

Definition at line 5287 of file postmaster.c.

References BlockSig, and PG_SETMASK.

void BackgroundWorkerInitializeConnection ( char *  dbname,
char *  username 
)

Definition at line 5264 of file postmaster.c.

References BackgroundWorker::bgw_flags, BGWORKER_BACKEND_DATABASE_CONNECTION, ereport, errcode(), errmsg(), ERROR, FATAL, InitPostgres(), InvalidOid, IsInitProcessingMode, NormalProcessing, NULL, and SetProcessingMode.

Referenced by worker_spi_main().

{
    BackgroundWorker *worker = MyBgworkerEntry;

    /* XXX is this the right errcode? */
    if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
        ereport(FATAL,
                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                 errmsg("database connection requirement not indicated during registration")));

    InitPostgres(dbname, InvalidOid, username, NULL);

    /* it had better not gotten out of "init" mode yet */
    if (!IsInitProcessingMode())
        ereport(ERROR,
                (errmsg("invalid processing mode in bgworker")));
    SetProcessingMode(NormalProcessing);
}

void BackgroundWorkerUnblockSignals ( void   ) 

Definition at line 5293 of file postmaster.c.

References PG_SETMASK, and UnBlockSig.

Referenced by worker_spi_main().

void RegisterBackgroundWorker ( BackgroundWorker worker  ) 

Definition at line 5143 of file postmaster.c.

References autovacuum_max_workers, BackgroundWorker::bgw_flags, BackgroundWorker::bgw_name, BGW_NEVER_RESTART, BackgroundWorker::bgw_restart_time, BackgroundWorker::bgw_start_time, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_SHMEM_ACCESS, BgWorkerStart_PostmasterStart, ereport, errcode(), errdetail(), errmsg(), IsUnderPostmaster, LOG, malloc, MAX_BACKENDS, MaxConnections, NULL, process_shared_preload_libraries_in_progress, RegisteredBgWorker::rw_backend, RegisteredBgWorker::rw_child_slot, RegisteredBgWorker::rw_crashed_at, RegisteredBgWorker::rw_lnode, RegisteredBgWorker::rw_pid, RegisteredBgWorker::rw_worker, slist_push_head(), strlcpy(), and USECS_PER_DAY.

Referenced by _PG_init().

{
    RegisteredBgWorker *rw;
    int         namelen = strlen(worker->bgw_name);
    static int  maxworkers;
    static int  numworkers = 0;

#ifdef EXEC_BACKEND

    /*
     * Use 1 here, not 0, to avoid confusing a possible bogus cookie read by
     * atoi() in SubPostmasterMain.
     */
    static int  BackgroundWorkerCookie = 1;
#endif

    /* initialize upper limit on first call */
    if (numworkers == 0)
        maxworkers = MAX_BACKENDS -
            (MaxConnections + autovacuum_max_workers + 1);

    if (!IsUnderPostmaster)
        ereport(LOG,
            (errmsg("registering background worker: %s", worker->bgw_name)));

    if (!process_shared_preload_libraries_in_progress)
    {
        if (!IsUnderPostmaster)
            ereport(LOG,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("background worker \"%s\": must be registered in shared_preload_libraries",
                            worker->bgw_name)));
        return;
    }

    /* sanity check for flags */
    if (worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION)
    {
        if (!(worker->bgw_flags & BGWORKER_SHMEM_ACCESS))
        {
            if (!IsUnderPostmaster)
                ereport(LOG,
                        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                         errmsg("background worker \"%s\": must attach to shared memory in order to request a database connection",
                                worker->bgw_name)));
            return;
        }

        if (worker->bgw_start_time == BgWorkerStart_PostmasterStart)
        {
            if (!IsUnderPostmaster)
                ereport(LOG,
                        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                         errmsg("background worker \"%s\": cannot request database access if starting at postmaster start",
                                worker->bgw_name)));
            return;
        }

        /* XXX other checks? */
    }

    if ((worker->bgw_restart_time < 0 &&
         worker->bgw_restart_time != BGW_NEVER_RESTART) ||
        (worker->bgw_restart_time > USECS_PER_DAY / 1000))
    {
        if (!IsUnderPostmaster)
            ereport(LOG,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("background worker \"%s\": invalid restart interval",
                        worker->bgw_name)));
        return;
    }

    /*
     * Enforce maximum number of workers.  Note this is overly restrictive:
     * we could allow more non-shmem-connected workers, because these don't
     * count towards the MAX_BACKENDS limit elsewhere.  This doesn't really
     * matter for practical purposes; several million processes would need to
     * run on a single server.
     */
    if (++numworkers > maxworkers)
    {
        ereport(LOG,
                (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
                 errmsg("too many background workers"),
                 errdetail("Up to %d background workers can be registered with the current settings.",
                           maxworkers)));
        return;
    }

    /*
     * Copy the registration data into the registered workers list.
     */
    rw = malloc(sizeof(RegisteredBgWorker) + namelen + 1);
    if (rw == NULL)
    {
        ereport(LOG,
                (errcode(ERRCODE_OUT_OF_MEMORY),
                 errmsg("out of memory")));
        return;
    }

    rw->rw_worker = *worker;
    rw->rw_worker.bgw_name = ((char *) rw) + sizeof(RegisteredBgWorker);
    strlcpy(rw->rw_worker.bgw_name, worker->bgw_name, namelen + 1);

    rw->rw_backend = NULL;
    rw->rw_pid = 0;
    rw->rw_child_slot = 0;
    rw->rw_crashed_at = 0;
#ifdef EXEC_BACKEND
    rw->rw_cookie = BackgroundWorkerCookie++;
#endif

    slist_push_head(&BackgroundWorkerList, &rw->rw_lnode);
}


Variable Documentation

Definition at line 204 of file postmaster.c.

Referenced by worker_spi_main().