From: | Yuriy Rusinov <yrusinov(at)gmail(dot)com> |
---|---|
To: | Merlin Moncure <mmoncure(at)gmail(dot)com> |
Cc: | POSTGRES <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Return setof values from C-function |
Date: | 2013-12-10 15:44:32 |
Message-ID: | CAA5U4szBJ--4QtWmriHaP_Awgi-TDJQtrA+nKDW6iSVabprJpA@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
I have added code
Oid * oids = (Oid *)palloc (2*sizeof (Oid));
oids[0] = INT8OID;
oids[1] = FLOAT8OID;
if (get_call_result_type(fcinfo, oids, &tupdescRes) !=
TYPEFUNC_COMPOSITE)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context "
"that cannot accept type record")));
and try to obtain tupdescRes from get_call_result_type function, before
this I do
create type hist_point as (inum bigint, bin float8);
and
create or replace function histogram (varchar, float8, float8, int4)
returns setof hist_point
as '$libdir/libfloader.so', 'histogram' language 'c' strict security
definer;
but the result does not obtained.
On Tue, Dec 10, 2013 at 7:33 PM, Merlin Moncure <mmoncure(at)gmail(dot)com> wrote:
> On Tue, Dec 10, 2013 at 1:30 AM, Yuriy Rusinov <yrusinov(at)gmail(dot)com> wrote:
> > Dear Colleagues !
> >
> > I have to return setof values from C-function
> >
> > I wrote
> >
> > FuncCallContext *funcctx;
> > int call_cntr;
> > int max_calls;
> > AttInMetadata *attinmeta;
> > unsigned long il;
> > if (SRF_IS_FIRSTCALL())
> > {
> > MemoryContext oldcontext;
> > funcctx = SRF_FIRSTCALL_INIT();
> > oldcontext =
> MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
> > funcctx->max_calls = num;
> > 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")));
> > attinmeta = 0;//TupleDescGetAttInMetadata(tupdesc);
> > funcctx->attinmeta = 0;//attinmeta;
> > MemoryContextSwitchTo(oldcontext);
> > funcctx->tuple_desc = BlessTupleDesc( tupdesc );
> > elog (INFO, "1st row");
> > }
> > funcctx = SRF_PERCALL_SETUP();
> > call_cntr = funcctx->call_cntr;
> > max_calls = funcctx->max_calls;
> > attinmeta = funcctx->attinmeta;
> > HeapTuple tuple;
> > if (call_cntr < max_calls)
> > {
> > elog (INFO, "Rows");
> > unsigned long il = call_cntr;
> > Datum * hvalues = (Datum *)palloc (2*sizeof(Datum));
> > double val = gsl_histogram_get (gHist, il);
> > hvalues[0] = UInt32GetDatum (il);
> > hvalues[1] = Float8GetDatum (val);
> > bool * nulls = palloc( 2 * sizeof( bool ) );
> >
> > elog (INFO, "%lu", il);
> > tuple = heap_form_tuple( tupdesc, hvalues, nulls);
> > elog (INFO, "%lu", il);
> > //BuildTupleFromCStrings (attinmeta, hvalues);
> > pfree (nulls);
> > pfree (hvalues);
> > SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum( tuple ) );
> > }
> > else
> > {
> > SRF_RETURN_DONE(funcctx);
> > }
> > as written in documentation, but my function drops on
> > tuple = heap_form_tuple( tupdesc, hvalues, nulls);
> > Could you tell me, are there any errors ?
>
> Where is 'tupdesc' coming from. Don't you need to copy it from the
> function context?
>
> merlin
>
--
Best regards,
Sincerely yours,
Yuriy Rusinov.
From | Date | Subject | |
---|---|---|---|
Next Message | Merlin Moncure | 2013-12-10 15:57:09 | Re: Return setof values from C-function |
Previous Message | Merlin Moncure | 2013-12-10 15:33:49 | Re: Return setof values from C-function |