Re: Function name variable within a non-trigger function

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Berend Tober <btober(at)seaworthysys(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Function name variable within a non-trigger function
Date: 2005-11-26 02:36:38
Message-ID: 20051126023638.GA85424@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Wes 2005-11-26 02:52:59 Re: Deadlock Detected (revisited)
Previous Message Jaime Casanova 2005-11-25 20:37:10 Re: howto create dynamic table name in plpgsql function.