Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lMZwd-0007Oo-Bq for pgsql-sql@arkaria.postgresql.org; Wed, 17 Mar 2021 17:26:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lMZwc-0008MW-9l for pgsql-sql@arkaria.postgresql.org; Wed, 17 Mar 2021 17:26:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lMZwc-0008MP-3F for pgsql-sql@lists.postgresql.org; Wed, 17 Mar 2021 17:26:54 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lMZwa-0006ib-3y for pgsql-sql@lists.postgresql.org; Wed, 17 Mar 2021 17:26:53 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 12HHQoRF3523854; Wed, 17 Mar 2021 13:26:50 -0400 From: Tom Lane To: Gary Stainburn cc: "pgsql-sql@lists.postgresql.org" Subject: Re: select into composite type / return In-reply-to: <39dcfab9-4eb2-382b-9186-baf722ca56a0@ringways.co.uk> References: <39dcfab9-4eb2-382b-9186-baf722ca56a0@ringways.co.uk> Comments: In-reply-to Gary Stainburn message dated "Wed, 17 Mar 2021 16:48:45 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <3523852.1616002010.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Wed, 17 Mar 2021 13:26:50 -0400 Message-ID: <3523853.1616002010@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Gary Stainburn writes: > I have a function that takes 7 numerical inputs, performs calculations, > and then returns a composite type. > create type breakdown as >   f1    numeric(9,2), >   f2    numeric(9,2), >   f3    numeric(9,2), >   f4    numeric(9,2), >   f5    numeric(9,2), >   f6    numeric(9,2) > ); > create function do_breakdown( >   v1 numeric(9,2), >   v2 numeric(9,2), >   v3 numeric(9,2), >   v4 numeric(9,2), >   v5 numeric(9,2), >   v6 numeric(9,2), >   v7 numeric(9,2) > ) returns breakdown as $$ > DECLARE >   D breakdown; > BEGIN >  -- calculate breakdown >   return D; > END; > $$ > LANGUAGE PLPGSQL; > This works great, returning one row with the separate columns. > I now want to set up another function which will take a key, retrieve > the arguments from a table, and call the first function. The problem is > that I can't get the syntax correct to return the composite type.  I > have tried > create function do_breakdown(key text) returns breakdown as $$ > DECLARE >   v RECORD; > BEGIN >   select into v * from stored s where key s.key = key; >   RETURN do_breakdown(v.f1,v.f2,v.f3,v.f4,v.f5,v.f6); > END; > $$ > LANGUAGE PLPGSQL; > but it returns the whole thing as a single column. AFAICS these two functions will have exactly the same output behavior, ie returning a "breakdown" composite type. If they act differently for you, either you are calling them in different ways or you made a mistake somewhere. I can't help noticing that the RETURN in the second function is calling a six-argument function, which is not the one you showed first. Maybe that version of do_breakdown() returns something different? regards, tom lane