Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qlFcy-001xHx-0q for pgsql-general@arkaria.postgresql.org; Tue, 26 Sep 2023 21:29:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qlFcw-00FFNx-5M for pgsql-general@arkaria.postgresql.org; Tue, 26 Sep 2023 21:29:54 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qlFcu-00FFN0-AF for pgsql-general@lists.postgresql.org; Tue, 26 Sep 2023 21:29:52 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qlFcq-006oEL-KH for pgsql-general@lists.postgresql.org; Tue, 26 Sep 2023 21:29:50 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 9B9245C260F; Tue, 26 Sep 2023 17:29:47 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Tue, 26 Sep 2023 17:29:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm1; t= 1695763787; x=1695850187; bh=ooUd57z74bgvmbxHFkqB+ZAhhqVAUA9rAOR NYMZT3To=; b=fwzHQ1Opy2uIw7sF9cVTBq+la0YQaglYpUhhRWLaVoj/Z9yrYeC oWCVU13DRrYf3KKfthnQ7SHFH9bHjtgrnVE21TRl2PmKH9AXycQoxFtn/1ucm5H3 OX+v/MTS9cTrDgZC2zX9N1PT2eY1ZduzqRgdEMquguLeOtt2Qd3xLk37/rLZfv9A WTe4YoWo8s8VepWgLirilM+J7SRWqjcCtiHJre/KFni/IoHUMhiJOMjRLycJtMzH dKDgae6RsCZdEJU4VX/2hG7vOWYV8A6uqMS6/aPgGQW20SrWQ/fTrzWVXy7ug1Z5 hQ+Iznvhl4/KjnTLwD1oirqmc+nEA3+ntDw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1695763787; x= 1695850187; bh=ooUd57z74bgvmbxHFkqB+ZAhhqVAUA9rAORNYMZT3To=; b=A Jmi1i4zwcW1/KfKbmq27jA5CJEj+EozjfTBOOIgvDrEBQRfDDe33s+rSdjXBok93 2JWIFFONwGmHiunV53xpCknQ3lTC6FBGMh81RFjCTmD+nJjfhZri45xGgwAMlAqL QUltEB8JdgQ0h0f8niB98UUhW8G0G9kDIOb70aPYh5i0498IhBO2lzd3sE5F+E1x C8UkbK/TDUcZIFbTxNcvqgmb/eeMZW1H/cyssOZgLOdDuln0moAiGE+KzjKTT3bw fe+RjXzDp8y5GZYbU9qFgT3f4SoQINqgqWg0T0kDQPFqBAUy98HlLJn4rW3p+g6o WHatHvBShSDAe5kQRjdJw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvjedrtddtgddufeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefkffggfgfuhffvfhgjtgfgsehtje ertddtvdejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhephefgueethf dtiedvieehgfevuefhhfelkeejheejgffgleeuheejffdvheekueefnecuvehluhhsthgv rhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvh gvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 26 Sep 2023 17:29:47 -0400 (EDT) Message-ID: <623fe0c1-bdb4-49f7-9115-284159b3b137@aklaver.com> Date: Tue, 26 Sep 2023 14:29:46 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Ad hoc SETOF type definition? From: Adrian Klaver To: Ron , pgsql-general@lists.postgresql.org References: <9707e21e-eed3-3c06-821e-e935eab4c098@gmail.com> <432138.1695750386@sss.pgh.pa.us> <73c7a357-62fe-3274-7f27-7ef9adc3b9b5@gmail.com> <436021.1695752103@sss.pgh.pa.us> <538ed08d-55ba-4a51-8e46-4e424aaebb3c@aklaver.com> Content-Language: en-US In-Reply-To: <538ed08d-55ba-4a51-8e46-4e424aaebb3c@aklaver.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 9/26/23 13:29, Adrian Klaver wrote: > On 9/26/23 12:30, Ron wrote: >> On 9/26/23 13:15, Tom Lane wrote: >>> Ron writes: >>>> On 9/26/23 12:46, Tom Lane wrote: > >>> I'm not really sure what functionality you think is missing from RETURNS >>> TABLE, granting that you do want to return a set of rows and not exactly >>> one row. >> >> There might be some other logic in the body of the FOR loop that is >> not practical to embed in the body of the SELECT statement. > > I think you are conflating RETURNS TABLE and RETURN QUERY. You can build > a 'TABLE' from variables outside of a query. > As a very simple example: create table source(id integer, fld_1 varchar); insert into source values (1, 'cat'), (2, 'dog'), (3, 'fish'); CREATE OR REPLACE FUNCTION public.table_return(multiplier integer, suffix character varying) RETURNS TABLE(multiplied integer, fld_suffix character varying, rand_number numeric) LANGUAGE plpgsql AS $function$ DECLARE _id integer; _fld varchar; BEGIN FOR _id, _fld IN SELECT id, fld_1 FROM source LOOP multiplied = _id * multiplier; fld_suffix = _fld || '_' || suffix; rand_number = random() * 100; RETURN NEXT; END LOOP; END; $function$ ; select * from table_return(2, 'test'); multiplied | fld_suffix | rand_number ------------+------------+------------------ 2 | cat_test | 79.7745033326483 4 | dog_test | 12.5713231966519 6 | fish_test | 3.21770069680842 -- Adrian Klaver adrian.klaver@aklaver.com