pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
how to return rows of data via function written by language C strict
3+ messages / 3 participants
[nested] [flat]

* how to return rows of data via function written by language C strict
@ 2019-07-09 20:00  Christoph Bilz <christoph.bilz@icloud.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Christoph Bilz @ 2019-07-09 20:00 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hello,

i want write functions like this:

CREATE FUNCTION foo(text) returns real as '<path>/foo.dll', 'foo'  LANGUAGE C STRICT;"
CREATE FUNCTION foo2(text) returns table(c1 text, c2 int) as '<path>/foo2.dll', 'foo'  LANGUAGE C STRICT;
So far, so clear. I don't want to return one scalar value or SETOF smth, I want to start the function like this:
select * from foo; … and the rows will be returned.
I read the chapter
https://www.postgresql.org/docs/11/spi.html <https://www.postgresql.org/docs/11/spi.html;
and especially 
https://www.postgresql.org/docs/11/xfunc-c.html#id-1.8.3.13.11 <https://www.postgresql.org/docs/11/xfunc-c.html#id-1.8.3.13.11;

again and again but I found no solution. So, the functions should return rows of data not just „one“ result or composite types.
The documentation regarding usable examples are very sparse, so I found and tried with this pice of code:

ArrayType* pg_array = DatumGetArrayTypeP(_row_val);
c_array = (float *)ARR_DATA_PTR(pg_array);
pg_array_size = ARR_DIMS(pg_array)[0];
—> so it’s clear  how do I get my data via SPI* functionalty, the result set is within the pg_array or the casted c_array.

I found this within funcapi.h:
 /* Type categories for get_call_result_type and siblings */
typedef enum TypeFuncClass
{
    TYPEFUNC_SCALAR,            /* scalar result type */
    TYPEFUNC_COMPOSITE,         /* determinable rowtype result */
    TYPEFUNC_COMPOSITE_DOMAIN,  /* domain over determinable rowtype result */
    TYPEFUNC_RECORD,            /* indeterminate rowtype result */
    TYPEFUNC_OTHER              /* bogus type, eg pseudotype */
} TypeFuncClass;

and tried:

TupleDesc   tupdesc;
HeapTuple   tuple;
Datum       rs[100];
int         tuplen;
bool        nulls;

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

BlessTupleDesc(tupdesc);

for (int j = 0; j < 100; j++) {
    rs[j] = Float8GetDatum(c_array[j]);
}

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

tuple = heap_form_tuple(tupdesc, rs, nulls);

pfree(nulls);

SPI_finish();

PG_RETURN_DATUM(HeapTupleGetDatum(tuple));


But it doesn’t work. Either the get_call_result_type fails because the function definition doesn’t match or the the client process crashes because smth. happens and I don’t know how this stuff should work.
So, due to the lack of examples in general and the sparse documentation about it, any help will be appreciate.
Thanks.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: how to return rows of data via function written by language C strict
@ 2019-07-10 00:58  Ian Barwick <ian.barwick@2ndquadrant.com>
  parent: Christoph Bilz <christoph.bilz@icloud.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Ian Barwick @ 2019-07-10 00:58 UTC (permalink / raw)
  To: Christoph Bilz <christoph.bilz@icloud.com>; pgsql-general@lists.postgresql.org

