Unix Technical Forum

problem with CR+LF in files in psql \i command

This is a discussion on problem with CR+LF in files in psql \i command within the Pgsql Patches forums, part of the PostgreSQL category; --> Hi, I need to backup my database with pg_dump and recover it in other windows machine. I recover my ...


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

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 11:08 PM
Luiz K. Matsumura
 
Posts: n/a
Default problem with CR+LF in files in psql \i command

Hi,
I need to backup my database with pg_dump and recover it in other
windows machine.
I recover my database running the pg_dump generated script in psql with
\i command
At each time I do this operation , misteriously the space between lines
multiplies in my functions and comments.

Well, I discover that when I use \i <file> command in psql with a file
generated in windows format ( CR+LF terminated lines) psql actually
sends the CR character at each line break to postgres, causing this effect.

I make a change in input.c that resolve this problem, but I'm not a
expert in c, therefore if someone have a better solution,
please do the necessary changes.

Thanks in advance.



*** input.c.ori 2005-03-09 21:05:36.000000000 -0300
--- input.c 2005-03-09 22:40:32.000000000 -0300
***************
*** 142,147 ****
--- 142,152 ----
if (buffer.data[buffer.len - 1] == '\n')
{
buffer.data[buffer.len - 1] = '\0';
+ /* in case of CR + LF */
+ if (buffer.data[buffer.len - 2] == '\r')
+ {
+ buffer.data[buffer.len - 2] = '\0';
+ }
return buffer.data;
}
}


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 11:11 PM
Bruce Momjian
 
Posts: n/a
Default Re: problem with CR+LF in files in psql \i command


The \i file is opened in binary mode because we don't want control-Z to
mark end-of-file. We have to do this because bytea allows a stream of
binary data and control-Z might be in there. The parser knows if the
control-Z is in quotes or not, but the file I/O routines to not and this
would cause a problem. There is also the problem if literal carriage
returns in COPY, though that shouldn't happen.

What is happening in your function and comments is that the original
line endings are preserved in the dump file and are recreated on the
Unix side. The good news is that it shouldn't affect the operation of
the database, but I can see how it would be annoying.

Function and comments are just _strings_ to PostgreSQL, so we have no
good way of cleaning the output up, unless we hack pg_dump to somehow
change line endings when outputting such information, though currently we
don't. The would perhaps cause problems in viewing the functions if
restored in Win32.

Fixing it at the \i level is too crude because it might remove carriage
returns that are wanted in the input stream.

We are open to ideas on how to improve this.

---------------------------------------------------------------------------

Luiz K. Matsumura wrote:
> Hi,
> I need to backup my database with pg_dump and recover it in other
> windows machine.
> I recover my database running the pg_dump generated script in psql with
> \i command
> At each time I do this operation , misteriously the space between lines
> multiplies in my functions and comments.
>
> Well, I discover that when I use \i <file> command in psql with a file
> generated in windows format ( CR+LF terminated lines) psql actually
> sends the CR character at each line break to postgres, causing this effect.
>
> I make a change in input.c that resolve this problem, but I'm not a
> expert in c, therefore if someone have a better solution,
> please do the necessary changes.
>
> Thanks in advance.
>
>


> *** input.c.ori 2005-03-09 21:05:36.000000000 -0300
> --- input.c 2005-03-09 22:40:32.000000000 -0300
> ***************
> *** 142,147 ****
> --- 142,152 ----
> if (buffer.data[buffer.len - 1] == '\n')
> {
> buffer.data[buffer.len - 1] = '\0';
> + /* in case of CR + LF */
> + if (buffer.data[buffer.len - 2] == '\r')
> + {
> + buffer.data[buffer.len - 2] = '\0';
> + }
> return buffer.data;
> }
> }


>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org


--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

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

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 11:12 AM.


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