C function to return tuple

From: Marios Vodas <mvodas(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: C function to return tuple
Date: 2010-09-26 15:35:10
Message-ID: AANLkTi=U63_AgNUfPxrJKU4MEz0mJXgvss74s5OHO1m=@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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));
}

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2010-09-26 15:48:37 Re: [COMMITTERS] pgsql: Still more tweaking of git_changelog.
Previous Message Magnus Hagander 2010-09-26 15:11:22 Re: Stalled post to pgsql-committers