On 7/10/19 5:00 AM, Christoph Bilz wrote:
> Hello,
> 
> i want write functions like this:
> 
> |CREATEFUNCTIONfoo(text)returns real as'<path>/foo.dll','foo'LANGUAGE C STRICT;" CREATE FUNCTION foo2(text) returns table(c1 text, c2 int) as '<path>/foo2.dll', 'foo' LANGUAGE C STRICT;|
> 
> So far, so clear. I don't want to return one scalar value or SETOF smth, I want to start the function like this:
> 
> select * from foo; … and the rows will be returned.
> 
> I read the chapter
> 
> https://www.postgresql.org/docs/11/spi.html
> 
> and especially
> 
> https://www.postgresql.org/docs/11/xfunc-c.html#id-1.8.3.13.11
> 
> again and again but I found no solution. So, the functions should return rows of data not just „one“ result or composite types.
> The documentation regarding usable examples are very sparse, so I found and tried with this pice of code:
> 
> |ArrayType*pg_array =DatumGetArrayTypeP(_row_val);c_array =(float *)ARR_DATA_PTR(pg_array);pg_array_size =ARR_DIMS(pg_array)[0];|
> 
> —> so it’s clear  how do I get my data via SPI* functionalty, the result set is within the pg_array or the casted c_array.
> 
> I found this within funcapi.h:
> 
> |/* Type categories for get_call_result_type and siblings */typedef enum TypeFuncClass {TYPEFUNC_SCALAR,/* scalar result type */TYPEFUNC_COMPOSITE,/* determinable rowtype result */TYPEFUNC_COMPOSITE_DOMAIN,/* domain over determinable rowtype result */TYPEFUNC_RECORD,/* indeterminate rowtype result */TYPEFUNC_OTHER /* bogus type, eg pseudotype */}TypeFuncClass;|
> 
> 
> and tried:
> 
> |TupleDesc tupdesc;HeapTuple tuple;Datum rs[100];int tuplen;bool nulls;if(get_call_result_type(fcinfo,NULL,&tupdesc)!=TYPEFUNC_RECORD)ereport(ERROR,(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),errmsg("function returning record called in context ""that cannot accept type record")));BlessTupleDesc(tupdesc);for(int j =0;j <100;j++){rs[j]=Float8GetDatum(c_array[j]);}tuplen =tupdesc->natts;nulls =palloc(tuplen *sizeof(bool));tuple =heap_form_tuple(tupdesc,rs,nulls);pfree(nulls);SPI_finish();PG_RETURN_DATUM(HeapTupleGetDatum(tuple));|
> 
> 
> 
> But it doesn’t work. Either the get_call_result_type fails because the function definition doesn’t match or the the client process crashes because smth. happens and I don’t know how this stuff should work.
> So, due to the lack of examples in general and the sparse documentation about it, any help will be appreciate.

A good place to find working examples is the source code of the various contrib modules.


Regards

Ian Barwick

-- 
  Ian Barwick                   https://www.2ndQuadrant.com/
  PostgreSQL Development, 24x7 Support, Training & Services





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: how to return rows of data via function written by language C strict
@ 2019-07-10 01:16  David G. Johnston <david.g.johnston@gmail.com>
  parent: Christoph Bilz <christoph.bilz@icloud.com>
  1 sibling, 0 replies; 3+ messages in thread

From: David G. Johnston @ 2019-07-10 01:16 UTC (permalink / raw)
  To: Christoph Bilz <christoph.bilz@icloud.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Tue, Jul 9, 2019 at 1:00 PM Christoph Bilz <christoph.bilz@icloud.com>
wrote:

> Hello,
>
> i want write functions like this:
>
> CREATE FUNCTION foo(text) returns real as '<path>/foo.dll', 'foo'  LANGUAGE C STRICT;"
> CREATE FUNCTION foo2(text) returns table(c1 text, c2 int) as '<path>/foo2.dll', 'foo'  LANGUAGE C STRICT;
>
> So far, so clear. I don't want to return one scalar value or SETOF smth, I want to start the function like this:
>
> select * from foo; … and the rows will be returned.
>
> Maybe SQL or pl/pgSQL would be a better choice then?

Also, if you plan to return more than one row you are, by definition,
creating a SETOF (TABLE is just shorthand) function.

Also, for what its worth I have no idea with "smth" means here - but I also
don't program C.

> if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_RECORD)
>     ereport(ERROR,
>     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
>         errmsg("function returning record called in context "
>             "that cannot accept type record")));
>
> The inequality check above seems wrong - you want to error if you are
presented with a record, not when you aren't.

But it doesn’t work. Either the get_call_result_type fails because the
> function definition doesn’t match or the the client process crashes because
> smth. happens and I don’t know how this stuff should work.
> So, due to the lack of examples in general and the sparse documentation
> about it, any help will be appreciate.
>

As Ian noted, contrib is usually the recommended source for up-to-date
coding examples.

David J.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2019-07-10 01:16 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-07-09 20:00 how to return rows of data via function written by language C strict Christoph Bilz <christoph.bilz@icloud.com>
2019-07-10 00:58 ` Ian Barwick <ian.barwick@2ndquadrant.com>
2019-07-10 01:16 ` David G. Johnston <david.g.johnston@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox