Function arguments are converted from their PostgreSQL type
to a corresponding Python type:

PostgreSQL boolean is converted to
Python bool.

PostgreSQL smallint and int are converted to Python int. PostgreSQL bigint
and oid are converted to long in Python 2 and to int in Python 3.

PostgreSQL real, double, and numeric are
converted to Python float. Note that
for the numeric this loses
information and can lead to incorrect results. This might
be fixed in a future release.

PostgreSQL bytea is converted to
Python str in Python 2 and to
bytes in Python 3. In Python 2, the
string should be treated as a byte sequence without any
character encoding.

All other data types, including the PostgreSQL character
string types, are converted to a Python str. In Python 2, this string will be in the
PostgreSQL server encoding; in Python 3, it will be a
Unicode string like all strings.

When the PostgreSQL return type is boolean, the return value will be evaluated for
truth according to the Python rules. That is, 0 and
empty string are false, but notably 'f' is true.

When the PostgreSQL return type is bytea, the return value will be converted to a
string (Python 2) or bytes (Python 3) using the respective
Python built-ins, with the result being converted
bytea.

For all other PostgreSQL return types, the returned
Python value is converted to a string using the Python
built-in str, and the result is
passed to the input function of the PostgreSQL data
type.

Strings in Python 2 are required to be in the PostgreSQL
server encoding when they are passed to PostgreSQL. Strings
that are not valid in the current server encoding will
raise an error, but not all encoding mismatches can be
detected, so garbage data can still result when this is not
done correctly. Unicode strings are converted to the
correct encoding automatically, so it can be safer and more
convenient to use those. In Python 3, all strings are
Unicode strings.

For nonscalar data types, see below.

Note that logical mismatches between the declared PostgreSQL
return type and the Python data type of the actual return
object are not flagged; the value will be converted in any
case.

If an SQL null value is passed to a function, the argument
value will appear as None in Python.
For example, the function definition of pymax shown in Section 43.2 will return the wrong
answer for null inputs. We could add STRICT to the function definition to make
PostgreSQL do something more
reasonable: if a null value is passed, the function will not be
called at all, but will just return a null result
automatically. Alternatively, we could check for null inputs in
the function body:

Composite-type arguments are passed to the function as
Python mappings. The element names of the mapping are the
attribute names of the composite type. If an attribute in the
passed row has the null value, it has the value None in the mapping. Here is an example:

There are multiple ways to return row or composite types
from a Python function. The following examples assume we
have:

CREATE TYPE named_value AS (
name text,
value integer
);

A composite result can be returned as a:

Sequence type (a tuple or list, but not a set because
it is not indexable)

Returned sequence objects must have the same number of
items as the composite result type has fields. The item
with index 0 is assigned to the first field of the
composite type, 1 to the second and so on. For
example:

A PL/Python function can
also return sets of scalar or composite types. There are
several ways to achieve this because the returned object is
internally turned into an iterator. The following examples
assume we have composite type:

Due to Python bug #1483133, some debug versions of
Python 2.4 (configured and compiled with option
--with-pydebug) are
known to crash the PostgreSQL server when using
an iterator to return a set result. Unpatched
versions of Fedora 4 contain this bug. It does
not happen in production versions of Python or on
patched versions of Fedora 4.

Set-returning functions with OUT
parameters (using RETURNS SETOF
record) are also supported. For example: