Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 197F71336FA7 for ; Thu, 17 Feb 2011 03:58:16 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 28845-06 for ; Thu, 17 Feb 2011 07:58:08 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp2.oslo.dnmi.no (smtp2.oslo.dnmi.no [128.39.62.242]) by mail.postgresql.org (Postfix) with ESMTP id C11FE133616B for ; Thu, 17 Feb 2011 03:58:07 -0400 (AST) Received: from imap1b.met.no ([157.249.16.99] helo=imap1.met.no) by smtp2.oslo.dnmi.no with esmtp (Exim 4.69) (envelope-from ) id 1PpykY-0002uf-8S; Thu, 17 Feb 2011 07:58:06 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by imap1.met.no (Postfix) with ESMTP id 2B2B23CEA459; Thu, 17 Feb 2011 07:58:06 +0000 (UTC) X-Virus-Scanned: amavisd-new at met.no Received: from imap1.met.no ([127.0.0.1]) by localhost (imap1.met.no [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 0d+15DIUGln5; Thu, 17 Feb 2011 07:58:05 +0000 (UTC) Received: from imap1.met.no (imap1.met.no [157.249.16.100]) by imap1.met.no (Postfix) with ESMTP id 97C943CEA466; Thu, 17 Feb 2011 07:58:05 +0000 (UTC) Date: Thu, 17 Feb 2011 07:58:05 +0000 (UTC) From: =?utf-8?Q?Vegard_B=C3=B8nes?= To: Robert Haas Cc: Tom Lane , pgsql-bugs@postgresql.org Message-ID: <61692821.85256.1297929485588.JavaMail.root@imap1b> In-Reply-To: Subject: Re: BUG #5883: Error when mixing SPI_returntuple with returning regular HeapTuple MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [157.249.32.237] X-Mailer: Zimbra 6.0.7_GA_2476.RHEL4 (ZimbraWebClient - FF3.0 (Linux)/6.0.7_GA_2473.UBUNTU8_64) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 201102/154 X-Sequence-Number: 29426 The updated example is pasted in below.=20 The given example will cause an error. You may (for example) change the cod= e in the stwitch statement like this, and it will work like a charm: from: =09case 1: =09=09SRF_RETURN_NEXT(funcctx, get_data_alternative_b()); =09=09//get_data_alternative_a(ret, isnull); to: =09case 1: =09=09//SRF_RETURN_NEXT(funcctx, get_data_alternative_b()); =09=09get_data_alternative_a(ret, isnull); VG #include #include #include #include #ifdef PG_MODULE_MAGIC PG_MODULE_MAGIC; #endif /* * CREATE TABLE test (a int, b int); * INSERT INTO test VALUES (1, 2); * CREATE FUNCTION run_test() RETURNS SETOF test AS 'SOMEWHERE/something.so= ', 'run_test' LANGUAGE C VOLATILE; * * SELECT * FROM run_test(); * * ERROR: rows returned by function are not all of the same row type * */ static const char * query =3D "SELECT a, b FROM test"; static void get_data_alternative_a(Datum * data_out, bool * isnull) { =09SPI_connect(); =09SPI_execute(query, true, 1); =09data_out[0] =3D SPI_getbinval(* SPI_tuptable->vals, SPI_tuptable->tupdes= c, 1, & isnull[0]); =09data_out[1] =3D SPI_getbinval(* SPI_tuptable->vals, SPI_tuptable->tupdes= c, 2, & isnull[1]); =09SPI_finish(); } static Datum get_data_alternative_b() { =09SPI_connect(); =09SPI_execute(query, true, 1); =09HeapTupleHeader ret =3D SPI_returntuple(* SPI_tuptable->vals, SPI_tuptab= le->tupdesc); =09Datum d =3D PointerGetDatum(ret); =09SPI_finish(); =09return d; } PG_FUNCTION_INFO_V1(run_test); Datum run_test(PG_FUNCTION_ARGS) { =09FuncCallContext * funcctx; =09int * return_count =3D NULL; =09if ( SRF_IS_FIRSTCALL() ) =09{ =09=09funcctx =3D SRF_FIRSTCALL_INIT(); =09=09//SPI_connect(); =09=09MemoryContext oldcontext =3D MemoryContextSwitchTo(funcctx->multi_cal= l_memory_ctx); =09=09TupleDesc tupdesc; =09=09if (get_call_result_type(fcinfo, NULL, &tupdesc) !=3D TYPEFUNC_COMPOS= ITE) =09=09=09ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg( =09=09=09=09=09"function returning record called in context " =09=09=09=09=09=09"that cannot accept type record"))); =09=09funcctx->tuple_desc =3D BlessTupleDesc(tupdesc); =09=09return_count =3D (int *) palloc(sizeof(int)); =09=09MemoryContextSwitchTo(oldcontext); =09=09* return_count =3D 0; =09=09funcctx->user_fctx =3D (void*) return_count; =09} =09funcctx =3D SRF_PERCALL_SETUP(); =09return_count =3D (int *) funcctx->user_fctx; =09Datum ret[2]; =09bool isnull[2]; =09switch ( (* return_count) ++ ) =09{ =09case 0: =09=09//SRF_RETURN_NEXT(funcctx, get_data_alternative_b()); =09=09get_data_alternative_a(ret, isnull); =09=09break; =09case 1: =09=09SRF_RETURN_NEXT(funcctx, get_data_alternative_b()); =09=09//get_data_alternative_a(ret, isnull); =09=09break; =09default: =09=09SRF_RETURN_DONE(funcctx); =09} =09HeapTuple heap_tuple =3D heap_form_tuple(funcctx->tuple_desc, ret, isnul= l); =09Datum packed_ret =3D HeapTupleGetDatum(heap_tuple); =09SRF_RETURN_NEXT(funcctx, packed_ret); } ----- Original Message ----- Fra: "Robert Haas" Til: "Vegard B=C3=B8nes" Kopi: "Tom Lane" , pgsql-bugs@postgresql.org Sendt: 17. februar 2011 02:41:47 Emne: Re: [BUGS] BUG #5883: Error when mixing SPI_returntuple with returnin= g regular HeapTuple On Tue, Feb 15, 2011 at 2:43 AM, Vegard B=C3=B8nes wr= ote: > Ok, I tried to modify the example functions get_data_alternative_a() and = get_data_alternative_b(), so that they start with SPI_connect, and end with= SPI_finish. Also I removed SPI_connect and SPI_finish from the main functi= on. As I understand you, that should have solved the problem. The result, h= owever, is exactly the same as before. You may get further if you post the modified code, instead of simply asserting that some code we can't see doesn't work. --=20 Robert Haas EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company