Struct pgrx::bgworkers::BackgroundWorkerBuilder
source · pub struct BackgroundWorkerBuilder { /* private fields */ }Expand description
A builder-style interface for creating a new Background Worker
For a static background worker, this must be used from within your extension’s _PG_init() function,
finishing with the .load() function. Dynamic background workers are loaded with .load_dynamic() and
have no restriction as to where they can be loaded.
Example
use pgrx::prelude::*;
use pgrx::bgworkers::BackgroundWorkerBuilder;
#[pg_guard]
pub extern "C" fn _PG_init() {
BackgroundWorkerBuilder::new("My Example BGWorker")
.set_function("background_worker_main")
.set_library("example")
.enable_spi_access()
.load();
}
#[pg_guard]
pub extern "C" fn background_worker_main(_arg: pg_sys::Datum) {
// do bgworker stuff here
}Implementations§
source§impl BackgroundWorkerBuilder
impl BackgroundWorkerBuilder
sourcepub fn new(name: &str) -> BackgroundWorkerBuilder
pub fn new(name: &str) -> BackgroundWorkerBuilder
Construct a new BackgroundWorker of the specified name
By default, its type is also set to the specified name
and it is configured to
- start at BgWorkerStartTime::PostmasterStart.
- never restart in the event it crashes
sourcepub fn enable_shmem_access(
self,
startup: Option<unsafe extern "C" fn()>
) -> Self
pub fn enable_shmem_access( self, startup: Option<unsafe extern "C" fn()> ) -> Self
Does this BackgroundWorker want Shared Memory access?
startup allows specifying shared memory initialization startup hook. Ignored
if BackgroundWorkerBuilder::load_dynamic is used.
sourcepub fn enable_spi_access(self) -> Self
pub fn enable_spi_access(self) -> Self
Does this BackgroundWorker intend to use SPI?
If set, then the configured start time becomes BgWorkerStartTIme::RecoveryFinished
as accessing SPI prior to possible database recovery is not possible
sourcepub fn set_start_time(self, input: BgWorkerStartTime) -> Self
pub fn set_start_time(self, input: BgWorkerStartTime) -> Self
When should this BackgroundWorker be started by Postgres?
sourcepub fn set_restart_time(self, input: Option<Duration>) -> Self
pub fn set_restart_time(self, input: Option<Duration>) -> Self
the interval, in seconds, that postgres should wait before restarting the process,
in case it crashes. It can be Some(any positive duration value), or None`, indicating not to restart the process in case of a crash.
sourcepub fn set_library(self, input: &str) -> Self
pub fn set_library(self, input: &str) -> Self
What is the library name that contains the “main” function?
Typically, this will just be your extension’s name
sourcepub fn set_function(self, input: &str) -> Self
pub fn set_function(self, input: &str) -> Self
What is the “main” function that should be run when the BackgroundWorker process is started?
The specified function must be:
- extern "C",
- guarded with #[pg_guard],
- take 1 argument of type pgrx::pg_sys::Datum, and
- return “void”
Example
use pgrx::prelude::*;
#[pg_guard]
pub extern "C" fn background_worker_main(_arg: pg_sys::Datum) {
}sourcepub fn set_argument(self, input: Option<Datum>) -> Self
pub fn set_argument(self, input: Option<Datum>) -> Self
Datum argument to the background worker main function. This main function should take a single argument of type Datum and return void. bgw_main_arg will be passed as the argument. In addition, the global variable MyBgworkerEntry points to a copy of the BackgroundWorker structure passed at registration time; the worker may find it helpful to examine this structure.
On Windows (and anywhere else where EXEC_BACKEND is defined) or in dynamic background workers it is not safe to pass a Datum by reference, only by value. If an argument is required, it is safest to pass an int32 or other small value and use that as an index into an array allocated in shared memory.
Important
If a value like a cstring or text is passed then the pointer won’t be valid from the new background worker process.
In general, this means that you should stick to primitive Rust types such as i32,
bool, etc.
You you use pgrx’s IntoDatum trait to make the conversion into a datum easy:
use pgrx::prelude::*;
use pgrx::bgworkers::BackgroundWorkerBuilder;
BackgroundWorkerBuilder::new("Example")
.set_function("background_worker_main")
.set_library("example")
.set_argument(42i32.into_datum())
.load();sourcepub fn set_extra(self, input: &str) -> Self
pub fn set_extra(self, input: &str) -> Self
extra data to be passed to the background worker. Unlike bgw_main_arg, this
data is not passed as an argument to the worker’s main function, but it can be
accessed via the BackgroundWorker struct.
sourcepub fn set_notify_pid(self, input: i32) -> Self
pub fn set_notify_pid(self, input: i32) -> Self
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
pgrx::pg_sys::MyProcPid
sourcepub fn load(self)
pub fn load(self)
Once properly configured, call load() to get the BackgroundWorker registered and
started at the proper time by Postgres.
sourcepub fn load_dynamic(self) -> DynamicBackgroundWorker
pub fn load_dynamic(self) -> DynamicBackgroundWorker
Once properly configured, call load_dynamic() to get the BackgroundWorker registered and started dynamically.
Trait Implementations§
source§impl<'a> Into<BackgroundWorker> for &'a BackgroundWorkerBuilder
impl<'a> Into<BackgroundWorker> for &'a BackgroundWorkerBuilder
This conversion is useful only in limited context outside of pgrx, such as when this structure is required by other libraries and the worker is not to be started by pgrx itself. In this case, the builder is useful for building this structure.
source§fn into(self) -> BackgroundWorker
fn into(self) -> BackgroundWorker
Auto Trait Implementations§
impl RefUnwindSafe for BackgroundWorkerBuilder
impl !Send for BackgroundWorkerBuilder
impl !Sync for BackgroundWorkerBuilder
impl Unpin for BackgroundWorkerBuilder
impl UnwindSafe for BackgroundWorkerBuilder
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where &'a Self: for<'a> IntoIterator,
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,
self, then passes self.as_mut() into the pipe
function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
.tap_ref_mut() only in debug builds, and is erased in release
builds.