How to write array[] bytea using libpq PQexecParams?

154 Views Asked by At

I need help with write array[] bytea using libpq PQexecParams

I'v done a simple version where i'm write a single binary data in a single bytea arg using PQexecParams like this solution Insert Binary Large Object (BLOB) in PostgreSQL using libpq from remote machine

But I have problems with write array[] bytea like this

select * func(array[3, 4], array[bytea, bytea])

1

There are 1 best solutions below

4
Laurenz Albe On

Unless you want to read the PostgreSQL source to figure out the binary format for arrays, you will have to use the text format, e.g.

{\\xDEADBEEF,\\x00010203}

The binary format for arrays is defined in array_send in src/backend/utils/adt/arrayfuncs.c. Have a look at the somments and definitions in src/include/utils/array.h as well. Consider also that all integers are sent in “network bit order”.

One way you can examine the binary output format and saving yourself a lot of trouble experimenting is to use binary copy, e.g.

COPY (SELECT ARRAY['\xDEADBEEF'::bytea,'\x00010203'::bytea])
   TO '/tmp/file' (FORMAT 'binary');