tada / pljava

PL/Java is a free add-on module that brings Java™ Stored Procedures, Triggers, Functions, Aggregates, Operators, Types, etc., to the PostgreSQL™ backend.
http://tada.github.io/pljava/
Other
250 stars 80 forks source link

GraalVM native #358

Open ccleve opened 3 years ago

ccleve commented 3 years ago

Is it possible to use the GraalVM native compiler to create a binary, and use that as an extension?

I asked about this on the Graal Slack quite some time ago, and they suggested that yes, it was possible to create a C-compatible .so library, but the instructions for doing so were unclear. They did identify these starting points:

https://github.com/kirillp/graalSamples/tree/master/simpleApp

https://github.com/oracle/graal/blob/master/substratevm/src/com.oracle.svm.jvmtiagentbase/src/com/oracle/svm/jvmtiagentbase/AgentIsolate.java

I think that if we could get true native speed from a Postgres extension written in Java, or any polyglot language, it would be a real game-changer.

jcflack commented 3 years ago

This is a good idea that has been at the back of my mind ever since I made sure PL/Java works with GraalVM, but I have not had time to test the native-image idea or develop a procedure for using it.

More of my effort at the moment is going toward refactoring PL/Java to make GraalVM polyglot languages more naturally accessible.

But native-image building is a worthwhile area to experiment in.

jcflack commented 3 years ago

oracle/graal#2761 looks relevant, and if I'm reading it correctly, the right stuff should be there now since GrallVM 21.0.

Sounds like it will need some kind of configuration file to say what classes and methods will need to be included for method handles to point to. But that would be trivial to generate from pg_proc if starting with a database with a normal dynamic PL/Java installation and the desired functions already declared. Extra credit would be to generate it by parsing the DDR files in existing PL/Java jars. Or generate it from the annotation processor while compiling PL/Java sources.

ccleve commented 3 years ago

I don't know enough to comment. I do know that they were very helpful on the Graal Slack and though this was a cool use of Graal. They seemed to think that this was totally possible. I just had a hard time understanding the syntax for making function calls, and also how to instantiate and manage the "Agent Isolate", which is apparently a giant uber object that manages memory, threads and everything. Somehow you have to create this object, maintain a reference to it, and pass it in as the first parameter on every function call from the outside world.