#include <postgres.h>
#include <fmgr.h>
#include <funcapi.h>

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

Datum packSessionData( int a, int b, int c, FunctionCallInfo fcinfo )
{
    TupleDesc td;
    if ( get_call_result_type( fcinfo, NULL, & td ) != TYPEFUNC_COMPOSITE )
    {
        ereport( ERROR,
                 ( errcode( ERRCODE_DATA_EXCEPTION ),
                   errmsg( "\'packSessionData\': Function returning record called in context that cannot accept type record" ) ) );
    }
    td = BlessTupleDesc( td );

    Datum * ret = ( Datum * ) palloc( 3 * sizeof( Datum ) );
    bool isNull[ 3 ] = {false, false, false};

    ret[ 0 ] = Int32GetDatum( a );
    ret[ 1 ] = Int32GetDatum( b );
    ret[ 2 ] = Int32GetDatum( c );

    HeapTuple ht = ( HeapTuple ) heap_form_tuple( td, ret, isNull );
    return HeapTupleGetDatum( ht );
}

PG_FUNCTION_INFO_V1 (session_get);
Datum session_get(PG_FUNCTION_ARGS) {
  Datum ret = packSessionData( 0, 0, 0, fcinfo);
  return ret;
}

