sortas="PL/Python">PL/Python is passed to a
function, return the value None. This can be done whether the
function is strict or not.
+
+
+
Arrays, Lists
SQL array values are passed into PL/Python as a Python list. To
return an SQL array value out of a PL/Python function, return a
(1 row)
+
+
+
Composite Types
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 you do not provide a return value, Python returns the default
-
None.
PL/Python translates
- Python's None into the SQL null value.
-
-
+
+
Set-Returning Functions
A
PL/Python function can also return sets of
scalar or composite types. There are several ways to achieve this because
- Currently, due to Python
+ Due to Python
some debug versions of Python 2.4
(configured and compiled with option --with-pydebug)
+
+
+
+
Sharing Data
The global dictionary SD is available to store
data between function calls. This variable is private static data.
When a function is used as a trigger, the dictionary
- TD contains trigger-related values.
- TD["event"]> contains
- the event as a string (INSERT>, UPDATE>,
- DELETE>, TRUNCATE>, or UNKNOWN>).
- TD["when"]> contains one of BEFORE>,
- AFTER>, or UNKNOWN>.
- TD["level"]> contains one of ROW>,
- STATEMENT>, or UNKNOWN>.
- For a row-level trigger, the trigger
- rows are in TD["new"]> and/or TD["old"]>
- depending on the trigger event.
- TD["name"]> contains the trigger name,
- TD["table_name"]> contains the name of the table on which the trigger occurred,
- TD["table_schema"]> contains the schema of the table on which the trigger occurred,
- and TD["relid"]> contains the OID of the table on
- which the trigger occurred. If the CREATE TRIGGER> command
- included arguments, they are available in TD["args"][0]> to
- TD["args"][n>-1]>.
+ TD contains trigger-related values:
+
+
+ TD["event"]>
+
+ contains the event as a string:
+ INSERT>, UPDATE>,
+ DELETE>, TRUNCATE>,
+ or UNKNOWN>.
+
+
+
+
+
+ TD["when"]>
+
+ contains one of BEFORE>, AFTER>,
+ or UNKNOWN>.
+
+
+
+
+
+ TD["level"]>
+
+ contains one of ROW>,
+ STATEMENT>, or UNKNOWN>.
+
+
+
+
+
+ TD["new"]>
+ TD["old"]>
+
+ For a row-level trigger, one or both of these fields contain
+ the respective trigger rows, depending on the trigger event.
+
+
+
+
+
+ TD["name"]>
+
+ contains the trigger name.
+
+
+
+
+
+ TD["table_name"]>
+
+ contains the name of the table on which the trigger occurred.
+
+
+
+
+
+ TD["table_schema"]>
+
+ contains the schema of the table on which the trigger occurred.
+
+
+
+
+
+ TD["relid"]>
+
+ contains the OID of the table on which the trigger occurred.
+
+
+
+
+
+ TD["args"]>
+
+ If the CREATE TRIGGER> command
+ included arguments, they are available in TD["args"][0]> to
+ TD["args"][n>-1]>.
+
+
+
+
The PL/Python language module automatically imports a Python module
called plpy. The functions and constants in
this module are available to you in the Python code as
- plpy.foo. At present
- plpy implements the functions
- plpy.debug(msg>),
- plpy.log(msg>),
- plpy.info(msg>),
- plpy.notice(msg>),
- plpy.warning(msg>),
- plpy.error(msg>), and
-
plpy.fatal(msg>).
elog>in PL/Python>
- plpy.error and
- plpy.fatal actually raise a Python exception
- which, if uncaught, propagates out to the calling query, causing
- the current transaction or subtransaction to be aborted.
- raise plpy.ERROR(msg>) and
- raise plpy.FATAL(msg>) are
- equivalent to calling
- plpy.error and
- plpy.fatal, respectively.
- The other functions only generate messages of different
- priority levels.
- Whether messages of a particular priority are reported to the client,
- written to the server log, or both is controlled by the
- and
- configuration
- variables. See for more information.
+ plpy.foo.
- Additionally, the plpy module provides two
+ The plpy module provides two
functions called execute and
prepare. Calling
plpy.execute with a query string and an
In order to make effective use of this across function calls
one needs to use one of the persistent storage dictionaries
SD or GD (see
- funcs">). For example:
+ sharing">). For example:
CREATE FUNCTION usesavedplan() RETURNS trigger AS $$
if SD.has_key("plan"):
-
-
-
-
-
Restricted Environment
-
+
+
Utility Functions
- The current version of
PL/Python
- functions as a trusted language only; access to the file system and
- other local resources is disabled. Specifically,
-
PL/Python uses the Python restricted
- execution environment, further restricts it to prevent the use of
- the file open> call, and allows only modules from a
- specific list to be imported. Presently, that list includes:
- array>, bisect>, binascii>,
- calendar>, cmath>, codecs>,
- errno>, marshal>, math>, md5>,
- mpz>, operator>, pcre>,
- pickle>, random>, re>, regex>,
- sre>, sha>, string>, StringIO>,
- struct>, time>, whrandom>, and
- zlib>.
+ The plpy module also provides the functions
+ plpy.debug(msg>),
+ plpy.log(msg>),
+ plpy.info(msg>),
+ plpy.notice(msg>),
+ plpy.warning(msg>),
+ plpy.error(msg>), and
+
plpy.fatal(msg>).
elog>in PL/Python>
+ plpy.error and
+ plpy.fatal actually raise a Python exception
+ which, if uncaught, propagates out to the calling query, causing
+ the current transaction or subtransaction to be aborted.
+ raise plpy.ERROR(msg>) and
+ raise plpy.FATAL(msg>) are
+ equivalent to calling
+ plpy.error and
+ plpy.fatal, respectively.
+ The other functions only generate messages of different
+ priority levels.
+ Whether messages of a particular priority are reported to the client,
+ written to the server log, or both is controlled by the
+ and
+ configuration
+ variables. See for more information.
-]]>
-