Postgresql – n efficient way to use 3rd party packages in PL/Python

plpythonpostgresql

I've found examples of people importing 3rd party packages into PL/Python scripts, but it seems to me that re-importing libraries upon every run of a stored procedure is terribly inefficient.

Does Postgres maintain a process-wide python interpreter that's re-used for subsequent PL/Python script executions?

If so, is there any way to have it execute import statements in that context BEFORE a PL/Python procedure runs?

Alternatively, would it be possible/practical to provide a C-compatible dynamically-linked library that hosts a persistent Python interpreter?

Best Answer

I've found examples of people importing 3rd part packages into PL/Python scripts, but it seems to me that re-importing libraries upon every run of a stored procedure is terribly inefficient.

Using strace, I can see that the files are only actually read the first time an import is done in any given session (there may be exceptions, like if the effective user id changes within a session). There may be some initialization which is re-done every time the import is run, I don't know how to investigate that.

I don't think you can boost the loading to above the session level.