GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.postgresql.org — 46.1. PL/Python Functions November 19, 2026: PostgreSQL 19 Released!
Development Versions:
devel
46.1. PL/Python Functions #Functions in PL/Python are declared via the standard CREATE FUNCTION syntax: CREATE FUNCTION The body of a function is simply a Python script. When the function is called, its arguments are passed as elements of the list 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 plpython3u;
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 b
assuming that 23456 is the OID assigned to the function by PostgreSQL. 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: CREATE FUNCTION pystrip(x text) RETURNS text AS $$ x = x.strip() # error return x $$ LANGUAGE plpython3u; because assigning to CREATE FUNCTION pystrip(x text) RETURNS text AS $$ global x x = x.strip() # ok now return x $$ LANGUAGE plpython3u; 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. Submit correctionIf you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue. |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |