AnalyticDB for PostgreSQL allows you to create user-defined functions (UDFs) in the PL/Python procedural language.

Important To install or upgrade extensions on an instance that runs V6.3.8.9 or later, Submit a ticket.

For more information about how to view the minor version of an instance, see View the minor engine version.

Limits

  • AnalyticDB for PostgreSQL does not support triggers in PL/Python.
  • AnalyticDB for PostgreSQL does not support cursors for data updates. For example, you cannot use UPDATE...WHERE CURRENT OF or DELETE...WHERE CURRENT OF.
  • AnalyticDB for PostgreSQL supports only Python 2.

Create or delete a PL/Python extension

In AnalyticDB for PostgreSQL, execute the following statements to create or delete a PL/Python extension. You need to execute a statement only once for each database. PL/Python is considered an untrusted language. In this case, you must execute the statements by using a privileged account. In the following statements, the admin privileged account and the testdb database are used.

Create a PL/Python extension

$ psql -U admin -d testdb -c 'CREATE EXTENSION plpythonu;'

Delete a PL/Python extension

$ psql -U admin -d testdb -c 'DROP EXTENSION plpythonu;'

Use PL/Python to create functions

Important For security considerations, you do not have the permissions to create functions in PL/Python. To create such a function, Submit a ticket. After your request is accepted, Alibaba Cloud technicians help you create the function.

Create a function in PL/Python

CREATE FUNCTION return_int_array()
  RETURNS int[]
AS $$
  return [1, 2, 3, 4]
$$ LANGUAGE plpythonu;

Call the function

SELECT return_int_array();
return_int_array
---------------------
{1,11,21,31}
(1 row) 

References

For more information about how to use Python, visit the Python official website.

For more information about how to use PL/Python, see PL/Python - Python Procedural Language.