Thread: Function name variable within a non-trigger function

Function name variable within a non-trigger function

From
Berend Tober
Date:
I know that within a trigger function the functin name can be referenced
by the special variable TG_NAME, so I could include raise an exception
that identified its source with a line like:

      RAISE EXCEPTION ''ERROR IN %'', TG_NAME;

Is there a similar set of special variables defined for "normal", i.e.,
non-trigger functions, too?



Re: Function name variable within a non-trigger function

From
Michael Fuhr
Date:
On Thu, Nov 24, 2005 at 10:44:19AM -0500, Berend Tober wrote:
> I know that within a trigger function the functin name can be referenced
> by the special variable TG_NAME, so I could include raise an exception
> that identified its source with a line like:
>
>      RAISE EXCEPTION ''ERROR IN %'', TG_NAME;

TG_NAME contains the name of the trigger, not the name of the
function the trigger calls.  If you define a trigger as

  CREATE TRIGGER footrig BEFORE INSERT ON foo
    FOR EACH ROW EXECUTE PROCEDURE trigfunc();

then the error message from the code you posted will be

  ERROR:  ERROR IN footrig

A trigger function can find its name by querying pg_trigger and pg_proc:

  funcname := p.proname
              FROM pg_trigger AS t JOIN pg_proc AS p ON p.oid = t.tgfoid
              WHERE t.tgrelid = TG_RELID AND t.tgname = TG_NAME;

> Is there a similar set of special variables defined for "normal", i.e.,
> non-trigger functions, too?

I'm not aware of a way for a non-trigger PL/pgSQL function to find
out its name or oid.  Functions written in C can do it.

--
Michael Fuhr