forming tuple as an attribute inside another tuple in c function
Is it posible? Either by using heap_form_tuple or BuildTupleFromCStrings.
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 demo()
RETURNS d_type
AS '/home/user/PostgreSQL/9.0/lib/mylib','demo'
LANGUAGE C STRICT;
Datum demo(PG_FUNCTION_ARGS) {
float8 xi = 1;
float8 yi = 2;
float8 xe = 3;
float8 ye = 4;
int32 id = 1;
bool isnull;
TupleDesc tupdesc;
Datum values[3];
HeapTuple tuple;
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] = ?; //how?**
*
* values[1] = ?;** //how?*
values[2] = Int32GetDatum(id);
tuple = heap_form_tuple(tupdesc, values, &isnull);
PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
}
Marios Vodas <mvodas@gmail.com> writes:
Is it posible? Either by using heap_form_tuple or BuildTupleFromCStrings.
heap_form_tuple followed by HeapTupleGetDatum should work; that's what's
done in ExecEvalRow() for instance. You've omitted to tell us just
what's going wrong for you.
regards, tom lane
OK but what is the recommended way to get TupleDesc for p_type? I am aware
of RelationNameGetTupleDesc but I shouldn't use it since it is deprecated.
However by using it the code would look something like this (I tested it and
it works as expected):
Datum demo(PG_FUNCTION_ARGS) {
float8 xi = 1;
float8 yi = 2;
float8 xe = 3;
float8 ye = 4;
int32 id = 1;
bool isnull;
TupleDesc tupdesc;
TupleDesc tupdesc_p;
Datum values[3];
Datum vi[2];
Datum ve[2];
HeapTuple tuple;
HeapTuple i;
HeapTuple e;
tupdesc_p = *RelationNameGetTupleDesc*("p_type");
vi[0] = Float8GetDatum(xi);
vi[1] = Float8GetDatum(yi);
i = heap_form_tuple(tupdesc_p, vi, &isnull);
ve[0] = Float8GetDatum(xe);
ve[1] = Float8GetDatum(ye);
e = heap_form_tuple(tupdesc_p, ve, &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] = HeapTupleGetDatum(i);
values[1] = HeapTupleGetDatum(e);
values[2] = Int32GetDatum(id);
tuple = heap_form_tuple(tupdesc, values, &isnull);
PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
}
On Mon, Sep 27, 2010 at 3:32 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Show quoted text
You've omitted to tell us just
what's going wrong for you.regards, tom lane
On Mon, Sep 27, 2010 at 2:52 AM, Marios Vodas <mvodas@gmail.com> wrote:
OK but what is the recommended way to get TupleDesc for p_type?
It's mentioned in the documentation...
http://www.postgresql.org/docs/current/static/xfunc-c.html#AEN47214
--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise Postgres Company