Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hkwHj-0006OO-J5 for pgsql-general@arkaria.postgresql.org; Tue, 09 Jul 2019 20:00:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hkwHh-0005zT-Dg for pgsql-general@arkaria.postgresql.org; Tue, 09 Jul 2019 20:00:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hkwHg-0005xT-Re for pgsql-general@lists.postgresql.org; Tue, 09 Jul 2019 20:00:17 +0000 Received: from mr85p00im-hyfv06011301.me.com ([17.58.23.184]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hkwHc-0002TW-Rp for pgsql-general@lists.postgresql.org; Tue, 09 Jul 2019 20:00:16 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=04042017; t=1562702409; bh=a/rr9SqSTw4qJMNwqD+8mUBxCIskausPRmfvHe/9RZo=; h=From:Content-Type:Subject:Message-Id:Date:To; b=JI3dBUir/y0NB2PHFgruW4MyGwuJqHGhTR/isGTBS5SXBHeADnp01lGmlUQrA6X0d CsdqjMfUDtghQlvcYC+G631mYukiXlCnHZ6Of9rMhTSq6M4Ij4XCrkz4bSnwoTPOVl JhcavRVJVTvn3tGNaE1/UuDtnKYSsmwu7/v2zzkX7r6aVlrB1FdFSqGMtttFryn8Yx VyaAwM/l3imBq8QlWsz2A+k/vFgPWPk9wYXzVs/5hHGZ6L+eOPIW8bs1BP9WYjHFPO S4vvyOT4rqIMZSeyIGSf8R303uBaqGSmU3rMM/mhz3xc4A1dPYC4s0wRwj3FESYndm c38+ZlwA37CNw== Received: from nordlichts-imac.local (dslb-188-101-154-140.188.101.pools.vodafone-ip.de [188.101.154.140]) by mr85p00im-hyfv06011301.me.com (Postfix) with ESMTPSA id 33733580FAB for ; Tue, 9 Jul 2019 20:00:09 +0000 (UTC) From: Christoph Bilz Content-Type: multipart/alternative; boundary="Apple-Mail=_134D9D34-5421-45E8-ABE5-3ABE86F55C44" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.8\)) Subject: how to return rows of data via function written by language C strict Message-Id: Date: Tue, 9 Jul 2019 22:00:06 +0200 To: pgsql-general@lists.postgresql.org X-Mailer: Apple Mail (2.3445.104.8) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-07-09_08:,, signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1812120000 definitions=main-1907090238 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_134D9D34-5421-45E8-ABE5-3ABE86F55C44 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hello, i want write functions like this: CREATE FUNCTION foo(text) returns real as '/foo.dll', 'foo' = LANGUAGE C STRICT;" CREATE FUNCTION foo2(text) returns table(c1 text, c2 int) as = '/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; =E2=80=A6 and the rows will be returned. I read the chapter https://www.postgresql.org/docs/11/spi.html = and especially=20 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 =E2=80=9Eone=E2=80=9C 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 =3D DatumGetArrayTypeP(_row_val); c_array =3D (float *)ARR_DATA_PTR(pg_array); pg_array_size =3D ARR_DIMS(pg_array)[0]; =E2=80=94> so it=E2=80=99s 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) !=3D 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 =3D 0; j < 100; j++) { rs[j] =3D Float8GetDatum(c_array[j]); } tuplen =3D tupdesc->natts; nulls =3D palloc(tuplen * sizeof(bool)); tuple =3D heap_form_tuple(tupdesc, rs, nulls); pfree(nulls); SPI_finish(); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); But it doesn=E2=80=99t work. Either the get_call_result_type fails = because the function definition doesn=E2=80=99t match or the the client = process crashes because smth. happens and I don=E2=80=99t 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. --Apple-Mail=_134D9D34-5421-45E8-ABE5-3ABE86F55C44 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 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; =E2=80=A6 =
and the rows will be returned.
I read the chapter
https://www.postgresql.org/docs/11/spi.html
and especially =








= --Apple-Mail=_134D9D34-5421-45E8-ABE5-3ABE86F55C44--