Search code examples
multithreadingpostgresqlstored-procedurespljava

Porting PL/pgSQL stored procedures?


We have some PL/pgSQL stored procedures in our DB (PostgreSQL 9.x).

These are strictly sequential, and under some circumstances, can be very slow.

We are thinking on porting these to PL/Java, PL/Python or something like these, and exploit the multithreading abilities of these languages.

The main question is: how "effectively" these language supports are implemented? For example, I'm thinking on the Virtual Machines that run Java code: when calling my PL/Java code, each time it summons a new VM for it, or does PL/Java keeps some kind of pool of VMs, and associate one of them for the actual call?


Solution

  • Pl/Java runs in a jvm embedded in the postgres backend process. See: PL/Java wiki

    I'd suggest that you don't just translate everything to java without first looking at pure SQL solutions. In many cases SQL's ability to work on a large number of records in bulk will outweigh any benefit of dealing with records individually but leading to more SQL calls.

    The multi-threading capability of Java doesn't give benefits if the task is inherently sequential.