Getting the name of the current function inside a function using plpgsql

In any case, can you get the function name from the plpgsql function? Or even OID functions?

I know that in plpgsql there are some "special" variables (like FOUND), but it doesn't seem to be that way. (Although, I read where it seems possible if your function is written in C). This is not critical, but it will do something that I do a little better / less fragile.

I am using PostgreSQL v. 9.1.5

+8
plpgsql postgresql
source share
3 answers

For triggers, use TG_NAME to get the name of the trigger (rather than the trigger function).

You also have current_query() to get the top-level query executed by the application, which is the main reason for executing your function. It will not show you any intermediate functions.

Otherwise, indeed, AFAIK and I really looked for it some time ago, when I wanted to print the "current function stack" for debugging. Others may know more.

UPDATE: in Pg 9.4 and later, you can also use PG_CONTEXT for the call stack, but not just for the current function name.

+2
source share

Update: The ability to take a call stack is available in PostgreSQL 9.4

No, there is no way to get the name of the currently executing function in the plpgsql function.

A few years ago, I wrote functions to access the call stack - it is part of orafce. You can get the latest function from stack

+5
source share

As in Postgres 9.4, the function below returns its name:

 CREATE OR REPLACE FUNCTION your_schema.get_curr_fx_name() RETURNS text AS $$ DECLARE stack text; fcesig text; BEGIN GET DIAGNOSTICS stack = PG_CONTEXT; fcesig := substring(stack from 'function (.*?) line'); RETURN fcesig::regprocedure::text; END; $$ LANGUAGE plpgsql; 
+5
source share

All Articles