src/test/modules/worker_spi/worker_spi.c
static void
initialize_worker_spi(worktable *table)
{
	int			ret;
	int			ntup;
	bool		isnull;
	StringInfoData buf;

	SetCurrentStatementStartTimestamp();
	StartTransactionCommand();
	SPI_connect();
	PushActiveSnapshot(GetTransactionSnapshot());
	pgstat_report_activity(STATE_RUNNING, "initializing worker_spi schema");

	/* XXX could we use CREATE SCHEMA IF NOT EXISTS? */
	initStringInfo(&buf);
	appendStringInfo(&buf, "select count(*) from pg_namespace where nspname = '%s'",
					 table->schema);

	debug_query_string = buf.data;
	ret = SPI_execute(buf.data, true, 0);
	if (ret != SPI_OK_SELECT)
		elog(FATAL, "SPI_execute failed: error code %d", ret);

	if (SPI_processed != 1)
		elog(FATAL, "not a singleton result");

	ntup = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0],
									   SPI_tuptable->tupdesc,
									   1, &isnull));
	if (isnull)
		elog(FATAL, "null result");

	if (ntup == 0)
	{
		debug_query_string = NULL;
		resetStringInfo(&buf);
		appendStringInfo(&buf,
						 "CREATE SCHEMA \"%s\" "
						 "CREATE TABLE \"%s\" ("
						 "		type text CHECK (type IN ('total', 'delta')), "
						 "		value	integer)"
						 "CREATE UNIQUE INDEX \"%s_unique_total\" ON \"%s\" (type) "
						 "WHERE type = 'total'",
						 table->schema, table->name, table->name, table->name);

		/* set statement start time */
		SetCurrentStatementStartTimestamp();

		debug_query_string = buf.data;
		ret = SPI_execute(buf.data, false, 0);

		if (ret != SPI_OK_UTILITY)
			elog(FATAL, "failed to create my schema");

		debug_query_string = NULL;	/* rest is not statement-specific */
	}

	SPI_finish();
	PopActiveSnapshot();
	CommitTransactionCommand();
	debug_query_string = NULL;
	pgstat_report_activity(STATE_IDLE, NULL);
}
/*
 * Initialize workspace for a worker process: create the schema if it doesn't
 * already exist.
 */
Initialize workspace for a worker process: create the schema if it doesn't
already exist.
Initialize workspace for a worker process : create the schema if it doesn ' t already exist .
static void
initialize_worker_spi(worktable *table)
{
	int			ret;
	int			ntup;
	bool		isnull;
	StringInfoData buf;
	SetCurrentStatementStartTimestamp();
	StartTransactionCommand();
	SPI_connect();
	PushActiveSnapshot(GetTransactionSnapshot());
	pgstat_report_activity(STATE_RUNNING, "initializing worker_spi schema");
	initStringInfo(&buf);
	appendStringInfo(&buf, "select count(*) from pg_namespace where nspname = '%s'",
					 table->schema);
	debug_query_string = buf.data;
	ret = SPI_execute(buf.data, true, 0);
	if (ret != SPI_OK_SELECT)
		elog(FATAL, "SPI_execute failed: error code %d", ret);
	if (SPI_processed != 1)
		elog(FATAL, "not a singleton result");
	ntup = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0],
									   SPI_tuptable->tupdesc,
									   1, &isnull));
	if (isnull)
		elog(FATAL, "null result");
	if (ntup == 0)
	{
		debug_query_string = NULL;
		resetStringInfo(&buf);
		appendStringInfo(&buf,
						 "CREATE SCHEMA \"%s\" "
						 "CREATE TABLE \"%s\" ("
						 "		type text CHECK (type IN ('total', 'delta')), "
						 "		value	integer)"
						 "CREATE UNIQUE INDEX \"%s_unique_total\" ON \"%s\" (type) "
						 "WHERE type = 'total'",
						 table->schema, table->name, table->name, table->name);
		SetCurrentStatementStartTimestamp();
		debug_query_string = buf.data;
		ret = SPI_execute(buf.data, false, 0);
		if (ret != SPI_OK_UTILITY)
			elog(FATAL, "failed to create my schema");
		debug_query_string = NULL;	
	}
	SPI_finish();
	PopActiveSnapshot();
	CommitTransactionCommand();
	debug_query_string = NULL;
	pgstat_report_activity(STATE_IDLE, NULL);
}
static void initialize_worker_spi ( worktable * table ) { int ret ; int ntup ; bool isnull ; StringInfoData buf ; SetCurrentStatementStartTimestamp ( ) ; StartTransactionCommand ( ) ; SPI_connect ( ) ; PushActiveSnapshot ( GetTransactionSnapshot ( ) ) ; pgstat_report_activity ( STATE_RUNNING , " " ) ; initStringInfo ( & buf ) ; appendStringInfo ( & buf , " " , table -> schema ) ; debug_query_string = buf . data ; ret = SPI_execute ( buf . data , true , 0 ) ; if ( ret != SPI_OK_SELECT ) elog ( FATAL , " " , ret ) ; if ( SPI_processed != 1 ) elog ( FATAL , " " ) ; ntup = DatumGetInt64 ( SPI_getbinval ( SPI_tuptable -> vals [ 0 ] , SPI_tuptable -> tupdesc , 1 , & isnull ) ) ; if ( isnull ) elog ( FATAL , " " ) ; if ( ntup == 0 ) { debug_query_string = NULL ; resetStringInfo ( & buf ) ; appendStringInfo ( & buf , " \" \" " " \" \" " " " " " " \" \" \" \" " " " , table -> schema , table -> name , table -> name , table -> name ) ; SetCurrentStatementStartTimestamp ( ) ; debug_query_string = buf . data ; ret = SPI_execute ( buf . data , false , 0 ) ; if ( ret != SPI_OK_UTILITY ) elog ( FATAL , " " ) ; debug_query_string = NULL ; } SPI_finish ( ) ; PopActiveSnapshot ( ) ; CommitTransactionCommand ( ) ; debug_query_string = NULL ; pgstat_report_activity ( STATE_IDLE , NULL ) ; }
Initialize workspace for a worker process: create the schema if it doesn't
 already exist.
Initialize workspace for a worker process : create the schema if it doesn ' t already exist .
/* XXX could we use CREATE SCHEMA IF NOT EXISTS? */ /* set statement start time */ /* rest is not statement-specific */