Re: Function name variable within a non-trigger function - Mailing list pgsql-general

From Michael Fuhr
Subject Re: Function name variable within a non-trigger function
Date
Msg-id 20051126023638.GA85424@winnie.fuhr.org
Whole thread Raw
In response to Function name variable within a non-trigger function  (Berend Tober <btober@seaworthysys.com>)
List pgsql-general
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

pgsql-general by date:

Previous
From: Jaime Casanova
Date:
Subject: Re: howto create dynamic table name in plpgsql function.
Next
From: Wes
Date:
Subject: Re: Deadlock Detected (revisited)