Re: Determine the name of the calling function

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jack Kaufman <jack(dot)kaufman(at)sanmina(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Determine the name of the calling function
Date: 2013-01-18 20:09:03
Message-ID: 26370.1358539743@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Jack Kaufman <jack(dot)kaufman(at)sanmina(dot)com> writes:
> By the way, PG_EXCEPTION_CONTEXT provided only the (currently executing)
> API function name and line number of the exception (where I caused a
> divide-by-zero). No function-call stack. :( But thanks, anyway.

Really? I tried this:

create function f1() returns int language plpgsql as $$
declare x int; t text;
begin
begin
x := 1/0;
exception
when division_by_zero then
get stacked diagnostics t = pg_exception_context;
raise notice '%', t;
end;
return 0;
end
$$;

create function f2() returns int language plpgsql as $$
begin
return f1();
end$$;

and I get

regression=# select f2();
NOTICE: PL/pgSQL function f1() line 5 at assignment
PL/pgSQL function f2() line 3 at RETURN
CONTEXT: PL/pgSQL function f2() line 3 at RETURN
f2
----
0
(1 row)

So the result of pg_exception_context clearly does contain the info you
want. Pulling it out is left as an exercise for the reader ;-)

Curiously, the behavior doesn't seem to be totally consistent --- when I
run the same case again, I get an additional line of context:

regression=# select f2();
NOTICE: SQL statement "SELECT 1/0"
PL/pgSQL function f1() line 5 at assignment
PL/pgSQL function f2() line 3 at RETURN
CONTEXT: PL/pgSQL function f2() line 3 at RETURN
f2
----
0
(1 row)

That seems like a bug ...

regards, tom lane

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Jack Kaufman 2013-01-18 21:04:43 Re: Determine the name of the calling function
Previous Message Jack Kaufman 2013-01-18 19:45:15 Re: Determine the name of the calling function