]> granicus.if.org Git - postgresql/blob - doc/src/sgml/plpython.sgml
docs: remove unnecessary references to old PG versions
[postgresql] / doc / src / sgml / plpython.sgml
1 <!-- doc/src/sgml/plpython.sgml -->
2
3 <chapter id="plpython">
4  <title>PL/Python - Python Procedural Language</title>
5
6  <indexterm zone="plpython"><primary>PL/Python</></>
7  <indexterm zone="plpython"><primary>Python</></>
8
9  <para>
10   The <application>PL/Python</application> procedural language allows
11   <productname>PostgreSQL</productname> functions to be written in the
12   <ulink url="http://www.python.org">Python language</ulink>.
13  </para>
14
15  <para>
16   To install PL/Python in a particular database, use
17   <literal>CREATE EXTENSION plpythonu</>, or from the shell command line use
18   <literal>createlang plpythonu <replaceable>dbname</></literal> (but
19   see also <xref linkend="plpython-python23">).
20  </para>
21
22   <tip>
23    <para>
24     If a language is installed into <literal>template1</>, all subsequently
25     created databases will have the language installed automatically.
26    </para>
27   </tip>
28
29  <para>
30   PL/Python is only available as an <quote>untrusted</> language, meaning
31   it does not offer any way of restricting what users can do in it and
32   is therefore named <literal>plpythonu</>.  A trusted
33   variant <literal>plpython</> might become available in the future
34   if a secure execution mechanism is developed in Python.  The
35   writer of a function in untrusted PL/Python must take care that the
36   function cannot be used to do anything unwanted, since it will be
37   able to do anything that could be done by a user logged in as the
38   database administrator.  Only superusers can create functions in
39   untrusted languages such as <literal>plpythonu</literal>.
40  </para>
41
42  <note>
43   <para>
44    Users of source packages must specially enable the build of
45    PL/Python during the installation process.  (Refer to the
46    installation instructions for more information.)  Users of binary
47    packages might find PL/Python in a separate subpackage.
48   </para>
49  </note>
50
51  <sect1 id="plpython-python23">
52   <title>Python 2 vs. Python 3</title>
53
54   <para>
55    PL/Python supports both the Python 2 and Python 3 language
56    variants.  (The PostgreSQL installation instructions might contain
57    more precise information about the exact supported minor versions
58    of Python.)  Because the Python 2 and Python 3 language variants
59    are incompatible in some important aspects, the following naming
60    and transitioning scheme is used by PL/Python to avoid mixing them:
61
62    <itemizedlist>
63     <listitem>
64      <para>
65       The PostgreSQL language named <literal>plpython2u</literal>
66       implements PL/Python based on the Python 2 language variant.
67      </para>
68     </listitem>
69
70     <listitem>
71      <para>
72       The PostgreSQL language named <literal>plpython3u</literal>
73       implements PL/Python based on the Python 3 language variant.
74      </para>
75     </listitem>
76
77     <listitem>
78      <para>
79       The language named <literal>plpythonu</literal> implements
80       PL/Python based on the default Python language variant, which is
81       currently Python 2.  (This default is independent of what any
82       local Python installations might consider to be
83       their <quote>default</quote>, for example,
84       what <filename>/usr/bin/python</filename> might be.)  The
85       default will probably be changed to Python 3 in a distant future
86       release of PostgreSQL, depending on the progress of the
87       migration to Python 3 in the Python community.
88      </para>
89     </listitem>
90    </itemizedlist>
91
92    This scheme is analogous to the recommendations in <ulink
93    url="http://www.python.org/dev/peps/pep-0394/">PEP 394</ulink> regarding the
94    naming and transitioning of the <command>python</command> command.
95   </para>
96
97   <para>
98    It depends on the build configuration or the installed packages
99    whether PL/Python for Python 2 or Python 3 or both are available.
100   </para>
101
102   <tip>
103    <para>
104     The built variant depends on which Python version was found during
105     the installation or which version was explicitly set using
106     the <envar>PYTHON</envar> environment variable;
107     see <xref linkend="install-procedure">.  To make both variants of
108     PL/Python available in one installation, the source tree has to be
109     configured and built twice.
110    </para>
111   </tip>
112
113   <para>
114    This results in the following usage and migration strategy:
115
116    <itemizedlist>
117     <listitem>
118      <para>
119       Existing users and users who are currently not interested in
120       Python 3 use the language name <literal>plpythonu</literal> and
121       don't have to change anything for the foreseeable future.  It is
122       recommended to gradually <quote>future-proof</quote> the code
123       via migration to Python 2.6/2.7 to simplify the eventual
124       migration to Python 3.
125      </para>
126
127      <para>
128       In practice, many PL/Python functions will migrate to Python 3
129       with few or no changes.
130      </para>
131     </listitem>
132
133     <listitem>
134      <para>
135       Users who know that they have heavily Python 2 dependent code
136       and don't plan to ever change it can make use of
137       the <literal>plpython2u</literal> language name.  This will
138       continue to work into the very distant future, until Python 2
139       support might be completely dropped by PostgreSQL.
140      </para>
141     </listitem>
142
143     <listitem>
144      <para>
145       Users who want to dive into Python 3 can use
146       the <literal>plpython3u</literal> language name, which will keep
147       working forever by today's standards.  In the distant future,
148       when Python 3 might become the default, they might like to
149       remove the <quote>3</quote> for aesthetic reasons.
150      </para>
151     </listitem>
152
153     <listitem>
154      <para>
155       Daredevils, who want to build a Python-3-only operating system
156       environment, can change the contents of
157       <link linkend="catalog-pg-pltemplate"><structname>pg_pltemplate</structname></link>
158       to make <literal>plpythonu</literal> be equivalent
159       to <literal>plpython3u</literal>, keeping in mind that this
160       would make their installation incompatible with most of the rest
161       of the world.
162      </para>
163     </listitem>
164    </itemizedlist>
165   </para>
166
167   <para>
168    See also the
169    document <ulink url="http://docs.python.org/py3k/whatsnew/3.0.html">What's
170    New In Python 3.0</ulink> for more information about porting to
171    Python 3.
172   </para>
173
174   <para>
175    It is not allowed to use PL/Python based on Python 2 and PL/Python
176    based on Python 3 in the same session, because the symbols in the
177    dynamic modules would clash, which could result in crashes of the
178    PostgreSQL server process.  There is a check that prevents mixing
179    Python major versions in a session, which will abort the session if
180    a mismatch is detected.  It is possible, however, to use both
181    PL/Python variants in the same database, from separate sessions.
182   </para>
183  </sect1>
184
185  <sect1 id="plpython-funcs">
186   <title>PL/Python Functions</title>
187
188   <para>
189    Functions in PL/Python are declared via the
190    standard <xref linkend="sql-createfunction"> syntax:
191
192 <programlisting>
193 CREATE FUNCTION <replaceable>funcname</replaceable> (<replaceable>argument-list</replaceable>)
194   RETURNS <replaceable>return-type</replaceable>
195 AS $$
196   # PL/Python function body
197 $$ LANGUAGE plpythonu;
198 </programlisting>
199   </para>
200
201   <para>
202    The body of a function is simply a Python script. When the function
203    is called, its arguments are passed as elements of the list
204    <varname>args</varname>; named arguments are also passed as
205    ordinary variables to the Python script.  Use of named arguments is
206    usually more readable.  The result is returned from the Python code
207    in the usual way, with <literal>return</literal> or
208    <literal>yield</literal> (in case of a result-set statement).  If
209    you do not provide a return value, Python returns the default
210    <symbol>None</symbol>. <application>PL/Python</application> translates
211    Python's <symbol>None</symbol> into the SQL null value.
212   </para>
213
214   <para>
215    For example, a function to return the greater of two integers can be
216    defined as:
217
218 <programlisting>
219 CREATE FUNCTION pymax (a integer, b integer)
220   RETURNS integer
221 AS $$
222   if a &gt; b:
223     return a
224   return b
225 $$ LANGUAGE plpythonu;
226 </programlisting>
227
228    The Python code that is given as the body of the function definition
229    is transformed into a Python function. For example, the above results in:
230
231 <programlisting>
232 def __plpython_procedure_pymax_23456():
233   if a &gt; b:
234     return a
235   return b
236 </programlisting>
237
238    assuming that 23456 is the OID assigned to the function by
239    <productname>PostgreSQL</productname>.
240   </para>
241
242   <para>
243    The arguments are set as global variables.  Because of the scoping
244    rules of Python, this has the subtle consequence that an argument
245    variable cannot be reassigned inside the function to the value of
246    an expression that involves the variable name itself, unless the
247    variable is redeclared as global in the block.  For example, the
248    following won't work:
249 <programlisting>
250 CREATE FUNCTION pystrip(x text)
251   RETURNS text
252 AS $$
253   x = x.strip()  # error
254   return x
255 $$ LANGUAGE plpythonu;
256 </programlisting>
257    because assigning to <varname>x</varname>
258    makes <varname>x</varname> a local variable for the entire block,
259    and so the <varname>x</varname> on the right-hand side of the
260    assignment refers to a not-yet-assigned local
261    variable <varname>x</varname>, not the PL/Python function
262    parameter.  Using the <literal>global</literal> statement, this can
263    be made to work:
264 <programlisting>
265 CREATE FUNCTION pystrip(x text)
266   RETURNS text
267 AS $$
268   global x
269   x = x.strip()  # ok now
270   return x
271 $$ LANGUAGE plpythonu;
272 </programlisting>
273    But it is advisable not to rely on this implementation detail of
274    PL/Python.  It is better to treat the function parameters as
275    read-only.
276   </para>
277  </sect1>
278
279  <sect1 id="plpython-data">
280   <title>Data Values</title>
281   <para>
282    Generally speaking, the aim of PL/Python is to provide
283    a <quote>natural</quote> mapping between the PostgreSQL and the
284    Python worlds.  This informs the data mapping rules described
285    below.
286   </para>
287
288   <sect2>
289    <title>Data Type Mapping</title>
290    <para>
291     Function arguments are converted from their PostgreSQL type to a
292     corresponding Python type:
293     <itemizedlist>
294      <listitem>
295       <para>
296        PostgreSQL <type>boolean</type> is converted to Python <type>bool</type>.
297       </para>
298      </listitem>
299
300      <listitem>
301       <para>
302        PostgreSQL <type>smallint</type> and <type>int</type> are
303        converted to Python <type>int</type>.
304        PostgreSQL <type>bigint</type> and <type>oid</type> are converted
305        to <type>long</type> in Python 2 and to <type>int</type> in
306        Python 3.
307       </para>
308      </listitem>
309
310      <listitem>
311       <para>
312        PostgreSQL <type>real</type> and <type>double</type> are converted to
313        Python <type>float</type>.
314       </para>
315      </listitem>
316
317      <listitem>
318       <para>
319        PostgreSQL <type>numeric</type> is converted to
320        Python <type>Decimal</type>.  This type is imported from
321        the <literal>cdecimal</literal> package if that is available.
322        Otherwise,
323        <literal>decimal.Decimal</literal> from the standard library will be
324        used.  <literal>cdecimal</literal> is significantly faster
325        than <literal>decimal</literal>.  In Python 3.3,
326        however, <literal>cdecimal</literal> has been integrated into the
327        standard library under the name <literal>decimal</literal>, so there is
328        no longer any difference.
329       </para>
330      </listitem>
331
332      <listitem>
333       <para>
334        PostgreSQL <type>bytea</type> is converted to
335        Python <type>str</type> in Python 2 and to <type>bytes</type>
336        in Python 3.  In Python 2, the string should be treated as a
337        byte sequence without any character encoding.
338       </para>
339      </listitem>
340
341      <listitem>
342       <para>
343        All other data types, including the PostgreSQL character string
344        types, are converted to a Python <type>str</type>.  In Python
345        2, this string will be in the PostgreSQL server encoding; in
346        Python 3, it will be a Unicode string like all strings.
347       </para>
348      </listitem>
349
350      <listitem>
351       <para>
352        For nonscalar data types, see below.
353       </para>
354      </listitem>
355     </itemizedlist>
356    </para>
357
358    <para>
359     Function return values are converted to the declared PostgreSQL
360     return data type as follows:
361     <itemizedlist>
362      <listitem>
363       <para>
364        When the PostgreSQL return type is <type>boolean</type>, the
365        return value will be evaluated for truth according to the
366        <emphasis>Python</emphasis> rules.  That is, 0 and empty string
367        are false, but notably <literal>'f'</literal> is true.
368       </para>
369      </listitem>
370
371      <listitem>
372       <para>
373        When the PostgreSQL return type is <type>bytea</type>, the
374        return value will be converted to a string (Python 2) or bytes
375        (Python 3) using the respective Python built-ins, with the
376        result being converted <type>bytea</type>.
377       </para>
378      </listitem>
379
380      <listitem>
381       <para>
382        For all other PostgreSQL return types, the returned Python
383        value is converted to a string using the Python
384        built-in <literal>str</literal>, and the result is passed to the
385        input function of the PostgreSQL data type.
386       </para>
387
388       <para>
389        Strings in Python 2 are required to be in the PostgreSQL server
390        encoding when they are passed to PostgreSQL.  Strings that are
391        not valid in the current server encoding will raise an error,
392        but not all encoding mismatches can be detected, so garbage
393        data can still result when this is not done correctly.  Unicode
394        strings are converted to the correct encoding automatically, so
395        it can be safer and more convenient to use those.  In Python 3,
396        all strings are Unicode strings.
397       </para>
398      </listitem>
399
400      <listitem>
401       <para>
402        For nonscalar data types, see below.
403       </para>
404      </listitem>
405     </itemizedlist>
406
407     Note that logical mismatches between the declared PostgreSQL
408     return type and the Python data type of the actual return object
409     are not flagged; the value will be converted in any case.
410    </para>
411   </sect2>
412
413   <sect2>
414    <title>Null, None</title>
415   <para>
416    If an SQL null value<indexterm><primary>null value</primary><secondary
417    sortas="PL/Python">in PL/Python</secondary></indexterm> is passed to a
418    function, the argument value will appear as <symbol>None</symbol> in
419    Python. For example, the function definition of <function>pymax</function>
420    shown in <xref linkend="plpython-funcs"> will return the wrong answer for null
421    inputs. We could add <literal>STRICT</literal> to the function definition
422    to make <productname>PostgreSQL</productname> do something more reasonable:
423    if a null value is passed, the function will not be called at all,
424    but will just return a null result automatically. Alternatively,
425    we could check for null inputs in the function body:
426
427 <programlisting>
428 CREATE FUNCTION pymax (a integer, b integer)
429   RETURNS integer
430 AS $$
431   if (a is None) or (b is None):
432     return None
433   if a &gt; b:
434     return a
435   return b
436 $$ LANGUAGE plpythonu;
437 </programlisting>
438
439    As shown above, to return an SQL null value from a PL/Python
440    function, return the value <symbol>None</symbol>. This can be done whether the
441    function is strict or not.
442   </para>
443   </sect2>
444
445   <sect2 id="plpython-arrays">
446    <title>Arrays, Lists</title>
447   <para>
448    SQL array values are passed into PL/Python as a Python list.  To
449    return an SQL array value out of a PL/Python function, return a
450    Python sequence, for example a list or tuple:
451
452 <programlisting>
453 CREATE FUNCTION return_arr()
454   RETURNS int[]
455 AS $$
456 return (1, 2, 3, 4, 5)
457 $$ LANGUAGE plpythonu;
458
459 SELECT return_arr();
460  return_arr  
461 -------------
462  {1,2,3,4,5}
463 (1 row)
464 </programlisting>
465
466    Note that in Python, strings are sequences, which can have
467    undesirable effects that might be familiar to Python programmers:
468
469 <programlisting>
470 CREATE FUNCTION return_str_arr()
471   RETURNS varchar[]
472 AS $$
473 return "hello"
474 $$ LANGUAGE plpythonu;
475
476 SELECT return_str_arr();
477  return_str_arr
478 ----------------
479  {h,e,l,l,o}
480 (1 row)
481 </programlisting>
482   </para>
483   </sect2>
484
485   <sect2>
486    <title>Composite Types</title>
487   <para>
488    Composite-type arguments are passed to the function as Python mappings. The
489    element names of the mapping are the attribute names of the composite type.
490    If an attribute in the passed row has the null value, it has the value
491    <symbol>None</symbol> in the mapping. Here is an example:
492
493 <programlisting>
494 CREATE TABLE employee (
495   name text,
496   salary integer,
497   age integer
498 );
499
500 CREATE FUNCTION overpaid (e employee)
501   RETURNS boolean
502 AS $$
503   if e["salary"] &gt; 200000:
504     return True
505   if (e["age"] &lt; 30) and (e["salary"] &gt; 100000):
506     return True
507   return False
508 $$ LANGUAGE plpythonu;
509 </programlisting>
510   </para>
511
512   <para>
513    There are multiple ways to return row or composite types from a Python
514    function. The following examples assume we have:
515
516 <programlisting>
517 CREATE TYPE named_value AS (
518   name   text,
519   value  integer
520 );
521 </programlisting>
522
523    A composite result can be returned as a:
524
525    <variablelist>
526     <varlistentry>
527      <term>Sequence type (a tuple or list, but not a set because
528      it is not indexable)</term>
529      <listitem>
530       <para>
531        Returned sequence objects must have the same number of items as the
532        composite result type has fields. The item with index 0 is assigned to
533        the first field of the composite type, 1 to the second and so on. For
534        example:
535
536 <programlisting>
537 CREATE FUNCTION make_pair (name text, value integer)
538   RETURNS named_value
539 AS $$
540   return [ name, value ]
541   # or alternatively, as tuple: return ( name, value )
542 $$ LANGUAGE plpythonu;
543 </programlisting>
544
545        To return a SQL null for any column, insert <symbol>None</symbol> at
546        the corresponding position.
547       </para>
548      </listitem>
549     </varlistentry>
550
551     <varlistentry>
552      <term>Mapping (dictionary)</term>
553      <listitem>
554       <para>
555        The value for each result type column is retrieved from the mapping
556        with the column name as key. Example:
557
558 <programlisting>
559 CREATE FUNCTION make_pair (name text, value integer)
560   RETURNS named_value
561 AS $$
562   return { "name": name, "value": value }
563 $$ LANGUAGE plpythonu;
564 </programlisting>
565
566        Any extra dictionary key/value pairs are ignored. Missing keys are
567        treated as errors.
568        To return a SQL null value for any column, insert
569        <symbol>None</symbol> with the corresponding column name as the key.
570       </para>
571      </listitem>
572     </varlistentry>
573
574     <varlistentry>
575      <term>Object (any object providing method <literal>__getattr__</literal>)</term>
576      <listitem>
577       <para>
578        This works the same as a mapping.
579        Example:
580
581 <programlisting>
582 CREATE FUNCTION make_pair (name text, value integer)
583   RETURNS named_value
584 AS $$
585   class named_value:
586     def __init__ (self, n, v):
587       self.name = n
588       self.value = v
589   return named_value(name, value)
590
591   # or simply
592   class nv: pass
593   nv.name = name
594   nv.value = value
595   return nv
596 $$ LANGUAGE plpythonu;
597 </programlisting>
598       </para>
599      </listitem>
600     </varlistentry>
601    </variablelist>
602   </para>
603
604    <para>
605     Functions with <literal>OUT</literal> parameters are also supported.  For example:
606 <programlisting>
607 CREATE FUNCTION multiout_simple(OUT i integer, OUT j integer) AS $$
608 return (1, 2)
609 $$ LANGUAGE plpythonu;
610
611 SELECT * FROM multiout_simple();
612 </programlisting>
613    </para>
614   </sect2>
615
616   <sect2>
617    <title>Set-returning Functions</title>
618   <para>
619    A <application>PL/Python</application> function can also return sets of
620    scalar or composite types. There are several ways to achieve this because
621    the returned object is internally turned into an iterator. The following
622    examples assume we have composite type:
623
624 <programlisting>
625 CREATE TYPE greeting AS (
626   how text,
627   who text
628 );
629 </programlisting>
630
631    A set result can be returned from a:
632
633    <variablelist>
634     <varlistentry>
635      <term>Sequence type (tuple, list, set)</term>
636      <listitem>
637       <para>
638 <programlisting>
639 CREATE FUNCTION greet (how text)
640   RETURNS SETOF greeting
641 AS $$
642   # return tuple containing lists as composite types
643   # all other combinations work also
644   return ( [ how, "World" ], [ how, "PostgreSQL" ], [ how, "PL/Python" ] )
645 $$ LANGUAGE plpythonu;
646 </programlisting>
647       </para>
648      </listitem>
649     </varlistentry>
650
651     <varlistentry>
652      <term>Iterator (any object providing <symbol>__iter__</symbol> and
653       <symbol>next</symbol> methods)</term>
654      <listitem>
655       <para>
656 <programlisting>
657 CREATE FUNCTION greet (how text)
658   RETURNS SETOF greeting
659 AS $$
660   class producer:
661     def __init__ (self, how, who):
662       self.how = how
663       self.who = who
664       self.ndx = -1
665
666     def __iter__ (self):
667       return self
668
669     def next (self):
670       self.ndx += 1
671       if self.ndx == len(self.who):
672         raise StopIteration
673       return ( self.how, self.who[self.ndx] )
674
675   return producer(how, [ "World", "PostgreSQL", "PL/Python" ])
676 $$ LANGUAGE plpythonu;
677 </programlisting>
678       </para>
679      </listitem>
680     </varlistentry>
681
682     <varlistentry>
683      <term>Generator (<literal>yield</literal>)</term>
684      <listitem>
685       <para>
686 <programlisting>
687 CREATE FUNCTION greet (how text)
688   RETURNS SETOF greeting
689 AS $$
690   for who in [ "World", "PostgreSQL", "PL/Python" ]:
691     yield ( how, who )
692 $$ LANGUAGE plpythonu;
693 </programlisting>
694
695        <warning>
696         <para>
697          Due to Python
698          <ulink url="http://bugs.python.org/issue1483133">bug #1483133</ulink>,
699          some debug versions of Python 2.4
700          (configured and compiled with option <literal>--with-pydebug</literal>)
701          are known to crash the <productname>PostgreSQL</productname> server
702          when using an iterator to return a set result.
703          Unpatched versions of Fedora 4 contain this bug.
704          It does not happen in production versions of Python or on patched
705          versions of Fedora 4.
706         </para>
707        </warning>
708       </para>
709      </listitem>
710     </varlistentry>
711    </variablelist>
712   </para>
713
714    <para>
715     Set-returning functions with <literal>OUT</literal> parameters
716     (using <literal>RETURNS SETOF record</literal>) are also
717     supported.  For example:
718 <programlisting>
719 CREATE FUNCTION multiout_simple_setof(n integer, OUT integer, OUT integer) RETURNS SETOF record AS $$
720 return [(1, 2)] * n
721 $$ LANGUAGE plpythonu;
722
723 SELECT * FROM multiout_simple_setof(3);
724 </programlisting>
725    </para>
726   </sect2>
727  </sect1>
728
729  <sect1 id="plpython-sharing">
730   <title>Sharing Data</title>
731   <para>
732    The global dictionary <varname>SD</varname> is available to store
733    data between function calls.  This variable is private static data.
734    The global dictionary <varname>GD</varname> is public data,
735    available to all Python functions within a session.  Use with
736    care.<indexterm><primary>global data</>
737    <secondary>in PL/Python</></indexterm>
738   </para>
739
740   <para>
741    Each function gets its own execution environment in the
742    Python interpreter, so that global data and function arguments from
743    <function>myfunc</function> are not available to
744    <function>myfunc2</function>.  The exception is the data in the
745    <varname>GD</varname> dictionary, as mentioned above.
746   </para>
747  </sect1>
748
749  <sect1 id="plpython-do">
750   <title>Anonymous Code Blocks</title>
751
752   <para>
753    PL/Python also supports anonymous code blocks called with the
754    <xref linkend="sql-do"> statement:
755
756 <programlisting>
757 DO $$
758     # PL/Python code
759 $$ LANGUAGE plpythonu;
760 </programlisting>
761
762    An anonymous code block receives no arguments, and whatever value it
763    might return is discarded.  Otherwise it behaves just like a function.
764   </para>
765  </sect1>
766
767  <sect1 id="plpython-trigger">
768   <title>Trigger Functions</title>
769
770   <indexterm zone="plpython-trigger">
771    <primary>trigger</primary>
772    <secondary>in PL/Python</secondary>
773   </indexterm>
774
775   <para>
776    When a function is used as a trigger, the dictionary
777    <literal>TD</literal> contains trigger-related values:
778    <variablelist>
779     <varlistentry>
780      <term><literal>TD["event"]</></term>
781      <listitem>
782       <para>
783        contains the event as a string:
784        <literal>INSERT</>, <literal>UPDATE</>,
785        <literal>DELETE</>, or <literal>TRUNCATE</>.
786       </para>
787      </listitem>
788     </varlistentry>
789
790     <varlistentry>
791      <term><literal>TD["when"]</></term>
792      <listitem>
793       <para>
794        contains one of <literal>BEFORE</>, <literal>AFTER</>, or
795        <literal>INSTEAD OF</>.
796       </para>
797      </listitem>
798     </varlistentry>
799
800     <varlistentry>
801      <term><literal>TD["level"]</></term>
802      <listitem>
803       <para>
804        contains <literal>ROW</> or <literal>STATEMENT</>.
805       </para>
806      </listitem>
807     </varlistentry>
808
809     <varlistentry>
810      <term><literal>TD["new"]</></term>
811      <term><literal>TD["old"]</></term>
812      <listitem>
813       <para>
814        For a row-level trigger, one or both of these fields contain
815        the respective trigger rows, depending on the trigger event.
816       </para>
817      </listitem>
818     </varlistentry>
819
820     <varlistentry>
821      <term><literal>TD["name"]</></term>
822      <listitem>
823       <para>
824        contains the trigger name.
825       </para>
826      </listitem>
827     </varlistentry>
828
829     <varlistentry>
830      <term><literal>TD["table_name"]</></term>
831      <listitem>
832       <para>
833        contains the name of the table on which the trigger occurred.
834       </para>
835      </listitem>
836     </varlistentry>
837
838     <varlistentry>
839      <term><literal>TD["table_schema"]</></term>
840      <listitem>
841       <para>
842        contains the schema of the table on which the trigger occurred.
843       </para>
844      </listitem>
845     </varlistentry>
846
847     <varlistentry>
848      <term><literal>TD["relid"]</></term>
849      <listitem>
850       <para>
851        contains the OID of the table on which the trigger occurred.
852       </para>
853      </listitem>
854     </varlistentry>
855
856     <varlistentry>
857      <term><literal>TD["args"]</></term>
858      <listitem>
859       <para>
860        If the <command>CREATE TRIGGER</> command
861        included arguments, they are available in <literal>TD["args"][0]</> to
862        <literal>TD["args"][<replaceable>n</>-1]</>.
863       </para>
864      </listitem>
865     </varlistentry>
866    </variablelist>
867   </para>
868
869   <para>
870    If <literal>TD["when"]</literal> is <literal>BEFORE</> or
871    <literal>INSTEAD OF</> and
872    <literal>TD["level"]</literal> is <literal>ROW</>, you can
873    return <literal>None</literal> or <literal>"OK"</literal> from the
874    Python function to indicate the row is unmodified,
875    <literal>"SKIP"</> to abort the event, or if <literal>TD["event"]</>
876    is <command>INSERT</> or <command>UPDATE</> you can return
877    <literal>"MODIFY"</> to indicate you've modified the new row.
878    Otherwise the return value is ignored.
879   </para>
880  </sect1>
881
882  <sect1 id="plpython-database">
883   <title>Database Access</title>
884
885   <para>
886    The PL/Python language module automatically imports a Python module
887    called <literal>plpy</literal>.  The functions and constants in
888    this module are available to you in the Python code as
889    <literal>plpy.<replaceable>foo</replaceable></literal>.
890   </para>
891
892   <sect2>
893     <title>Database Access Functions</title>
894
895   <para>
896    The <literal>plpy</literal> module provides several functions to execute
897    database commands:
898   </para>
899
900   <variablelist>
901    <varlistentry>
902     <term><literal>plpy.<function>execute</function>(<replaceable>query</replaceable> [, <replaceable>max-rows</replaceable>])</literal></term>
903     <listitem>
904      <para>
905       Calling <function>plpy.execute</function> with a query string and an
906       optional row limit argument causes that query to be run and the result to
907       be returned in a result object.
908      </para>
909
910      <para>
911       The result object emulates a list or dictionary object.  The result
912       object can be accessed by row number and column name.  For example:
913 <programlisting>
914 rv = plpy.execute("SELECT * FROM my_table", 5)
915 </programlisting>
916       returns up to 5 rows from <literal>my_table</literal>.  If
917       <literal>my_table</literal> has a column
918       <literal>my_column</literal>, it would be accessed as:
919 <programlisting>
920 foo = rv[i]["my_column"]
921 </programlisting>
922       The number of rows returned can be obtained using the built-in
923       <function>len</function> function.
924      </para>
925
926      <para>
927       The result object has these additional methods:
928       <variablelist>
929        <varlistentry>
930         <term><literal><function>nrows</function>()</literal></term>
931         <listitem>
932          <para>
933           Returns the number of rows processed by the command.  Note that this
934           is not necessarily the same as the number of rows returned.  For
935           example, an <command>UPDATE</command> command will set this value but
936           won't return any rows (unless <literal>RETURNING</literal> is used).
937          </para>
938         </listitem>
939        </varlistentry>
940
941        <varlistentry>
942         <term><literal><function>status</function>()</literal></term>
943         <listitem>
944          <para>
945           The <function>SPI_execute()</function> return value.
946          </para>
947         </listitem>
948        </varlistentry>
949
950        <varlistentry>
951         <term><literal><function>colnames</function>()</literal></term>
952         <term><literal><function>coltypes</function>()</literal></term>
953         <term><literal><function>coltypmods</function>()</literal></term>
954         <listitem>
955          <para>
956           Return a list of column names, list of column type OIDs, and list of
957           type-specific type modifiers for the columns, respectively.
958          </para>
959
960          <para>
961           These methods raise an exception when called on a result object from
962           a command that did not produce a result set, e.g.,
963           <command>UPDATE</command> without <literal>RETURNING</literal>, or
964           <command>DROP TABLE</command>.  But it is OK to use these methods on
965           a result set containing zero rows.
966          </para>
967         </listitem>
968        </varlistentry>
969
970        <varlistentry>
971         <term><literal><function>__str__</function>()</literal></term>
972         <listitem>
973          <para>
974           The standard <literal>__str__</literal> method is defined so that it
975           is possible for example to debug query execution results
976           using <literal>plpy.debug(rv)</literal>.
977          </para>
978         </listitem>
979        </varlistentry>
980       </variablelist>
981      </para>
982
983      <para>
984       The result object can be modified.
985      </para>
986
987      <para>
988       Note that calling <literal>plpy.execute</literal> will cause the entire
989       result set to be read into memory.  Only use that function when you are
990       sure that the result set will be relatively small.  If you don't want to
991       risk excessive memory usage when fetching large results,
992       use <literal>plpy.cursor</literal> rather
993       than <literal>plpy.execute</literal>.
994      </para>
995     </listitem>
996    </varlistentry>
997
998    <varlistentry>
999     <term><literal>plpy.<function>prepare</function>(<replaceable>query</replaceable> [, <replaceable>argtypes</replaceable>])</literal></term>
1000     <term><literal>plpy.<function>execute</function>(<replaceable>plan</replaceable> [, <replaceable>arguments</replaceable> [, <replaceable>max-rows</replaceable>]])</literal></term>
1001     <listitem>
1002      <para>
1003       <indexterm><primary>preparing a query</><secondary>in PL/Python</></indexterm>
1004       <function>plpy.prepare</function> prepares the execution plan for a
1005       query.  It is called with a query string and a list of parameter types,
1006       if you have parameter references in the query.  For example:
1007 <programlisting>
1008 plan = plpy.prepare("SELECT last_name FROM my_users WHERE first_name = $1", ["text"])
1009 </programlisting>
1010       <literal>text</literal> is the type of the variable you will be passing
1011       for <literal>$1</literal>.  The second argument is optional if you don't
1012       want to pass any parameters to the query.
1013      </para>
1014      <para>
1015       After preparing a statement, you use a variant of the
1016       function <function>plpy.execute</function> to run it:
1017 <programlisting>
1018 rv = plpy.execute(plan, ["name"], 5)
1019 </programlisting>
1020       Pass the plan as the first argument (instead of the query string), and a
1021       list of values to substitute into the query as the second argument.  The
1022       second argument is optional if the query does not expect any parameters.
1023       The third argument is the optional row limit as before.
1024      </para>
1025
1026      <para>
1027       Query parameters and result row fields are converted between PostgreSQL
1028       and Python data types as described in <xref linkend="plpython-data">.
1029       The exception is that composite types are currently not supported: They
1030       will be rejected as query parameters and are converted to strings when
1031       appearing in a query result.  As a workaround for the latter problem, the
1032       query can sometimes be rewritten so that the composite type result
1033       appears as a result row rather than as a field of the result row.
1034       Alternatively, the resulting string could be parsed apart by hand, but
1035       this approach is not recommended because it is not future-proof.
1036      </para>
1037
1038      <para>
1039       When you prepare a plan using the PL/Python module it is automatically
1040       saved.  Read the SPI documentation (<xref linkend="spi">) for a
1041       description of what this means.  In order to make effective use of this
1042       across function calls one needs to use one of the persistent storage
1043       dictionaries <literal>SD</literal> or <literal>GD</literal> (see
1044       <xref linkend="plpython-sharing">). For example:
1045 <programlisting>
1046 CREATE FUNCTION usesavedplan() RETURNS trigger AS $$
1047     plan = SD.setdefault("plan", plpy.prepare("SELECT 1"))
1048     # rest of function
1049 $$ LANGUAGE plpythonu;
1050 </programlisting>
1051      </para>
1052     </listitem>
1053    </varlistentry>
1054
1055    <varlistentry>
1056     <term><literal>plpy.<function>cursor</function>(<replaceable>query</replaceable>)</literal></term>
1057     <term><literal>plpy.<function>cursor</function>(<replaceable>plan</replaceable> [, <replaceable>arguments</replaceable>])</literal></term>
1058     <listitem>
1059      <para>
1060       The <literal>plpy.cursor</literal> function accepts the same arguments
1061       as <literal>plpy.execute</literal> (except for the row limit) and returns
1062       a cursor object, which allows you to process large result sets in smaller
1063       chunks.  As with <literal>plpy.execute</literal>, either a query string
1064       or a plan object along with a list of arguments can be used.
1065      </para>
1066
1067      <para>
1068       The cursor object provides a <literal>fetch</literal> method that accepts
1069       an integer parameter and returns a result object.  Each time you
1070       call <literal>fetch</literal>, the returned object will contain the next
1071       batch of rows, never larger than the parameter value.  Once all rows are
1072       exhausted, <literal>fetch</literal> starts returning an empty result
1073       object.  Cursor objects also provide an
1074       <ulink url="http://docs.python.org/library/stdtypes.html#iterator-types">iterator
1075       interface</ulink>, yielding one row at a time until all rows are
1076       exhausted.  Data fetched that way is not returned as result objects, but
1077       rather as dictionaries, each dictionary corresponding to a single result
1078       row.
1079      </para>
1080
1081      <para>
1082       An example of two ways of processing data from a large table is:
1083 <programlisting>
1084 CREATE FUNCTION count_odd_iterator() RETURNS integer AS $$
1085 odd = 0
1086 for row in plpy.cursor("select num from largetable"):
1087     if row['num'] % 2:
1088          odd += 1
1089 return odd
1090 $$ LANGUAGE plpythonu;
1091
1092 CREATE FUNCTION count_odd_fetch(batch_size integer) RETURNS integer AS $$
1093 odd = 0
1094 cursor = plpy.cursor("select num from largetable")
1095 while True:
1096     rows = cursor.fetch(batch_size)
1097     if not rows:
1098         break
1099     for row in rows:
1100         if row['num'] % 2:
1101             odd += 1
1102 return odd
1103 $$ LANGUAGE plpythonu;
1104
1105 CREATE FUNCTION count_odd_prepared() RETURNS integer AS $$
1106 odd = 0
1107 plan = plpy.prepare("select num from largetable where num % $1 &lt;&gt; 0", ["integer"])
1108 rows = list(plpy.cursor(plan, [2]))
1109
1110 return len(rows)
1111 $$ LANGUAGE plpythonu;
1112 </programlisting>
1113      </para>
1114
1115      <para>
1116       Cursors are automatically disposed of.  But if you want to explicitly
1117       release all resources held by a cursor, use the <literal>close</literal>
1118       method.  Once closed, a cursor cannot be fetched from anymore.
1119      </para>
1120
1121      <tip>
1122       <para>
1123         Do not confuse objects created by <literal>plpy.cursor</literal> with
1124         DB-API cursors as defined by
1125         the <ulink url="http://www.python.org/dev/peps/pep-0249/">Python
1126         Database API specification</ulink>.  They don't have anything in common
1127         except for the name.
1128       </para>
1129      </tip>
1130     </listitem>
1131    </varlistentry>
1132   </variablelist>
1133
1134   </sect2>
1135
1136   <sect2 id="plpython-trapping">
1137    <title>Trapping Errors</title>
1138
1139    <para>
1140     Functions accessing the database might encounter errors, which
1141     will cause them to abort and raise an exception.  Both
1142     <function>plpy.execute</function> and
1143     <function>plpy.prepare</function> can raise an instance of a subclass of
1144     <literal>plpy.SPIError</literal>, which by default will terminate
1145     the function.  This error can be handled just like any other
1146     Python exception, by using the <literal>try/except</literal>
1147     construct.  For example:
1148 <programlisting>
1149 CREATE FUNCTION try_adding_joe() RETURNS text AS $$
1150     try:
1151         plpy.execute("INSERT INTO users(username) VALUES ('joe')")
1152     except plpy.SPIError:
1153         return "something went wrong"
1154     else:
1155         return "Joe added"
1156 $$ LANGUAGE plpythonu;
1157 </programlisting>
1158    </para>
1159
1160    <para>
1161     The actual class of the exception being raised corresponds to the
1162     specific condition that caused the error.  Refer
1163     to <xref linkend="errcodes-table"> for a list of possible
1164     conditions.  The module
1165     <literal>plpy.spiexceptions</literal> defines an exception class
1166     for each <productname>PostgreSQL</productname> condition, deriving
1167     their names from the condition name.  For
1168     instance, <literal>division_by_zero</literal>
1169     becomes <literal>DivisionByZero</literal>, <literal>unique_violation</literal>
1170     becomes <literal>UniqueViolation</literal>, <literal>fdw_error</literal>
1171     becomes <literal>FdwError</literal>, and so on.  Each of these
1172     exception classes inherits from <literal>SPIError</literal>.  This
1173     separation makes it easier to handle specific errors, for
1174     instance:
1175 <programlisting>
1176 CREATE FUNCTION insert_fraction(numerator int, denominator int) RETURNS text AS $$
1177 from plpy import spiexceptions
1178 try:
1179     plan = plpy.prepare("INSERT INTO fractions (frac) VALUES ($1 / $2)", ["int", "int"])
1180     plpy.execute(plan, [numerator, denominator])
1181 except spiexceptions.DivisionByZero:
1182     return "denominator cannot equal zero"
1183 except spiexceptions.UniqueViolation:
1184     return "already have that fraction"
1185 except plpy.SPIError, e:
1186     return "other error, SQLSTATE %s" % e.sqlstate
1187 else:
1188     return "fraction inserted"
1189 $$ LANGUAGE plpythonu;
1190 </programlisting>
1191     Note that because all exceptions from
1192     the <literal>plpy.spiexceptions</literal> module inherit
1193     from <literal>SPIError</literal>, an <literal>except</literal>
1194     clause handling it will catch any database access error.
1195    </para>
1196
1197    <para>
1198     As an alternative way of handling different error conditions, you
1199     can catch the <literal>SPIError</literal> exception and determine
1200     the specific error condition inside the <literal>except</literal>
1201     block by looking at the <literal>sqlstate</literal> attribute of
1202     the exception object.  This attribute is a string value containing
1203     the <quote>SQLSTATE</quote> error code.  This approach provides
1204     approximately the same functionality
1205    </para>
1206   </sect2>
1207  </sect1>
1208
1209  <sect1 id="plpython-subtransaction">
1210   <title>Explicit Subtransactions</title>
1211
1212   <para>
1213    Recovering from errors caused by database access as described in
1214    <xref linkend="plpython-trapping"> can lead to an undesirable
1215    situation where some operations succeed before one of them fails,
1216    and after recovering from that error the data is left in an
1217    inconsistent state.  PL/Python offers a solution to this problem in
1218    the form of explicit subtransactions.
1219   </para>
1220
1221   <sect2>
1222    <title>Subtransaction Context Managers</title>
1223
1224    <para>
1225     Consider a function that implements a transfer between two
1226     accounts:
1227 <programlisting>
1228 CREATE FUNCTION transfer_funds() RETURNS void AS $$
1229 try:
1230     plpy.execute("UPDATE accounts SET balance = balance - 100 WHERE account_name = 'joe'")
1231     plpy.execute("UPDATE accounts SET balance = balance + 100 WHERE account_name = 'mary'")
1232 except plpy.SPIError, e:
1233     result = "error transferring funds: %s" % e.args
1234 else:
1235     result = "funds transferred correctly"
1236 plan = plpy.prepare("INSERT INTO operations (result) VALUES ($1)", ["text"])
1237 plpy.execute(plan, [result])
1238 $$ LANGUAGE plpythonu;
1239 </programlisting>
1240     If the second <literal>UPDATE</literal> statement results in an
1241     exception being raised, this function will report the error, but
1242     the result of the first <literal>UPDATE</literal> will
1243     nevertheless be committed.  In other words, the funds will be
1244     withdrawn from Joe's account, but will not be transferred to
1245     Mary's account.
1246    </para>
1247
1248    <para>
1249     To avoid such issues, you can wrap your
1250     <literal>plpy.execute</literal> calls in an explicit
1251     subtransaction.  The <literal>plpy</literal> module provides a
1252     helper object to manage explicit subtransactions that gets created
1253     with the <literal>plpy.subtransaction()</literal> function.
1254     Objects created by this function implement the
1255     <ulink url="http://docs.python.org/library/stdtypes.html#context-manager-types">
1256     context manager interface</ulink>.  Using explicit subtransactions
1257     we can rewrite our function as:
1258 <programlisting>
1259 CREATE FUNCTION transfer_funds2() RETURNS void AS $$
1260 try:
1261     with plpy.subtransaction():
1262         plpy.execute("UPDATE accounts SET balance = balance - 100 WHERE account_name = 'joe'")
1263         plpy.execute("UPDATE accounts SET balance = balance + 100 WHERE account_name = 'mary'")
1264 except plpy.SPIError, e:
1265     result = "error transferring funds: %s" % e.args
1266 else:
1267     result = "funds transferred correctly"
1268 plan = plpy.prepare("INSERT INTO operations (result) VALUES ($1)", ["text"])
1269 plpy.execute(plan, [result])
1270 $$ LANGUAGE plpythonu;
1271 </programlisting>
1272     Note that the use of <literal>try/catch</literal> is still
1273     required.  Otherwise the exception would propagate to the top of
1274     the Python stack and would cause the whole function to abort with
1275     a <productname>PostgreSQL</productname> error, so that the
1276     <literal>operations</literal> table would not have any row
1277     inserted into it.  The subtransaction context manager does not
1278     trap errors, it only assures that all database operations executed
1279     inside its scope will be atomically committed or rolled back.  A
1280     rollback of the subtransaction block occurs on any kind of
1281     exception exit, not only ones caused by errors originating from
1282     database access.  A regular Python exception raised inside an
1283     explicit subtransaction block would also cause the subtransaction
1284     to be rolled back.
1285    </para>
1286   </sect2>
1287
1288   <sect2>
1289    <title>Older Python Versions</title>
1290
1291    <para>
1292     Context managers syntax using the <literal>with</literal> keyword
1293     is available by default in Python 2.6.  If using PL/Python with an
1294     older Python version, it is still possible to use explicit
1295     subtransactions, although not as transparently.  You can call the
1296     subtransaction manager's <literal>__enter__</literal> and
1297     <literal>__exit__</literal> functions using the
1298     <literal>enter</literal> and <literal>exit</literal> convenience
1299     aliases.  The example function that transfers funds could be
1300     written as:
1301 <programlisting>
1302 CREATE FUNCTION transfer_funds_old() RETURNS void AS $$
1303 try:
1304     subxact = plpy.subtransaction()
1305     subxact.enter()
1306     try:
1307         plpy.execute("UPDATE accounts SET balance = balance - 100 WHERE account_name = 'joe'")
1308         plpy.execute("UPDATE accounts SET balance = balance + 100 WHERE account_name = 'mary'")
1309     except:
1310         import sys
1311         subxact.exit(*sys.exc_info())
1312         raise
1313     else:
1314         subxact.exit(None, None, None)
1315 except plpy.SPIError, e:
1316     result = "error transferring funds: %s" % e.args
1317 else:
1318     result = "funds transferred correctly"
1319
1320 plan = plpy.prepare("INSERT INTO operations (result) VALUES ($1)", ["text"])
1321 plpy.execute(plan, [result])
1322 $$ LANGUAGE plpythonu;
1323 </programlisting>
1324    </para>
1325
1326    <note>
1327     <para>
1328      Although context managers were implemented in Python 2.5, to use
1329      the <literal>with</literal> syntax in that version you need to
1330      use a <ulink
1331      url="http://docs.python.org/release/2.5/ref/future.html">future
1332      statement</ulink>.  Because of implementation details, however,
1333      you cannot use future statements in PL/Python functions.
1334     </para>
1335    </note>
1336   </sect2>
1337  </sect1>
1338
1339  <sect1 id="plpython-util">
1340   <title>Utility Functions</title>
1341   <para>
1342    The <literal>plpy</literal> module also provides the functions
1343    <literal>plpy.debug(<replaceable>msg</>)</literal>,
1344    <literal>plpy.log(<replaceable>msg</>)</literal>,
1345    <literal>plpy.info(<replaceable>msg</>)</literal>,
1346    <literal>plpy.notice(<replaceable>msg</>)</literal>,
1347    <literal>plpy.warning(<replaceable>msg</>)</literal>,
1348    <literal>plpy.error(<replaceable>msg</>)</literal>, and
1349    <literal>plpy.fatal(<replaceable>msg</>)</literal>.<indexterm><primary>elog</><secondary>in PL/Python</></indexterm>
1350    <function>plpy.error</function> and
1351    <function>plpy.fatal</function> actually raise a Python exception
1352    which, if uncaught, propagates out to the calling query, causing
1353    the current transaction or subtransaction to be aborted.
1354    <literal>raise plpy.Error(<replaceable>msg</>)</literal> and
1355    <literal>raise plpy.Fatal(<replaceable>msg</>)</literal> are
1356    equivalent to calling
1357    <function>plpy.error</function> and
1358    <function>plpy.fatal</function>, respectively.
1359    The other functions only generate messages of different
1360    priority levels.
1361    Whether messages of a particular priority are reported to the client,
1362    written to the server log, or both is controlled by the
1363    <xref linkend="guc-log-min-messages"> and
1364    <xref linkend="guc-client-min-messages"> configuration
1365    variables. See <xref linkend="runtime-config"> for more information.
1366   </para>
1367
1368   <para>
1369    Another set of utility functions are
1370    <literal>plpy.quote_literal(<replaceable>string</>)</literal>,
1371    <literal>plpy.quote_nullable(<replaceable>string</>)</literal>, and
1372    <literal>plpy.quote_ident(<replaceable>string</>)</literal>.  They
1373    are equivalent to the built-in quoting functions described in <xref
1374    linkend="functions-string">.  They are useful when constructing
1375    ad-hoc queries.  A PL/Python equivalent of dynamic SQL from <xref
1376    linkend="plpgsql-quote-literal-example"> would be:
1377 <programlisting>
1378 plpy.execute("UPDATE tbl SET %s = %s WHERE key = %s" % (
1379     plpy.quote_ident(colname),
1380     plpy.quote_nullable(newvalue),
1381     plpy.quote_literal(keyvalue)))
1382 </programlisting>
1383   </para>
1384  </sect1>
1385
1386  <sect1 id="plpython-envar">
1387   <title>Environment Variables</title>
1388
1389   <para>
1390    Some of the environment variables that are accepted by the Python
1391    interpreter can also be used to affect PL/Python behavior.  They
1392    would need to be set in the environment of the main PostgreSQL
1393    server process, for example in a start script.  The available
1394    environment variables depend on the version of Python; see the
1395    Python documentation for details.  At the time of this writing, the
1396    following environment variables have an affect on PL/Python,
1397    assuming an adequate Python version:
1398    <itemizedlist>
1399     <listitem>
1400      <para><envar>PYTHONHOME</envar></para>
1401     </listitem>
1402
1403     <listitem>
1404      <para><envar>PYTHONPATH</envar></para>
1405     </listitem>
1406
1407     <listitem>
1408      <para><envar>PYTHONY2K</envar></para>
1409     </listitem>
1410
1411     <listitem>
1412      <para><envar>PYTHONOPTIMIZE</envar></para>
1413     </listitem>
1414
1415     <listitem>
1416      <para><envar>PYTHONDEBUG</envar></para>
1417     </listitem>
1418
1419     <listitem>
1420      <para><envar>PYTHONVERBOSE</envar></para>
1421     </listitem>
1422
1423     <listitem>
1424      <para><envar>PYTHONCASEOK</envar></para>
1425     </listitem>
1426
1427     <listitem>
1428      <para><envar>PYTHONDONTWRITEBYTECODE</envar></para>
1429     </listitem>
1430
1431     <listitem>
1432      <para><envar>PYTHONIOENCODING</envar></para>
1433     </listitem>
1434
1435     <listitem>
1436      <para><envar>PYTHONUSERBASE</envar></para>
1437     </listitem>
1438
1439     <listitem>
1440      <para><envar>PYTHONHASHSEED</envar></para>
1441     </listitem>
1442    </itemizedlist>
1443
1444    (It appears to be a Python implementation detail beyond the control
1445    of PL/Python that some of the environment variables listed on
1446    the <command>python</command> man page are only effective in a
1447    command-line interpreter and not an embedded Python interpreter.)
1448   </para>
1449  </sect1>
1450 </chapter>