PL/Python — Python Procedural Language

I want to read about PL/Python to see if it offers any advantages over PL./pgSQL and to see if I might like it better.

Date Created:
1 39

References



Notes


The PL/Python procedural language allows PostgreSQL functions and procedures to be written in the Python language.
To install PL/Python in a particular database, use CREATE EXTENSION plpython3u.

PL/Python is only available as an untrusted language, meaning it does not offer any way of restricting what users can do in it and is therefore named plpython3u. A trusted variant plpython might become available in the future iif a secure execution mechanism is developed in Python. The writer of a function is untrusted PL/Python must take care that the function cannot be used to do anything unwanted, since it will be able to do anything that could be done by a user logged in as the database administrator.

PL/Python Functions

Functions in PL/Python are declared via standard CREATE FUNCTION syntax:

CREATE FUNCTION funcname (argument-list)
RETURNS return-type
AS $$
# PL/Python function body
$$ LANGUAGE plpython3u;

The body of a function is simply a Python script. When the function is called, its arguments are passed as elements of the list args; named arguments are also passed as ordinary variables to the Python script. Use of named arguments is usually more readable. The result is returned from the Python code in the usual way, with return or yield (in case of a result-set statement). If you do not provide a return value, Python returns the default None., PL/Python translates Python's None into the SQL null value. In a procedure, the result form the Python code must be None; otherwise, an error will be raised.

CREATE FUNCTION pymax (a integer, b integer)
RETURNS integer
AS $$
if a > b:
return a
return b
$$ LANGUAGE plpython3u;

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. It is best to treat function parameters as read-only.

Data Values

Generally speaking, the aim of PL/Python is to provide a natural mapping between the PostgreSQL and the Python worlds. This informs the data mapping rules described below.

When a PL/Python function is called, its arguments are converted from their PostgreSQL data type to the corresponding Python type.

When a PL/Python function returns, its return value is converted to the function's declared PostgreSQL return data type.

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 Python list.

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.

Sharing Data

The global dictionary SD is available to store private data between repeated calls to the same function. The global dictionary GD is public data, that is available to all python functions within a session. Each function gets its own execution environment in the Python interpreter, so that global data and function arguments from myfunc are not available to myfunc2.

Comments

You have to be logged in to add a comment

User Comments

Frank Frank
CREATE FUNCTION pymax (a integer, b integer)
RETURNS integer

this is

1

Insert Math Markup

ESC
About Inserting Math Content
Display Style:

Embed News Content

ESC
About Embedding News Content

Embed Youtube Video

ESC
Embedding Youtube Videos

Embed TikTok Video

ESC
Embedding TikTok Videos

Embed X Post

ESC
Embedding X Posts

Embed Instagram Post

ESC
Embedding Instagram Posts

Insert Details Element

ESC

Example Output:

Summary Title
You will be able to insert content here after confirming the title of the <details> element.

Insert Table

ESC
Customization
Align:
Preview:

Insert Horizontal Rule

#000000

Preview:


View Content At Different Sizes

ESC

Edit Style of Block Nodes

ESC

Edit the background color, default text color, margin, padding, and border of block nodes. Editable block nodes include paragraphs, headers, and lists.

#ffffff
#000000

Edit Selected Cells

Change the background color, vertical align, and borders of the cells in the current selection.

#ffffff
Vertical Align:
Border
#000000
Border Style:

Edit Table

ESC
Customization:
Align:

Upload Lexical State

ESC

Upload a .lexical file. If the file type matches the type of the current editor, then a preview will be shown below the file input.

Upload 3D Object

ESC

Upload Jupyter Notebook

ESC

Upload a Jupyter notebook and embed the resulting HTML in the text editor.

Insert Custom HTML

ESC

Edit Image Background Color

ESC
#ffffff

Insert Columns Layout

ESC
Column Type:

Select Code Language

ESC
Select Coding Language

Insert Chart

ESC

Use the search box below

Upload Previous Version of Article State

ESC