I have these types:
CREATE TYPE p_type AS
(

x double precision,
y double precision

);
CREATE TYPE d_type AS
(

i p_type,
e p_type,
id integer

);

CREATE OR REPLACE FUNCTION d_swap(d_type)
RETURNS d_type
AS '/home/user/PostgreSQL/9.0/lib/mylib','d_swap'
LANGUAGE C STRICT;

My problem is that I don't know how to construct the d_type tuple in the c
function. I read the documentation but it only explains how i could do that
for a more simple type like p_type.
The difficulty is that d_type has attributes of p_type.
Thank you in advance.

This is the c code I wrote until now:
Datum d_swap(PG_FUNCTION_ARGS) {
    HeapTupleHeader t = PG_GETARG_HEAPTUPLEHEADER(0);
    HeapTupleHeader i;
    HeapTupleHeader e;
    bool isnull;

    TupleDesc tupdesc;
    Datum values[3];
    HeapTuple tuple;
    int tuplen;
    bool *nulls;

    i = DatumGetHeapTupleHeader(GetAttributeByName(t, "i", &isnull));
    e = DatumGetHeapTupleHeader(GetAttributeByName(t, "e", &isnull));

    if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
{
        ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context that cannot accept type
record")));
    }

    BlessTupleDesc(tupdesc);

    values[0] = e;
    values[1] = i;
    values[2] = GetAttributeByName(t, "id", &isnull);


    tuplen = tupdesc->natts;
    nulls = palloc(tuplen * sizeof (bool));

    tuple = heap_form_tuple(tupdesc, values, nulls);

    pfree(nulls);

    PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
}

Reply via email to