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 1kfY01-00034j-MJ for pgsql-general@arkaria.postgresql.org; Thu, 19 Nov 2020 00:40:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kfY00-0004dg-Kg for pgsql-general@arkaria.postgresql.org; Thu, 19 Nov 2020 00:40:32 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kfXzz-0004Zz-NV for pgsql-general@lists.postgresql.org; Thu, 19 Nov 2020 00:40:32 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kfXzw-0006RG-VK for pgsql-general@lists.postgresql.org; Thu, 19 Nov 2020 00:40:30 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id 3E15FCAF; Wed, 18 Nov 2020 19:40:27 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Wed, 18 Nov 2020 19:40:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=x pWycJt2p/wjfhEveaVZp3HAJwZdp7e5OedzLzBOrz8=; b=ZwsK3Er+uDCpaUSYM 3eEhSKDkplhNbolz045HG00Xwdxr7MW2jOuYBQLVvZDtEy59rWKlXOeTDL+kGbhV f2OUmlqVcxXn3fHCAZUKAWBwWiZD7HOXhL9zKHr63k4hCNmlHLxojp5iJyURtEFe s4KHSSC23QQzWKT0RnhM/Bk2DTFMkjmlW1S3QUXb4fnoztUOHnWX420KpFK0qQ6y G+84G3Ih5NYfnTn2rNzMqwvVm8mnR2lRk+D5XFc0AukVfVsiw0C4hiFu1+XWATLg q/5mRtiWCIAnc35WVhQEqgPIYIiRTaGxB1E4xilV6R0HzkMIRPLUkBw4aE5J5DfG 26w5g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=xpWycJt2p/wjfhEveaVZp3HAJwZdp7e5OedzLzBOr z8=; b=kgE7o4JN1goQ5Kg3rXuHRa3WpizE2YWuWf+L+e+ehx63L7R1tQItJWhD4 s2b5S6nNPCIrrudxUA/ojSv2bwkvP4A4ci35lyfhzM2703RKQedwEZ/MNujglQGV 60jcPwWLUEj5W7lav0XX/O5z6fHy/uTIS3R8gj5QrlktB9JwUGnKPIKnC6hUfQGC /ACfu8EMhQlm8b0l9FAseSOu3mD3Qcbd66vtdy/MfXBzB8AKJYD1fqFaTQ7WZpmI sRevkbcgUDkt47caWJBFNCds25oPDYlaQwsV7Egfy7zCb03vnHNopTZbXgEy/1C2 6IawFEyJ3Yv0IGNH823GsnEZcrKlA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudefiedgvdefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeduudefjeetveffhefgudehtdfhvdetkeeiueehfeev veduteekleeuheegudehhfenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgne cukfhppeejhedrudejvddrudelrddufeelnecuvehluhhsthgvrhfuihiivgeptdenucfr rghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrh drtghomh X-ME-Proxy: Received: from [192.168.1.4] (75-172-19-139.tukw.qwest.net [75.172.19.139]) by mail.messagingengine.com (Postfix) with ESMTPA id B63B53280063; Wed, 18 Nov 2020 19:40:25 -0500 (EST) Subject: Re: create type with %type or %rowtype To: Post Gresql , pgsql-general@lists.postgresql.org References: <4fb49f43-6753-b692-9ca7-61052f63a929@taljaren.se> <82b59d98-bc35-7212-b950-45ff585f3b12@taljaren.se> <75e98bba-afa1-5c7a-f6a1-434fac52e4a0@aklaver.com> From: Adrian Klaver Message-ID: <9e599ad6-6ea9-1689-8c59-e816f3eef5cc@aklaver.com> Date: Wed, 18 Nov 2020 16:40:24 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.3 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 11/18/20 1:08 PM, Post Gresql wrote: > > On 2020-11-18 17:07, Adrian Klaver wrote: >> >> \d cell_per >>                        Foreign table "public.cell_per" >>   Column  |       Type        | Collation | Nullable | Default | FDW >> options >> ----------+-------------------+-----------+----------+---------+------------- >> >>  category | character varying |           |          |         | >>  cell_per | integer           |           |          |         | >> Server: test_db >> >> CREATE OR REPLACE FUNCTION public.type_test() >>  RETURNS cell_per >>  LANGUAGE plpgsql >> AS $function$ >> DECLARE >>     cp_type cell_per; >> BEGIN >>     SELECT INTO cp_type * from cell_per limit 1; >>     RETURN cp_type; >> END; >> $function$ >> >> select * from type_test(); >>   category  | cell_per >> ------------+---------- >>  H PREM 3.5 |       18 >> >> You can change the RETURNS to RETURNS SETOF and return multiple rows. >> > > I might be stupid, but where in the document for create function does it > say that the return type can be a table? It doesn't but the above is not returning a table, it is returning a (composite)type. And earlier in this thread, my comment: "To me that is redundant as a table has a composite type already." and from the %ROWTYPE portion of the plpgsql section: https://www.postgresql.org/docs/12/plpgsql-declarations.html#PLPGSQL-DECLARATION-ROWTYPES "(Since every table has an associated composite type of the same name, it actually does not matter in PostgreSQL whether you write %ROWTYPE or not. But the form with %ROWTYPE is more portable.)" > > From the doc for version 13 > https://www.postgresql.org/docs/13/sql-createfunction.html > > /|"rettype|/ > > The return data type (optionally schema-qualified). The return type > can be a base, composite, or domain type, or can reference the type > of a table column." > > -- Adrian Klaver adrian.klaver@aklaver.com