Re: How to return argument data type from sql function

Andrus <kobruleht2@hot.ee>

From: Andrus <kobruleht2@hot.ee>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Adrian Klaver <adrian.klaver@aklaver.com>, pgsql-general <pgsql-general@postgresql.org>
Date: 2022-10-14T21:48:35Z
Lists: pgsql-general
Hi!

 >Yeah, you could do that if you have the column information at hand.
> Personally I'd also throw in "... and atttypid = 'bpchar'::regtype",
> because that atttypmod calculation will give you garbage for types
> other than bpchar and varchar.

I added this:

create or replace function public.ColWidth(p_namespace text, p_table 
text, p_field text)
     returns int as $f$
select atttypmod-4 from pg_namespace n, pg_class c, pg_attribute a
  where n.nspname = p_namespace and
     c.relnamespace = n.oid and
     c.relname = p_table and
     a.attrelid = c.oid and
     atttypid = 'bpchar'::regtype and
     a.attname = p_field;
$f$ LANGUAGE SQL ;

Tables with same name are in different schemas.

How to change this query so that it searches schemas in set search_path 
order and returns column width from it ? In this case p_namespace 
parameter can removed.

Or should it replaced with dynamic query like

execute 'select ' || p_field || ' from ' || p_table || ' limit 0'

and get column size from this query result somehow ?

Andrus.