postgresqlhaskellhdbc

HDBC-postgreSQL bytea fields get returned as SqlByteString hex strings


When I select bytea fields filled with binary data in my Postgres database using the HDBC-postgreSQL driver (version 2.3.2.3), they come out as:

SqlByteString "\x<hex representation of binary data>"

That is, it returns a ByteString which contains a string containing \x followed by the hex representation of my binary data. This is inconvenient, dreadfully inefficient and basically makes no sense to me.

Is there any reason why it doesn't return a SqlByteString containing a byte string with the actual binary data inside it? Is there something I am missing, or how do I configure the driver to do that?

thanks


Solution

  • This is a longtime known issue with this library. See this bug for example.

    The broader problem is that getting raw bytes requires a fair amount of cleverness that the postgres api doesn't make obvious. You have to call your entire query with binary rather than text output (which is arguably better anyway, but would require a rewrite of that portion of the binding layer).

    You can see where pqexecparams is called and note it is called with a last parameter of 0, which by the postgres docs means everything comes back in text. And for postgres that means this funny hex representation you see.

    If that argument were swapped to 1, then things could be done more efficiently (including getting raw binary for bytea fields) but Statement.hsc would have to be pervasively rewritten to deserialize those binary values.

    This is one of those things where its slightly irritating to lots of people, but nobody has yet been sufficiently motivated to go rewrite and debug the whole thing. But, of course, somebody really should! :-)