#include <stdio.h>
#include <stdlib.h>

#include "postgres.h"
#include "funcapi.h"
#include "miscadmin.h"

PG_MODULE_MAGIC;

/*
 * gcc -I/home/tomas/tmp/postgresql-9.1.2/src/include testcomp.c -shared -fPIC
 *     -o testcomp.so
 * 
 *     CREATE FUNCTION test_computation()
 *         RETURNS void
 *         AS 'testcomp','test_computation'
 *         LANGUAGE C STRICT;
 * 
 * 
 */

PG_FUNCTION_INFO_V1(test_computation);

Datum test_computation(PG_FUNCTION_ARGS);
void *print_message_function( void *ptr );

Datum test_computation(PG_FUNCTION_ARGS)
{	
	int64 result;
	
	char * message1 = print_message_function(&result);
	
	PG_RETURN_VOID();
}
 
void *print_message_function( void *ptr )
{
	int64 i;
	int64 x = 0;
	
	/* let's torture the CPU a bit ... */
	for (i = 0; i < 5000000000; i++) {
		
		x = x + i;
		
		if (i % 1000000 == 0) {
			/* needed for a quick response to cancel-query */
			elog(WARNING, "i = %ld v = %ld", i, x);
		}
		
		
	}
	
	*((int64*)ptr) = x;
	
}
