0

I have the following function:

func_name(p_1, p_2, p_3, ...)

Is there any way in the body of the function to query and get, for example:

a) the total number of the arguments/parameters of the current function

b) the type of them

Of course, I could modify the function as

func_name(n, ..., p_1, p_2, p_3, ...)

and include the info I require as arguments but it does not make me happy!

I would prefer to create a table with all functions' info (function name, function argument names and types) and query that when I have to.

Tia

Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
SONewbiee
  • 363
  • 2
  • 15
  • 1
    Not sure about a function getting its own name, but this answer shows how you can get a function's parameter names/types/count: https://stackoverflow.com/questions/12127274/how-to-get-function-parameter-lists-so-i-can-drop-a-function – 404 Mar 21 '18 at 12:21

1 Answers1

2

you can try parsing it. lets fn() be:

t=# create or replace function fn(i int, t text) returns void as $$
declare
 c text;
 a text[];
begin
 GET DIAGNOSTICS c = PG_CONTEXT;
 raise info 'name: %',substring(c from 'function (.*?)\(');
 a := string_to_array(substring(c from '\((.*?)\)'), ',');
 raise info 'args: %',a;
 raise info 'names: %', (select proargnames from pg_proc where proname = substring(c from 'function (.*?)\('));
 raise info 'amount: %',array_length(a,1);
end;
$$ language plpgsql;
CREATE FUNCTION

so the info is:

t=# select fn(1,null);
INFO:  name: fn
INFO:  args: {integer,text}
INFO:  names: {i,t}
INFO:  amount: 2
 fn
----

(1 row)

of course it will be more complicated to support not unique function name select and so on. This is just a simple example

Vao Tsun
  • 47,234
  • 13
  • 100
  • 132