Unix Technical Forum

bytea hex input/output

This is a discussion on bytea hex input/output within the Pgsql General forums, part of the PostgreSQL category; --> Silly little question, but is there something to input/output hex escaped data into a bytea, ala CREATE TABLE a ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008, 10:46 AM
Michael Artz
 
Posts: n/a
Default bytea hex input/output

Silly little question, but is there something to input/output hex
escaped data into a bytea, ala

CREATE TABLE a (lob BYTEA);
INSERT into a (lob) VALUES ('\x01\x02\x00\x03\x04');
INSERT into a (lob) VALUES ('\x01\x00\x02\x00\x03\x04');

It seems to work (doesn't error), but when selecting the data back out,
it is truncated at the first \x00, ala

SELECT lob FROM a;

lob
----------
\001\002
\001

SELECT octet_length(lob) from a;

octet_length
--------------
2
1

Why does this happen? Also, can I get the output in similar hex form?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-09-2008, 10:46 AM
Tom Lane
 
Posts: n/a
Default Re: bytea hex input/output

"Michael Artz" <mlartz@gmail.com> writes:
> Silly little question, but is there something to input/output hex
> escaped data into a bytea, ala


PQescapeBytea, perhaps? The way you are doing it has multiple problems.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-09-2008, 10:46 AM
Michael Artz
 
Posts: n/a
Default Re: bytea hex input/output

Eh, I'll just convert it all to octal, I just thought that I could get away
without any middleman.

What ig going on behind the scenes? Does it first get converted to text and
then on to bytea? Would an explicit cast get around this (perhaps with
associated UDF)?

-Mike

On 5/16/06, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> "Michael Artz" <mlartz@gmail.com> writes:
> > Silly little question, but is there something to input/output hex
> > escaped data into a bytea, ala

>
> PQescapeBytea, perhaps? The way you are doing it has multiple problems.
>
> regards, tom lane
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-09-2008, 10:46 AM
Tom Lane
 
Posts: n/a
Default Re: bytea hex input/output

"Michael Artz" <mlartz@gmail.com> writes:
> What ig going on behind the scenes? Does it first get converted to text and
> then on to bytea?


No, the \nnn escape is built into the lexer's syntax for a string
literal (see backend/parser/scan.l), and only after that does the string
get fed to bytea's input routine (or any other datatype's either).
In hindsight this was a horribly bad idea that we'll be paying through
the nose for, for some time to come :-(. But we're stuck with it for
the moment.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-09-2008, 10:46 AM
Bruce Momjian
 
Posts: n/a
Default Re: bytea hex input/output

Tom Lane wrote:
> "Michael Artz" <mlartz@gmail.com> writes:
> > What ig going on behind the scenes? Does it first get converted to text and
> > then on to bytea?

>
> No, the \nnn escape is built into the lexer's syntax for a string
> literal (see backend/parser/scan.l), and only after that does the string
> get fed to bytea's input routine (or any other datatype's either).
> In hindsight this was a horribly bad idea that we'll be paying through
> the nose for, for some time to come :-(. But we're stuck with it for
> the moment.


Should we rethink this for SQL standard strings?

--
Bruce Momjian http://candle.pha.pa.us
EnterpriseDB http://www.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-09-2008, 10:46 AM
Tom Lane
 
Posts: n/a
Default Re: bytea hex input/output

Bruce Momjian <pgman@candle.pha.pa.us> writes:
> Tom Lane wrote:
>> No, the \nnn escape is built into the lexer's syntax for a string
>> literal (see backend/parser/scan.l),


> Should we rethink this for SQL standard strings?


We already have: \ isn't an escape anymore when
standard_conforming_strings is true.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:01 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com