Re: Determine the name of the calling function - Mailing list pgsql-novice

From Tom Lane
Subject Re: Determine the name of the calling function
Date
Msg-id 26370.1358539743@sss.pgh.pa.us
Whole thread Raw
In response to Re: Determine the name of the calling function  (Jack Kaufman <jack.kaufman@sanmina.com>)
Responses Re: Determine the name of the calling function
Re: Determine the name of the calling function
List pgsql-novice
Jack Kaufman <jack.kaufman@sanmina.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


pgsql-novice by date:

Previous
From: Jack Kaufman
Date:
Subject: Re: Determine the name of the calling function
Next
From: Jack Kaufman
Date:
Subject: Re: Determine the name of the calling function