static void
worker_spi1_sigterm(SIGNAL_ARGS)
{
	int			save_errno = errno;

	got_sigterm = true;
	if (MyProc)
		SetLatch(&MyProc->procLatch);

	errno = save_errno;
}

	static void
worker_spi1_sighup(SIGNAL_ARGS)
{
	int			save_errno = errno;

	got_sighup = true;
	if (MyProc)
		SetLatch(&MyProc->procLatch);

	errno = save_errno;
}

	void
worker_spi1_main(Datum main_arg)
{
		/* Establish signal handlers before unblocking signals. */
	pqsignal(SIGHUP, worker_spi1_sighup);
	pqsignal(SIGTERM, worker_spi1_sigterm);

	/* We're now ready to receive signals */
	BackgroundWorkerUnblockSignals();

	/* Connect to our database */
	BackgroundWorkerInitializeConnection("TestWorker", "nsadmin");

	elog(LOG, "initialized Background Worker");
	initialize_worker_spi1(table);

		while (!got_sigterm)
	{
		//Perform some operation         
		pgstat_report_activity(STATE_IDLE, NULL);
	}

	proc_exit(1);
}

	void
_PG_init(void)
{
	BackgroundWorker worker;
	unsigned int i;
	/* get the configuration */
	DefineCustomIntVariable("worker_spi1.naptime",
			"Duration between each check (in seconds).",
			NULL,
			&worker_spi1_naptime,
			10,
			1,
			INT_MAX,
			PGC_SIGHUP,
			0,
			NULL,
			NULL,
			NULL);

	if (!process_shared_preload_libraries_in_progress)
		return;

	DefineCustomIntVariable("worker_spi1.total_workers",
			"Number of workers.",
			NULL,
			&worker_spi1_total_workers,
			2,
			1,
			100,
			PGC_POSTMASTER,
			0,
			NULL,
			NULL,
			NULL);

	/* set up common data for all our workers */
	worker.bgw_flags = BGWORKER_SHMEM_ACCESS |
		BGWORKER_BACKEND_DATABASE_CONNECTION;
	worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
	worker.bgw_restart_time = 1*1000L*1000L ;   //BGW_NEVER_RESTART;
	worker.bgw_main = worker_spi1_main;

	/*
	 * Now fill in worker-specific data, and do the actual registrations.
	 */
	for (i = 1; i <= worker_spi1_total_workers; i++)
	{
		snprintf(worker.bgw_name, BGW_MAXLEN, "worker %d", i);
		worker.bgw_main_arg = Int32GetDatum(i);

		RegisterBackgroundWorker(&worker);
	}
}

/*
 * Dynamically launch an SPI worker.
 */
	Datum
worker_spi1_launch(PG_FUNCTION_ARGS)
{
	int32		i = PG_GETARG_INT32(0);
	BackgroundWorker worker;
	BackgroundWorkerHandle *handle;
	BgwHandleStatus	status;
	pid_t		pid;

	worker.bgw_flags = BGWORKER_SHMEM_ACCESS |
		BGWORKER_BACKEND_DATABASE_CONNECTION;
	worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
	worker.bgw_restart_time = BGW_NEVER_RESTART;
	worker.bgw_main = NULL;		/* new worker might not have library loaded */
	sprintf(worker.bgw_library_name, "worker_spi1");
	sprintf(worker.bgw_function_name, "worker_spi1_main");
	snprintf(worker.bgw_name, BGW_MAXLEN, "worker %d", i);
	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);
}