Re: Proposal "stack trace" like debugging option in PostgreSQL - Mailing list pgsql-general

From Charles Clavadetscher
Subject Re: Proposal "stack trace" like debugging option in PostgreSQL
Date
Msg-id 095b09a0-d052-6a3d-83aa-94dbc2f087e3@swisspug.org
Whole thread Raw
In response to Re: Proposal "stack trace" like debugging option in PostgreSQL  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
Hello

On 08/01/2016 12:15 AM, Tom Lane wrote:
> Edson Richter <edsonrichter@hotmail.com> writes:
>>> From: tgl@sss.pgh.pa.us
>>> Uh, doesn't the CONTEXT field of error messages give you that already?
>
>> Would you give me an example where I can get the info you mention above? Do I need to enable some kind of parameter
toget this context field? 
>
> Well, you have not mentioned what sort of client environment you are
> using, but the server certainly sends that information.  In psql,
> for example, I can do this:
>
> regression=# create table foo (f1 int primary key, f2 text);
> CREATE TABLE
> regression=# create function ifoo(int, text) returns void as
> $$ begin insert into foo values($1,$2); end $$ language plpgsql;
> CREATE FUNCTION
> regression=# create function ifoo2(int, text) returns void as
> $$ begin perform ifoo($1,$2); end $$ language plpgsql;
> CREATE FUNCTION
> regression=# select ifoo2(1,'foo');
>  ifoo2
> -------
>
> (1 row)
>
> regression=# select ifoo2(1,'foo');
> ERROR:  duplicate key value violates unique constraint "foo_pkey"
> DETAIL:  Key (f1)=(1) already exists.
> CONTEXT:  SQL statement "insert into foo values($1,$2)"
> PL/pgSQL function ifoo(integer,text) line 1 at SQL statement
> SQL statement "SELECT ifoo($1,$2)"
> PL/pgSQL function ifoo2(integer,text) line 1 at PERFORM
>
> If you're using a misdesigned client that will not show these auxiliary
> error fields, you could try looking in the server log --- at default
> log verbosity, it will contain that info too.  The above test case
> gave me this log entry:
>
> ERROR:  duplicate key value violates unique constraint "foo_pkey"
> DETAIL:  Key (f1)=(1) already exists.
> CONTEXT:  SQL statement "insert into foo values($1,$2)"
>         PL/pgSQL function ifoo(integer,text) line 1 at SQL statement
>         SQL statement "SELECT ifoo($1,$2)"
>         PL/pgSQL function ifoo2(integer,text) line 1 at PERFORM
> STATEMENT:  select ifoo2(1,'foo');
>
>             regards, tom lane
>
>

Maybe this could help, too.

In the documentation there is a description of how to get the call stack
of functions:

https://www.postgresql.org/docs/current/static/plpgsql-control-structures.html

40.6.7. Obtaining Current Execution Information

The GET [ CURRENT ] DIAGNOSTICS command retrieves information about
current execution state (whereas the GET STACKED DIAGNOSTICS command
discussed above reports information about the execution state as of a
previous error). This command has the form:

GET [ CURRENT ] DIAGNOSTICS variable { = | := } item [ , ... ];

Currently only one information item is supported. Status item PG_CONTEXT
will return a text string with line(s) of text describing the call
stack. The first line refers to the current function and currently
executing GET DIAGNOSTICS command. The second and any subsequent lines
refer to calling functions further up the call stack. For example:

CREATE OR REPLACE FUNCTION outer_func() RETURNS integer AS $$
BEGIN
   RETURN inner_func();
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION inner_func() RETURNS integer AS $$
DECLARE
   stack text;
BEGIN
   GET DIAGNOSTICS stack = PG_CONTEXT;
   RAISE NOTICE E'--- Call Stack ---\n%', stack;
   RETURN 1;
END;
$$ LANGUAGE plpgsql;

SELECT outer_func();

NOTICE:  --- Call Stack ---
PL/pgSQL function inner_func() line 5 at GET DIAGNOSTICS
PL/pgSQL function outer_func() line 3 at RETURN
CONTEXT:  PL/pgSQL function outer_func() line 3 at RETURN
  outer_func
  ------------
            1
(1 row)

Regards
Charles

--
Swiss PostgreSQL Users Group
c/o Charles Clavadetscher
Treasurer
Motorenstrasse 18
CH – 8005 Zürich

http://www.swisspug.org

+-----------------------+
|   ____  ______  ___   |
|  /    )/      \/   \  |
| (     / __    _\    ) |
|  \    (/ o)  ( o)   ) |
|   \_  (_  )   \ ) _/  |
|     \  /\_/    \)/    |
|      \/ <//|  |\\>    |
|           _|  |       |
|           \|_/        |
|                       |
| PostgreSQL 1996-2016  |
|  20 Years of Success  |
|                       |
+-----------------------+


pgsql-general by date:

Previous
From: rob stone
Date:
Subject: Re: 9.6beta3
Next
From: Moreno Andreo
Date:
Subject: Re: [SPAM] Re: WAL directory size calculation