Search code examples
postgresqlplpython

How are python functions compiled and stored in PostgreSQL?


Scenario:

  1. I have a python function on disk somewhere.
  2. I compile a plpythonu function in pg server, which is referencing that python function from step 1 on disk.
  3. I change something in python function (from step 1).
  4. If I compile plpythonu function (from step 2), change done in step 3 does not take effect when calling from pg server.

Example of such functions can be seen in my other question: Python function hangs when called from within sql function

My assumptions (what this seems like to me):

  • PG server stores both the python function (step 1) code and plpythonu code somewhere at first compilation, where ?
  • It does not check again the linked function (step 1) when recompiling (step 4) the plpythonu function. Can this behavior be changed or affected somehow ?

If these assumptions are wrong, please also correct me and explain. Or even point me to documentation where this can be found, I have not succeeded in finding it yet.


Solution

  • PG server stores both the python function (step 1) code and plpythonu code somewhere at first compilation, where ?

    The Python code for the pl/python function its self (but not any modules, libraries, etc) is stored in the pg_proc table in the database.

    The compiled Python bytecode gets stored in the syscache in memory when it's first run by a given backend. It isn't updated after that if files on disk change. New connections will see the new code, old connections the existing code.