Re: Determine the name of the calling function

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

Hello, Tom,

I found the problem. I was doing just about the same thing as you, except
I was passing the value of "t" up the stack as a return value from the sub-
and calling functions. When I looked in the pgadmin log, I saw what you
were seeing. It looks like "t" has LFs or CRs embedded in the text and I
was getting just "line 1", which was the "PL/pgSQL function f1() line 5 at
assignment" part. The part I wanted was in line 2.

The output from PG_EXCEPTION_CONTEXT looks parseable, with line 1
containing the current function, and line 2, the calling function. The
lines are variable length but the LF or CR should be identifiable. Add in
the fact of the variable number of lines you got from
PG_EXCEPTION_CONTEXT--where line 1 become 2, and line 2 becomes 3--and the
project is a pain but doable.

Thank you for your help. I don't think I would have figured this out on my
own, at least not for quite a while.

Sincerely, Jack

On Fri, Jan 18, 2013 at 3:09 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> 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
>

--
Jack Kaufman
MDS Application Devl (US)
Sanmina-SCI Corporation - Owego site
Email: jack(dot)kaufman(at)sanmina(dot)com
Skype: jack_kaufman_sanm
607-723-0507

CONFIDENTIALITY
This e-mail message and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail message, you are hereby notified that any dissemination, distribution or copying of this e-mail message, and any attachments thereto, is strictly prohibited. If you have received this e-mail message in error, please immediately notify the sender and permanently delete the original and any copies of this email and any prints thereof.
ABSENT AN EXPRESS STATEMENT TO THE CONTRARY HEREINABOVE, THIS E-MAIL IS NOT INTENDED AS A SUBSTITUTE FOR A WRITING. Notwithstanding the Uniform Electronic Transactions Act or the applicability of any other law of similar substance and effect, absent an express statement to the contrary hereinabove, this e-mail message its contents, and any attachments hereto are not intended to represent an offer or acceptance to enter into a contract and are not otherwise intended to bind the sender, Sanmina Corporation (or any of its subsidiaries), or any other person or entity.

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Casey Allen Shobe 2013-01-18 22:34:56 Re: Determine the name of the calling function
Previous Message Tom Lane 2013-01-18 20:09:03 Re: Determine the name of the calling function