summaryrefslogtreecommitdiff
path: root/doc/src
diff options
context:
space:
mode:
authorStephen Frost <sfrost@snowman.net>2013-07-24 18:53:27 -0400
committerStephen Frost <sfrost@snowman.net>2013-07-24 18:53:27 -0400
commit831283256796d1c20858862b568d73e505eb4a84 (patch)
treeb8f958e915666dc4efc8458cdfc5c07272b8421f /doc/src
parentfa2fad3c06bfde03594ff38d53acdf9a60c56bb2 (diff)
Add GET DIAGNOSTICS ... PG_CONTEXT in PL/PgSQL
This adds the ability to get the call stack as a string from within a PL/PgSQL function, which can be handy for logging to a table, or to include in a useful message to an end-user. Pavel Stehule, reviewed by Rushabh Lathia and rather heavily whacked around by Stephen Frost.
Diffstat (limited to 'doc/src')
-rw-r--r--doc/src/sgml/plpgsql.sgml57
1 files changed, 57 insertions, 0 deletions
diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml
index 6fffec18b70..0fe6bcf470d 100644
--- a/doc/src/sgml/plpgsql.sgml
+++ b/doc/src/sgml/plpgsql.sgml
@@ -2613,6 +2613,15 @@ SELECT merge_db(1, 'dennis');
expected.
</para>
</example>
+ </sect2>
+
+ <sect2 id="plpgsql-diagnostics">
+ <title>Getting Diagnostics Information</title>
+
+ <indexterm>
+ <primary>diagnostics</primary>
+ <secondary>in PL/pgSQL</secondary>
+ </indexterm>
<sect3 id="plpgsql-exception-diagnostics">
<title>Obtaining information about an error</title>
@@ -2736,6 +2745,54 @@ END;
</programlisting>
</para>
</sect3>
+
+ <sect3 id="plpgsql-get-diagnostics-context">
+ <title>Obtaining the call stack context information</title>
+
+ <para>
+
+<synopsis>
+GET <optional> CURRENT </optional> DIAGNOSTICS <replaceable>variable</replaceable> = <replaceable>PG_CONTEXT</replaceable> <optional> , ... </optional>;
+</synopsis>
+
+
+ Calling <command>GET DIAGNOSTICS</command> with status
+ item <varname>PG_CONTEXT</> will return a text string with line(s) of
+ text describing the call stack. The first row refers to the
+ current function and currently executing <command>GET DIAGNOSTICS</command>
+ command. The second and any subsequent rows refer to the calling functions
+ up the call stack.
+
+<programlisting>
+CREATE OR REPLACE FUNCTION public.outer_func() RETURNS integer AS $$
+BEGIN
+ RETURN inner_func();
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION public.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 4 at GET DIAGNOSTICS
+PL/pgSQL function outer_func() line 3 at RETURN
+ outer_func
+ ------------
+ 1
+(1 row)
+</programlisting>
+
+ </para>
+ </sect3>
</sect2>
</sect1>