Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id AD5C5B5DC02 for ; Mon, 15 Aug 2011 04:27:48 -0300 (ADT) 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 65250-08 for ; Mon, 15 Aug 2011 07:27:42 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from exprod7og106.obsmtp.com (exprod7og106.obsmtp.com [64.18.2.165]) by mail.postgresql.org (Postfix) with ESMTP id A4D11B5DBBE for ; Mon, 15 Aug 2011 04:27:41 -0300 (ADT) Received: from mail-fx0-f49.google.com ([209.85.161.49]) (using TLSv1) by exprod7ob106.postini.com ([64.18.6.12]) with SMTP ID DSNKTkjKbLhJLQtsv4hC1p+8hAXerymqkfh7@postini.com; Mon, 15 Aug 2011 00:27:42 PDT Received: by fxd20 with SMTP id 20so3994877fxd.8 for ; Mon, 15 Aug 2011 00:27:38 -0700 (PDT) Received: by 10.223.89.131 with SMTP id e3mr5097125fam.144.1313393258787; Mon, 15 Aug 2011 00:27:38 -0700 (PDT) Received: from [192.168.1.183] (dsl-hkibrasgw2-ff7ac300-240.dhcp.inet.fi [88.195.122.240]) by mx.google.com with ESMTPS id e10sm4783194fak.42.2011.08.15.00.27.36 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 15 Aug 2011 00:27:37 -0700 (PDT) Message-ID: <4E48CA64.5000900@enterprisedb.com> Date: Mon, 15 Aug 2011 10:27:32 +0300 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; fi-FI; rv:1.9.2.18) Gecko/20110626 Icedove/3.1.11 MIME-Version: 1.0 To: Craig Miles CC: pgsql-bugs@postgresql.org Subject: Re: Error calling function which returns a ROWTYPE from within another function References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201108/93 X-Sequence-Number: 30905 On 15.08.2011 08:54, Craig Miles wrote: > I am experiencing unexpected behaviour on Postgres 9.0.4 using pl/pgsql > relating to selecting from a function that returns a ROWTYPE into a ROWTYPE > variable from within another function. > In the example below I : > > 1) Create a table, TESTTABLE and insert a row. > 2) Create a function FN_TEST_GET_ROW that returns a row of ROWTYPE TESTTABLE > based on selection of a single row from TESTTABLE > 3) Create a test harness in the form of a function TESTX that calls > FN_TEST_GET_ROW with ID=1 > 4) Call the test harness > > The error shown below is returned unexpectedly > ERROR: invalid input syntax for integer: "(1,Fred)" > > I would just expect the values (1, Fred) to be returned which is what > happens if I execute > > SELECT fn_test_get_row(1); > > directly. You need to define testx as: CREATE OR REPLACE FUNCTION testx() RETURNS testtable AS $$ DECLARE i_row testtable; BEGIN -- Note the "* FROM " SELECT * FROM fn_test_get_row(1) INTO i_row; -- Success RETURN i_row; END; $$ LANGUAGE plpgsql; It's surprising at first, but makes sense when you compare the SELECTs in testx and fn_test_get_row. In fn_test_get_row: SELECT * INTO i_row FROM testtable WHERE id = a; The SELECT returns two columns of types integer and varchar, and assigns them to the two fields in i_row. In testx you have: SELECT fn_test_get_row(1) INTO i_row; That SELECT returns only one column, of type testtable. It can't be assigned into i_row, which expects an integer and a varchar. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com