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 1hl0vy-0000Ux-VC for pgsql-general@arkaria.postgresql.org; Wed, 10 Jul 2019 00:58:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hl0vx-0000tw-2i for pgsql-general@arkaria.postgresql.org; Wed, 10 Jul 2019 00:58:09 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hl0vw-0000s5-Jp for pgsql-general@lists.postgresql.org; Wed, 10 Jul 2019 00:58:08 +0000 Received: from mail-pg1-x541.google.com ([2607:f8b0:4864:20::541]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hl0vt-0004Gz-DW for pgsql-general@lists.postgresql.org; Wed, 10 Jul 2019 00:58:07 +0000 Received: by mail-pg1-x541.google.com with SMTP id i18so299531pgl.11 for ; Tue, 09 Jul 2019 17:58:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=a7JBYpk5ei6TBlwQdSpf8aDy1SFLt/fQAHL+ns6wHHU=; b=fFD8PxTzMVTyxLkU/Mlj6dojG0VqlKbmBkH89JPcHZt7YZ8k/w/a/H5OwuWyZ3uQbb tPfiAZyw3PKWSaHodXByelc9QJtMnYrqohznLx1jLORQfLNbd237XUkQZQI9xd/PMUQs 63nA9zJ5FnFF3XYYxoSHJkGzQSMwaU1FHfdFzrOa7uvGVK5iNLYKPFdNm+/SyGAwGseP dZR2bnR0Az04h402VYhLLaw5hMXYTgP7wF2QXc5yh6bNH8xL148mp6CtBRjdFE6Bi5JF 3iyWlMVmpmFBKwALXfMvkn2DJiboKTdqO4zsGk9jLky94QZSVcBuXc7ZrGJADFbPCOdW h5gQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=a7JBYpk5ei6TBlwQdSpf8aDy1SFLt/fQAHL+ns6wHHU=; b=MOAArlx+LnGKw+v7mVDFBILfZ4XE1f/4E+R9pBhoVu1t080rJ3yZNMk3WZ3ii82TXR zgMOiBvItK1Rbi9u+DKUf6gSO8VUM1ifX5VNIN0iodReVE6V2/XoYl4xXMBMjq/GZ0JG 1GFRpu1dVnqvCYs6wZ/5WOqzQadLme42fnqjIoUCFdsdN55O3hm3TCT4wuhitZ84VCgE xxjKnbjq9N4Y8nTvFbzlWhVPK/TTSLRjSu1BR/tL0HWUZkQ0yEvamnjAsckTmDBPWMPy 8RJWmcYz5OBrrGaF+A+OTgjW6qKpwaPonOdsgXXD9HFuSMn4u8ftiMi7NprYROFzwCYn H1Yw== X-Gm-Message-State: APjAAAVOKCpHQVJ+rzDy2ybepLGZpTymS2etqwlHBEmyWiIcYheDiHbv XwqjQCQAmAoFPR7SzpTPWdrikMqWd/S/ecqKCVDfmNgbaUQxDgeNV8Y60vQFq7JG6onnD5OTMil XzW/+0Wu8ef0uEZWGX0c6VbxK0+9WG16Nd8P526jRP2xLHmLPzToFOwKCSkCldQI+J4UZ+aDK/N gVPTe7OooK21oe9JZwbc69 X-Google-Smtp-Source: APXvYqxxDhEBxeJ/XBq9c+l8J/ASD3SDu0eCfQS7ir9myRFe/sSODYsbP5H2ACrrk9UZmGb7lLXXCA== X-Received: by 2002:a63:2264:: with SMTP id t36mr27846901pgm.87.1562720283715; Tue, 09 Jul 2019 17:58:03 -0700 (PDT) Received: from osaka.octopusnet (p274154-ipngn12401marunouchi.tokyo.ocn.ne.jp. [153.201.73.154]) by smtp.gmail.com with ESMTPSA id d14sm263556pfo.154.2019.07.09.17.58.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 Jul 2019 17:58:03 -0700 (PDT) Subject: Re: how to return rows of data via function written by language C strict To: Christoph Bilz , pgsql-general@lists.postgresql.org References: From: Ian Barwick Message-ID: <2393c6ab-6d63-8a5b-0ef7-5c01d1eee52f@2ndquadrant.com> Date: Wed, 10 Jul 2019 09:58:00 +0900 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 7/10/19 5:00 AM, Christoph Bilz wrote: > Hello, > > i want write functions like this: > > |CREATEFUNCTIONfoo(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; … 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