The ruby-postgres driver (2005-11-27 snapshot) unescapes bytea columsn before handing them back up to the Ruby layer:
switch (PQftype(result, column)) {
case BOOLOID:
return *string == 't' ? Qtrue : Qfalse;
case BYTEAOID:
return pgconn_s_unescape_bytea(NULL, rb_tainted_str_new2(string));
case NUMERICOID:
...
This seems like the right place to do the translation, especially if other translations ('t' => Qtrue, 'f' => Qfalse, etc) are being done here. But the ActiveRecord PostgreSQL Connection Adapter runs a second unescape on it:
column = row[cel_index]
if res.type(cel_index) == BYTEA_COLUMN_TYPE_OID
column = unescape_bytea(column)
end
hashed_row[fields[cel_index]] = column
This results in data truncation (if the unescaped string includes any NULL zeroes) or data corruption (if the unescaped string includes any backslashes).