bgworker.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*--------------------------------------------------------------------
  2. * bgworker.h
  3. * POSTGRES pluggable background workers interface
  4. *
  5. * A background worker is a process able to run arbitrary, user-supplied code,
  6. * including normal transactions.
  7. *
  8. * Any external module loaded via shared_preload_libraries can register a
  9. * worker. Workers can also be registered dynamically at runtime. In either
  10. * case, the worker process is forked from the postmaster and runs the
  11. * user-supplied "main" function. This code may connect to a database and
  12. * run transactions. Workers can remain active indefinitely, but will be
  13. * terminated if a shutdown or crash occurs.
  14. *
  15. * If the fork() call fails in the postmaster, it will try again later. Note
  16. * that the failure can only be transient (fork failure due to high load,
  17. * memory pressure, too many processes, etc); more permanent problems, like
  18. * failure to connect to a database, are detected later in the worker and dealt
  19. * with just by having the worker exit normally. A worker which exits with
  20. * a return code of 0 will never be restarted and will be removed from worker
  21. * list. A worker which exits with a return code of 1 will be restarted after
  22. * the configured restart interval (unless that interval is BGW_NEVER_RESTART).
  23. * The TerminateBackgroundWorker() function can be used to terminate a
  24. * dynamically registered background worker; the worker will be sent a SIGTERM
  25. * and will not be restarted after it exits. Whenever the postmaster knows
  26. * that a worker will not be restarted, it unregisters the worker, freeing up
  27. * that worker's slot for use by a new worker.
  28. *
  29. * Note that there might be more than one worker in a database concurrently,
  30. * and the same module may request more than one worker running the same (or
  31. * different) code.
  32. *
  33. *
  34. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  35. * Portions Copyright (c) 1994, Regents of the University of California
  36. *
  37. * IDENTIFICATION
  38. * src/include/postmaster/bgworker.h
  39. *--------------------------------------------------------------------
  40. */
  41. #ifndef BGWORKER_H
  42. #define BGWORKER_H
  43. /*---------------------------------------------------------------------
  44. * External module API.
  45. *---------------------------------------------------------------------
  46. */
  47. /*
  48. * Pass this flag to have your worker be able to connect to shared memory.
  49. */
  50. #define BGWORKER_SHMEM_ACCESS 0x0001
  51. /*
  52. * This flag means the bgworker requires a database connection. The connection
  53. * is not established automatically; the worker must establish it later.
  54. * It requires that BGWORKER_SHMEM_ACCESS was passed too.
  55. */
  56. #define BGWORKER_BACKEND_DATABASE_CONNECTION 0x0002
  57. typedef void (*bgworker_main_type) (Datum main_arg);
  58. /*
  59. * Points in time at which a bgworker can request to be started
  60. */
  61. typedef enum
  62. {
  63. BgWorkerStart_PostmasterStart,
  64. BgWorkerStart_ConsistentState,
  65. BgWorkerStart_RecoveryFinished
  66. } BgWorkerStartTime;
  67. #define BGW_DEFAULT_RESTART_INTERVAL 60
  68. #define BGW_NEVER_RESTART -1
  69. #define BGW_MAXLEN 64
  70. #define BGW_EXTRALEN 128
  71. typedef struct BackgroundWorker
  72. {
  73. char bgw_name[BGW_MAXLEN];
  74. int bgw_flags;
  75. BgWorkerStartTime bgw_start_time;
  76. int bgw_restart_time; /* in seconds, or BGW_NEVER_RESTART */
  77. bgworker_main_type bgw_main;
  78. char bgw_library_name[BGW_MAXLEN]; /* only if bgw_main is NULL */
  79. char bgw_function_name[BGW_MAXLEN]; /* only if bgw_main is NULL */
  80. Datum bgw_main_arg;
  81. char bgw_extra[BGW_EXTRALEN];
  82. pid_t bgw_notify_pid; /* SIGUSR1 this backend on start/stop */
  83. } BackgroundWorker;
  84. typedef enum BgwHandleStatus
  85. {
  86. BGWH_STARTED, /* worker is running */
  87. BGWH_NOT_YET_STARTED, /* worker hasn't been started yet */
  88. BGWH_STOPPED, /* worker has exited */
  89. BGWH_POSTMASTER_DIED /* postmaster died; worker status unclear */
  90. } BgwHandleStatus;
  91. struct BackgroundWorkerHandle;
  92. typedef struct BackgroundWorkerHandle BackgroundWorkerHandle;
  93. /* Register a new bgworker during shared_preload_libraries */
  94. extern void RegisterBackgroundWorker(BackgroundWorker *worker);
  95. /* Register a new bgworker from a regular backend */
  96. extern bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker,
  97. BackgroundWorkerHandle **handle);
  98. /* Query the status of a bgworker */
  99. extern BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle,
  100. pid_t *pidp);
  101. extern BgwHandleStatus
  102. WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *
  103. handle, pid_t *pid);
  104. extern BgwHandleStatus
  105. WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *);
  106. /* Terminate a bgworker */
  107. extern void TerminateBackgroundWorker(BackgroundWorkerHandle *handle);
  108. /* This is valid in a running worker */
  109. extern PGDLLIMPORT BackgroundWorker *MyBgworkerEntry;
  110. /*
  111. * Connect to the specified database, as the specified user. Only a worker
  112. * that passed BGWORKER_BACKEND_DATABASE_CONNECTION during registration may
  113. * call this.
  114. *
  115. * If username is NULL, bootstrapping superuser is used.
  116. * If dbname is NULL, connection is made to no specific database;
  117. * only shared catalogs can be accessed.
  118. */
  119. extern void BackgroundWorkerInitializeConnection(char *dbname, char *username);
  120. /* Just like the above, but specifying database and user by OID. */
  121. extern void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid);
  122. /* Block/unblock signals in a background worker process */
  123. extern void BackgroundWorkerBlockSignals(void);
  124. extern void BackgroundWorkerUnblockSignals(void);
  125. #endif /* BGWORKER_H */