Header And Logo

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

bgworker.h

Go to the documentation of this file.
00001 /*--------------------------------------------------------------------
00002  * bgworker.h
00003  *      POSTGRES pluggable background workers interface
00004  *
00005  * A background worker is a process able to run arbitrary, user-supplied code,
00006  * including normal transactions.
00007  *
00008  * Any external module loaded via shared_preload_libraries can register a
00009  * worker.  Then, at the appropriate time, the worker process is forked from
00010  * the postmaster and runs the user-supplied "main" function.  This code may
00011  * connect to a database and run transactions.  Once started, it stays active
00012  * until shutdown or crash.  The process should sleep during periods of
00013  * inactivity.
00014  *
00015  * If the fork() call fails in the postmaster, it will try again later.  Note
00016  * that the failure can only be transient (fork failure due to high load,
00017  * memory pressure, too many processes, etc); more permanent problems, like
00018  * failure to connect to a database, are detected later in the worker and dealt
00019  * with just by having the worker exit normally.  Postmaster will launch a new
00020  * worker again later.
00021  *
00022  * Note that there might be more than one worker in a database concurrently,
00023  * and the same module may request more than one worker running the same (or
00024  * different) code.
00025  *
00026  *
00027  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00028  * Portions Copyright (c) 1994, Regents of the University of California
00029  *
00030  * IDENTIFICATION
00031  *      src/include/postmaster/bgworker.h
00032  *--------------------------------------------------------------------
00033  */
00034 #ifndef BGWORKER_H
00035 #define BGWORKER_H
00036 
00037 /*---------------------------------------------------------------------
00038  * External module API.
00039  *---------------------------------------------------------------------
00040  */
00041 
00042 /*
00043  * Pass this flag to have your worker be able to connect to shared memory.
00044  */
00045 #define BGWORKER_SHMEM_ACCESS                       0x0001
00046 
00047 /*
00048  * This flag means the bgworker requires a database connection.  The connection
00049  * is not established automatically; the worker must establish it later.
00050  * It requires that BGWORKER_SHMEM_ACCESS was passed too.
00051  */
00052 #define BGWORKER_BACKEND_DATABASE_CONNECTION        0x0002
00053 
00054 
00055 typedef void (*bgworker_main_type)(void *main_arg);
00056 typedef void (*bgworker_sighdlr_type)(SIGNAL_ARGS);
00057 
00058 /*
00059  * Points in time at which a bgworker can request to be started
00060  */
00061 typedef enum
00062 {
00063     BgWorkerStart_PostmasterStart,
00064     BgWorkerStart_ConsistentState,
00065     BgWorkerStart_RecoveryFinished
00066 } BgWorkerStartTime;
00067 
00068 #define BGW_DEFAULT_RESTART_INTERVAL    60
00069 #define BGW_NEVER_RESTART               -1
00070 
00071 typedef struct BackgroundWorker
00072 {
00073     char       *bgw_name;
00074     int         bgw_flags;
00075     BgWorkerStartTime bgw_start_time;
00076     int         bgw_restart_time;       /* in seconds, or BGW_NEVER_RESTART */
00077     bgworker_main_type  bgw_main;
00078     void       *bgw_main_arg;
00079     bgworker_sighdlr_type bgw_sighup;
00080     bgworker_sighdlr_type bgw_sigterm;
00081 } BackgroundWorker;
00082 
00083 /* Register a new bgworker */
00084 extern void RegisterBackgroundWorker(BackgroundWorker *worker);
00085 
00086 /* This is valid in a running worker */
00087 extern BackgroundWorker *MyBgworkerEntry;
00088 
00089 /*
00090  * Connect to the specified database, as the specified user.  Only a worker
00091  * that passed BGWORKER_BACKEND_DATABASE_CONNECTION during registration may
00092  * call this.
00093  *
00094  * If username is NULL, bootstrapping superuser is used.
00095  * If dbname is NULL, connection is made to no specific database;
00096  * only shared catalogs can be accessed.
00097  */
00098 extern void BackgroundWorkerInitializeConnection(char *dbname, char *username);
00099 
00100 /* Block/unblock signals in a background worker process */
00101 extern void BackgroundWorkerBlockSignals(void);
00102 extern void BackgroundWorkerUnblockSignals(void);
00103 
00104 #endif /* BGWORKER_H */