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:
CREATEFUNCTION funcname (argument-list) RETURNSreturn-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.
CREATEFUNCTION pymax (a integer, b integer) RETURNSinteger 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.
CREATE FUNCTION pymax (a integer, b integer) RETURNS integer
this is
0
1
0
Font Weight
Font Family
Undo Last Change
Redo Last Change
Clear Content
Insert Line Break
Indent Content
Outdent Content
Left Align
Center Align
Right Align
Justify Align
Bold Text
Italicize Text
Strikethrough Text
Underline Text
Superscript Text
Subscript Text
Insert Header
Change Background Color
Change Text Color
Highlight Text
Insert Saved Version of Article
Insert Image
Insert Audio
Insert Video
Upload GIF
Embed News or Youtube Video
Insert a Quote
Insert Lists
Insert <aside> Element
Insert <details> Element
Insert Code Markup
Insert Math Markup
Insert Table
Insert Chart
Insert Horizontal Rule
Insert Paragraph at End of Editor
View Result at Different Screen Sizes
Edit the Style of the Block Elements in Selection
Insert Link
Code Text
Clear Text Formatting
Speech To Text
Download Editor State
Upload Lexical, Markdown, Notebook, or TeX
Insert Keyboard Command
Insert Abbreviation
Insert Inline Quote
Insert Section Heading
Insert Custom HTML
Insert Columns Layout
Copy Editor Contents
Rewrite Selection with AI
Please input a valid url.
#ffffff
#000000
Are you sure you want to delete this article section? You can not undo this change.
Add a Comment
Annotate Article
Copy Link to Selection
Share Article Selection
Ask AI About Selection
Write with AI
Rewrite Paragraph with AI
Rewrite with AI
A previous, saved version of this article is saved. To compare the current version of the article with the previous version - or to replace the current version with the saved version - click the icon above the text editor.
Successfully copied URL to clipboard!
Copied code to clipboard.
Copied TeX code to clipboard.
Uploaded file must be an image of type .jpeg, .jpg, .png, .webp, .gif, .bmp, or .svg.
Image Size must be less than 5MB.
Uploaded file must be an image of type jpeg, jpg, png, webp, avif, tiff, or svg.
Something went wrong uploading the image to the database. Try reloading the page.
The maximum number of images you can upload is 30.
There was an error taking an image using the device's camera. Try uploading an image instead.
Uploaded file must be an audio file.
Audio file must be an audio file of type .m4a, .flac, .mp3, .mp4, .wav, .wma, .aac, .webm, or .mpeg.
Audio file must be less than 300 MB in size.
Something went wrong uploading the audio file. Try reloading the page.
The maximum number of audio files you can upload is 10.
There was an error capturing an audio recording using the device's microphone. Try uploading an audio recording or video instead.
Uploaded file must be an video file.
Video file must be an audio file of type .mp4, .mov, .avi, .wmv, .avchd, .webm, or .flv.
Video file must be less than 300 MB in size.
Something went wrong uploading the video file. Try reloading the page.
The maximum number of video files you can upload is 10.
There was an error taking a video using the device's camera / microphone. Try uploading a video instead.
Comments
You can read more about how comments are sorted in this blog post.
User Comments
this is