Search code examples
postgresqlplpython

How is PL/Python code executed by Postgresql


When a PL/Python procedure is executed, the python code is executed by a Python interpreter. My question is, is the Python interpreter running as a separate process, or is it a shared library that gets linked to the calling databases process?

I'm concerned about what happens when we call something like plpy.execute(...). If the python interpreter is running as a separate process I imagine there would be a lot of overhead involved in passing the result of the sql query back to the python interpreter, which would require reading from a file or pipe.


Solution

  • The language handler function (plpython3_call_handler()) loads the plpython3.so library into the PostgreSQL process, which is linked to libpython3.so. So the interpreter is loaded into the backend, it is not executed as a separate process (multiprocessing/multithreading is not allowed in PostgreSQL client backends, with the exception of parallel workers).