src/test/modules/worker_spi/worker_spi.c
Datum
worker_spi_launch(PG_FUNCTION_ARGS)
{
	int32		i = PG_GETARG_INT32(0);
	BackgroundWorker worker;
	BackgroundWorkerHandle *handle;
	BgwHandleStatus status;
	pid_t		pid;

	memset(&worker, 0, sizeof(worker));
	worker.bgw_flags = BGWORKER_SHMEM_ACCESS |
		BGWORKER_BACKEND_DATABASE_CONNECTION;
	worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
	worker.bgw_restart_time = BGW_NEVER_RESTART;
	sprintf(worker.bgw_library_name, "worker_spi");
	sprintf(worker.bgw_function_name, "worker_spi_main");
	snprintf(worker.bgw_name, BGW_MAXLEN, "worker_spi worker %d", i);
	snprintf(worker.bgw_type, BGW_MAXLEN, "worker_spi");
	worker.bgw_main_arg = Int32GetDatum(i);
	/* set bgw_notify_pid so that we can use WaitForBackgroundWorkerStartup */
	worker.bgw_notify_pid = MyProcPid;

	if (!RegisterDynamicBackgroundWorker(&worker, &handle))
		PG_RETURN_NULL();

	status = WaitForBackgroundWorkerStartup(handle, &pid);

	if (status == BGWH_STOPPED)
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
				 errmsg("could not start background process"),
				 errhint("More details may be available in the server log.")));
	if (status == BGWH_POSTMASTER_DIED)
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
				 errmsg("cannot start background processes without postmaster"),
				 errhint("Kill all remaining database processes and restart the database.")));
	Assert(status == BGWH_STARTED);

	PG_RETURN_INT32(pid);
}
/*
 * Dynamically launch an SPI worker.
 */
Dynamically launch an SPI worker.
Dynamically launch an SPI worker .
Datum
worker_spi_launch(PG_FUNCTION_ARGS)
{
	int32		i = PG_GETARG_INT32(0);
	BackgroundWorker worker;
	BackgroundWorkerHandle *handle;
	BgwHandleStatus status;
	pid_t		pid;
	memset(&worker, 0, sizeof(worker));
	worker.bgw_flags = BGWORKER_SHMEM_ACCESS |
		BGWORKER_BACKEND_DATABASE_CONNECTION;
	worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
	worker.bgw_restart_time = BGW_NEVER_RESTART;
	sprintf(worker.bgw_library_name, "worker_spi");
	sprintf(worker.bgw_function_name, "worker_spi_main");
	snprintf(worker.bgw_name, BGW_MAXLEN, "worker_spi worker %d", i);
	snprintf(worker.bgw_type, BGW_MAXLEN, "worker_spi");
	worker.bgw_main_arg = Int32GetDatum(i);
	worker.bgw_notify_pid = MyProcPid;
	if (!RegisterDynamicBackgroundWorker(&worker, &handle))
		PG_RETURN_NULL();
	status = WaitForBackgroundWorkerStartup(handle, &pid);
	if (status == BGWH_STOPPED)
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
				 errmsg("could not start background process"),
				 errhint("More details may be available in the server log.")));
	if (status == BGWH_POSTMASTER_DIED)
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
				 errmsg("cannot start background processes without postmaster"),
				 errhint("Kill all remaining database processes and restart the database.")));
	Assert(status == BGWH_STARTED);
	PG_RETURN_INT32(pid);
}
Datum worker_spi_launch ( PG_FUNCTION_ARGS ) { int32 i = PG_GETARG_INT32 ( 0 ) ; BackgroundWorker worker ; BackgroundWorkerHandle * handle ; BgwHandleStatus status ; pid_t pid ; memset ( & worker , 0 , sizeof ( worker ) ) ; worker . bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION ; worker . bgw_start_time = BgWorkerStart_RecoveryFinished ; worker . bgw_restart_time = BGW_NEVER_RESTART ; sprintf ( worker . bgw_library_name , " " ) ; sprintf ( worker . bgw_function_name , " " ) ; snprintf ( worker . bgw_name , BGW_MAXLEN , " " , i ) ; snprintf ( worker . bgw_type , BGW_MAXLEN , " " ) ; worker . bgw_main_arg = Int32GetDatum ( i ) ; worker . bgw_notify_pid = MyProcPid ; if ( ! RegisterDynamicBackgroundWorker ( & worker , & handle ) ) PG_RETURN_NULL ( ) ; status = WaitForBackgroundWorkerStartup ( handle , & pid ) ; if ( status == BGWH_STOPPED ) ereport ( ERROR , ( errcode ( ERRCODE_INSUFFICIENT_RESOURCES ) , errmsg ( " " ) , errhint ( " " ) ) ) ; if ( status == BGWH_POSTMASTER_DIED ) ereport ( ERROR , ( errcode ( ERRCODE_INSUFFICIENT_RESOURCES ) , errmsg ( " " ) , errhint ( " " ) ) ) ; Assert ( status == BGWH_STARTED ) ; PG_RETURN_INT32 ( pid ) ; }
Dynamically launch an SPI worker.
Dynamically launch an SPI worker .
/* set bgw_notify_pid so that we can use WaitForBackgroundWorkerStartup */