Database Reference
In-Depth Information
Now that we have created an extension, let's start writing a simple Python function,
and you will see that it won't be that different from the Python code style.
If you face Python installation issues, don't worry you will surely land
safely after some research.
If you need some help, solutions for Python issues can be found at
http://raghavt.blogspot.com/2013/07/error-could-not-
access-file.html .
Writing functions in PL/Python
The test_python() function simply takes two arguments as integers, compares
their values, and returns the respective result after evaluation. We will write more
meaningful examples ahead.
Be careful about indentation when programming in Python. You
should learn by trying to disturb the indentation irst and observe
Python's behavior.
The following example makes use of the test_python() function:
warehouse_db=# CREATE OR REPLACE FUNCTION test_python(x integer,
y integer)
RETURNS integer
AS $$
if x < y:
return x
return y
$$ LANGUAGE plpython3u;
Execute the preceding command in the psql utility:
warehouse_db=# SELECT test_python(5,6);
test_python
-------------
5
(1 row)
Handling arguments with PL/Python
Now, let's write another simple function that takes text as input and returns text
to the client program in the following manner:
warehouse_db=# CREATE OR REPLACE FUNCTION python_string(txtdata
text)
RETURNS text
 
Search WWH ::




Custom Search