Re: How to return argument data type from sql function

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrus <kobruleht2(at)hot(dot)ee>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to return argument data type from sql function
Date: 2022-10-14 14:55:57
Message-ID: 1185464.1665759357@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Andrus <kobruleht2(at)hot(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dominique Devienne 2022-10-14 14:59:35 Re: [libpq] OIDs of extension types? Of custom types?
Previous Message Daniel Verite 2022-10-14 14:39:13 Re: Number of updated rows with LibPQ