Is there a way to access PostgreSQL functions from within a PL/Python function?
When I'm writing in PL/pgSQL I can just call them. For example I could write char_length('Bob') in PL/pgSQL and it would use the builtin PostgreSQL function char_length. When I try that for PL/Python it errors with char_length is undefined. Is there a namespace where I can access the builtin functions? plpy.functions.whatever or something like that?
I'm using plpython3u on PostgreSQL 9.5.9.
1 Answer 1
You can execute any sql command using the function plpy.execute(), example:
create or replace function py_test(str text)
returns int
language plpython3u
as $$
res = plpy.execute("select char_length('{}')".format(str))
return res[0]["char_length"]
$$;
select py_test('abc');
py_test
---------
3
(1 row)
Read in the documentation: Database Access Functions.