From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andrus <kobruleht2@hot.ee>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: How to return argument data type from sql function
Date: Fri, 14 Oct 2022 10:55:57 -0400
Message-ID: <1185464.1665759357@sss.pgh.pa.us> (raw)
In-Reply-To: <86fe1ce7-3d6f-b723-b0fc-0eb796100588@hot.ee>
References: <86fe1ce7-3d6f-b723-b0fc-0eb796100588@hot.ee>
Andrus <kobruleht2@hot.ee> 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, kobruleht2@hot.ee
Subject: Re: How to return argument data type from sql function
In-Reply-To: <1185464.1665759357@sss.pgh.pa.us>
References: <86fe1ce7-3d6f-b723-b0fc-0eb796100588@hot.ee> <1185464.1665759357@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox