ocaml - How do I return a binary string (bytea) in a PL/Python PostgreSQL routine? -
i'm trying write procedure in pl/python perform conversion of data, return result bytea
. (it's quite ugly, actually: marshalling data in ocaml! ugly in python , ocaml @ once; should medal?)
here's looks like:
create or replace function ml_marshal(data varchar) returns bytea $$ import tempfile, os fn = tempfile.mktemp() f = open(fn, 'w') dest_fn = tempfile.mktemp() f.write("let outch = open_out_bin \"" + dest_fn + "\" in " + "marshal.to_channel outch (" + data + ") [marshal.no_sharing]; " + "close_out outch") f.close() os.system("ocaml " + fn) os.unlink(fn) f = open(dest_fn, 'r') res = f.read() f.close() os.unlink(dest_fn) return res $$ language plpythonu;
in short, writes out small ocaml program tempfile creates tempfile data want. read tempfile in, destroy them both, , return result.
only doesn't quite work:
meidi=# select * tblmodel; modelid | polies ---------+------------------ 1 | \204\225\246\276 2 | \204\225\246\276
there 4 bytes in each (there should ~130). if stop unlinking files, becomes obvious why; there 4 non-nul bytes, followed couple of nuls, , appears nuls treated terminators @ stage conversion python postgres!
does know why happens, or how stop it? docs not enlightening.
thanks!
edit: found someone else same problem, solution unsatisfactory.
this fixed release 9.0. had same problem upgraded. release notes:
improve bytea support in pl/python (caleb welton) bytea values passed pl/python represented binary, rather postgresql bytea text format. bytea values containing null bytes output pl/python. passing of boolean, integer, , float values improved.
i not think there elegant solution problem in previous postgresql versions.
Comments
Post a Comment