示例#1
0
/*
 * 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);
	/* 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);
}
示例#2
0
Datum
worker_test(PG_FUNCTION_ARGS)
{
	int i, nworkers;
	dsm_segment *seg;
	test_shm_mq_header *hdr;
	MemoryContext oldcontext;
	worker_state *wstate;

	nworkers = PG_GETARG_INT32(0);

#if PG_VERSION_NUM >= 90500
	seg = dsm_create(sizeof(test_shm_mq_header), 0);
#else
	seg = dsm_create(sizeof(test_shm_mq_header)
#endif

	hdr = dsm_segment_address(seg);

	printf("begin worker_test: %d, %p\n", dsm_segment_handle(seg), hdr);

	MemSet(hdr, 0, sizeof(test_shm_mq_header));

	SpinLockInit(&hdr->mutex);

	strncpy(hdr->dbname, get_database_name(MyDatabaseId), sizeof(hdr->dbname));

	oldcontext = MemoryContextSwitchTo(CurTransactionContext);

	wstate = MemoryContextAlloc(TopTransactionContext,
								offsetof(worker_state, handle) +
								sizeof(BackgroundWorkerHandle *) * nworkers);

	MemSet(wstate, 0, offsetof(worker_state, handle) + sizeof(BackgroundWorkerHandle *) * nworkers);

	on_dsm_detach(seg, cleanup_background_workers,
				  PointerGetDatum(wstate));

	for (i = 0; i < nworkers; i++)
	{
		BackgroundWorker worker;

		MemSet(&worker, 0, sizeof(worker));

		worker.bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
		worker.bgw_start_time = BgWorkerStart_ConsistentState;
		worker.bgw_restart_time = BGW_NEVER_RESTART;
		worker.bgw_main = NULL;		/* new worker might not have library loaded */

		sprintf(worker.bgw_library_name, "worker_test");
		sprintf(worker.bgw_function_name, "worker_test_main");
		snprintf(worker.bgw_name, BGW_MAXLEN, "worker_test %d", i);

		worker.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg));
		worker.bgw_notify_pid = MyProcPid;

		if (!RegisterDynamicBackgroundWorker(&worker, &wstate->handle[i]))
			ereport(ERROR,
					(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
					 errmsg("could not register background process"),
				 errhint("You may need to increase max_worker_processes.")));

		++wstate->nworkers;
	}

	for (i = 0; i < nworkers; i++)
	{
		BgwHandleStatus status;
		pid_t pid;

		status = WaitForBackgroundWorkerStartup(wstate->handle[i], &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);
	}

	wait_for_workers_to_become_ready(wstate, hdr);

	cancel_on_dsm_detach(seg, cleanup_background_workers,
						 PointerGetDatum(wstate));

	dsm_detach(seg);

	pfree(wstate);

	MemoryContextSwitchTo(oldcontext);


	PG_RETURN_VOID();
}
示例#3
0
/*
 * Starts background worker that will create new partitions,
 * waits till it finishes the job and returns the result (new partition oid)
 */
Oid
create_partitions_bg_worker(Oid relid, Datum value, Oid value_type, bool *crashed)
{
	BackgroundWorker		worker;
	BackgroundWorkerHandle *worker_handle;
	BgwHandleStatus			status;
	dsm_segment	   *segment;
	dsm_handle		segment_handle;
	pid_t 			pid;
	PartitionArgs  *args;
	Oid 			child_oid;
	TypeCacheEntry *tce;

	/* Create a dsm segment for the worker to pass arguments */
	segment = dsm_create(sizeof(PartitionArgs), 0);
	segment_handle = dsm_segment_handle(segment);

	tce = lookup_type_cache(value_type, 0);

	/* Fill arguments structure */
	args = (PartitionArgs *) dsm_segment_address(segment);
	args->dbid = MyDatabaseId;
	args->relid = relid;
	if (tce->typbyval)
		args->value = value;
	else
		memcpy(&args->value, DatumGetPointer(value), sizeof(args->value));
	args->by_val = tce->typbyval;
	args->value_type = value_type;
	args->result = 0;

	/* Initialize worker struct */
	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 = bg_worker_main;
	worker.bgw_main_arg = Int32GetDatum(segment_handle);
	worker.bgw_notify_pid = MyProcPid;

	/* Start dynamic worker */
	if (!RegisterDynamicBackgroundWorker(&worker, &worker_handle))
	{
		elog(WARNING, "Unable to create background worker for pg_pathman");
	}

	status = WaitForBackgroundWorkerStartup(worker_handle, &pid);
	if (status == BGWH_POSTMASTER_DIED)
	{
		ereport(WARNING,
                (errmsg("Postmaster died during the pg_pathman background worker process"),
                 errhint("More details may be available in the server log.")));
	}

	/* Wait till the worker finishes its job */
	status = WaitForBackgroundWorkerShutdown(worker_handle);
	*crashed = args->crashed;
	child_oid = args->result;

	/* Free dsm segment */
	dsm_detach(segment);

	return child_oid;
}