SCM

[#1010921] Data corrupted transferring bytea data from PostgreSQL 9 server

View Trackers | Bugs | Download .csv | Monitor

Date:
2010-10-02 01:00
Priority:
3
State:
Closed
Submitted by:
Matthew Gabeler-Lee (cheetah)
Assigned to:
Nobody (None)
Npgsql Version:
2.0.10
Category:
Group:
Resolution:
Accepted
Summary:
Data corrupted transferring bytea data from PostgreSQL 9 server

Detailed description
PostgreSQL 9 has changed the default format for transmitting bytea data: http://www.postgresql.org/docs/current/static/release-9-0.html section "E.1.2.3. Data Types".

Data sent to the server appears to be processed properly, but data received from the server is not under the default configuration.

Using the new default for transferring bytea data, Npgsql decodes data from the server incorrectly. If a bytea column has data bytes 0xab 0xcd 0xef (3 bytes), Npgsql returns a byte array corresponding to the string @"\abcdef".

Changing the parameter bytea_output from 'hex' to 'escape' as suggested in the release notes does "fix" the issue.

Example test code:

var csb = new NpgsqlConnectionStringBuilder()
{
// ...
};
using (var conn = new NpgsqlConnection(csb.ToString()))
{
conn.Open();
// without this, the assert at the end will fail
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "set bytea_output = 'escape'";
cmd.ExecuteNonQuery();
}
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "create temp table test_blob_transfer (data bytea)";
cmd.ExecuteNonQuery();
}
var bytes = Guid.NewGuid().ToByteArray();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "insert into test_blob_transfer values (:data)";
cmd.Parameters.Add(new NpgsqlParameter()
{
ParameterName = "data",
DbType = DbType.Binary,
Value = bytes,
});
cmd.ExecuteNonQuery();
}
byte[] reread;
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from test_blob_transfer";
reread = (byte[])cmd.ExecuteScalar();
}
CollectionAssert.AreEqual(bytes, reread);
}

Followup

Message
Date: 2011-01-01 21:25
Sender: Francisco Figueiredo jr.

I'm closing this bug report as it seems to have been fixed.
Date: 2010-10-25 17:07
Sender: Matthew Gabeler-Lee

Tested with latest CVS against both bytea output modes and
it works for me, thank you.
Date: 2010-10-24 19:34
Sender: Francisco Figueiredo jr.


I just applied a long pending patch to add support for receiving data in the new bytea representation. Please, give it a try and let me know if it works for you.

Attached Files:

Changes:

Field Old Value Date By
status_idOpen2011-01-01 21:25fxjr
ResolutionNone2011-01-01 21:25fxjr
close_date2011-01-01 21:252011-01-01 21:25fxjr
Powered By FusionForge