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 1lMZLz-0005p6-7I for pgsql-sql@arkaria.postgresql.org; Wed, 17 Mar 2021 16:49:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lMZLy-0001hd-8B for pgsql-sql@arkaria.postgresql.org; Wed, 17 Mar 2021 16:49:02 +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 1lMZLn-0008MP-BI for pgsql-sql@lists.postgresql.org; Wed, 17 Mar 2021 16:48:51 +0000 Received: from hub.ringways.co.uk ([88.211.105.30] helo=ringways.co.uk) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lMZLl-0006Kc-3R for pgsql-sql@lists.postgresql.org; Wed, 17 Mar 2021 16:48:50 +0000 Received: from rwsys1.ringways.co.uk ([10.1.1.105] helo=eddie.ringways.co.uk) by ringways.co.uk with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94) (envelope-from ) id 1lMZLi-00059G-Nz for pgsql-sql@lists.postgresql.org; Wed, 17 Mar 2021 16:48:47 +0000 To: "pgsql-sql@lists.postgresql.org" From: Gary Stainburn Subject: select into composite type / return Message-ID: <39dcfab9-4eb2-382b-9186-baf722ca56a0@ringways.co.uk> Date: Wed, 17 Mar 2021 16:48:45 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Spam-Score: -48.4 (------------------------------------------------) X-Spam-Report: Spam detection software, running on the system "ollie2.ringways.co.uk", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see Gary Stainburn for details. Content preview: 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) ); [...] Content analysis details: (-48.4 points, 15.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -50 ALL_TRUSTED Passed through trusted hosts only via SMTP -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 2.0 FLOWED Conten Type format=flowed appears in the new SPAMS 0.1 SCORE_RCPTS Adding score for each recipient 0.1 SCORE_RCPTS Adding score for each recipient -0.7 AWL AWL: Adjusted score from AWL reputation of From: address 1.0 MISSING_FROM Missing From: header 1.0 RING_SAFE No description available. List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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.  Adding a typecase didn't help. I have also tried create function do_breakdown(key text) returns breakdown as $$ DECLARE   v RECORD;   D breakdown; BEGIN   select into v * from stored s where key s.key = key;   select into D * from do_breakdown(v.f1,v.f2,v.f3,v.f4,v.f5,v.f6);   RETURN D; END; $$ LANGUAGE PLPGSQL; but that also returns everything as a single column.  Any help would be appreciated. Gary