Creating a PostgreSQL procedural language – Part 2 – Embedding Julia
Julia provides an API so that Julia functions can be called from C. PL/Julia will use this C API to execute Julia code from its user defined functions and stored procedures.
Julia’s documentation provides an example C program that starts up the Julia environment, evaluates the expression sqrt(2.0)
, displays the resulting value to the standard output, and cleans up the Julia environment.
We will integrate this example into PL/Julia, with some minor modifications, to demonstrate that a PostgreSQL extension can really execute Julia code. This adapted example will send the results of the Julia expression to the PostgreSQL log instead of to the standard output.
jl_value_t *ret; double ret_unboxed; /* required: setup the Julia context */ jl_init(); /* run Julia command */ ret = jl_eval_string("sqrt(2.0)"); if (jl_typeis(ret, jl_float64_type)) { ret_unboxed = jl_unbox_float64(ret); elog(INFO, "sqrt(2.0) in C: %e", ret_unboxed); } /* strongly recommended: notify Julia that the * program is about to terminate. this allows Julia time to cleanup * pending write requests and run all finalizers */ jl_atexit_hook(0);
I’ll continue to gloss over the steps to create a user defined function or stored procedure in PL/Julia since the usefulness of the extension is still quite limited.
Backtracking a little bit, the Makefile also needs to be updated so that the PL/Julia extension is able to link up with the Julia C API. Julia provides a script that helps generate the appropriate build flags:
JL_SHARE = $(shell julia -e 'print(joinpath(Sys.BINDIR, Base.DATAROOTDIR, "julia"))') PG_CPPFLAGS += $(shell $(JL_SHARE)/julia-config.jl --cflags) PG_LDFLAGS += $(shell $(JL_SHARE)/julia-config.jl --ldflags) SHLIB_LINK += $(shell $(JL_SHARE)/julia-config.jl --ldlibs)
For another round of instant gratification, PL/Julia successfully builds and links to the Julia library. Then if you decided to write your own PL/Julia function, it will execute Julia code to calculate the square root of 2 and emit the result to the Postgres log:
INFO: sqrt(2.0) in C: 1.414214e+00
Trackbacks & Pingbacks
[…] Julia provides an API so that Julia functions can be called from C. PL/Julia will use this C API to execute Julia code from its user defined functions and stored procedures. Julia’s documentation provides an example C program that starts up the Julia env… Read more […]
Leave a Reply
Want to join the discussion?Feel free to contribute!