Postgresql c function returning one row with 2 fileds

From: lucamarletta <info(at)beopen(dot)it>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Postgresql c function returning one row with 2 fileds
Date: 2013-11-09 10:52:25
Message-ID: 1383994345608-5777581.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I'm new in postgresql c function and I start following examples.

I want to write a simple function that have inside an SQL and passing
parameter evaluete anbd return 2 fields as sum (for now to be simpler).

The function below has problem passing the check

(get_call_result_type(fcinfo, &resultTypeId, &resultTupleDesc) !=
TYPEFUNC_COMPOSITE)

If I remove this line I get 1 integer as result from this query

select * from pdc_imuanno(2012);
and error from

select (a).* from pdc_imuanno(2012) a;
because is not a composite type.

Question is I can prepare template for tuple if it's not correct this

resultTupleDesc = CreateTemplateTupleDesc(2, false);
TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "abp1", FLOAT4OID, -1,
0);
TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "abp2", FLOAT4OID, -1,
0);
And in

get_call_result_type(fcinfo, &resultTypeId, &resultTupleDesc)
fcinfo what is and where come from?

Thanks a lot for any help.

Luca

Datum
test_query(PG_FUNCTION_ARGS)
{
TupleDesc resultTupleDesc, tupledesc;
Oid resultTypeId;
Datum retvals[2];
bool retnulls[2];
HeapTuple rettuple;

sprintf(query,"SELECT anno, abp1::real, abp2::real "
"FROM imu.calcolo WHERE anno = %d AND codfis LIKE
'MR%';",PG_GETARG_INT32(0));

int ret;
int proc;
float abp1 = 0;
float abp2 = 0;
SPI_connect();
ret = SPI_exec(query,0);
proc = SPI_processed;

if (ret > 0 && SPI_tuptable != NULL)
{
HeapTuple tuple;
tupledesc = SPI_tuptable->tupdesc;
SPITupleTable *tuptable = SPI_tuptable;
for (j = 0; j < proc; j++)
{
tuple = tuptable->vals[j];
abp1 += DatumGetFloat4(SPI_getbinval(tuple, tupledesc, 2, &bisnull));
abp2 += DatumGetFloat4(SPI_getbinval(tuple, tupledesc, 3, &cisnull));
}
}
resultTupleDesc = CreateTemplateTupleDesc(2, false);
TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "abp1", FLOAT4OID, -1,
0);
TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "abp2", FLOAT4OID, -1,
0);

if (get_call_result_type(fcinfo, &resultTypeId, &resultTupleDesc) !=
TYPEFUNC_COMPOSITE) {
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context that
cannot accept type record")));
}
resultTupleDesc = BlessTupleDesc(resultTupleDesc);
SPI_finish();
retvals[0] = Float4GetDatum(abp1);
retvals[1] = Float4GetDatum(abp2);
retnulls[0] = false;
retnulls[1] = false;
rettuple = heap_form_tuple( resultTupleDesc, retvals, retnulls);
PG_RETURN_DATUM( HeapTupleGetDatum( rettuple ) );
}

--
View this message in context: http://postgresql.1045698.n5.nabble.com/Postgresql-c-function-returning-one-row-with-2-fileds-tp5777581.html
Sent from the PostgreSQL - hackers mailing list archive at Nabble.com.

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2013-11-09 10:56:08 Re: logical changeset generation v6.5
Previous Message David Rowley 2013-11-09 07:30:14 Re: patch to fix unused variable warning on windows build