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 1ojM6g-0005Ut-Ma for pgsql-general@arkaria.postgresql.org; Fri, 14 Oct 2022 14:56:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ojM6f-0007eU-Ho for pgsql-general@arkaria.postgresql.org; Fri, 14 Oct 2022 14:56:13 +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 1ojM6f-0007eL-7c for pgsql-general@lists.postgresql.org; Fri, 14 Oct 2022 14:56:13 +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 1ojM6Y-00066A-UX for pgsql-general@postgresql.org; Fri, 14 Oct 2022 14:56:12 +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 29EEtvUt1185465; Fri, 14 Oct 2022 10:55:58 -0400 From: Tom Lane To: Andrus cc: pgsql-general Subject: Re: How to return argument data type from sql function In-reply-to: <86fe1ce7-3d6f-b723-b0fc-0eb796100588@hot.ee> References: <86fe1ce7-3d6f-b723-b0fc-0eb796100588@hot.ee> Comments: In-reply-to Andrus message dated "Thu, 13 Oct 2022 23:16:09 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <1185463.1665759357.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Fri, 14 Oct 2022 10:55:57 -0400 Message-ID: <1185464.1665759357@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Andrus writes: > PostgreSQL 12.2+ function is defined as >     create FUNCTION torus(eevarus text) returns text immutable AS $f$ >      select translate( $1, U&'\00f8\00e9', U&'\0451\0439' ); >     $f$ LANGUAGE SQL ; > if char(n) column is passed as argument, torus() should also return > char(n) data type. You can't preserve the length constraint, if that's what you're worried about; we simply don't track those for function arguments or results. > I tried to use bpchar instead on text >     create or replace FUNCTION torusbpchar(eevarus bpchar) returns > bpchar immutable AS $f$ >      select translate( $1, U&'\00f8\00e9', U&'\0451\0439' ); >     $f$ LANGUAGE SQL ; > torusbpchar(charcol) still returns text data type. Making separate functions for text and bpchar works for me. regression=# select pg_typeof(torus(f1)) from char_tbl; pg_typeof ----------- character Another possibility is to have just one function declared to take and return anyelement. You'd get failures at execution if the actual argument type isn't coercible to and from text (since translate() deals in text) but that might be fine. regards, tom lane