Re: How to return argument data type from sql function - Mailing list pgsql-general

From Tom Lane
Subject Re: How to return argument data type from sql function
Date
Msg-id 1185464.1665759357@sss.pgh.pa.us
Whole thread Raw
In response to How to return argument data type from sql function  (Andrus <kobruleht2@hot.ee>)
Responses Re: How to return argument data type from sql function
List pgsql-general
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



pgsql-general by date:

Previous
From: "Daniel Verite"
Date:
Subject: Re: Number of updated rows with LibPQ
Next
From: Dominique Devienne
Date:
Subject: Re: [libpq] OIDs of extension types? Of custom types?