Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > 9b6cc37ce608401d44f6535a0c7cb777 > files > 600

postgresql11-docs-11.5-1.mga7.noarch.rpm

<?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.2. PL/Python 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-python23.html" title="46.1. Python 2 vs. Python 3" /><link rel="next" href="plpython-data.html" title="46.3. Data Values" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">46.2. PL/Python Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plpython-python23.html" title="46.1. Python 2 vs. Python 3">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 11.5 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="plpython-data.html" title="46.3. Data Values">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="PLPYTHON-FUNCS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">46.2. PL/Python Functions</h2></div></div></div><p>
   Functions in PL/Python are declared via the
   standard <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a> syntax:

</p><pre class="programlisting">
CREATE FUNCTION <em class="replaceable"><code>funcname</code></em> (<em class="replaceable"><code>argument-list</code></em>)
  RETURNS <em class="replaceable"><code>return-type</code></em>
AS $$
  # PL/Python function body
$$ LANGUAGE plpythonu;
</pre><p>
  </p><p>
   The body of a function is simply a Python script. When the function
   is called, its arguments are passed as elements of the list
   <code class="varname">args</code>; named arguments are also passed as
   ordinary variables to the Python script.  Use of named arguments is
   usually more readable.  The result is returned from the Python code
   in the usual way, with <code class="literal">return</code> or
   <code class="literal">yield</code> (in case of a result-set statement).  If
   you do not provide a return value, Python returns the default
   <code class="symbol">None</code>. <span class="application">PL/Python</span> translates
   Python's <code class="symbol">None</code> into the SQL null value.  In a procedure,
   the result from the Python code must be <code class="symbol">None</code> (typically
   achieved by ending the procedure without a <code class="literal">return</code>
   statement or by using a <code class="literal">return</code> statement without
   argument); otherwise, an error will be raised.
  </p><p>
   For example, a function to return the greater of two integers can be
   defined as:

</p><pre class="programlisting">
CREATE FUNCTION pymax (a integer, b integer)
  RETURNS integer
AS $$
  if a &gt; b:
    return a
  return b
$$ LANGUAGE plpythonu;
</pre><p>

   The Python code that is given as the body of the function definition
   is transformed into a Python function. For example, the above results in:

</p><pre class="programlisting">
def __plpython_procedure_pymax_23456():
  if a &gt; b:
    return a
  return b
</pre><p>

   assuming that 23456 is the OID assigned to the function by
   <span class="productname">PostgreSQL</span>.
  </p><p>
   The arguments are set as global variables.  Because of the scoping
   rules of Python, this has the subtle consequence that an argument
   variable cannot be reassigned inside the function to the value of
   an expression that involves the variable name itself, unless the
   variable is redeclared as global in the block.  For example, the
   following won't work:
</p><pre class="programlisting">
CREATE FUNCTION pystrip(x text)
  RETURNS text
AS $$
  x = x.strip()  # error
  return x
$$ LANGUAGE plpythonu;
</pre><p>
   because assigning to <code class="varname">x</code>
   makes <code class="varname">x</code> a local variable for the entire block,
   and so the <code class="varname">x</code> on the right-hand side of the
   assignment refers to a not-yet-assigned local
   variable <code class="varname">x</code>, not the PL/Python function
   parameter.  Using the <code class="literal">global</code> statement, this can
   be made to work:
</p><pre class="programlisting">
CREATE FUNCTION pystrip(x text)
  RETURNS text
AS $$
  global x
  x = x.strip()  # ok now
  return x
$$ LANGUAGE plpythonu;
</pre><p>
   But it is advisable not to rely on this implementation detail of
   PL/Python.  It is better to treat the function parameters as
   read-only.
  </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plpython-python23.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plpython.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plpython-data.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">46.1. Python 2 vs. Python 3 </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 46.3. Data Values</td></tr></table></div></body></html>