![]() | ![]() | ![]() | ![]() |
41.1. PL/Python FunctionsFunctions in PL/Python are declared via the standard CREATE FUNCTION syntax: CREATE FUNCTION funcname (argument-list) RETURNS return-type AS $$ # PL/Python function body $$ LANGUAGE plpythonu;
The body of a function is simply a Python script. When the function is called, its arguments are passed as elements of the array args[]; named arguments are also passed as ordinary variables to the Python script. The result is returned from the Python code in the usual way, with return or yield (in case of a result-set statement). For example, a function to return the greater of two integers can be defined as: CREATE FUNCTION pymax (a integer, b integer)
RETURNS integer
AS $$
if a > b:
return a
return b
$$ LANGUAGE plpythonu;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: def __plpython_procedure_pymax_23456():
if a > b:
return a
return bassuming that 23456 is the OID assigned to the function by PostgreSQL. The PostgreSQL function parameters are available in
the global args list. In the
If an SQL null value is passed to a function, the argument value will appear as None in Python. The above function definition 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: CREATE FUNCTION pymax (a integer, b integer)
RETURNS integer
AS $$
if (a is None) or (b is None):
return None
if a > b:
return a
return b
$$ LANGUAGE plpythonu;As shown above, to return an SQL null value from a PL/Python function, return the value None. This can be done whether the function is strict or not. 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: CREATE TABLE employee (
name text,
salary integer,
age integer
);
CREATE FUNCTION overpaid (e employee)
RETURNS boolean
AS $$
if e["salary"] > 200000:
return True
if (e["age"] < 30) and (e["salary"] > 100000):
return True
return False
$$ LANGUAGE plpythonu;
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:
If you do not provide a return value, Python returns the default None. PL/Python translates Python's None into the SQL null value. 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: CREATE TYPE greeting AS ( how text, who text ); A set result can be returned from a:
The global dictionary SD is available to store data between function calls. This variable is private static data. The global dictionary GD is public data, available to all Python functions within a session. Use with care. Each function gets its own execution environment in the
Python interpreter, so that global data and function arguments from
|
||||||||||||||