summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/plpython-util.html
blob: 7b6651e4e8ea03d333218dee5b49f9b95370e70b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>46.9. Utility Functions</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="plpython-transactions.html" title="46.8. Transaction Management" /><link rel="next" href="plpython-python23.html" title="46.10. Python 2 vs. Python 3" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">46.9. Utility Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plpython-transactions.html" title="46.8. Transaction Management">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="plpython.html" title="Chapter 46. PL/Python — Python Procedural Language">Up</a></td><th width="60%" align="center">Chapter 46. PL/Python — Python Procedural Language</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="plpython-python23.html" title="46.10. Python 2 vs. Python 3">Next</a></td></tr></table><hr /></div><div class="sect1" id="PLPYTHON-UTIL"><div class="titlepage"><div><div><h2 class="title" style="clear: both">46.9. Utility Functions <a href="#PLPYTHON-UTIL" class="id_link">#</a></h2></div></div></div><p>
   The <code class="literal">plpy</code> module also provides the functions
   </p><table border="0" summary="Simple list" class="simplelist"><tr><td><code class="literal">plpy.debug(<em class="replaceable"><code>msg, **kwargs</code></em>)</code></td></tr><tr><td><code class="literal">plpy.log(<em class="replaceable"><code>msg, **kwargs</code></em>)</code></td></tr><tr><td><code class="literal">plpy.info(<em class="replaceable"><code>msg, **kwargs</code></em>)</code></td></tr><tr><td><code class="literal">plpy.notice(<em class="replaceable"><code>msg, **kwargs</code></em>)</code></td></tr><tr><td><code class="literal">plpy.warning(<em class="replaceable"><code>msg, **kwargs</code></em>)</code></td></tr><tr><td><code class="literal">plpy.error(<em class="replaceable"><code>msg, **kwargs</code></em>)</code></td></tr><tr><td><code class="literal">plpy.fatal(<em class="replaceable"><code>msg, **kwargs</code></em>)</code></td></tr></table><p>
   <a id="id-1.8.11.17.2.3" class="indexterm"></a>
   <code class="function">plpy.error</code> and <code class="function">plpy.fatal</code>
   actually raise a Python exception which, if uncaught, propagates out to
   the calling query, causing the current transaction or subtransaction to
   be aborted.  <code class="literal">raise plpy.Error(<em class="replaceable"><code>msg</code></em>)</code> and
   <code class="literal">raise plpy.Fatal(<em class="replaceable"><code>msg</code></em>)</code> are
   equivalent to calling <code class="literal">plpy.error(<em class="replaceable"><code>msg</code></em>)</code> and
   <code class="literal">plpy.fatal(<em class="replaceable"><code>msg</code></em>)</code>, respectively but
   the <code class="literal">raise</code> form does not allow passing keyword arguments.
   The other functions only generate messages of different priority levels.
   Whether messages of a particular priority are reported to the client,
   written to the server log, or both is controlled by the
   <a class="xref" href="runtime-config-logging.html#GUC-LOG-MIN-MESSAGES">log_min_messages</a> and
   <a class="xref" href="runtime-config-client.html#GUC-CLIENT-MIN-MESSAGES">client_min_messages</a> configuration
   variables. See <a class="xref" href="runtime-config.html" title="Chapter 20. Server Configuration">Chapter 20</a> for more information.
  </p><p>
   The <em class="replaceable"><code>msg</code></em> argument is given as a positional argument.  For
   backward compatibility, more than one positional argument can be given. In
   that case, the string representation of the tuple of positional arguments
   becomes the message reported to the client.
  </p><p>
   The following keyword-only arguments are accepted:
   </p><table border="0" summary="Simple list" class="simplelist"><tr><td><code class="literal">detail</code></td></tr><tr><td><code class="literal">hint</code></td></tr><tr><td><code class="literal">sqlstate</code></td></tr><tr><td><code class="literal">schema_name</code></td></tr><tr><td><code class="literal">table_name</code></td></tr><tr><td><code class="literal">column_name</code></td></tr><tr><td><code class="literal">datatype_name</code></td></tr><tr><td><code class="literal">constraint_name</code></td></tr></table><p>
   The string representation of the objects passed as keyword-only arguments
   is used to enrich the messages reported to the client. For example:

</p><pre class="programlisting">
CREATE FUNCTION raise_custom_exception() RETURNS void AS $$
plpy.error("custom exception message",
           detail="some info about exception",
           hint="hint for users")
$$ LANGUAGE plpython3u;

=# SELECT raise_custom_exception();
ERROR:  plpy.Error: custom exception message
DETAIL:  some info about exception
HINT:  hint for users
CONTEXT:  Traceback (most recent call last):
  PL/Python function "raise_custom_exception", line 4, in &lt;module&gt;
    hint="hint for users")
PL/Python function "raise_custom_exception"
</pre><p>
  </p><p>
   Another set of utility functions are
   <code class="literal">plpy.quote_literal(<em class="replaceable"><code>string</code></em>)</code>,
   <code class="literal">plpy.quote_nullable(<em class="replaceable"><code>string</code></em>)</code>, and
   <code class="literal">plpy.quote_ident(<em class="replaceable"><code>string</code></em>)</code>.  They
   are equivalent to the built-in quoting functions described in <a class="xref" href="functions-string.html" title="9.4. String Functions and Operators">Section 9.4</a>.  They are useful when constructing
   ad-hoc queries.  A PL/Python equivalent of dynamic SQL from <a class="xref" href="plpgsql-statements.html#PLPGSQL-QUOTE-LITERAL-EXAMPLE" title="Example 43.1. Quoting Values in Dynamic Queries">Example 43.1</a> would be:
</p><pre class="programlisting">
plpy.execute("UPDATE tbl SET %s = %s WHERE key = %s" % (
    plpy.quote_ident(colname),
    plpy.quote_nullable(newvalue),
    plpy.quote_literal(keyvalue)))
</pre><p>
  </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plpython-transactions.html" title="46.8. Transaction Management">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plpython.html" title="Chapter 46. PL/Python — Python Procedural Language">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plpython-python23.html" title="46.10. Python 2 vs. Python 3">Next</a></td></tr><tr><td width="40%" align="left" valign="top">46.8. Transaction Management </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 46.10. Python 2 vs. Python 3</td></tr></table></div></body></html>