, . PostgreSQL Python. Python, Python, PostgreSQL, PATH. , Python PostgreSQL, . PostgreSQL 9.6.5 Windows, Python 3.3. Python 3.6, . Python 3.3 Windows, 3.3.5.
Python PostgreSQL, CREATE EXTENSION plpython3u; , https://www.postgresql.org/docs/current/static/plpython.html. Python.
bytea double precision[] :
CREATE FUNCTION bytea_to_double_array(b bytea)
RETURNS double precision[]
LANGUAGE 'plpython3u'
AS $BODY$
if 'struct' in GD:
struct = GD['struct']
else:
import struct
GD['struct'] = struct
return struct.unpack('<' + str(int(len(b) / 8)) + 'd', b)
$BODY$;
CREATE FUNCTION double_array_to_bytea(dblarray double precision[])
RETURNS bytea
LANGUAGE 'plpython3u'
AS $BODY$
if 'struct' in GD:
struct = GD['struct']
else:
import struct
GD['struct'] = struct
return struct.pack('<' + str(int(len(dblarray))) + 'd', *dblarray)
$BODY$;
, <. struct , fooobar.com/questions/376634/.... GD SD, , , . GD SD . https://www.postgresql.org/docs/current/static/plpython-sharing.html.
, , endian,
SELECT bytea_to_double_array(decode('efbeaddeefbeadde', 'hex')), encode(double_array_to_bytea(array[-1.1885959257070704E148]), 'hex');
, ,
bytea_to_double_array | encode
double precision[] | text
-------------------------+------------------
{-1.18859592570707e+148} | efbeaddeefbeadde
'efbeaddeefbeadde' 'deadbeefdeadbeef'.